~homeworkprod/byceps

ref: 342e5296ae417372e544ce931b442e98301c17a1 byceps/byceps/blueprints/admin/orga/forms.py -rw-r--r-- 1.2 KiB
342e5296 — Jochen Kupperschmidt Require body blocks in base templates 6 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
"""
byceps.blueprints.admin.orga.forms
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

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

from flask_babel import lazy_gettext
from wtforms import StringField
from wtforms.validators import InputRequired, ValidationError

from ....services.orga import service as orga_service
from ....services.user import service as user_service
from ....util.l10n import LocalizedForm


def validate_user_screen_name(form, field):
    screen_name = field.data.strip()

    user = user_service.find_user_by_screen_name(
        screen_name, case_insensitive=True
    )

    if user is None:
        raise ValidationError(lazy_gettext('Unknown username'))

    orga_flag = orga_service.find_orga_flag(form.brand_id, user.id)
    if orga_flag is not None:
        raise ValidationError(
            lazy_gettext('The user already is an organizer for this brand.')
        )

    field.data = user.to_dto()


class OrgaFlagCreateForm(LocalizedForm):

    def __init__(self, *args, brand_id=None, **kwargs):
        super().__init__(*args, **kwargs)
        self.brand_id = brand_id

    user = StringField(
        lazy_gettext('Username'),
        [InputRequired(), validate_user_screen_name],
    )