~homeworkprod/byceps

ref: 4144a29fdeb34e20d44e5cd78a324d9f9f6a2b0d byceps/byceps/blueprints/admin/ticketing/forms.py -rw-r--r-- 1.2 KiB
4144a29f — Jochen Kupperschmidt Dim archived brands in admin brand navigation dropdown 1 year, 1 month 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
"""
byceps.blueprints.admin.ticketing.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.ticketing import ticket_code_service
from ....services.user import service as user_service
from ....util.l10n import LocalizedForm


def validate_user(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'))

    field.data = user.to_dto()


class UpdateCodeForm(LocalizedForm):
    code = StringField(lazy_gettext('Code'), [InputRequired()])

    @staticmethod
    def validate_code(form, field):
        if not ticket_code_service.is_ticket_code_wellformed(field.data):
            raise ValidationError(lazy_gettext('Invalid format'))


class SpecifyUserForm(LocalizedForm):
    user = StringField(
        lazy_gettext('Username'), [InputRequired(), validate_user]
    )