WebApp

Check-in [083431b7de]
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Created checker decorator and a decorator template, checker implemented to simple_webapp
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256:083431b7dec572499d27a542893bf9af84e56c82f9d56b39d9d6f5188c9fb7d6
User & Date: sebpc 2018-05-25 18:28:53
Context
2018-05-26
15:54
viewlog access protected by decorator check-in: 209eecc6f8 user: sebpc tags: trunk
2018-05-25
18:28
Created checker decorator and a decorator template, checker implemented to simple_webapp check-in: 083431b7de user: sebpc tags: trunk
2018-05-24
16:44
Testing session with simple_webapp.py check-in: e4d4efd593 user: sebpc tags: trunk
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Added checker.py.





















>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
from flask import session
from functools import wraps

def check_logged_in(func):
    @wraps(func)
    def wrapper(*args, **kwargs):
        if 'logged_in' in session:
            return func(*args, **kwargs)
        return 'You are not logged in'
    return wrapper

Changes to simple_webapp.py.

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
from flask import Flask, session


app = Flask("__name__")

app.secret_key = "ddf4545gf$%56fASD"

@app.route('/login')
def do_login() -> str:
    session['logged_in'] = True
................................................................................
    return 'Jestes teraz zalogowany'

@app.route('/logout')
def do_logout() -> str:
    session.pop('logged_in')
    return 'Nie jestes zalogowany'

@app.route('/status')
def check_status() -> str:
    if 'logged_in' in session:
        return 'Jestes zalogowany'
    return 'Nie jestes zalogowany'
@app.route('/')
def hello() -> str:
    return 'Hello from the simple webapp'

@app.route('/page1')

def page1() -> str:
    return 'this is page 1'

@app.route('/page2')

def page2() -> str:
    return 'this is page 2'

@app.route('/page3')

def page3() -> str:
    return 'this is page 3'

app.run(debug=True)


>







 







<
<
<
<
<





>




>




>




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
from flask import Flask, session

from checker import check_logged_in
app = Flask("__name__")

app.secret_key = "ddf4545gf$%56fASD"

@app.route('/login')
def do_login() -> str:
    session['logged_in'] = True
................................................................................
    return 'Jestes teraz zalogowany'

@app.route('/logout')
def do_logout() -> str:
    session.pop('logged_in')
    return 'Nie jestes zalogowany'






@app.route('/')
def hello() -> str:
    return 'Hello from the simple webapp'

@app.route('/page1')
@check_logged_in
def page1() -> str:
    return 'this is page 1'

@app.route('/page2')
@check_logged_in
def page2() -> str:
    return 'this is page 2'

@app.route('/page3')
@check_logged_in
def page3() -> str:
    return 'this is page 3'

app.run(debug=True)

Added tmpl_decorator.py.



























>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
from functools import wraps

def decorator_name(func):
    @wraps(func)
    def wrapper(*args, **kwargs):
        # 1. Code to execute before calling the decorated function.

        # 2. Call the decorated function as required, returning its
        #    results if needed.
             return func(*args, **kwargs)

        # 3. Code to execute INSTEAD of calling the decorated function.
    return wrapper