1
|
|
|
""" |
2
|
|
|
byceps.blueprints.common.core.views |
3
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
4
|
|
|
|
5
|
|
|
:Copyright: 2014-2024 Jochen Kupperschmidt |
6
|
|
|
:License: Revised BSD (see `LICENSE` file for details) |
7
|
|
|
""" |
8
|
|
|
|
9
|
1 |
|
from datetime import date, datetime |
10
|
1 |
|
from typing import Any |
11
|
|
|
|
12
|
1 |
|
from flask import current_app, g, render_template |
13
|
|
|
|
14
|
1 |
|
from byceps.util.authz import ( |
15
|
|
|
has_current_user_any_permission, |
16
|
|
|
has_current_user_permission, |
17
|
|
|
) |
18
|
1 |
|
from byceps.util.framework.blueprint import create_blueprint |
19
|
1 |
|
from byceps.util.navigation import Navigation |
20
|
|
|
|
21
|
|
|
|
22
|
1 |
|
blueprint = create_blueprint('core_common', __name__) |
23
|
|
|
|
24
|
|
|
|
25
|
1 |
|
@blueprint.app_errorhandler(403) |
26
|
1 |
|
def forbidden(error) -> tuple[str, int]: |
27
|
|
|
return render_template('error/forbidden.html'), 403 |
28
|
|
|
|
29
|
|
|
|
30
|
1 |
|
@blueprint.app_errorhandler(404) |
31
|
1 |
|
def not_found(error) -> tuple[str, int]: |
32
|
1 |
|
return render_template('error/not_found.html'), 404 |
33
|
|
|
|
34
|
|
|
|
35
|
1 |
|
@blueprint.app_context_processor |
36
|
1 |
|
def inject_template_variables() -> dict[str, Any]: |
37
|
1 |
|
return { |
38
|
|
|
'datetime': datetime, |
39
|
|
|
'now': datetime.utcnow(), |
40
|
|
|
'today': date.today(), |
41
|
|
|
'Navigation': Navigation, |
42
|
|
|
'has_current_user_any_permission': has_current_user_any_permission, |
43
|
|
|
'has_current_user_permission': has_current_user_permission, |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
|
47
|
1 |
|
@blueprint.app_template_global() |
48
|
1 |
|
def add_page_arg(args, page) -> dict[str, Any]: |
49
|
|
|
"""Add the 'page' value. |
50
|
|
|
|
51
|
|
|
Used for pagination. |
52
|
|
|
""" |
53
|
1 |
|
if args is None: |
54
|
|
|
args = {} |
55
|
|
|
|
56
|
1 |
|
args['page'] = page |
57
|
1 |
|
return args |
58
|
|
|
|
59
|
|
|
|
60
|
1 |
|
@blueprint.before_app_request |
61
|
1 |
|
def prepare_request_globals() -> None: |
62
|
|
|
g.app_mode = current_app.byceps_app_mode |
63
|
|
|
|