~homeworkprod/byceps

ref: 7d53072ee4883f70b04658c43af5a1cdd66101eb byceps/byceps/services/seating/seat_service.py -rw-r--r-- 4.2 KiB
7d53072e — Jochen Kupperschmidt Introduce `get_seat` 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
"""
byceps.services.seating.seat_service
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

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

from __future__ import annotations
from typing import Optional, Sequence

from ...database import db
from ...typing import PartyID

from ..ticketing.dbmodels.category import Category as DbTicketCategory
from ..ticketing.dbmodels.ticket import Ticket as DbTicket
from ..ticketing.transfer.models import TicketCategory, TicketCategoryID

from .dbmodels.area import Area as DbArea
from .dbmodels.seat import Seat as DbSeat
from .transfer.models import AreaID, SeatID, SeatUtilization


def create_seat(
    area: DbArea, coord_x: int, coord_y: int, category_id: TicketCategoryID
) -> DbSeat:
    """Create a seat."""
    seat = DbSeat(area, category_id, coord_x=coord_x, coord_y=coord_y)

    db.session.add(seat)
    db.session.commit()

    return seat


def delete_seat(seat_id: SeatID) -> None:
    """Delete a seat."""
    db.session.query(DbSeat) \
        .filter_by(id=seat_id) \
        .delete()
    db.session.commit()


def count_occupied_seats_by_category(
    party_id: PartyID,
) -> list[tuple[TicketCategory, int]]:
    """Count occupied seats for the party, grouped by ticket category."""
    subquery = db.session \
        .query(
            DbSeat.id,
            DbSeat.category_id
        ) \
        .join(DbTicket) \
        .filter_by(revoked=False) \
        .subquery()

    rows = db.session \
        .query(
            DbTicketCategory.id,
            DbTicketCategory.party_id,
            DbTicketCategory.title,
            db.func.count(subquery.c.id)
        ) \
        .outerjoin(subquery, db.and_(DbTicketCategory.id == subquery.c.category_id)) \
        .filter(DbTicketCategory.party_id == party_id) \
        .group_by(DbTicketCategory.id) \
        .order_by(DbTicketCategory.id) \
        .all()

    return [(TicketCategory(row[0], row[1], row[2]), row[3]) for row in rows]


def count_occupied_seats_for_party(party_id: PartyID) -> int:
    """Count occupied seats for the party."""
    return DbSeat.query \
        .join(DbTicket) \
        .join(DbTicketCategory) \
        .filter(DbTicket.revoked == False) \
        .filter(DbTicketCategory.party_id == party_id) \
        .count()


def count_seats_for_party(party_id: PartyID) -> int:
    """Return the number of seats in seating areas for that party."""
    return DbSeat.query \
        .join(DbArea) \
        .filter(DbArea.party_id == party_id) \
        .count()


def get_seat_utilization(party_id: PartyID) -> SeatUtilization:
    """Return how many seats of how many in total are occupied."""
    occupied_seat_count = count_occupied_seats_for_party(party_id)
    total_seat_count = count_seats_for_party(party_id)

    return SeatUtilization(occupied_seat_count, total_seat_count)


def get_seat_total_per_area(party_id: PartyID) -> dict[AreaID, int]:
    """Return the number of seats per area for that party."""
    area_ids_and_seat_counts = db.session \
        .query(
            DbArea.id,
            db.func.count(DbSeat.id)
        ) \
        .filter_by(party_id=party_id) \
        .outerjoin(DbSeat) \
        .group_by(DbArea.id) \
        .all()

    return dict(area_ids_and_seat_counts)


def find_seat(seat_id: SeatID) -> Optional[DbSeat]:
    """Return the seat with that id, or `None` if not found."""
    return DbSeat.query.get(seat_id)


def get_seat(seat_id: SeatID) -> DbSeat:
    """Return the seat with that id, or raise an exception."""
    seat = find_seat(seat_id)

    if seat is None:
        raise ValueError(f'Unknown seat ID "{seat_id}"')

    return seat


def find_seats(seat_ids: set[SeatID]) -> set[DbSeat]:
    """Return the seats with those IDs."""
    if not seat_ids:
        return set()

    seats = DbSeat.query \
        .filter(DbSeat.id.in_(frozenset(seat_ids))) \
        .all()

    return set(seats)


def get_seats_with_tickets_for_area(area_id: AreaID) -> Sequence[DbSeat]:
    """Return the seats and their associated tickets (if available) for
    that area.
    """
    return DbSeat.query \
        .filter_by(area_id=area_id) \
        .options(
            db.joinedload('occupied_by_ticket'),
        ) \
        .all()