Guest User

Untitled

a guest
Jul 17th, 2018
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.70 KB | None | 0 0
  1. from flask import Flask, render_template, flash, redirect, url_for, session, logging, request
  2. from data import articles #import the function
  3. from flask_mysqldb import MySQL
  4. from wtforms import Form, StringField, TextAreaField, PasswordField, validators
  5. from passlib.hash import sha256_crypt
  6. from flask_mail import Mail, Message
  7.  
  8. app = Flask(__name__) # object app of class Flask, pass __name__ argument to __init__
  9.  
  10. app.config.update(dict(
  11. DEBUG = True,
  12. MAIL_SERVER = 'smtp.gmail.com',
  13. MAIL_PORT = 587,
  14. MAIL_USE_TLS = True,
  15. MAIL_USE_SSL = False,
  16. MAIL_USERNAME = 'limory37@gmail.com',
  17. MAIL_PASSWORD = 'shit7265',
  18. ))
  19.  
  20.  
  21. mail = Mail(app)
  22.  
  23. mysql = MySQL(app)
  24.  
  25. MySqlSettings = {"MYSQL_HOST":"localhost","MYSQL_USER":"root","MYSQL_PASSWORD":"eaea7265","MYSQL_DB":"myflaskapp","MYSQL_CURSORCLASS":"DictCursor"}
  26. for k,v in MySqlSettings.items(): # k=key, v=value
  27. app.config[k]=v
  28.  
  29.  
  30.  
  31.  
  32. Articles = articles()
  33.  
  34. @app.route('/') # what URL should activate the function below
  35. def index():
  36. return render_template('index.html')
  37.  
  38. @app.route('/about')
  39. def about():
  40. return render_template('about.html')
  41.  
  42. @app.route('/articles')
  43. def articles():
  44. return render_template('articles.html', articles = Articles)
  45.  
  46. @app.route('/articles/<string:id>/')
  47. def article(id):
  48. return render_template('article.html', id=id, articles=Articles)
  49.  
  50.  
  51. class RegisterForm(Form): # subclass of Form
  52. name = StringField('Name', [validators.Length(min=1, max=50)]) #pass args to __init__ of StringField class
  53. username = StringField('Username', [validators.Length(min=4, max=25)])
  54. email = StringField('Email', [validators.Length(min=6, max=50)])
  55. password = PasswordField('Password', [
  56. validators.Length(min=6, max=25),
  57. validators.EqualTo('confirm', message='Passwords do not match')
  58. ])
  59. confirm = PasswordField('Confirm Password')
  60.  
  61. @app.route('/register', methods=["GET","POST"])
  62. def register():
  63. form = RegisterForm(request.form)
  64. if request.method == 'POST' and form.validate():
  65. name = form.name.data
  66. email = form.email.data
  67. username = form.username.data
  68. password = sha256_crypt.encrypt(str(form.password.data))
  69.  
  70. cur = mysql.connection.cursor()
  71. data = cur.execute("SELECT * FROM users WHERE username = %s", [form.username.data])
  72. if data is 0:
  73. cur.execute("INSERT INTO users(name, email, username, password) VALUES(%s, %s, %s, %s)", (name, email, username, password))
  74. mysql.connection.commit()
  75. flash('You are now registered and can log in', 'success')
  76. return redirect(url_for('login'))
  77. else:
  78. flash('Username already exists','danger')
  79. cur.close()
  80. return render_template('register.html', form=form)
  81.  
  82.  
  83.  
  84. @app.route('/login', methods=['POST','GET'])
  85. def login():
  86. if request.method == 'POST':
  87. username = request.form['username']
  88. password_candidate = request.form['password']
  89.  
  90. cur = mysql.connection.cursor()
  91. result = cur.execute("SELECT * FROM users WHERE username = %s", [username])
  92.  
  93. if result > 0:
  94. data = cur.fetchone()
  95.  
  96. app.logger.info(data['email'])
  97. password = data['password']
  98. if sha256_crypt.verify(password_candidate, password):
  99. app.logger.info('password matched homo')
  100. else:
  101. app.logger.info('password did not match still homo')
  102. else:
  103. app.logger.info('no user')
  104. return render_template('login.html')
  105.  
  106.  
  107. if __name__ == '__main__':
  108. app.secret_key = 'secret123'
  109. app.run(debug=True) # run() method of Flask class, passing debug argument with value True
  110. #we can also do app.debug = True, to set the debug property of the app object to True.
Add Comment
Please, Sign In to add comment