~homeworkprod/byceps

ref: 342e5296ae417372e544ce931b442e98301c17a1 byceps/byceps/blueprints/admin/shop/shop/views.py -rw-r--r-- 2.5 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
"""
byceps.blueprints.admin.shop.shop.views
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

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

from flask import abort, request, url_for
from flask_babel import gettext

from .....services.brand import service as brand_service
from .....services.shop.order import service as order_service
from .....services.shop.order.transfer.models import PaymentState
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.flash import flash_success
from .....util.framework.templating import templated
from .....util.views import (
    permission_required,
    redirect_to,
    respond_no_content_with_location,
)

from .authorization import ShopPermission


blueprint = create_blueprint('shop_shop_admin', __name__)


register_permission_enum(ShopPermission)


@blueprint.route('/for_shop/<shop_id>')
@permission_required(ShopPermission.view)
@templated
def view(shop_id):
    """Show the shop."""
    shop = _get_shop_or_404(shop_id)

    brand = brand_service.get_brand(shop.brand_id)

    order_counts_by_payment_state = (
        order_service.count_orders_per_payment_state(shop.id)
    )

    return {
        'shop': shop,
        'brand': brand,

        'order_counts_by_payment_state': order_counts_by_payment_state,
        'PaymentState': PaymentState,

        'settings': shop.extra_settings,
    }


@blueprint.route('/for_brand/<brand_id>')
@permission_required(ShopPermission.view)
@templated
def view_for_brand(brand_id):
    brand = brand_service.find_brand(brand_id)
    if brand is None:
        abort(404)

    shop = shop_service.find_shop_for_brand(brand.id)
    if shop is not None:
        return redirect_to('.view', shop_id=shop.id)

    return {
        'brand': brand,
    }


@blueprint.route('/for_brand/<brand_id>', methods=['POST'])
@permission_required(ShopPermission.create)
@respond_no_content_with_location
def create(brand_id):
    """Create a shop."""
    brand = brand_service.find_brand(brand_id)
    if brand is None:
        abort(404)

    shop_id = brand.id
    title = brand.title

    shop = shop_service.create_shop(shop_id, brand.id, title)

    flash_success(gettext('Shop has been created.'))
    return url_for('.view', shop_id=shop.id)


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

    if shop is None:
        abort(404)

    return shop