Passed
Push — master ( 3058b1...ede4b9 )
by Jochen
02:15
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 7 1
A _get_ticket_users_by_id() 0 4 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 itertools import chain
10
from typing import Dict, Iterator, Sequence
11
12
from ...services.seating.models.seat import Seat
13
from ...services.ticketing.models.ticket import Ticket as DbTicket
14
from ...services.user import service as user_service
15
from ...services.user.transfer.models import User
16
from ...typing import UserID
17
18
19
def get_users(
20
    seats: Sequence[Seat], managed_tickets: Sequence[DbTicket]
21
) -> Dict[UserID, User]:
22
    seat_tickets = _get_seat_tickets(seats)
23
    tickets = chain(seat_tickets, managed_tickets)
24
25
    return _get_ticket_users_by_id(tickets)
26
27
28
def _get_seat_tickets(seats: Sequence[Seat]) -> Iterator[DbTicket]:
29
    for seat in seats:
30
        if seat.has_user:
31
            yield seat.occupied_by_ticket
32
33
34
def _get_ticket_users_by_id(tickets: Sequence[DbTicket]) -> Dict[UserID, User]:
35
    user_ids = set(_get_ticket_user_ids(tickets))
36
    users = user_service.find_users(user_ids, include_avatars=True)
37
    return user_service.index_users_by_id(users)
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