1. youngking
  2. flask-actions

Source

flask-actions / examples / example / example / views / frontend.py

# -*- encoding:utf-8 -*-
from flask import Module
from flask import Flask, request, session, g, redirect, url_for, abort, \
             render_template, flash

from example.models import User

frontend = Module(__name__)

@frontend.route('/')
def index():
    return render_template('index.html')

@frontend.route('/login', methods=['GET', 'POST'])
def login():
    error = None
    if request.method == 'POST':
        username = request.form['username']
        password = request.form['password']
        user = User.query.filter_by(username=username).first()
        if not user or not user.check_password(password):
            error = 'Invalid username or  password'
        else:
            session['logged_in'] = True
            session['username'] = username
            flash('You were logged in')
            return redirect(url_for('index'))
    return render_template('login.html', error=error)


@frontend.route('/logout')
def logout():
    session.pop('logged_in', None)
    flash('You were logged out')
    return redirect(url_for('index'))