Advertisement
Guest User

Untitled

a guest
Nov 20th, 2017
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.23 KB | None | 0 0
  1. from flask import Flask, render_template, flash, request, redirect, url_for, abort
  2. from flask_login import *
  3. import settings
  4. import logging
  5.  
  6.  
  7. DEBUG = True
  8. logging.basicConfig(filename='entries.log', format='%(asctime)s %(message)s', datefmt='%m/%d/%Y %I:%M:%S %p', level=logging.INFO)
  9. application = Flask(__name__)
  10. application.secret_key = os.urandom(24)
  11. settings.init()
  12.  
  13. login_manager = LoginManager()
  14. login_manager.init_app(application)
  15. login_manager.login_view = "login"
  16.  
  17.  
  18.  
  19. ## User Class ##
  20. class User():
  21.  
  22.     def __init__(self, id):
  23.         self.id = id
  24.         self.email = str(id) + "@email.com"
  25.         self.password = "password"
  26.     def __repr__(self):
  27.         return '<User %r>' % self.email
  28.     def is_authenticated(self):
  29.         return True
  30.     def is_active(self):
  31.         return True
  32.     def is_anonymous(self):
  33.         return False
  34.     def get_id(self):
  35.         return str(self.id)
  36.  
  37. ### Create Test Users ###
  38. users = [User(id) for id in range(1, 6)]
  39. for user in users:
  40.  
  41.  
  42.  
  43. ### Login Manager per Flask-Login Documentation ###
  44. @login_manager.user_loader
  45. def load_user(email):
  46.     user = None
  47.     for x in users:
  48.         if x.email == email:
  49.             user = x
  50.         else:
  51.             continue
  52.     return user
  53.  
  54.  
  55. ## Home Page Route ##
  56. @application.route('/')
  57. @login_required
  58. def home():
  59.     return render_template('home.html')
  60.  
  61.  
  62.  ## Login Route ##
  63. @application.route('/login', methods=['GET','POST'])
  64. def login():
  65.     if request.method == 'GET':
  66.         return render_template('login.html')
  67.         print("Did GET")
  68.     elif request.method == 'POST':
  69.         user = load_user(request.form['email'])
  70.         if user.password == request.form['password']:
  71.             next = request.args.get('next')
  72.             login_user(user)
  73.             print(user)
  74.             print("logged in")    
  75.            
  76.             print(next)
  77.  
  78.             return redirect(next or url_for('home'))
  79.         return render_template('login.html')
  80.          
  81.  
  82. ## Check Your  Status Form ###
  83. @application.route("/OtherRoute", methods=['GET','POST'])
  84. @login_required
  85. def status_form():
  86.     if request.method == 'POST':
  87.         ## Doing some logic here to establish variables - Test variables used below ###
  88.  
  89.         name = "FirstName LastName"
  90.         email = "Email"
  91.  
  92.            
  93.         else:
  94.             return render_template('no_result.html', name=name, email=email)
  95.  
  96.     return render_template('status_form.html')
  97.  
  98.  
  99.  
  100. #<----------------------------------HTML ---------------------------->
  101.  
  102. ### Login Page ####
  103. {% extends 'layout.html' %}
  104. {% block title %}Login{% endblock %}
  105. {% block content %}
  106.  
  107. <div class="log-form">
  108.   <h2>Login to your account</h2>
  109.   <form  method="post" action="/login">
  110.     <input type="text" title="email" name="email" placeholder="username" />
  111.     <input type="password" title="password" name="password" placeholder="password" />
  112.     <button type="submit" value="login" class="btn">Login</button>
  113.     <a class="forgot" href="#">Forgot Username?</a>
  114.   </form>
  115. </div>
  116. {% endblock %}
  117.  
  118.  
  119. ### Home Page (Route is ('/') ###
  120.  
  121. {% extends 'layout.html' %}
  122. {% block title %}Home{% endblock %}
  123. {% block content %}
  124.  
  125. <div class="Container">
  126.   Test Home Page
  127. </div>
  128. {% endblock %}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement