Passed
Push — main ( bbdf0c...72e25d )
by Jochen
01:31
created

CreateForm.validate_id()   A

Complexity

Conditions 2

Size

Total Lines 8
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 7
nop 2
dl 0
loc 8
ccs 3
cts 3
cp 1
crap 2
rs 10
c 0
b 0
f 0
1
"""
2
byceps.blueprints.admin.site.forms
3
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
4
5
:Copyright: 2014-2024 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, ValidationError
12
13 1
from byceps.services.board import board_service
14 1
from byceps.services.news import news_channel_service
15 1
from byceps.services.party import party_service
16 1
from byceps.services.shop.shop import shop_service
17 1
from byceps.services.shop.storefront import storefront_service
18 1
from byceps.services.site import site_service
19
from byceps.util.forms import MultiCheckboxField
20
from byceps.util.l10n import LocalizedForm
21 1
22 1
23
class _BaseForm(LocalizedForm):
24
    title = StringField(
25
        lazy_gettext('Title'),
26 1
        validators=[InputRequired(), Length(min=1, max=40)],
27
    )
28
    server_name = StringField(
29 1
        lazy_gettext('Server name'), validators=[InputRequired()]
30 1
    )
31 1
    party_id = SelectField(lazy_gettext('Party'), validators=[Optional()])
32
    enabled = BooleanField(lazy_gettext('enabled'))
33
    user_account_creation_enabled = BooleanField(
34 1
        lazy_gettext('User registration open')
35 1
    )
36 1
    login_enabled = BooleanField(lazy_gettext('User login open'))
37
    board_id = SelectField(lazy_gettext('Board ID'), validators=[Optional()])
38
    storefront_id = SelectField(
39 1
        lazy_gettext('Storefront ID'), validators=[Optional()]
40 1
    )
41
    is_intranet = BooleanField(lazy_gettext('Use as intranet'))
42
    check_in_on_login = BooleanField(
43
        lazy_gettext('Check in attendees on login to this site')
44 1
    )
45 1
46 1
    def set_party_choices(self, brand_id):
47
        parties = party_service.get_parties_for_brand(brand_id)
48 1
        parties.sort(key=lambda party: party.starts_at, reverse=True)
49 1
50 1
        choices = [(p.id, p.title) for p in parties]
51
        choices.insert(0, ('', '<' + pgettext('party', 'none') + '>'))
52 1
        self.party_id.choices = choices
53 1
54 1
    def set_board_choices(self, brand_id):
55
        boards = board_service.get_boards_for_brand(brand_id)
56 1
        boards.sort(key=lambda board: board.id)
57 1
58 1
        choices = [(b.id, b.id) for b in boards]
59
        choices.insert(0, ('', '<' + pgettext('board', 'none') + '>'))
60 1
        self.board_id.choices = choices
61 1
62 1
    def set_storefront_choices(self, brand_id):
63
        shop = shop_service.find_shop_for_brand(brand_id)
64 1
        if shop:
65 1
            storefronts = list(
66 1
                storefront_service.get_storefronts_for_shop(shop.id)
67
            )
68
            storefronts.sort(key=lambda storefront: storefront.id)
69 1
70 1
            choices = [(s.id, s.id) for s in storefronts]
71
        else:
72
            choices = []
73
74
        choices.insert(0, ('', '<' + pgettext('storefront', 'none') + '>'))
75 1
        self.storefront_id.choices = choices
76 1
77 1
78
class CreateForm(_BaseForm):
79 1
    id = StringField(
80 1
        lazy_gettext('ID'), validators=[InputRequired(), Length(min=1, max=40)]
81 1
    )
82 1
83
    @staticmethod
84
    def validate_id(form, field):
85 1
        site_id = field.data.strip()
86 1
87
        if site_service.find_site(site_id):
88
            raise ValidationError(
89
                lazy_gettext(
90 1
                    'This value is not available. Please choose another.'
91
                )
92
            )
93
94
    @staticmethod
95
    def validate_title(form, field):
96
        title = field.data.strip()
97
98
        if not site_service.is_title_available(title):
99
            raise ValidationError(
100
                lazy_gettext(
101
                    'This value is not available. Please choose another.'
102
                )
103
            )
104
105
    @staticmethod
106
    def validate_server_name(form, field):
107
        server_name = field.data.strip()
108
109
        if not site_service.is_server_name_available(server_name):
110
            raise ValidationError(
111
                lazy_gettext(
112
                    'This value is not available. Please choose another.'
113
                )
114
            )
115
116
117
class UpdateForm(_BaseForm):
118
    archived = BooleanField(lazy_gettext('archived'))
119
120
    def __init__(
121
        self, current_title: str, current_server_name: str, *args, **kwargs
122
    ):
123
        super().__init__(*args, **kwargs)
124
        self._current_title = current_title
125
        self._current_server_name = current_server_name
126
127
    @staticmethod
128
    def validate_title(form, field):
129
        title = field.data.strip()
130
131
        if title != form._current_title and not site_service.is_title_available(
132
            title
133
        ):
134
            raise ValidationError(
135
                lazy_gettext(
136
                    'This value is not available. Please choose another.'
137
                )
138
            )
139
140
    @staticmethod
141
    def validate_server_name(form, field):
142
        server_name = field.data.strip()
143
144
        if (
145
            server_name != form._current_server_name
146
            and not site_service.is_server_name_available(server_name)
147
        ):
148
            raise ValidationError(
149
                lazy_gettext(
150
                    'This value is not available. Please choose another.'
151
                )
152
            )
153
154
155
class AssignNewsChannelsForm(LocalizedForm):
156
    news_channel_ids = MultiCheckboxField(
157
        lazy_gettext('News channels'), validators=[Optional()]
158
    )
159
160
    def set_news_channel_id_choices(self, brand_id):
161
        news_channels = news_channel_service.get_channels_for_brand(brand_id)
162
163
        news_channel_ids = [c.id for c in news_channels]
164
        news_channel_ids.sort()
165
166
        choices = [(channel_id, channel_id) for channel_id in news_channel_ids]
167
        self.news_channel_ids.choices = choices
168