Completed
Push — main ( b71682...907b06 )
by Jochen
03:34
created

byceps/blueprints/core/views.py (1 issue)

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) and (nav_item_path == current_page)
73
74
75 1
@blueprint.before_app_request
76
def provide_app_mode():
77
    # app mode
78 1
    app_mode = config.get_app_mode()
79 1
    g.app_mode = app_mode
80
81
    # site ID
82 1
    if app_mode.is_site():
83 1
        site_id = config.get_current_site_id()
84 1
        g.site_id = site_id
85
86
    # current party and brand
87 1
    party_id = None
88 1
    if app_mode.is_site():
89 1
        site = site_service.get_site(site_id)
0 ignored issues
show
The variable site_id does not seem to be defined in case app_mode.is_site() on line 82 is False. Are you sure this can never be the case?
Loading history...
90
91 1
        g.brand_id = site.brand_id
92
93 1
        party_id = site.party_id
94 1
        if party_id is not None:
95 1
            party = party_service.get_party(party_id)
96 1
            party_id = party.id
97 1
        g.party_id = party_id
98
99
    # current user
100 1
    is_admin_mode = app_mode.is_admin()
101 1
    g.current_user = authentication_blueprint_service.get_current_user(
102
        is_admin_mode, party_id=party_id
103
    )
104