Guest User

Untitled

a guest
Mar 3rd, 2018
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.38 KB | None | 0 0
  1. from flask import Flask, render_template, request, flash, redirect, url_for
  2. from wtforms import Form, StringField, PasswordField, FloatField, DateField, validators, form
  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('/home')
  18. def home():
  19. return render_template('home.html')
  20.  
  21.  
  22. class RegisterForm(Form):
  23. name = StringField('Name', [validators.Length(min=1, max=50)])
  24. username = StringField('Username', [validators.Length(min=4, max=50)])
  25. password = PasswordField('Password', [
  26. validators.DataRequired(),
  27. validators.EqualTo('confirm', message='Passwords are different')])
  28. confirm = PasswordField('Confirm Password')
  29. email = StringField('Email', [validators.Length(min=6, max=50)])
  30.  
  31.  
  32. @app.route('/register', methods=['GET', 'POST'])
  33. def register():
  34. form = RegisterForm(request.form)
  35. if request.method == 'POST' and form.validate():
  36. name = form.name.data
  37. username = form.username.data
  38. password = form.password.data
  39. email = form.email.data
  40.  
  41. cursor = mysql.connection.cursor()
  42.  
  43. cursor.execute("INSERT INTO users(name, username, password, email) VALUES(%s, %s, %s, %s)",
  44. (name, username, password, email))
  45.  
  46. mysql.connection.commit()
  47.  
  48. cursor.close()
  49. flash('You are now registered', 'success')
  50.  
  51. return redirect(url_for('budgetguard'))
  52. return render_template('register.html', form=form)
  53.  
  54.  
  55. class Login(Form):
  56. username = StringField('Username', [validators.Length(min=4, max=50)])
  57. password = PasswordField('Password', [validators.DataRequired()])
  58.  
  59.  
  60. @app.route('/login', methods=['GET', 'POST'])
  61. def login():
  62. form = Login(request.form)
  63. if request.method == 'POST' and form.validate:
  64. username = form.username.data
  65. password = form.password.data
  66.  
  67. cursor = mysql.connection.cursor()
  68.  
  69. cursor.execute("SELECT * FROM users where username = %s and password = %s", (username, password))
  70.  
  71. mysql.connection.commit()
  72.  
  73. user = cursor.fetchone()
  74. cursor.close()
  75.  
  76. if user is None:
  77. flash('No user found with these details.', 'danger')
  78.  
  79. # print(user)
  80. else:
  81. flash('You are now logged in', 'success')
  82.  
  83. return redirect(url_for('budgetguard'))
  84. return render_template('login.html', form=form)
  85.  
  86.  
  87. class BudgetGuard(Form):
  88. month = DateField('Month')
  89. income = FloatField('Income', [validators.Length(max=50)])
  90. expenses = FloatField('Expenses', [validators.Length(max=45)])
  91. balance = FloatField('Balance',)
  92.  
  93.  
  94. def sub():
  95. form = BudgetGuard()
  96. income = form.income.data
  97. expenses = form.expenses.data
  98. balance = income - expenses
  99. return balance
  100.  
  101.  
  102. income = 0
  103. expenses = 0
  104. balance = 0
  105. next_page = 0
  106.  
  107.  
  108. @app.route('/budgetguard', methods=['GET', 'POST'])
  109. def budgetguard():
  110. global balance
  111. form = BudgetGuard(request.form)
  112. if request.method == 'POST' and form.validate:
  113. income = form.income.data
  114. expenses = form.expenses.data
  115. balance = income - expenses
  116.  
  117. return render_template('budgetguard.html', form=form, balance=balance)
  118.  
  119.  
  120.  
  121. if __name__ == '__main__':
  122. app.secret_key = 'cleopatra'
  123. app.run(debug=True)
Add Comment
Please, Sign In to add comment