app.py 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. """
  2. Authentication module
  3. """
  4. import os
  5. from flask_login import login_required, login_user, logout_user, current_user
  6. from flask_menu import Menu, register_menu
  7. from flask import render_template, request, redirect, url_for, flash, Blueprint, abort, jsonify
  8. from jinja2 import TemplateNotFound
  9. from app import app, login_manager, db
  10. from app.models import User, Page
  11. BLUEPRINT = Blueprint(
  12. 'auth',
  13. __name__,
  14. template_folder='templates'
  15. )
  16. @BLUEPRINT.route("/login", methods=["GET", "POST"])
  17. def login():
  18. """Handle login page and data"""
  19. if request.method == 'POST':
  20. email = request.form['email']
  21. password = request.form['password']
  22. user = User.query.filter(User.email == email).first()
  23. if not user:
  24. flash('Email not found.', 'warning')
  25. return render_template('login.j2')
  26. if not user.approved:
  27. flash('Account not approved yet.', 'warning')
  28. return render_template('login.j2', login_email=email)
  29. if not user.check_password(password):
  30. flash('Password Incorrect.', 'warning')
  31. return render_template('login.j2', login_email=email)
  32. login_user(user, remember=True)
  33. flash('Successfully loggend in.', 'success')
  34. if request.args.get("next") and request.args.get("next") != "/logout":
  35. print(request.args.get("next"))
  36. return redirect(request.args.get('next'))
  37. return redirect(url_for('backend.index'))
  38. return render_template('login.j2')
  39. @BLUEPRINT.route("/register", methods=["POST"])
  40. def register():
  41. """Register a new user"""
  42. name = request.form['name'] if 'name' in request.form else None
  43. email = request.form['email'] if 'email' in request.form else None
  44. password = request.form['password'] if 'email' in request.form else None
  45. if name is None:
  46. flash('Fill in the name.', 'warning')
  47. return render_template('login.j2')
  48. if email is None:
  49. flash('Fill in the email.', 'warning')
  50. return render_template(
  51. 'login.j2',
  52. name=name
  53. )
  54. if password is None:
  55. flash('Fill in the password.', 'warning')
  56. return render_template(
  57. 'login.j2',
  58. name=name,
  59. email=email
  60. )
  61. user = User.query.filter(User.email == email).first()
  62. if user is not None:
  63. flash('Email already taken.', 'warning')
  64. return redirect(url_for('auth.login'))
  65. user = User()
  66. user.name = name
  67. user.email = email
  68. user.password = password
  69. db.session.add(user)
  70. db.session.commit()
  71. flash('Successfully registered account "%s". Wait for verfication.' % (user.name), 'success')
  72. return redirect(url_for('auth.login'))
  73. @BLUEPRINT.route("/logout")
  74. @login_required
  75. def logout():
  76. """Logout function for users"""
  77. logout_user()
  78. return redirect('')