rev: 491566fa24ddd88cee7987a361d3a63f466f8c76 Wikked/wikked/views/user.py -rw-r--r-- 1.6 KiB View raw Log this file
491566fa24ddLudovic Chabant cm: Upgrade dependencies and fix deprecated APIs. 1 year, 6 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
from flask import request, redirect, render_template
from flask_login import login_user, logout_user, current_user
from wikked.views import add_auth_data, add_navigation_data
from wikked.web import app, get_wiki


@app.route('/login', methods=['GET', 'POST'])
def login():
    wiki = get_wiki()

    data = {}
    add_auth_data(data)
    add_navigation_data(
            None, data,
            raw_url='/api/user/login')

    if request.method == 'GET':
        if current_user.is_authenticated:
            data['already_logged_in'] = True
            return render_template('logout.html', **data)
        else:
            return render_template('login.html', **data)

    if request.method == 'POST':
        username = request.form.get('username')
        password = request.form.get('password')
        remember = request.form.get('remember')
        back_url = request.form.get('back_url')

        user = wiki.auth.getUser(username)
        if user is not None and app.bcrypt:
            if app.bcrypt.check_password_hash(user.password, password):
                login_user(user, remember=bool(remember))
                return redirect(back_url or '/')

        data['has_error'] = True
        return render_template('login.html', **data)


@app.route('/logout', methods=['GET', 'POST'])
def logout():
    if request.method == 'GET':
        data = {}
        add_auth_data(data)
        add_navigation_data(
                None, data,
                raw_url='/api/user/logout')
        return render_template('logout.html', **data)

    if request.method == 'POST':
        logout_user()
        return redirect('/')