Passed
Push — main ( 4c626d...b574ef )
by Jochen
09:17
created

byceps.blueprints.admin.shop.shop.views.create()   A

Complexity

Conditions 2

Size

Total Lines 22
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 4.3145

Importance

Changes 0
Metric Value
cc 2
eloc 16
nop 0
dl 0
loc 22
ccs 2
cts 12
cp 0.1666
crap 4.3145
rs 9.6
c 0
b 0
f 0
1
"""
2
byceps.blueprints.admin.shop.shop.views
3
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
4
5
:Copyright: 2006-2020 Jochen Kupperschmidt
6
:License: Modified BSD, see LICENSE for details.
7
"""
8
9 1
from collections import defaultdict
10
11 1
from flask import abort, request
12
13 1
from .....services.shop.article import service as article_service
14 1
from .....services.shop.order import (
15
    action_service as order_action_service,
16
    service as order_service,
17
)
18 1
from .....services.shop.order.transfer.models import PaymentState
19 1
from .....services.shop.shop import service as shop_service
20 1
from .....util.framework.blueprint import create_blueprint
21 1
from .....util.framework.flash import flash_success
22 1
from .....util.framework.templating import templated
23 1
from .....util.views import redirect_to
24
25 1
from ....authorization.decorators import permission_required
26 1
from ....authorization.registry import permission_registry
27
28 1
from .authorization import ShopPermission
29 1
from .forms import CreateForm
30
31
32 1
blueprint = create_blueprint('shop_shop_admin', __name__)
33
34
35 1
permission_registry.register_enum(ShopPermission)
36
37
38 1
@blueprint.route('/')
39 1
@permission_required(ShopPermission.view)
40 1
@templated
41
def index():
42
    """List all shops."""
43
    shops = shop_service.get_all_shops()
44
45
    return {
46
        'shops': shops,
47
    }
48
49
50 1
@blueprint.route('/for_shop/<shop_id>')
51 1
@permission_required(ShopPermission.view)
52 1
@templated
53
def view_for_shop(shop_id):
54
    """Show the shop."""
55
    shop = _get_shop_or_404(shop_id)
56
57
    order_counts_by_payment_state = order_service.count_orders_per_payment_state(
58
        shop.id
59
    )
60
61
    order_actions_by_article_number = _get_order_actions_by_article_number(
62
        shop.id
63
    )
64
65
    return {
66
        'shop': shop,
67
68
        'order_counts_by_payment_state': order_counts_by_payment_state,
69
        'PaymentState': PaymentState,
70
71
        'settings': shop.extra_settings,
72
73
        'order_actions_by_article_number': order_actions_by_article_number,
74
    }
75
76
77 1
def _get_order_actions_by_article_number(shop_id):
78
    actions = order_action_service.get_actions(shop_id)
79
80
    actions.sort(key=lambda a: a.payment_state.name, reverse=True)
81
    actions.sort(key=lambda a: a.article_number)
82
83
    actions_by_article_number = defaultdict(list)
84
    for action in actions:
85
        actions_by_article_number[action.article_number].append(action)
86
87
    return actions_by_article_number
88
89
90 1
@blueprint.route('/shops/create')
91 1
@permission_required(ShopPermission.create)
92 1
@templated
93 1
def create_form(erroneous_form=None):
94
    """Show form to create a shop."""
95
    form = erroneous_form if erroneous_form else CreateForm()
96
    form.set_email_config_choices()
97
98
    return {
99
        'form': form,
100
    }
101
102
103 1
@blueprint.route('/shops', methods=['POST'])
104 1
@permission_required(ShopPermission.create)
105
def create():
106
    """Create a shop."""
107
    form = CreateForm(request.form)
108
    form.set_email_config_choices()
109
110
    if not form.validate():
111
        return create_form(form)
112
113
    shop_id = form.id.data.strip().lower()
114
    title = form.title.data.strip()
115
    email_config_id = form.email_config_id.data
116
117
    shop = shop_service.create_shop(
118
        shop_id,
119
        title,
120
        email_config_id,
121
    )
122
123
    flash_success(f'Der Shop "{shop.title}" wurde angelegt.')
124
    return redirect_to('.index')
125
126
127 1
def _get_shop_or_404(shop_id):
128
    shop = shop_service.find_shop(shop_id)
129
130
    if shop is None:
131
        abort(404)
132
133
    return shop
134