Passed
Push — main ( 893310...ef4ea5 )
by Jochen
06:04
created

dashboard()   A

Complexity

Conditions 1

Size

Total Lines 19
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1.1865

Importance

Changes 0
Metric Value
cc 1
eloc 13
nop 1
dl 0
loc 19
ccs 3
cts 7
cp 0.4286
crap 1.1865
rs 9.75
c 0
b 0
f 0
1
"""
2
byceps.blueprints.admin.shop.shop.views
3
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
4
5
:Copyright: 2006-2021 Jochen Kupperschmidt
6
:License: Revised BSD (see `LICENSE` file for details)
7
"""
8
9 1
from flask import abort, request, url_for
10 1
from flask_babel import gettext
11
12 1
from .....services.brand import service as brand_service
13 1
from .....services.shop.order import service as order_service
14 1
from .....services.shop.order.transfer.models import PaymentState
15 1
from .....services.shop.shop import service as shop_service
16 1
from .....util.authorization import register_permission_enum
17 1
from .....util.framework.blueprint import create_blueprint
18 1
from .....util.framework.flash import flash_success
19 1
from .....util.framework.templating import templated
20 1
from .....util.views import (
21
    permission_required,
22
    redirect_to,
23
    respond_no_content_with_location,
24
)
25
26 1
from .authorization import ShopPermission
27
28
29 1
blueprint = create_blueprint('shop_shop_admin', __name__)
30
31
32 1
register_permission_enum(ShopPermission)
33
34
35 1
@blueprint.get('/shops/<shop_id>/dashboard')
36 1
@permission_required(ShopPermission.view)
37 1
@templated
38
def dashboard(shop_id):
39
    """Show the shop dashboard."""
40
    shop = _get_shop_or_404(shop_id)
41
42
    brand = brand_service.get_brand(shop.brand_id)
43
44
    order_counts_by_payment_state = (
45
        order_service.count_orders_per_payment_state(shop.id)
46
    )
47
48
    return {
49
        'shop': shop,
50
        'brand': brand,
51
52
        'order_counts_by_payment_state': order_counts_by_payment_state,
53
        'PaymentState': PaymentState,
54
    }
55
56
57 1
@blueprint.get('/for_shop/<shop_id>')
58 1
@permission_required(ShopPermission.view)
59 1
@templated
60
def view(shop_id):
61
    """Show the shop."""
62
    shop = _get_shop_or_404(shop_id)
63
64
    brand = brand_service.get_brand(shop.brand_id)
65
66
    order_counts_by_payment_state = (
67
        order_service.count_orders_per_payment_state(shop.id)
68
    )
69
70
    return {
71
        'shop': shop,
72
        'brand': brand,
73
74
        'order_counts_by_payment_state': order_counts_by_payment_state,
75
        'PaymentState': PaymentState,
76
77
        'settings': shop.extra_settings,
78
    }
79
80
81 1
@blueprint.get('/for_brand/<brand_id>')
82 1
@permission_required(ShopPermission.view)
83 1
@templated
84
def view_for_brand(brand_id):
85
    brand = brand_service.find_brand(brand_id)
86
    if brand is None:
87
        abort(404)
88
89
    shop = shop_service.find_shop_for_brand(brand.id)
90
    if shop is not None:
91
        return redirect_to('.view', shop_id=shop.id)
92
93
    return {
94
        'brand': brand,
95
    }
96
97
98 1
@blueprint.post('/for_brand/<brand_id>')
99 1
@permission_required(ShopPermission.create)
100 1
@respond_no_content_with_location
101
def create(brand_id):
102
    """Create a shop."""
103 1
    brand = brand_service.find_brand(brand_id)
104 1
    if brand is None:
105
        abort(404)
106
107 1
    shop_id = brand.id
108 1
    title = brand.title
109
110 1
    shop = shop_service.create_shop(shop_id, brand.id, title)
111
112 1
    flash_success(gettext('Shop has been created.'))
113 1
    return url_for('.view', shop_id=shop.id)
114
115
116 1
def _get_shop_or_404(shop_id):
117
    shop = shop_service.find_shop(shop_id)
118
119
    if shop is None:
120
        abort(404)
121
122
    return shop
123