Guest User

Untitled

a guest
Feb 24th, 2018
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.00 KB | None | 0 0
  1. from flask import Flask, render_template, request, flash, redirect, url_for, session, logging
  2. from wtforms import Form, StringField, PasswordField, FloatField, DateField, validators
  3. from flask_mysqldb import MySQL
  4.  
  5.  
  6. app = Flask(__name__)
  7.  
  8. app.config['MYSQL_HOST'] = 'localhost'
  9. app.config['MYSQL_USER'] = 'root'
  10. app.config['MYSQL_PASSWORD'] = 'Dream1234'
  11. app.config['MYSQL_DB'] = 'myflaskapp'
  12. app.config['MYSQL_CURSORCLASS'] = 'DictCursor'
  13.  
  14. mysql = MySQL(app)
  15.  
  16.  
  17. @app.route('/')
  18. def index():
  19. return render_template('home.html')
  20.  
  21.  
  22. @app.route('/home')
  23. def home():
  24. return render_template('home.html')
  25.  
  26.  
  27. class RegisterForm(Form):
  28. name = StringField('Name', [validators.Length(min=1, max=50)])
  29. email = StringField('Email', [validators.Length(min=6, max=50)])
  30. username = StringField('Username', [validators.Length(min=4, max=50)])
  31. password = PasswordField('Password', [
  32. validators.DataRequired(),
  33. validators.EqualTo('confirm', message='Passwords are different')])
  34. confirm = PasswordField('Confirm Password')
  35.  
  36.  
  37. @app.route('/register', methods=['GET', 'POST'])
  38. def register():
  39. form = RegisterForm(request.form)
  40. if request.method == 'POST'and form.validate():
  41. name = form.name.data
  42. email = form.email.data
  43. username = form.username.data
  44. password = form.password.data
  45.  
  46. cursor = mysql.connection.cursor()
  47.  
  48. cursor.execute("INSERT INTO users(name, username, password, email), VALUES(name, username, password, email)"), \
  49. (name, email, username, password)
  50.  
  51. mysql.connection.commit()
  52.  
  53. cursor.close()
  54. flash('You are now registered', 'success')
  55.  
  56. return redirect(url_for('index'))
  57. return render_template('register.html', form=form)
  58.  
  59.  
  60. class Login(Form):
  61. username = StringField('Username', [validators.Length(min=4, max=50)])
  62. password = PasswordField('Password', [validators.DataRequired()])
  63.  
  64.  
  65. @app.route('/login', methods=['GET', 'POST'])
  66. def login():
  67. form = Login()
  68. if request.method == 'POST' and form.validate:
  69. username = form.username.data
  70. password = form.password.data
  71.  
  72. cursor = mysql.connection.cursor()
  73.  
  74. cursor.execute("SELECT FROM users(username, password) VALUES(%s, %s)"), (username, password)
  75.  
  76. mysql.connection.commit()
  77.  
  78. cursor.close()
  79. flash('You are now logged in', 'success')
  80.  
  81. return redirect(url_for('index'))
  82. return render_template('login.html', form=form)
  83.  
  84.  
  85. class BudgetGuard(Form):
  86. month = DateField('month')
  87. income = FloatField('Income', [validators.Length(max=50)])
  88. expenses = FloatField('expenses',[validators.Length(max=45)])
  89.  
  90.  
  91. @app.route('/budgetguard', methods=['GET', 'POST'])
  92. def budgetguard():
  93. form = budgetguard()
  94. if request.method == 'POST':
  95. month = DateField('month')
  96. income = FloatField('Income', [validators.Length(max=50)])
  97.  
  98. return redirect(url_for('index'))
  99.  
  100.  
  101. if __name__ == '__main__':
  102. app.secret_key = 'cleopatra'
  103. app.run(debug=True)
Add Comment
Please, Sign In to add comment