~homeworkprod/byceps

ref: 342e5296ae417372e544ce931b442e98301c17a1 byceps/byceps/blueprints/admin/shop/email/views.py -rw-r--r-- 3.1 KiB
342e5296 — Jochen Kupperschmidt Require body blocks in base templates 2 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
108
109
110
"""
byceps.blueprints.admin.shop.email.views
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

:Copyright: 2006-2021 Jochen Kupperschmidt
:License: Revised BSD (see `LICENSE` file for details)
"""

from typing import Optional

from flask import abort, current_app

from .....services.brand import service as brand_service
from .....services.email import service as email_service
from .....services.shop.order.email import (
    example_service as example_order_email_service,
)
from .....services.shop.shop import service as shop_service
from .....util.authorization import register_permission_enum
from .....util.framework.blueprint import create_blueprint
from .....util.framework.templating import templated
from .....util.views import permission_required

from ..shop.authorization import ShopPermission


blueprint = create_blueprint('shop_email_admin', __name__)


register_permission_enum(ShopPermission)


@blueprint.route('/for_shop/<shop_id>')
@permission_required(ShopPermission.view)
@templated
def view_for_shop(shop_id):
    """Show e-mail examples."""
    shop = _get_shop_or_404(shop_id)

    brand = brand_service.get_brand(shop.brand_id)

    email_config = email_service.get_config(shop.brand_id)

    example_placed_order_message_text = _get_example_placed_order_message_text(
        shop.id
    )
    example_paid_order_message_text = _get_example_paid_order_message_text(
        shop.id
    )
    example_canceled_order_message_text = (
        _get_example_canceled_order_message_text(shop.id)
    )

    return {
        'shop': shop,
        'brand': brand,
        'email_config': email_config,
        'placed_order_message_text': example_placed_order_message_text,
        'paid_order_message_text': example_paid_order_message_text,
        'canceled_order_message_text': example_canceled_order_message_text,
    }


def _get_example_placed_order_message_text(shop_id) -> Optional[str]:
    try:
        return (
            example_order_email_service.build_example_placed_order_message_text(
                shop_id
            )
        )
    except example_order_email_service.EmailAssemblyFailed as e:
        current_app.logger.error(
            f'Could not assemble example email for placed order:\n{e}'
        )
        return None


def _get_example_paid_order_message_text(shop_id) -> Optional[str]:
    try:
        return (
            example_order_email_service.build_example_paid_order_message_text(
                shop_id
            )
        )
    except example_order_email_service.EmailAssemblyFailed as e:
        current_app.logger.error(
            f'Could not assemble example email for paid order:\n{e}'
        )
        return None


def _get_example_canceled_order_message_text(shop_id) -> Optional[str]:
    try:
        return example_order_email_service.build_example_canceled_order_message_text(
            shop_id
        )
    except example_order_email_service.EmailAssemblyFailed as e:
        current_app.logger.error(
            f'Could not assemble example email for canceled order:\n{e}'
        )
        return None


def _get_shop_or_404(shop_id):
    shop = shop_service.find_shop(shop_id)

    if shop is None:
        abort(404)

    return shop