Guest User

Untitled

a guest
Sep 4th, 2018
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.81 KB | None | 0 0
  1. from flask import render_template, request, redirect, url_for
  2.  
  3.  
  4. @app.route("/", methods= ["GET"])
  5. def first_page():
  6. cookie = response.headers['cookie']
  7. if database.lookup(cookie):
  8. user = database.get(cookie) # it returns user_email related to that cookie id
  9. else:
  10. return redirect_url(url_for('login'))
  11. data = generateSomeData() # some function
  12. return redirect(url_for('do_that'), user_id, data, stats)
  13.  
  14. @app.route('/do_that', methods =['GET'])
  15. def do_that(user_id):
  16. return render_template('interface.html', user_id, stats,data) # it uses Jinja template
  17.  
  18. @app.route('/submit', methods =["GET"])
  19. def submit():
  20. # i want to get all the information here
  21. user_id = request.form['user_id']# some data
  22. answer = request.form['answer'] # some response to be recorded
  23. data = request.form['data'] # same data that I passed in do_that to keep
  24. database.update(data,answer,user_id)
  25. return redirect(url_for('/do_that'))
  26.  
  27. @app.route('/login', methods=['GET'])
  28. def login():
  29. return render_template('login.html')
  30.  
  31. @app.route('/loggedIn', methods =['GET'])
  32. def loggedIn():
  33. cookie = response.headers['cookie']
  34. user_email = response.form['user_email']
  35. database.insert(cookie, user_email)
  36. return redirect(url_for('first_page'))
  37.  
  38. @app.route("/")
  39. def home():
  40. user_id = request.cookies.get('YourSessionCookie')
  41. if user_id:
  42. user = database.get(user_id)
  43. if user:
  44. # Success!
  45. return render_template('welcome.html', user=user)
  46. else:
  47. return redirect(url_for('login'))
  48. else:
  49. return redirect(url_for('login'))
  50.  
  51. @app.route("/login", methods=["GET", "POST"])
  52. def login():
  53. if request.method == "POST":
  54. # You should really validate that these fields
  55. # are provided, rather than displaying an ugly
  56. # error message, but for the sake of a simple
  57. # example we'll just assume they are provided
  58.  
  59. user_name = request.form["name"]
  60. password = request.form["password"]
  61. user = db.find_by_name_and_password(user_name, password)
  62.  
  63. if not user:
  64. # Again, throwing an error is not a user-friendly
  65. # way of handling this, but this is just an example
  66. raise ValueError("Invalid username or password supplied")
  67.  
  68. # Note we don't *return* the response immediately
  69. response = redirect(url_for("do_that"))
  70. response.set_cookie('YourSessionCookie', user.id)
  71. return response
  72.  
  73. @app.route("/do-that")
  74. def do_that():
  75. user_id = request.cookies.get('YourSessionCookie')
  76. if user_id:
  77. user = database.get(user_id)
  78. if user:
  79. # Success!
  80. return render_template('do_that.html', user=user)
  81. else:
  82. return redirect(url_for('login'))
  83. else:
  84. return redirect(url_for('login'))
  85.  
  86. from functools import wraps
  87. from flask import flash
  88.  
  89. def login_required(function_to_protect):
  90. @wraps(function_to_protect)
  91. def wrapper(*args, **kwargs):
  92. user_id = request.cookies.get('YourSessionCookie')
  93. if user_id:
  94. user = database.get(user_id)
  95. if user:
  96. # Success!
  97. return function_to_protect(*args, **kwargs)
  98. else:
  99. flash("Session exists, but user does not exist (anymore)")
  100. return redirect(url_for('login'))
  101. else:
  102. flash("Please log in")
  103. return redirect(url_for('login'))
  104. return wrapper
  105.  
  106. # Note that login_required needs to come before app.route
  107. # Because decorators are applied from closest to furthest
  108. # and we don't want to route and then check login status
  109.  
  110. @app.route("/")
  111. @login_required
  112. def home():
  113. # For bonus points we *could* store the user
  114. # in a thread-local so we don't have to hit
  115. # the database again (and we get rid of *this* boilerplate too).
  116. user = database.get(request.cookies['YourSessionCookie'])
  117. return render_template('welcome.html', user=user)
  118.  
  119. @app.route("/do-that")
  120. @login_required
  121. def do_that():
  122. user = database.get(request.cookies['YourSessionCookie'])
  123. return render_template('welcome.html', user=user)
  124.  
  125. # You have to set the secret key for sessions to work
  126. # Make sure you keep this secret
  127. app.secret_key = 'something simple for now'
  128.  
  129. from flask import flash, session
  130.  
  131. def login_required(function_to_protect):
  132. @wraps(function_to_protect)
  133. def wrapper(*args, **kwargs):
  134. user_id = session.get('user_id')
  135. if user_id:
  136. user = database.get(user_id)
  137. if user:
  138. # Success!
  139. return function_to_protect(*args, **kwargs)
  140. else:
  141. flash("Session exists, but user does not exist (anymore)")
  142. return redirect(url_for('login'))
  143. else:
  144. flash("Please log in")
  145. return redirect(url_for('login'))
  146.  
  147. user = database.get(session['user_id'])
Add Comment
Please, Sign In to add comment