1
|
|
|
""" |
2
|
|
|
byceps.blueprints.site.core.views |
3
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
4
|
|
|
|
5
|
|
|
:Copyright: 2006-2021 Jochen Kupperschmidt |
6
|
|
|
:License: Revised BSD (see `LICENSE` file for details) |
7
|
|
|
""" |
8
|
|
|
|
9
|
1 |
|
from typing import Optional |
10
|
|
|
|
11
|
1 |
|
from flask import g, url_for |
12
|
|
|
|
13
|
1 |
|
from .... import config |
14
|
1 |
|
from ....services.party import service as party_service |
15
|
1 |
|
from ....services.site import service as site_service |
16
|
1 |
|
from ....util.framework.blueprint import create_blueprint |
17
|
1 |
|
from ....util.user_session import ( |
18
|
|
|
get_current_user, |
19
|
|
|
get_locale as get_session_locale, |
20
|
|
|
) |
21
|
|
|
|
22
|
|
|
|
23
|
1 |
|
blueprint = create_blueprint('core_site', __name__) |
24
|
|
|
|
25
|
|
|
|
26
|
1 |
|
@blueprint.app_template_global() |
27
|
1 |
|
def url_for_site_file(filename, **kwargs) -> Optional[str]: |
28
|
|
|
"""Render URL for a static file local to the current site.""" |
29
|
|
|
site_id = getattr(g, 'site_id', None) |
30
|
|
|
|
31
|
|
|
if site_id is None: |
32
|
|
|
return None |
33
|
|
|
|
34
|
|
|
return url_for('site_file', site_id=site_id, filename=filename, **kwargs) |
35
|
|
|
|
36
|
|
|
|
37
|
1 |
|
@blueprint.before_app_request |
38
|
|
|
def prepare_request_globals(): |
39
|
1 |
|
app_mode = config.get_app_mode() |
40
|
1 |
|
g.app_mode = app_mode |
41
|
|
|
|
42
|
1 |
|
locale = get_session_locale() |
43
|
|
|
|
44
|
1 |
|
site_id = config.get_current_site_id() |
45
|
1 |
|
site = site_service.get_site(site_id) |
46
|
1 |
|
g.site_id = site.id |
47
|
|
|
|
48
|
1 |
|
g.brand_id = site.brand_id |
49
|
|
|
|
50
|
1 |
|
party_id = site.party_id |
51
|
1 |
|
if party_id is not None: |
52
|
1 |
|
party = party_service.get_party(party_id) |
53
|
1 |
|
party_id = party.id |
54
|
1 |
|
g.party_id = party_id |
55
|
|
|
|
56
|
1 |
|
required_permissions = set() |
57
|
1 |
|
g.user = get_current_user( |
58
|
|
|
required_permissions, locale, party_id=party_id |
59
|
|
|
) |
60
|
|
|
|