Advertisement
Guest User

Untitled

a guest
Aug 10th, 2016
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.61 KB | None | 0 0
  1. from flask import *
  2. import MySQLdb
  3. import os
  4. import base64
  5. from functools import wraps
  6. import hashlib
  7.  
  8.  
  9.  
  10. from models import get_connecttion
  11. from content_manager import content
  12.  
  13.  
  14.  
  15. app = Flask(__name__)
  16. # added for session
  17. app.secret_key = os.urandom(24)
  18.  
  19. @app.route('/login', methods=['GET', 'POST'])
  20. def login():
  21. e = None
  22. if request.method == 'POST':
  23. username_form = request.form['username']
  24. password_form = request.form['password']
  25. passwd = base64.b64encode(password_form)
  26. try:
  27. dcn, cur = get_connecttion()
  28. cur.execute("SELECT COUNT(1) FROM user WHERE user_name = %s;", [username_form]) # CHECKS IF USERNAME EXSIST
  29. if cur.fetchone()[0]:
  30. cur.execute("SELECT password FROM user WHERE user_name = %s;", [username_form]) # FETCH THE HASHED PASSWORD
  31. for row in cur.fetchall():
  32. if passwd == row[0]:
  33. session['logged_in'] = True
  34. cur.execute("SELECT * FROM windata;"),
  35. commentsList = cur.fetchall()
  36. return render_template('table_V2.html', datas=commentsList)
  37. else:
  38. e = "Invalid Credential"
  39. return render_template('mainlogin.html', error=e)
  40. else:
  41. e = "Invalid Credential"
  42. return render_template('mainlogin.html', error=e)
  43. except (MySQLdb.Error, MySQLdb.Warning) as e:
  44.  
  45. return render_template('mainlogin.html', error=e)
  46.  
  47. def login_required(test):
  48. @wraps(test)
  49. def wrap(*args, **kwargs):
  50. if 'logged_in' in session:
  51. return test(*args, **kwargs)
  52. else:
  53. flash('You need to login first')
  54. return redirect(url_for('index'))
  55. return wrap
  56.  
  57. @app.route('/')
  58. def index():
  59. return render_template('mainlogin.html')
  60.  
  61. @app.route('/signup')
  62. def sign_up():
  63. return render_template('signup.html')
  64.  
  65.  
  66. @app.route("/adduseraction", methods=["post"])
  67. def add_user_action():
  68. # global first_name, last_name, email
  69. if request.form:
  70. user_name = request.form['usernamesignup']
  71. password = request.form['passwordsignup']
  72. email = request.form['emailsignup']
  73. passwd = base64.b64encode(password)
  74. query = "insert into user values (0,'%s','%s','%s')"
  75. query = query % (user_name, passwd, email)
  76. try:
  77. dcn, cur = get_connecttion()
  78. cur.execute(query)
  79. dcn.commit()
  80. return render_template('Sucess.html', user=user_name )
  81. except (MySQLdb.Error, MySQLdb.Warning) as e:
  82. e = "{} is already a user. Please use another username.".format(user_name)
  83. return render_template('signup.html', err = e)
  84.  
  85. @app.route('/home')
  86. @login_required
  87. def home():
  88. e = None
  89.  
  90. dcn, cur = get_connecttion()
  91. cur.execute("SELECT * FROM windata;"),
  92. commentsList = cur.fetchall()
  93. return render_template('table_V2.html', datas=commentsList)
  94.  
  95.  
  96.  
  97.  
  98. @app.route('/forgot')
  99. def forgot():
  100. return render_template('password.html')
  101. @app.route('/password', methods=['GET', 'POST'])
  102. def passwd():
  103. if request.method == 'POST':
  104. username_form = request.form['username']
  105. email_form = request.form['emailsignup']
  106. try:
  107. dcn, cur = get_connecttion()
  108. cur.execute("SELECT COUNT(1) FROM user WHERE user_name = %s;", [username_form]) # CHECKS IF USERNAME EXSIST
  109. if cur.fetchone()[0]:
  110. cur.execute("SELECT email FROM user WHERE user_name = %s;", [username_form]) # FETCH THE HASHED email
  111. for row in cur.fetchall():
  112. if email_form == row[0]:
  113. cur.execute("SELECT password FROM user WHERE user_name = %s;", [username_form])
  114. for row1 in cur.fetchall():
  115. pas = base64.b64decode(row1[0])
  116. return render_template('password.html', user='Your Password is : '+pas)
  117. else:
  118. e = "Email not match. Please try again."
  119. return render_template('password.html', error=e)
  120.  
  121. else:
  122. e = "Invalid Credential"
  123. return render_template('password.html', error=e)
  124. except (MySQLdb.Error, MySQLdb.Warning) as e:
  125.  
  126. return render_template('password.html', error=e)
  127.  
  128. @app.errorhandler(404)
  129. def page_not_found(e):
  130. return render_template('404.html')
  131.  
  132. @app.errorhandler(500)
  133. def server_error(e):
  134. flash("OOPS! Something went wrong.. Please login again")
  135. return redirect(url_for('index'))
  136.  
  137.  
  138.  
  139.  
  140. if __name__ == '__main__':
  141. app.run(host='0.0.0.0',port=6060)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement