Passed
Push — main ( fafd4d...2bc15d )
by Jochen
06:53
created

byceps.blueprints.admin.seating.views.area_view()   A

Complexity

Conditions 1

Size

Total Lines 21
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1.216

Importance

Changes 0
Metric Value
cc 1
eloc 14
nop 1
dl 0
loc 21
ccs 4
cts 10
cp 0.4
crap 1.216
rs 9.7
c 0
b 0
f 0
1
"""
2
byceps.blueprints.admin.seating.views
3
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
4
5
:Copyright: 2014-2023 Jochen Kupperschmidt
6
:License: Revised BSD (see `LICENSE` file for details)
7
"""
8
9 1
from flask import abort, request
10 1
from flask_babel import gettext
11
12 1
from ....services.party import party_service
13 1
from ....services.seating.models import SeatingArea, SeatingAreaID
14 1
from ....services.seating import (
15
    seat_group_service,
16
    seat_service,
17
    seating_area_service,
18
    seating_area_tickets_service,
19
)
20 1
from ....services.ticketing import ticket_category_service
21 1
from ....util.framework.blueprint import create_blueprint
22 1
from ....util.framework.flash import flash_success
23 1
from ....util.framework.templating import templated
24 1
from ....util.views import permission_required, redirect_to
25
26 1
from .forms import AreaCreateForm
27
28
29 1
blueprint = create_blueprint('seating_admin', __name__)
30
31
32 1
@blueprint.get('/<party_id>')
33 1
@permission_required('seating.view')
34 1
@templated
35 1
def index_for_party(party_id):
36
    """List seating areas for that party."""
37 1
    party = _get_party_or_404(party_id)
38
39 1
    seat_count = seat_service.count_seats_for_party(party.id)
40 1
    area_count = seating_area_service.count_areas_for_party(party.id)
41 1
    category_count = ticket_category_service.count_categories_for_party(
42
        party.id
43
    )
44 1
    group_count = seat_group_service.count_seat_groups_for_party(party.id)
45
46 1
    return {
47
        'party': party,
48
        'seat_count': seat_count,
49
        'area_count': area_count,
50
        'category_count': category_count,
51
        'group_count': group_count,
52
    }
53
54
55 1
@blueprint.get('/parties/<party_id>/areas')
56 1
@permission_required('seating.view')
57 1
@templated
58 1
def area_index(party_id):
59
    """List seating areas for that party."""
60 1
    party = _get_party_or_404(party_id)
61
62 1
    areas_with_utilization = (
63
        seating_area_service.get_areas_with_seat_utilization(party.id)
64
    )
65
66 1
    seat_utilizations = [awu[1] for awu in areas_with_utilization]
67 1
    total_seat_utilization = seat_service.aggregate_seat_utilizations(
68
        seat_utilizations
69
    )
70
71 1
    return {
72
        'party': party,
73
        'areas_with_utilization': areas_with_utilization,
74
        'total_seat_utilization': total_seat_utilization,
75
    }
76
77
78 1
@blueprint.get('/areas/<area_id>')
79 1
@permission_required('seating.view')
80 1
@templated
81 1
def area_view(area_id):
82
    """Show seating area."""
83
    area = _get_area_or_404(area_id)
84
85
    party = party_service.get_party(area.party_id)
86
87
    seats_with_tickets = seat_service.get_seats_with_tickets_for_area(area.id)
88
89
    users_by_id = seating_area_tickets_service.get_users(seats_with_tickets, [])
90
91
    seats_and_tickets = seating_area_tickets_service.get_seats_and_tickets(
92
        seats_with_tickets, users_by_id
93
    )
94
95
    return {
96
        'party': party,
97
        'area': area,
98
        'seats_and_tickets': seats_and_tickets,
99
    }
100
101
102 1
@blueprint.get('/parties/<party_id>/areas/create')
103 1
@permission_required('seating.administrate')
104 1
@templated
105 1
def area_create_form(party_id, erroneous_form=None):
106
    """Show form to create a seating area."""
107
    party = _get_party_or_404(party_id)
108
109
    form = erroneous_form if erroneous_form else AreaCreateForm()
110
111
    return {
112
        'party': party,
113
        'form': form,
114
    }
115
116
117 1
@blueprint.post('/parties/<party_id>/areas')
118 1
@permission_required('seating.administrate')
119 1
def area_create(party_id):
120
    """Create a seating area."""
121
    party = _get_party_or_404(party_id)
122
123
    form = AreaCreateForm(request.form)
124
    if not form.validate():
125
        return area_create_form(party.id, form)
126
127
    slug = form.slug.data.strip()
128
    title = form.title.data.strip()
129
130
    area = seating_area_service.create_area(party.id, slug, title)
131
132
    flash_success(
133
        gettext('Seating area "%(title)s" has been created.', title=area.title)
134
    )
135
136
    return redirect_to('.area_index', party_id=party.id)
137
138
139 1
@blueprint.get('/parties/<party_id>/seat_groups')
140 1
@permission_required('seating.view')
141 1
@templated
142 1
def seat_group_index(party_id):
143
    """List seat groups for that party."""
144 1
    party = _get_party_or_404(party_id)
145
146 1
    groups = seat_group_service.get_all_seat_groups_for_party(party.id)
147
148 1
    return {
149
        'party': party,
150
        'groups': groups,
151
    }
152
153
154 1
def _get_party_or_404(party_id):
155 1
    party = party_service.find_party(party_id)
156
157 1
    if party is None:
158
        abort(404)
159
160 1
    return party
161
162
163 1
def _get_area_or_404(area_id: SeatingAreaID) -> SeatingArea:
164
    area = seating_area_service.find_area(area_id)
165
166
    if area is None:
167
        abort(404)
168
169
    return area
170