Advertisement
Guest User

Untitled

a guest
Mar 15th, 2017
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.79 KB | None | 0 0
  1. from flask import Flask, render_template, redirect, url_for
  2. from flask_sqlalchemy import SQLAlchemy
  3. from flask_login import LoginManager
  4.  
  5. app = Flask(__name__)
  6.  
  7. db = SQLAlchemy(app)
  8. db.init_app(app)
  9.  
  10. login_manager = LoginManager()
  11. login_manager.session_protection = 'strong'
  12. login_manager.login_view = 'auth.singin'
  13. login_manager.init_app(app)
  14.  
  15. # Добавления файла конфигурации
  16. app.config.from_object('config')
  17.  
  18. @app.errorhandler(404)
  19. def not_found(error):
  20. return render_template('404.html'), 404
  21.  
  22. from app.home.controllers import home as home_module
  23. from app.auth.controllers import auth as auth_module
  24.  
  25. app.register_blueprint(home_module)
  26. app.register_blueprint(auth_module)
  27.  
  28. # Импорт моделей
  29. from app.models import user
  30.  
  31. auth = Blueprint('auth', __name__, url_prefix='/auth')
  32.  
  33. @auth.route('/signin/',methods=['GET','POST'])
  34. def signin():
  35. form = LoginForm()
  36. if form.validate_on_submit():
  37. user = User.query.filter_by(email=form.email.data).first()
  38. if user is not None and user.password == form.password.data:
  39. print(user.password)
  40. login_user(user, form.remember_me.data)
  41. return redirect(request.args.get('next') or url_for('home.index'))
  42. flash('Invalid username or password.')
  43. return render_template('auth/signin.html', form=form)
  44.  
  45. home = Blueprint('home', __name__,)
  46.  
  47. @home.route('/')
  48. @home.route('/home')
  49. @home.route('/index.html')
  50. @decorators.login_required
  51. def index():
  52. return render_template('home/index.html')
  53.  
  54. def login_required(f):
  55. @wraps(f)
  56. def decorated_function(*args, **kwargs):
  57. if 'username' not in session:
  58. return redirect(url_for('auth.signin'))
  59. return f(*args, **kwargs)
  60. return decorated_function
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement