~homeworkprod/byceps

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

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

from flask import abort

from ....services.brand import service as brand_service
from ....services.party import service as party_service
from ....services.ticketing import attendance_service
from ....services.user import service as user_service
from ....util.framework.blueprint import create_blueprint
from ....util.framework.templating import templated
from ....util.views import permission_required

from ..core.authorization import AdminPermission


blueprint = create_blueprint('attendance_admin', __name__)


@blueprint.get('/brands/<brand_id>')
@permission_required(AdminPermission.access)
@templated
def view_for_brand(brand_id):
    """Show most frequent attendees for parties of this brand."""
    brand = brand_service.find_brand(brand_id)
    if brand is None:
        abort(404)

    parties = party_service.get_parties_for_brand(brand.id)
    if parties:
        parties.sort(key=lambda party: party.starts_at, reverse=True)
        most_recent_party = parties[0]
    else:
        most_recent_party = None
    party_total = len(parties)

    top_attendees = _get_top_attendees(brand.id)

    return {
        'brand': brand,
        'party_total': party_total,
        'most_recent_party': most_recent_party,
        'top_attendees': top_attendees,
    }


def _get_top_attendees(brand_id):
    top_attendee_ids = attendance_service.get_top_attendees_for_brand(brand_id)

    top_attendees = _replace_user_ids_with_users(top_attendee_ids)

    # Sort by highest attendance count first, alphabetical screen name second.
    top_attendees.sort(key=lambda att: (-att[1], att[0].screen_name))

    return top_attendees


def _replace_user_ids_with_users(attendee_ids):
    users_by_id = _get_users_by_id(attendee_ids)

    return [
        (users_by_id[user_id], attendance_count)
        for user_id, attendance_count in attendee_ids
    ]


def _get_users_by_id(attendee_ids):
    user_ids = {user_id for user_id, attendance_count in attendee_ids}
    users = user_service.find_users(user_ids, include_avatars=False)
    return user_service.index_users_by_id(users)