Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from flask import Flask, render_template, flash, request, redirect, url_for, abort
- from flask_login import *
- import settings
- import logging
- DEBUG = True
- logging.basicConfig(filename='entries.log', format='%(asctime)s %(message)s', datefmt='%m/%d/%Y %I:%M:%S %p', level=logging.INFO)
- application = Flask(__name__)
- application.secret_key = os.urandom(24)
- settings.init()
- login_manager = LoginManager()
- login_manager.init_app(application)
- login_manager.login_view = "login"
- ## User Class ##
- class User():
- def __init__(self, id):
- self.id = id
- self.email = str(id) + "@email.com"
- self.password = "password"
- def __repr__(self):
- return '<User %r>' % self.email
- def is_authenticated(self):
- return True
- def is_active(self):
- return True
- def is_anonymous(self):
- return False
- def get_id(self):
- return str(self.id)
- ### Create Test Users ###
- users = [User(id) for id in range(1, 6)]
- for user in users:
- ### Login Manager per Flask-Login Documentation ###
- @login_manager.user_loader
- def load_user(email):
- user = None
- for x in users:
- if x.email == email:
- user = x
- else:
- continue
- return user
- ## Home Page Route ##
- @application.route('/')
- @login_required
- def home():
- return render_template('home.html')
- ## Login Route ##
- @application.route('/login', methods=['GET','POST'])
- def login():
- if request.method == 'GET':
- return render_template('login.html')
- print("Did GET")
- elif request.method == 'POST':
- user = load_user(request.form['email'])
- if user.password == request.form['password']:
- next = request.args.get('next')
- login_user(user)
- print(user)
- print("logged in")
- print(next)
- return redirect(next or url_for('home'))
- return render_template('login.html')
- ## Check Your Status Form ###
- @application.route("/OtherRoute", methods=['GET','POST'])
- @login_required
- def status_form():
- if request.method == 'POST':
- ## Doing some logic here to establish variables - Test variables used below ###
- name = "FirstName LastName"
- email = "Email"
- else:
- return render_template('no_result.html', name=name, email=email)
- return render_template('status_form.html')
- #<----------------------------------HTML ---------------------------->
- ### Login Page ####
- {% extends 'layout.html' %}
- {% block title %}Login{% endblock %}
- {% block content %}
- <div class="log-form">
- <h2>Login to your account</h2>
- <form method="post" action="/login">
- <input type="text" title="email" name="email" placeholder="username" />
- <input type="password" title="password" name="password" placeholder="password" />
- <button type="submit" value="login" class="btn">Login</button>
- <a class="forgot" href="#">Forgot Username?</a>
- </form>
- </div>
- {% endblock %}
- ### Home Page (Route is ('/') ###
- {% extends 'layout.html' %}
- {% block title %}Home{% endblock %}
- {% block content %}
- <div class="Container">
- Test Home Page
- </div>
- {% endblock %}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement