~homeworkprod/byceps

ref: 342e5296ae417372e544ce931b442e98301c17a1 byceps/byceps/blueprints/admin/shop/storefront/views.py -rw-r--r-- 7.6 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
"""
byceps.blueprints.admin.shop.storefront.views
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

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

from flask import abort, request
from flask_babel import gettext

from .....services.brand import service as brand_service
from .....services.shop.catalog import service as catalog_service
from .....services.shop.order import sequence_service as order_sequence_service
from .....services.shop.shop import service as shop_service
from .....services.shop.storefront import service as storefront_service
from .....util.framework.blueprint import create_blueprint
from .....util.framework.flash import flash_error, flash_success
from .....util.framework.templating import templated
from .....util.views import permission_required, redirect_to

from ..shop.authorization import ShopPermission

from .forms import StorefrontCreateForm, StorefrontUpdateForm


blueprint = create_blueprint('shop_storefront_admin', __name__)


@blueprint.route('/for_shop/<shop_id>')
@permission_required(ShopPermission.view)
@templated
def index_for_shop(shop_id):
    """List storefronts for that shop."""
    shop = _get_shop_or_404(shop_id)

    brand = brand_service.get_brand(shop.brand_id)

    storefronts = storefront_service.get_storefronts_for_shop(shop.id)

    order_number_prefixes_by_sequence_id = (
        _get_order_number_prefixes_by_sequence_id(storefronts, shop.id)
    )

    return {
        'shop': shop,
        'brand': brand,
        'storefronts': storefronts,
        'order_number_prefixes_by_sequence_id': order_number_prefixes_by_sequence_id,
    }


def _get_order_number_prefixes_by_sequence_id(storefronts, shop_id):
    sequence_ids = {sf.order_number_sequence_id for sf in storefronts}
    sequences = order_sequence_service.find_order_number_sequences_for_shop(
        shop_id
    )
    return {seq.id: seq.prefix for seq in sequences}


@blueprint.route('/<storefront_id>')
@permission_required(ShopPermission.view)
@templated
def view(storefront_id):
    """Show a single storefront."""
    storefront = _get_storefront_or_404(storefront_id)

    shop = shop_service.get_shop(storefront.shop_id)

    brand = brand_service.get_brand(shop.brand_id)

    order_number_sequence = order_sequence_service.find_order_number_sequence(
        storefront.order_number_sequence_id
    )
    order_number_prefix = order_number_sequence.prefix

    return {
        'storefront': storefront,
        'shop': shop,
        'brand': brand,
        'order_number_prefix': order_number_prefix,
    }


@blueprint.route('/for_shop/<shop_id>/create')
@permission_required(ShopPermission.create)
@templated
def create_form(shop_id, erroneous_form=None):
    """Show form to create a storefront."""
    shop = _get_shop_or_404(shop_id)

    brand = brand_service.get_brand(shop.brand_id)

    catalogs = catalog_service.get_all_catalogs()
    order_number_sequences = (
        order_sequence_service.find_order_number_sequences_for_shop(shop.id)
    )
    order_number_sequence_available = bool(order_number_sequences)

    form = erroneous_form if erroneous_form else StorefrontCreateForm()
    form.set_catalog_choices(catalogs)
    form.set_order_number_sequence_choices(order_number_sequences)

    return {
        'shop': shop,
        'brand': brand,
        'order_number_sequence_available': order_number_sequence_available,
        'form': form,
    }


@blueprint.route('/for_shop/<shop_id>', methods=['POST'])
@permission_required(ShopPermission.create)
def create(shop_id):
    """Create a storefront."""
    shop = _get_shop_or_404(shop_id)

    form = StorefrontCreateForm(request.form)

    catalogs = catalog_service.get_all_catalogs()

    order_number_sequences = (
        order_sequence_service.find_order_number_sequences_for_shop(shop.id)
    )
    if not order_number_sequences:
        flash_error(
            gettext('No order number sequences are defined for this shop.')
        )
        return create_form(shop_id, form)

    form.set_catalog_choices(catalogs)
    form.set_order_number_sequence_choices(order_number_sequences)
    if not form.validate():
        return create_form(shop_id, form)

    storefront_id = form.id.data.strip()
    catalog_id = form.catalog_id.data or None
    order_number_sequence_id = form.order_number_sequence_id.data

    if not order_number_sequence_id:
        flash_error(gettext('No valid order number sequence was specified.'))
        return create_form(shop_id, form)

    order_number_sequence = order_sequence_service.find_order_number_sequence(
        order_number_sequence_id
    )
    if (order_number_sequence is None) or (
        order_number_sequence.shop_id != shop.id
    ):
        flash_error(gettext('No valid order number sequence was specified.'))
        return create_form(shop_id, form)

    try:
        item_number = order_sequence_service.generate_order_number(
            order_number_sequence.id
        )
    except order_sequence_service.OrderNumberGenerationFailed as e:
        abort(500, e.message)

    storefront = storefront_service.create_storefront(
        storefront_id,
        shop.id,
        order_number_sequence.id,
        closed=False,
        catalog_id=catalog_id,
    )

    flash_success(
        gettext(
            'Storefront "%(storefront_id)s" has been created.',
            storefront_id=storefront.id,
        )
    )
    return redirect_to('.view', storefront_id=storefront.id)


@blueprint.route('/<storefront_id>/update')
@permission_required(ShopPermission.update)
@templated
def update_form(storefront_id, erroneous_form=None):
    """Show form to update a storefront."""
    storefront = _get_storefront_or_404(storefront_id)

    shop = shop_service.get_shop(storefront.shop_id)

    brand = brand_service.get_brand(shop.brand_id)

    catalogs = catalog_service.get_all_catalogs()
    order_number_sequences = (
        order_sequence_service.find_order_number_sequences_for_shop(shop.id)
    )

    form = (
        erroneous_form
        if erroneous_form
        else StorefrontUpdateForm(obj=storefront)
    )
    form.set_catalog_choices(catalogs)
    form.set_order_number_sequence_choices(order_number_sequences)

    return {
        'storefront': storefront,
        'shop': shop,
        'brand': brand,
        'form': form,
    }


@blueprint.route('/<storefront_id>', methods=['POST'])
@permission_required(ShopPermission.update)
def update(storefront_id):
    """Update a storefront."""
    storefront = _get_storefront_or_404(storefront_id)

    catalogs = catalog_service.get_all_catalogs()
    order_number_sequences = (
        order_sequence_service.find_order_number_sequences_for_shop(
            storefront.shop_id
        )
    )

    form = StorefrontUpdateForm(request.form)
    form.set_catalog_choices(catalogs)
    form.set_order_number_sequence_choices(order_number_sequences)
    if not form.validate():
        return update_form(storefront_id, form)

    order_number_sequence_id = form.order_number_sequence_id.data
    catalog_id = form.catalog_id.data or None
    closed = form.closed.data

    storefront = storefront_service.update_storefront(
        storefront.id, catalog_id, order_number_sequence_id, closed
    )

    flash_success(
        gettext(
            'Storefront "%(storefront_id)s" has been updated.',
            storefront_id=storefront.id,
        )
    )
    return redirect_to('.view', storefront_id=storefront.id)


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

    if shop is None:
        abort(404)

    return shop


def _get_storefront_or_404(storefront_id):
    storefront = storefront_service.find_storefront(storefront_id)

    if storefront is None:
        abort(404)

    return storefront