Nav apraksta

routes.py 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. # -*- encoding: utf-8 -*-
  2. """
  3. License: MIT
  4. Copyright (c) 2019 - present AppSeed.us
  5. """
  6. from flask import jsonify, render_template, redirect, request, url_for
  7. from flask_login import (
  8. current_user,
  9. login_required,
  10. login_user,
  11. logout_user
  12. )
  13. from app import db, login_manager
  14. from app.base import blueprint
  15. from app.base.forms import LoginForm, CreateAccountForm
  16. from app.base.models import User
  17. from app.base.util import verify_pass
  18. @blueprint.route('/')
  19. def route_default():
  20. return redirect(url_for('base_blueprint.login'))
  21. @blueprint.route('/error-<error>')
  22. def route_errors(error):
  23. return render_template('errors/{}.html'.format(error))
  24. ## Login & Registration
  25. @blueprint.route('/login', methods=['GET', 'POST'])
  26. def login():
  27. login_form = LoginForm(request.form)
  28. if 'login' in request.form:
  29. # read form data
  30. username = request.form['username']
  31. password = request.form['password']
  32. # Locate user
  33. user = User.query.filter_by(username=username).first()
  34. # Check the password
  35. if user and verify_pass( password, user.password):
  36. login_user(user)
  37. return redirect(url_for('base_blueprint.route_default'))
  38. # Something (user or pass) is not ok
  39. return render_template( 'login/login.html', msg='Wrong user or password', form=login_form)
  40. if not current_user.is_authenticated:
  41. return render_template( 'login/login.html',
  42. form=login_form)
  43. return redirect(url_for('home_blueprint.index'))
  44. @blueprint.route('/create_user', methods=['GET', 'POST'])
  45. def create_user():
  46. login_form = LoginForm(request.form)
  47. create_account_form = CreateAccountForm(request.form)
  48. if 'register' in request.form:
  49. username = request.form['username']
  50. email = request.form['email' ]
  51. user = User.query.filter_by(username=username).first()
  52. if user:
  53. return render_template( 'login/register.html', msg='Username already registered', form=create_account_form)
  54. user = User.query.filter_by(email=email).first()
  55. if user:
  56. return render_template( 'login/register.html', msg='Email already registered', form=create_account_form)
  57. # else we can create the user
  58. user = User(**request.form)
  59. db.session.add(user)
  60. db.session.commit()
  61. return render_template( 'login/register.html', msg='User created please <a href="/login">login</a>', form=create_account_form)
  62. else:
  63. return render_template( 'login/register.html', form=create_account_form)
  64. @blueprint.route('/logout')
  65. def logout():
  66. logout_user()
  67. return redirect(url_for('base_blueprint.login'))
  68. @blueprint.route('/shutdown')
  69. def shutdown():
  70. func = request.environ.get('werkzeug.server.shutdown')
  71. if func is None:
  72. raise RuntimeError('Not running with the Werkzeug Server')
  73. func()
  74. return 'Server shutting down...'
  75. ## Errors
  76. @login_manager.unauthorized_handler
  77. def unauthorized_handler():
  78. return render_template('errors/403.html'), 403
  79. @blueprint.errorhandler(403)
  80. def access_forbidden(error):
  81. return render_template('errors/403.html'), 403
  82. @blueprint.errorhandler(404)
  83. def not_found_error(error):
  84. return render_template('errors/404.html'), 404
  85. @blueprint.errorhandler(500)
  86. def internal_error(error):
  87. return render_template('errors/500.html'), 500