Completed
Push — main ( 4c245a...a71144 )
by Jochen
05:11
created

byceps.blueprints.core.views.provide_app_mode()   A

Complexity

Conditions 4

Size

Total Lines 31
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 18
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
eloc 20
nop 0
dl 0
loc 31
ccs 18
cts 18
cp 1
crap 4
rs 9.4
c 0
b 0
f 0
1
"""
2
byceps.blueprints.core.views
3
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
4
5
:Copyright: 2006-2020 Jochen Kupperschmidt
6
:License: Modified BSD, see LICENSE for details.
7
"""
8
9 1
from datetime import date, datetime
10 1
from typing import Optional
11
12 1
from flask import g, render_template, url_for
13
14 1
from ... import config
15 1
from ...services.party import service as party_service
16 1
from ...services.site import service as site_service
17 1
from ...util.framework.blueprint import create_blueprint
18 1
from ...util.navigation import Navigation
19
20 1
from ..authentication import service as authentication_blueprint_service
21
22
23 1
blueprint = create_blueprint('core', __name__)
24
25
26 1
@blueprint.app_errorhandler(403)
27
def forbidden(error):
28
    return render_template('error/forbidden.html'), 403
29
30
31 1
@blueprint.app_errorhandler(404)
32
def not_found(error):
33 1
    return render_template('error/not_found.html'), 404
34
35
36 1
@blueprint.app_context_processor
37
def inject_template_variables():
38 1
    return {
39
        'datetime': datetime,
40
        'now': datetime.utcnow(),
41
        'today': date.today(),
42
        'Navigation': Navigation,
43
    }
44
45
46 1
@blueprint.app_template_global()
47 1
def url_for_site_file(filename, **kwargs) -> Optional[str]:
48
    """Render URL for a static file local to the current site."""
49
    site_id = getattr(g, 'site_id', None)
50
51
    if site_id is None:
52
        return None
53
54
    return url_for('site_file', site_id=site_id, filename=filename, **kwargs)
55
56
57 1
@blueprint.app_template_global()
58
def add_page_arg(args, page):
59
    """Add the 'page' value.
60
61
    Used for pagination.
62
    """
63
    if args is None:
64
        args = {}
65
66
    args['page'] = page
67
    return args
68
69
70 1
@blueprint.app_template_test()
71 1
def is_current_page(nav_item_path, current_page=None):
72 1
    return (current_page is not None) \
73
            and (nav_item_path == current_page)
74
75
76 1
@blueprint.before_app_request
77
def provide_app_mode():
78
    # app mode
79 1
    app_mode = config.get_app_mode()
80 1
    g.app_mode = app_mode
81
82
    # site ID
83 1
    if app_mode.is_site():
84 1
        site_id = config.get_current_site_id()
85 1
        g.site_id = site_id
86
87
    # current party and brand
88 1
    party_id = None
89 1
    if app_mode.is_site():
90 1
        site = site_service.get_site(site_id)
0 ignored issues
show
introduced by Jochen Kupperschmidt
The variable site_id does not seem to be defined in case app_mode.is_site() on line 83 is False. Are you sure this can never be the case?
Loading history...
91
92 1
        party_id = site.party_id
93
94 1
        if party_id is not None:
95 1
            party = party_service.get_party(party_id)
96
97 1
            g.party_id = party.id
98 1
            g.brand_id = party.brand_id
99
        else:
100 1
            g.party_id = None
101 1
            g.brand_id = None
102
103
    # current user
104 1
    is_admin_mode = app_mode.is_admin()
105 1
    g.current_user = authentication_blueprint_service.get_current_user(
106
        is_admin_mode, party_id=party_id
107
    )
108