1
|
|
|
""" |
2
|
|
|
byceps.blueprints.admin.orga_presence.views |
3
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
4
|
|
|
|
5
|
|
|
:Copyright: 2006-2021 Jochen Kupperschmidt |
6
|
|
|
:License: Revised BSD (see `LICENSE` file for details) |
7
|
|
|
""" |
8
|
|
|
|
9
|
1 |
|
from __future__ import annotations |
10
|
1 |
|
from collections import defaultdict |
11
|
1 |
|
from datetime import datetime |
12
|
1 |
|
from typing import Iterable |
13
|
|
|
|
14
|
1 |
|
from flask import abort |
15
|
|
|
|
16
|
1 |
|
from ....permissions.orga_presence import OrgaPresencePermission |
17
|
1 |
|
from ....services.orga_presence import service as orga_presence_service |
18
|
1 |
|
from ....services.orga_presence.transfer.models import ( |
19
|
|
|
PartyTimeSlot, |
20
|
|
|
PresenceTimeSlot, |
21
|
|
|
TimeSlot, |
22
|
|
|
) |
23
|
1 |
|
from ....services.party import service as party_service |
24
|
1 |
|
from ....services.user.transfer.models import User |
25
|
1 |
|
from ....util.framework.blueprint import create_blueprint |
26
|
1 |
|
from ....util.framework.templating import templated |
27
|
1 |
|
from ....util.views import permission_required |
28
|
|
|
|
29
|
|
|
|
30
|
1 |
|
blueprint = create_blueprint('orga_presence', __name__) |
31
|
|
|
|
32
|
|
|
|
33
|
1 |
|
@blueprint.get('/<party_id>') |
34
|
1 |
|
@permission_required(OrgaPresencePermission.view) |
35
|
1 |
|
@templated |
36
|
|
|
def view(party_id): |
37
|
|
|
"""List orga presence and task time slots for that party.""" |
38
|
|
|
party = party_service.find_party(party_id) |
39
|
|
|
|
40
|
|
|
if party is None: |
41
|
|
|
abort(404) |
42
|
|
|
|
43
|
|
|
party_time_slot = PartyTimeSlot.from_party(party) |
44
|
|
|
presences = orga_presence_service.get_presences(party.id) |
45
|
|
|
tasks = orga_presence_service.get_tasks(party.id) |
46
|
|
|
|
47
|
|
|
presences_by_orga = _group_presences_by_orga(presences) |
48
|
|
|
|
49
|
|
|
time_slots = [party_time_slot] + presences + tasks |
50
|
|
|
hour_ranges = list(orga_presence_service.get_hour_ranges(time_slots)) |
51
|
|
|
|
52
|
|
|
days_and_hour_totals = list( |
53
|
|
|
orga_presence_service.get_days_and_hour_totals(hour_ranges) |
54
|
|
|
) |
55
|
|
|
|
56
|
|
|
return { |
57
|
|
|
'party': party, |
58
|
|
|
'days_and_hour_totals': days_and_hour_totals, |
59
|
|
|
'hour_ranges': hour_ranges, |
60
|
|
|
'party_time_slot': party_time_slot, |
61
|
|
|
'presences_by_orga': presences_by_orga, |
62
|
|
|
'tasks': tasks, |
63
|
|
|
'is_instant_contained_in_time_slots': is_instant_contained_in_time_slots, |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
|
67
|
1 |
|
def _group_presences_by_orga( |
68
|
|
|
presences: Iterable[PresenceTimeSlot], |
69
|
|
|
) -> dict[User, set[PresenceTimeSlot]]: |
70
|
|
|
d = defaultdict(set) |
71
|
|
|
|
72
|
|
|
for presence in presences: |
73
|
|
|
d[presence.orga].add(presence) |
74
|
|
|
|
75
|
|
|
return d |
76
|
|
|
|
77
|
|
|
|
78
|
1 |
|
def is_instant_contained_in_time_slots( |
79
|
|
|
instant: datetime, time_slots: Iterable[TimeSlot] |
80
|
|
|
) -> bool: |
81
|
|
|
return any(time_slot.range.contains(instant) for time_slot in time_slots) |
82
|
|
|
|