Passed
Push — master ( 1bb58d...3058b1 )
by Jochen
02:25
created

byceps.blueprints.seating.service   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 26
dl 0
loc 45
rs 10
c 0
b 0
f 0
wmc 8

4 Functions

Rating   Name   Duplication   Size   Complexity  
A _get_ticket_user_ids() 0 5 3
A get_users() 0 9 1
A _get_seat_occupier_ids() 0 3 1
A _get_seat_tickets() 0 4 3
1
"""
2
byceps.blueprints.seating.service
3
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
4
5
:Copyright: 2006-2019 Jochen Kupperschmidt
6
:License: Modified BSD, see LICENSE for details.
7
"""
8
9
from typing import Dict, Iterator, Sequence
10
11
from ...services.seating.models.seat import Seat
12
from ...services.ticketing.models.ticket import Ticket as DbTicket
13
from ...services.user import service as user_service
14
from ...services.user.transfer.models import User
15
from ...typing import UserID
16
17
18
def get_users(
19
    seats: Sequence[Seat], tickets: Sequence[DbTicket]
20
) -> Dict[UserID, User]:
21
    user_ids = set()
22
    user_ids.extend(_get_seat_occupier_ids(seats))
23
    user_ids.extend(_get_ticket_user_ids(tickets))
24
25
    users = user_service.find_users(user_ids, include_avatars=True)
26
    return user_service.index_users_by_id(users)
27
28
29
def _get_seat_occupier_ids(seats: Sequence[Seat]) -> Iterator[UserID]:
30
    tickets = _get_seat_tickets(seats)
31
    yield from _get_ticket_user_ids(tickets)
32
33
34
def _get_seat_tickets(seats: Sequence[Seat]) -> Iterator[DbTicket]:
35
    for seat in seats:
36
        if seat.has_user:
37
            yield seat.occupied_by_ticket
38
39
40
def _get_ticket_user_ids(tickets: Sequence[DbTicket]) -> Iterator[UserID]:
41
    for ticket in tickets:
42
        user_id = ticket.used_by_id
43
        if user_id is not None:
44
            yield user_id
45