~homeworkprod/byceps

ref: 43bf3630475839afde6e691f0ac561dcb01aaba6 byceps/byceps/blueprints/attendance/service.py -rw-r--r-- 1.2 KiB
43bf3630 — Jochen Kupperschmidt Upgrade pytest on Travis CI to get v3.3 rather than v3.0 provided by default 4 years 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
"""
byceps.blueprints.attendance.service
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

:Copyright: 2006-2017 Jochen Kupperschmidt
"""

from collections import namedtuple
from typing import Dict, Iterator, Sequence

from ...services.seating.models.seat import Seat, SeatID
from ...services.seating import seat_service
from ...services.ticketing.models.ticket import Ticket
from ...services.user.models.user import User
from ...services.user import service as user_service
from ...typing import UserID


Attendee = namedtuple('Attendee', ['user', 'seat'])


def get_attendees(tickets: Sequence[Ticket]) -> Iterator[Attendee]:
    users_by_id = _get_users_by_id(tickets)
    seats_by_id = _get_seats_by_id(tickets)

    for t in tickets:
        yield Attendee(
            users_by_id[t.used_by_id],
            seats_by_id.get(t.occupied_seat_id),
        )


def _get_users_by_id(tickets) -> Dict[UserID, User]:
    user_ids = {t.used_by_id for t in tickets}
    users = user_service.find_users(user_ids)
    return {user.id: user for user in users}


def _get_seats_by_id(tickets) -> Dict[SeatID, Seat]:
    seat_ids = {t.occupied_seat_id for t in tickets}
    seats = seat_service.find_seats(seat_ids)
    return {seat.id: seat for seat in seats}