Advertisement
Guest User

Untitled

a guest
Feb 27th, 2019
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.57 KB | None | 0 0
  1. # app.py
  2. from flask import Flask, render_template, flash, redirect, url_for, session, logging, request
  3. # from flask_sqlalchemy import SQLAlchemy
  4. # from data import Articles
  5. from data import Articles
  6. from flask_mysqldb import MySQL
  7. from wtforms import Form, StringField, TextAreaField, PasswordField, validators
  8. from passlib.hash import sha256_crypt
  9.  
  10. app = Flask(__name__)
  11. # app.config['SECRET_KEY'] = 'xxxxxxxxxx'
  12.  
  13. # Config MySQL
  14. app.config['MySQL_HOST'] = 'localhost'
  15. app.config['MySQL_USER'] = 'root'
  16. app.config['MySQL_PASSWORD'] = ''
  17. app.config['MySQL_DB'] = 'myflaskapp'
  18. app.config['MySQL_CURSORCLASS'] = 'DictCursor'
  19. # Init MySQL
  20. mysql = MySQL(app)
  21.  
  22. # Setting articles
  23. Articles = Articles()
  24.  
  25.  
  26. @app.route('/')
  27. def home():
  28. return render_template('home.html')
  29.  
  30.  
  31. @app.route('/about')
  32. def about():
  33. return render_template('about.html')
  34.  
  35.  
  36. @app.route('/articles')
  37. def articles():
  38. return render_template('articles.html', articles=Articles)
  39.  
  40.  
  41. @app.route('/article/<string:id>/')
  42. def article(id):
  43. return render_template('article.html', id=id)
  44.  
  45.  
  46. class RegisterForm(Form):
  47. name = StringField('Name', [validators.Length(min=1, max=50)])
  48. username = StringField('Username', [validators.Length(min=4, max=25)])
  49. email = StringField('Email', [validators.Length(min=6, max=50)])
  50. password = PasswordField('Password', [validators.DataRequired(),
  51. validators.EqualTo(
  52. 'confirm', message='Passwords do not match!')
  53.  
  54.  
  55. ])
  56. confirm = PasswordField('Confirm Password')
  57.  
  58.  
  59. @app.route('/register', methods=['GET', 'POST'])
  60. def register():
  61. form = RegisterForm(request.form)
  62. if request.method == 'POST' and form.validate():
  63. name = form.name.data
  64. email = form.email.data
  65. username = form.username.data
  66. password = sha256_crypt.encrypt(str(form.password.data))
  67.  
  68. # Creat Cursor
  69. cur = mysql.connection.cursor()
  70. # Execute Query
  71. cur.execute("INSERT INTO users(name, email, username, password) VALUES(%s, %s, %s, %s)",
  72. (name, email, username, password))
  73. # Commit to DB
  74. mysql.connection.commit()
  75. # close connection
  76. cur.close()
  77.  
  78. # Message to user once registered
  79. flash('You are now registered and can login, thank you', 'success')
  80. return redirect(url_for('index'))
  81. return render_template('register.html', form=form)
  82.  
  83.  
  84. if __name__ == '__main__':
  85. app.run(debug=True)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement