Completed
Push — main ( b7ef7f...4a33dd )
by Jochen
03:13
created

build_example_paid_order_message_text()   A

Complexity

Conditions 2

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 4.8094

Importance

Changes 0
Metric Value
cc 2
eloc 11
nop 1
dl 0
loc 18
ccs 1
cts 9
cp 0.1111
crap 4.8094
rs 9.85
c 0
b 0
f 0
1
"""
2
byceps.services.shop.order.email.example_service
3
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
4
5
:Copyright: 2006-2020 Jochen Kupperschmidt
6
:License: Modified BSD, see LICENSE for details.
7
"""
8
9 1
from datetime import datetime
10 1
from decimal import Decimal
11
12 1
from .....config import ConfigurationError
13 1
from .....database import generate_uuid
14
15 1
from ....email.transfer.models import Message
16
17 1
from ...shop import service as shop_service
18 1
from ...shop.transfer.models import ShopID
19
20 1
from ..email.service import OrderEmailData
21 1
from ..transfer.models import Order, PaymentMethod, PaymentState
22
23 1
from . import service as shop_order_email_service
24
25
26 1
class EmailAssemblyFailed(Exception):
27 1
    pass
28
29
30 1
def build_example_placed_order_message_text(shop_id: ShopID) -> str:
31
    """Assemble an exemplary e-mail for a placed order."""
32
    shop = shop_service.get_shop(shop_id)
33
34
    order = _build_order(shop.id, PaymentState.open, is_open=True)
35
36
    data = _build_email_data(order, shop.email_config_id)
37
38
    try:
39
        message = shop_order_email_service._assemble_email_for_incoming_order_to_orderer(
40
            data
41
        )
42
    except Exception as e:
43
        raise EmailAssemblyFailed()
44
45
    return _render_message(message)
46
47
48 1
def build_example_paid_order_message_text(shop_id: ShopID) -> str:
49
    """Assemble an exemplary e-mail for a paid order."""
50
    shop = shop_service.get_shop(shop_id)
51
52
    order = _build_order(shop.id, PaymentState.paid, is_paid=True)
53
54
    data = _build_email_data(order, shop.email_config_id)
55
56
    try:
57
        message = (
58
            shop_order_email_service._assemble_email_for_paid_order_to_orderer(
59
                data
60
            )
61
        )
62
    except Exception as e:
63
        raise EmailAssemblyFailed()
64
65
    return _render_message(message)
66
67
68 1
def build_example_canceled_order_message_text(shop_id: ShopID) -> str:
69
    """Assemble an exemplary e-mail for a canceled order."""
70
    shop = shop_service.get_shop(shop_id)
71
72
    order = _build_order(
73
        shop.id,
74
        PaymentState.canceled_before_paid,
75
        is_canceled=True,
76
        cancelation_reason='Kein fristgerechter Geldeingang feststellbar.',
77
    )
78
79
    data = _build_email_data(order, shop.email_config_id)
80
81
    try:
82
        message = shop_order_email_service._assemble_email_for_canceled_order_to_orderer(
83
            data
84
        )
85
    except Exception as e:
86
        raise EmailAssemblyFailed()
87
88
    return _render_message(message)
89
90
91 1
def _build_order(
92
    shop_id: ShopID,
93
    payment_state: PaymentState,
94
    *,
95
    is_open: bool = False,
96
    is_canceled: bool = False,
97
    is_paid: bool = False,
98
    cancelation_reason: str = None,
99
) -> Order:
100
    order_id = generate_uuid()
101
    order_number = 'AWSM-ORDR-9247'
102
103
    created_at = datetime.utcnow()
104
    placed_by_id = None
105
106
    first_names = 'Bella-Bernadine'
107
    last_name = 'Ballerwurm'
108
    address = None
109
110
    total_amount = Decimal('42.95')
111
    items = []
112
    payment_method = PaymentMethod.bank_transfer
113
114
    return Order(
115
        order_id,
116
        shop_id,
117
        order_number,
118
        created_at,
119
        placed_by_id,
120
        first_names,
121
        last_name,
122
        address,
123
        total_amount,
124
        items,
125
        payment_method,
126
        payment_state,
127
        is_open,
128
        is_canceled,
129
        is_paid,
130
        False,  # is_invoiced
131
        False,  # is_shipping_required
132
        False,  # is_shipped
133
        cancelation_reason,
134
    )
135
136
137 1
def _build_email_data(order: Order, email_config_id: str) -> OrderEmailData:
138
    return OrderEmailData(
139
        order=order,
140
        email_config_id=email_config_id,
141
        orderer_screen_name='Besteller',
142
        orderer_email_address='[email protected]',
143
    )
144
145
146 1
def _render_message(message: Message) -> str:
147
    if not message.sender:
148
        raise ConfigurationError(
149
            'No e-mail sender address configured for message.'
150
        )
151
152
    return (
153
        f'From: {message.sender}\n'
154
        f'To: {message.recipients}\n'
155
        f'Subject: {message.subject}\n'
156
        f'\n\n{message.body}\n'
157
    )
158