~homeworkprod/byceps

ref: 3cf502460f3f3efa7c76e1a063ef143254ea377e byceps/byceps/blueprints/core/views.py -rw-r--r-- 2.5 KiB
3cf50246 — Jochen Kupperschmidt Rename "site mode" to "app mode" 1 year, 5 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
"""
byceps.blueprints.core.views
~~~~~~~~~~~~~~~~~~~~~~~~~~~~

:Copyright: 2006-2020 Jochen Kupperschmidt
:License: Modified BSD, see LICENSE for details.
"""

from datetime import date, datetime
from typing import Optional

from flask import g, render_template, url_for

from ... import config
from ...services.party import service as party_service
from ...services.site import service as site_service
from ...util.framework.blueprint import create_blueprint
from ...util.navigation import Navigation

from ..authentication import service as authentication_blueprint_service


blueprint = create_blueprint('core', __name__)


@blueprint.app_errorhandler(403)
def forbidden(error):
    return render_template('error/forbidden.html'), 403


@blueprint.app_errorhandler(404)
def not_found(error):
    return render_template('error/not_found.html'), 404


@blueprint.app_context_processor
def inject_template_variables():
    return {
        'datetime': datetime,
        'now': datetime.utcnow(),
        'today': date.today(),
        'Navigation': Navigation,
    }


@blueprint.app_template_global()
def url_for_site_file(filename, **kwargs) -> Optional[str]:
    """Render URL for a static file local to the current site."""
    site_id = getattr(g, 'site_id', None)

    if site_id is None:
        return None

    return url_for('site_file', site_id=site_id, filename=filename, **kwargs)


@blueprint.app_template_global()
def add_page_arg(args, page):
    """Add the 'page' value.

    Used for pagination.
    """
    if args is None:
        args = {}

    args['page'] = page
    return args


@blueprint.app_template_test()
def is_current_page(nav_item_path, current_page=None):
    return (current_page is not None) \
            and (nav_item_path == current_page)


@blueprint.before_app_request
def provide_app_mode():
    # app mode
    app_mode = config.get_app_mode()
    g.app_mode = app_mode

    # site ID
    if app_mode.is_public():
        site_id = config.get_current_site_id()
        g.site_id = site_id

    # current party and brand
    party_id = None
    if app_mode.is_public():
        site = site_service.get_site(site_id)

        party_id = site.party_id

        if party_id is not None:
            party = party_service.get_party(party_id)

            g.party_id = party.id
            g.brand_id = party.brand_id
        else:
            g.party_id = None
            g.brand_id = None

    # current user
    is_admin_mode = app_mode.is_admin()
    g.current_user = authentication_blueprint_service.get_current_user(
        is_admin_mode, party_id=party_id
    )