Passed
Push — main ( 383fab...01458e )
by Jochen
04:17
created

UpdateForm.set_brand_choices()   A

Complexity

Conditions 2

Size

Total Lines 4
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 4
nop 1
dl 0
loc 4
ccs 4
cts 4
cp 1
crap 2
rs 10
c 0
b 0
f 0
1
"""
2
byceps.blueprints.admin.site.forms
3
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
4
5
:Copyright: 2006-2021 Jochen Kupperschmidt
6
:License: Revised BSD (see `LICENSE` file for details)
7
"""
8
9 1
from flask_babel import lazy_gettext, pgettext
10 1
from wtforms import BooleanField, SelectField, StringField
11 1
from wtforms.validators import InputRequired, Length, Optional
12
13 1
from ....util.l10n import LocalizedForm
14
15 1
from ....services.board import board_service
16 1
from ....services.brand import service as brand_service
17 1
from ....services.news import channel_service as news_channel_service
18 1
from ....services.party import service as party_service
19 1
from ....services.shop.storefront import service as storefront_service
20
21
22 1
class _BaseForm(LocalizedForm):
23 1
    title = StringField(
24
        lazy_gettext('Title'),
25
        validators=[InputRequired(), Length(min=1, max=40)],
26
    )
27 1
    server_name = StringField(
28
        lazy_gettext('Server name'), validators=[InputRequired()]
29
    )
30 1
    party_id = SelectField(lazy_gettext('Party'), validators=[Optional()])
31 1
    enabled = BooleanField(lazy_gettext('enabled'))
32 1
    user_account_creation_enabled = BooleanField(
33
        lazy_gettext('User registration open')
34
    )
35 1
    login_enabled = BooleanField(lazy_gettext('User login open'))
36 1
    board_id = SelectField(lazy_gettext('Board ID'), validators=[Optional()])
37 1
    storefront_id = SelectField(
38
        lazy_gettext('Storefront ID'), validators=[Optional()]
39
    )
40
41 1
    def set_party_choices(self, brand_id):
42 1
        parties = party_service.get_parties_for_brand(brand_id)
43 1
        parties.sort(key=lambda party: party.starts_at, reverse=True)
44
45 1
        choices = [(p.id, p.title) for p in parties]
46 1
        choices.insert(0, ('', pgettext('party', '<none>')))
47 1
        self.party_id.choices = choices
48
49 1
    def set_board_choices(self, brand_id):
50 1
        boards = board_service.get_boards_for_brand(brand_id)
51 1
        boards.sort(key=lambda board: board.id)
52
53 1
        choices = [(b.id, b.id) for b in boards]
54 1
        choices.insert(0, ('', pgettext('board', '<none>')))
55 1
        self.board_id.choices = choices
56
57 1
    def set_storefront_choices(self):
58 1
        storefronts = storefront_service.get_all_storefronts()
59 1
        storefronts.sort(key=lambda storefront: storefront.id)
60
61 1
        choices = [(s.id, s.id) for s in storefronts]
62 1
        choices.insert(0, ('', pgettext('storefront', '<none>')))
63 1
        self.storefront_id.choices = choices
64
65
66 1
class CreateForm(_BaseForm):
67 1
    id = StringField(
68
        lazy_gettext('ID'), validators=[InputRequired(), Length(min=1, max=40)]
69
    )
70
71
72 1
class UpdateForm(_BaseForm):
73 1
    brand_id = SelectField(lazy_gettext('Brand'), validators=[InputRequired()])
74 1
    archived = BooleanField(lazy_gettext('archived'))
75
76 1
    def set_brand_choices(self):
77 1
        brands = brand_service.get_all_brands()
78 1
        brands.sort(key=lambda brand: brand.title)
79 1
        self.brand_id.choices = [(brand.id, brand.title) for brand in brands]
80
81
82 1
class AddNewsChannelForm(LocalizedForm):
83 1
    news_channel_id = SelectField(
84
        lazy_gettext('News channel ID'), validators=[InputRequired()]
85
    )
86
87 1
    def set_news_channel_choices(self, brand_id):
88
        news_channels = news_channel_service.get_channels_for_brand(brand_id)
89
        news_channels.sort(key=lambda channel: channel.id)
90
91
        choices = [(c.id, c.id) for c in news_channels]
92
        self.news_channel_id.choices = choices
93