Advertisement
Guest User

Untitled

a guest
Jul 28th, 2016
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.68 KB | None | 0 0
  1. from flask import Flask, redirect, render_template, request, flash, url_for, session
  2. import MySQLdb
  3. from models import get_connecttion
  4.  
  5.  
  6. app = Flask(__name__)
  7.  
  8.  
  9. @app.route('/')
  10. def homepage():
  11. return render_template('mainlogin.html')
  12.  
  13. @app.route('/signup')
  14. def sign_up():
  15. return render_template('signup.html')
  16.  
  17.  
  18. @app.route("/adduseraction", methods=["post"])
  19. def add_user_action():
  20. # global first_name, last_name, email
  21. if request.form:
  22. user_name = request.form['usernamesignup']
  23. password = request.form['passwordsignup']
  24. email = request.form['emailsignup']
  25.  
  26. query = "insert into user values (0,'%s','%s','%s')"
  27. query = query % (user_name, password, email)
  28. try:
  29. dcn, cur = get_connecttion()
  30. cur.execute(query)
  31. dcn.commit()
  32. return render_template('Sucess.html', user=user_name )
  33. except (MySQLdb.Error, MySQLdb.Warning) as e:
  34. e = "{} is already a user. Please use another username.".format(user_name)
  35. return render_template('signup.html', err = e)
  36.  
  37. @app.route('/login', methods=['GET', 'POST'])
  38. def login():
  39. e = None
  40. if request.method == 'POST':
  41. username_form = request.form['username']
  42. password_form = request.form['password']
  43. try:
  44. dcn, cur = get_connecttion()
  45. cur.execute("SELECT COUNT(1) FROM user WHERE user_name = %s;", [username_form]) # CHECKS IF USERNAME EXSIST
  46. if cur.fetchone()[0]:
  47. cur.execute("SELECT password FROM user WHERE user_name = %s;", [username_form]) # FETCH THE HASHED PASSWORD
  48. for row in cur.fetchall():
  49. if password_form == row[0]:
  50.  
  51. return render_template('successfull.html', datas=username_form)
  52. else:
  53. e = "Invalid Credential"
  54. return render_template('successfull.html', error=e)
  55. else:
  56. e = "Invalid Credential"
  57. return render_template('successfull.html', error=e)
  58. except (MySQLdb.Error, MySQLdb.Warning) as e:
  59.  
  60. return render_template('successfull.html', error=e)
  61. @app.route('/logout')
  62. def logout():
  63. return render_template('logout.html')
  64. @app.route('/forgot')
  65. def forgot():
  66. return render_template('password.html')
  67. @app.route('/password', methods=['GET', 'POST'])
  68. def passwd():
  69. if request.method == 'POST':
  70. username_form = request.form['username']
  71. email_form = request.form['emailsignup']
  72. try:
  73. dcn, cur = get_connecttion()
  74. cur.execute("SELECT COUNT(1) FROM user WHERE user_name = %s;", [username_form]) # CHECKS IF USERNAME EXSIST
  75. if cur.fetchone()[0]:
  76. cur.execute("SELECT email FROM user WHERE user_name = %s;", [username_form]) # FETCH THE HASHED email
  77. for row in cur.fetchall():
  78. if email_form == row[0]:
  79. cur.execute("SELECT password FROM user WHERE user_name = %s;", [username_form])
  80. for row1 in cur.fetchall():
  81. return render_template('password.html', user='Your Password is : '+row1[0])
  82. else:
  83. e = "Email not match. Please try again."
  84. return render_template('password.html', error=e)
  85.  
  86. else:
  87. e = "Invalid Credential"
  88. return render_template('password.html', error=e)
  89. except (MySQLdb.Error, MySQLdb.Warning) as e:
  90.  
  91. return render_template('password.html', error=e)
  92.  
  93. @app.errorhandler(404)
  94. def page_not_found(e):
  95. return render_template('404.html')
  96.  
  97.  
  98. if __name__ == '__main__':
  99. app.run(port=5050)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement