Advertisement
Guest User

Untitled

a guest
Sep 24th, 2017
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.77 KB | None | 0 0
  1. #!/usr/bin/python3
  2. from flask import Flask, render_template, flash, request, redirect, url_for, session, logging
  3. from flask_mail import Mail, Message
  4. from flask_mysqldb import MySQL
  5. from wtforms import Form, StringField, TextAreaField, PasswordField, validators
  6. from passlib.hash import sha256_crypt
  7. from functools import wraps
  8.  
  9. app =Flask(__name__)
  10. # Option 1: app.debug = True (allow to auto refresh server)
  11.  
  12. # Config MySQL
  13. app.config['MYSQL_HOST'] = 'localhost'
  14. app.config['MYSQL_USER'] = 'root'
  15. app.config['MYSQL_PASSWORD'] = '123'
  16. app.config['MYSQL_DB'] = 'myflaskapp'
  17. app.config['MYSQL_CURSORCLASS'] = 'DictCursor' # by default the data set in MySQL will be in tuple; this one line of code sets the data set to dictionary structure
  18.  
  19. # init MYSQL
  20. mysql = MySQL(app)
  21.  
  22. @app.route('/blog')
  23. def blog():
  24. # Create cursor
  25. cur = mysql.connection.cursor()
  26.  
  27. # Get articles
  28. result = cur.execute("SELECT * FROM articles")
  29.  
  30. articles = cur.fetchall()
  31.  
  32. if result > 0:
  33. return render_template('blog.html', articles=articles)
  34. else:
  35. msg = "No Articles Found"
  36. return render_template('blog.html', msg = msg)
  37.  
  38. # Close connection
  39. cur.close()
  40.  
  41. # Register Form Class
  42. class RegisterForm(Form):
  43. name = StringField('Name', [validators.Length(min=1, max=50)])
  44. username = StringField('Username', [validators.Length(min=4, max=25)])
  45. email = StringField ('Email', [validators.Length(min=6, max=50)])
  46. password = PasswordField ('Password', [
  47. validators.DataRequired(),
  48. validators.EqualTo('confirm', message='Passwords do not match.')
  49. ])
  50. confirm = PasswordField('Confirm Password')
  51.  
  52. # Admin Register
  53. @app.route('/register', methods=['GET', 'POST'])
  54. def register():
  55. form = RegisterForm(request.form)
  56. if request.method == 'POST' and form.validate():
  57. name = form.name.data
  58. email = form.email.data
  59. username = form.username.data
  60. password = sha256_crypt.encrypt(str(form.password.data))
  61.  
  62. # Create cursor
  63. cur = mysql.connection.cursor()
  64.  
  65. # Execute query
  66. cur.execute("INSERT INTO users(name, email, username, password) VALUES(%s, %s, %s, %s)", (name, email, username, password))
  67.  
  68. # Commit to DB
  69. mysql.connection.commit()
  70.  
  71. # Close connection
  72. cur.close()
  73.  
  74. flash('You are now registered and can log in.', 'green')
  75.  
  76. return redirect(url_for('index'))
  77.  
  78. return render_template('register.html')
  79.  
  80. return render_template('register.html', form=form)
  81.  
  82. # Admin Login
  83. @app.route('/login', methods = ['GET', 'POST'])
  84. def login():
  85. if request.method == 'POST':
  86. # Get Form Fields
  87. username = request.form['username']
  88. password_candidate = request.form ['password']
  89.  
  90. # Create cursorcur
  91. cur = mysql.connection.cursor()
  92.  
  93. # Get user by username
  94. result = cur.execute("SELECT * FROM users WHERE username = %s", [username])
  95.  
  96. if result > 0:
  97. # Get stored hash
  98. data = cur.fetchone()
  99. password = data['password']
  100.  
  101. # Compare Passwords
  102. if sha256_crypt.verify(password_candidate, password):
  103. # Passed
  104. session['logged_in']= True
  105. session['username'] = username
  106.  
  107. flash('You are now logged in', 'green')
  108. return redirect(url_for('blog'))
  109.  
  110. else:
  111. error = 'Invalid login'
  112. return render_template('login.html', error = error)
  113.  
  114. # Close connection
  115. cur.close()
  116. else:
  117. error = 'Username not found'
  118. return render_template('login.html', error = error)
  119.  
  120. return render_template('login.html')
  121.  
  122. if __name__ == '__main__':
  123. app.secret_key='secert123456'
  124. app.run(host='0.0.0.0', port=6000, debug=True)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement