1
|
|
|
""" |
2
|
|
|
byceps.blueprints.admin.attendance.views |
3
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
4
|
|
|
|
5
|
|
|
:Copyright: 2006-2020 Jochen Kupperschmidt |
6
|
|
|
:License: Modified BSD, see LICENSE for details. |
7
|
|
|
""" |
8
|
|
|
|
9
|
1 |
|
from flask import abort |
10
|
|
|
|
11
|
1 |
|
from ....services.brand import service as brand_service |
12
|
1 |
|
from ....services.party import service as party_service |
13
|
1 |
|
from ....services.ticketing import attendance_service |
14
|
1 |
|
from ....services.user import service as user_service |
15
|
1 |
|
from ....util.framework.blueprint import create_blueprint |
16
|
1 |
|
from ....util.framework.templating import templated |
17
|
|
|
|
18
|
1 |
|
from ...common.authorization.decorators import permission_required |
19
|
1 |
|
from ...common.authorization.registry import permission_registry |
20
|
|
|
|
21
|
1 |
|
from ..core.authorization import AdminPermission |
22
|
|
|
|
23
|
|
|
|
24
|
1 |
|
blueprint = create_blueprint('attendance_admin', __name__) |
25
|
|
|
|
26
|
|
|
|
27
|
1 |
|
@blueprint.route('/brands/<brand_id>') |
28
|
1 |
|
@permission_required(AdminPermission.access) |
29
|
1 |
|
@templated |
30
|
|
|
def view_for_brand(brand_id): |
31
|
|
|
"""Show most frequent attendees for parties of this brand.""" |
32
|
1 |
|
brand = brand_service.find_brand(brand_id) |
33
|
1 |
|
if brand is None: |
34
|
|
|
abort(404) |
35
|
|
|
|
36
|
1 |
|
parties = party_service.get_parties_for_brand(brand.id) |
37
|
1 |
|
if parties: |
38
|
1 |
|
parties.sort(key=lambda party: party.starts_at, reverse=True) |
39
|
1 |
|
most_recent_party = parties[0] |
40
|
|
|
else: |
41
|
|
|
most_recent_party = None |
42
|
1 |
|
party_total = len(parties) |
43
|
|
|
|
44
|
1 |
|
top_attendees = _get_top_attendees(brand.id) |
45
|
|
|
|
46
|
1 |
|
return { |
47
|
|
|
'brand': brand, |
48
|
|
|
'party_total': party_total, |
49
|
|
|
'most_recent_party': most_recent_party, |
50
|
|
|
'top_attendees': top_attendees, |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
|
54
|
1 |
|
def _get_top_attendees(brand_id): |
55
|
1 |
|
top_attendee_ids = attendance_service.get_top_attendees_for_brand(brand_id) |
56
|
|
|
|
57
|
1 |
|
top_attendees = _replace_user_ids_with_users(top_attendee_ids) |
58
|
|
|
|
59
|
|
|
# Sort by highest attendance count first, alphabetical screen name second. |
60
|
1 |
|
top_attendees.sort(key=lambda att: (-att[1], att[0].screen_name)) |
61
|
|
|
|
62
|
1 |
|
return top_attendees |
63
|
|
|
|
64
|
|
|
|
65
|
1 |
|
def _replace_user_ids_with_users(attendee_ids): |
66
|
1 |
|
users_by_id = _get_users_by_id(attendee_ids) |
67
|
|
|
|
68
|
1 |
|
return [ |
69
|
|
|
(users_by_id[user_id], attendance_count) |
70
|
|
|
for user_id, attendance_count in attendee_ids |
71
|
|
|
] |
72
|
|
|
|
73
|
|
|
|
74
|
1 |
|
def _get_users_by_id(attendee_ids): |
75
|
1 |
|
user_ids = {user_id for user_id, attendance_count in attendee_ids} |
76
|
1 |
|
users = user_service.find_users(user_ids, include_avatars=False) |
77
|
|
|
return user_service.index_users_by_id(users) |
78
|
|
|
|