1
|
|
|
""" |
2
|
|
|
byceps.blueprints.admin.shop.email.views |
3
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
4
|
|
|
|
5
|
|
|
:Copyright: 2006-2020 Jochen Kupperschmidt |
6
|
|
|
:License: Modified BSD, see LICENSE for details. |
7
|
|
|
""" |
8
|
|
|
|
9
|
1 |
|
from typing import Optional |
10
|
|
|
|
11
|
1 |
|
from flask import abort, current_app |
12
|
|
|
|
13
|
1 |
|
from .....services.email import service as email_service |
14
|
1 |
|
from .....services.shop.order.email import ( |
15
|
|
|
example_service as example_order_email_service, |
16
|
|
|
) |
17
|
1 |
|
from .....services.shop.shop import service as shop_service |
18
|
1 |
|
from .....util.framework.blueprint import create_blueprint |
19
|
1 |
|
from .....util.framework.templating import templated |
20
|
|
|
|
21
|
1 |
|
from ....common.authorization.decorators import permission_required |
22
|
1 |
|
from ....common.authorization.registry import permission_registry |
23
|
|
|
|
24
|
1 |
|
from ..shop.authorization import ShopPermission |
25
|
|
|
|
26
|
|
|
|
27
|
1 |
|
blueprint = create_blueprint('shop_email_admin', __name__) |
28
|
|
|
|
29
|
|
|
|
30
|
1 |
|
permission_registry.register_enum(ShopPermission) |
31
|
|
|
|
32
|
|
|
|
33
|
1 |
|
@blueprint.route('/for_shop/<shop_id>') |
34
|
1 |
|
@permission_required(ShopPermission.view) |
35
|
1 |
|
@templated |
36
|
|
|
def view_for_shop(shop_id): |
37
|
|
|
"""Show e-mail examples.""" |
38
|
|
|
shop = _get_shop_or_404(shop_id) |
39
|
|
|
|
40
|
|
|
email_config = email_service.find_config(shop.email_config_id) |
41
|
|
|
|
42
|
|
|
example_placed_order_message_text = _get_example_placed_order_message_text( |
43
|
|
|
shop.id |
44
|
|
|
) |
45
|
|
|
example_paid_order_message_text = _get_example_paid_order_message_text( |
46
|
|
|
shop.id |
47
|
|
|
) |
48
|
|
|
example_canceled_order_message_text = ( |
49
|
|
|
_get_example_canceled_order_message_text(shop.id) |
50
|
|
|
) |
51
|
|
|
|
52
|
|
|
return { |
53
|
|
|
'shop': shop, |
54
|
|
|
'email_config': email_config, |
55
|
|
|
'placed_order_message_text': example_placed_order_message_text, |
56
|
|
|
'paid_order_message_text': example_paid_order_message_text, |
57
|
|
|
'canceled_order_message_text': example_canceled_order_message_text, |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
|
61
|
1 |
|
def _get_example_placed_order_message_text(shop_id) -> Optional[str]: |
62
|
|
|
try: |
63
|
|
|
return ( |
64
|
|
|
example_order_email_service.build_example_placed_order_message_text( |
65
|
|
|
shop_id |
66
|
|
|
) |
67
|
|
|
) |
68
|
|
|
except example_order_email_service.EmailAssemblyFailed as e: |
69
|
|
|
current_app.logger.error( |
70
|
|
|
f'Could not assemble example email for placed order:\n{e}' |
71
|
|
|
) |
72
|
|
|
return None |
73
|
|
|
|
74
|
|
|
|
75
|
1 |
|
def _get_example_paid_order_message_text(shop_id) -> Optional[str]: |
76
|
|
|
try: |
77
|
|
|
return ( |
78
|
|
|
example_order_email_service.build_example_paid_order_message_text( |
79
|
|
|
shop_id |
80
|
|
|
) |
81
|
|
|
) |
82
|
|
|
except example_order_email_service.EmailAssemblyFailed as e: |
83
|
|
|
current_app.logger.error( |
84
|
|
|
f'Could not assemble example email for paid order:\n{e}' |
85
|
|
|
) |
86
|
|
|
return None |
87
|
|
|
|
88
|
|
|
|
89
|
1 |
|
def _get_example_canceled_order_message_text(shop_id) -> Optional[str]: |
90
|
|
|
try: |
91
|
|
|
return example_order_email_service.build_example_canceled_order_message_text( |
92
|
|
|
shop_id |
93
|
|
|
) |
94
|
|
|
except example_order_email_service.EmailAssemblyFailed as e: |
95
|
|
|
current_app.logger.error( |
96
|
|
|
f'Could not assemble example email for canceled order:\n{e}' |
97
|
|
|
) |
98
|
|
|
return None |
99
|
|
|
|
100
|
|
|
|
101
|
1 |
|
def _get_shop_or_404(shop_id): |
102
|
|
|
shop = shop_service.find_shop(shop_id) |
103
|
|
|
|
104
|
|
|
if shop is None: |
105
|
|
|
abort(404) |
106
|
|
|
|
107
|
|
|
return shop |
108
|
|
|
|