~homeworkprod/byceps

ref: 533662eefb314d54b168e5a46bc07c88b5f88be4 byceps/byceps/blueprints/admin/orga_team/forms.py -rw-r--r-- 1.8 KiB
533662ee — Jochen Kupperschmidt Use Flask v2.0's `delete`/`get`/`patch`/`post` route shortcuts 5 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
"""
byceps.blueprints.admin.orga_team.forms
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

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

from flask_babel import lazy_gettext
from wtforms import SelectField, StringField
from wtforms.validators import InputRequired, Length

from ....util.l10n import LocalizedForm


class OrgaTeamCreateForm(LocalizedForm):
    title = StringField(
        lazy_gettext('Title'),
        validators=[InputRequired(), Length(min=1, max=40)],
    )


class OrgaTeamsCopyForm(LocalizedForm):
    party_id = SelectField(lazy_gettext('From party'), [InputRequired()])

    def set_party_choices(self, parties, team_count_per_party=None):
        def get_label(party):
            label = party.title
            if team_count_per_party:
                team_count = team_count_per_party.get(party.id, 0)
                label += lazy_gettext(
                    ' (%(team_count)s teams)', team_count=team_count
                )
            return label

        choices = [(party.id, get_label(party)) for party in parties]

        self.party_id.choices = choices


class MembershipFormBase(LocalizedForm):
    duties = StringField(lazy_gettext('Function'))


class MembershipCreateForm(MembershipFormBase):
    user_id = SelectField(lazy_gettext('User'), [InputRequired()])

    def set_user_choices(self, orgas):
        choices = [(str(orga.id), orga.screen_name) for orga in orgas]
        self.user_id.choices = choices


class MembershipUpdateForm(MembershipFormBase):
    orga_team_id = SelectField(lazy_gettext('Team'), [InputRequired()])

    def set_orga_team_choices(self, orga_teams):
        choices = [(str(team.id), team.title) for team in orga_teams]
        choices.sort(key=lambda t: t[1])
        self.orga_team_id.choices = choices