~homeworkprod/byceps

ref: 342e5296ae417372e544ce931b442e98301c17a1 byceps/byceps/blueprints/admin/orga/views.py -rw-r--r-- 6.2 KiB
342e5296 — Jochen Kupperschmidt Require body blocks in base templates 2 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
"""
byceps.blueprints.admin.orga.views
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

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

from typing import Optional

from flask import abort, g, request
from flask_babel import gettext

from ....services.brand import service as brand_service
from ....services.orga import birthday_service as orga_birthday_service
from ....services.orga import service as orga_service
from ....services.orga.transfer.models import Birthday
from ....services.user import service as user_service
from ....util.export import serialize_to_csv
from ....util.authorization import register_permission_enum
from ....util.framework.blueprint import create_blueprint
from ....util.framework.flash import flash_success
from ....util.framework.templating import templated
from ....util.views import (
    permission_required,
    redirect_to,
    respond_no_content,
    textified,
)

from ..orga_team.authorization import OrgaTeamPermission

from .authorization import OrgaBirthdayPermission, OrgaDetailPermission
from .forms import OrgaFlagCreateForm


blueprint = create_blueprint('orga_admin', __name__)


register_permission_enum(OrgaBirthdayPermission)
register_permission_enum(OrgaDetailPermission)
register_permission_enum(OrgaTeamPermission)


@blueprint.route('/persons/<brand_id>')
@permission_required(OrgaDetailPermission.view)
@templated
def persons_for_brand(brand_id):
    """List organizers for the brand with details."""
    brand = _get_brand_or_404(brand_id)

    orgas = orga_service.get_orgas_for_brand(brand.id)
    orgas.sort(key=user_service.get_sort_key_for_screen_name)

    orgas_with_birthdays = [(user, _to_birthday(user)) for user in orgas]

    return {
        'brand': brand,
        'orgas_with_birthdays': orgas_with_birthdays,
    }


def _to_birthday(user) -> Optional[Birthday]:
    dob = user.detail.date_of_birth

    if dob is None:
        return None

    return Birthday(dob)


@blueprint.route('/persons/<brand_id>/create')
@permission_required(OrgaTeamPermission.administrate_memberships)
@templated
def create_orgaflag_form(brand_id, erroneous_form=None):
    """Show form to give the organizer flag to a user."""
    brand = _get_brand_or_404(brand_id)

    form = erroneous_form if erroneous_form else OrgaFlagCreateForm()

    return {
        'brand': brand,
        'form': form,
    }


@blueprint.route('/persons/<brand_id>', methods=['POST'])
@permission_required(OrgaTeamPermission.administrate_memberships)
def create_orgaflag(brand_id):
    """Give the organizer flag to a user."""
    brand = _get_brand_or_404(brand_id)

    form = OrgaFlagCreateForm(request.form, brand_id=brand.id)
    if not form.validate():
        return create_orgaflag_form(brand.id, form)

    user = form.user.data
    initiator = g.user

    orga_flag = orga_service.add_orga_flag(brand.id, user.id, initiator.id)

    flash_success(
        gettext(
            '%(screen_name)s has received the orga flag '
            'for brand %(brand_title)s.',
            screen_name=orga_flag.user.screen_name,
            brand_title=orga_flag.brand.title,
        )
    )
    return redirect_to('.persons_for_brand', brand_id=orga_flag.brand.id)


@blueprint.route('/persons/<brand_id>/<uuid:user_id>', methods=['DELETE'])
@permission_required(OrgaTeamPermission.administrate_memberships)
@respond_no_content
def remove_orgaflag(brand_id, user_id):
    """Remove the organizer flag for a brand from a person."""
    orga_flag = orga_service.find_orga_flag(brand_id, user_id)

    if orga_flag is None:
        abort(404)

    brand = orga_flag.brand
    user = orga_flag.user
    initiator = g.user

    orga_service.remove_orga_flag(orga_flag, initiator.id)

    flash_success(
        gettext(
            '%(screen_name)s has lost the orga flag '
            'for brand %(brand_title)s.',
            screen_name=user.screen_name,
            brand_title=brand.title,
        )
    )


@blueprint.route('/persons/<brand_id>/export')
@permission_required(OrgaDetailPermission.view)
@textified
def export_persons(brand_id):
    """Export the list of organizers for the brand as a CSV document in
    Microsoft Excel dialect.
    """
    brand = _get_brand_or_404(brand_id)

    field_name_screen_name = 'Benutzername'
    field_name_first_names = 'Vorname'
    field_name_last_names = 'Nachname'
    field_name_date_of_birth = 'Geburtstag'
    field_name_street = 'Straße'
    field_name_zip_code = 'PLZ'
    field_name_city = 'Ort'
    field_name_country = 'Land'
    field_name_email_address = 'E-Mail-Adresse'
    field_name_phone_number = 'Telefonnummer'

    field_names = [
        field_name_screen_name,
        field_name_first_names,
        field_name_last_names,
        field_name_date_of_birth,
        field_name_street,
        field_name_zip_code,
        field_name_city,
        field_name_country,
        field_name_email_address,
        field_name_phone_number,
    ]

    def to_dict(user):
        date_of_birth = (
            user.detail.date_of_birth.strftime('%d.%m.%Y')
            if user.detail.date_of_birth
            else None
        )

        return {
            field_name_screen_name: user.screen_name,
            field_name_first_names: user.detail.first_names,
            field_name_last_names: user.detail.last_name,
            field_name_date_of_birth: date_of_birth,
            field_name_street: user.detail.street,
            field_name_zip_code: user.detail.zip_code,
            field_name_city: user.detail.city,
            field_name_country: user.detail.country,
            field_name_email_address: user.email_address,
            field_name_phone_number: user.detail.phone_number,
        }

    orgas = orga_service.get_orgas_for_brand(brand.id)
    orgas = [orga for orga in orgas if not orga.deleted]
    orgas.sort(key=user_service.get_sort_key_for_screen_name)
    rows = map(to_dict, orgas)
    return serialize_to_csv(field_names, rows)


@blueprint.route('/birthdays')
@permission_required(OrgaBirthdayPermission.view)
@templated
def birthdays():
    orgas = list(
        orga_birthday_service.collect_orgas_with_next_birthdays(limit=5)
    )

    return {
        'orgas': orgas,
    }


def _get_brand_or_404(brand_id):
    brand = brand_service.find_brand(brand_id)

    if brand is None:
        abort(404)

    return brand