1
|
|
|
""" |
2
|
|
|
byceps.blueprints.site.tourney.views |
3
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
4
|
|
|
|
5
|
|
|
:Copyright: 2006-2020 Jochen Kupperschmidt |
6
|
|
|
:License: Modified BSD, see LICENSE for details. |
7
|
|
|
""" |
8
|
|
|
|
9
|
1 |
|
from collections import defaultdict |
10
|
1 |
|
import dataclasses |
11
|
|
|
|
12
|
1 |
|
from flask import abort, g |
13
|
|
|
|
14
|
1 |
|
from ....services.party import service as party_service |
15
|
1 |
|
from ....services.tourney import ( |
16
|
|
|
category_service, |
17
|
|
|
participant_service, |
18
|
|
|
tourney_service, |
19
|
|
|
) |
20
|
1 |
|
from ....util.framework.blueprint import create_blueprint |
21
|
1 |
|
from ....util.framework.templating import templated |
22
|
|
|
|
23
|
|
|
|
24
|
1 |
|
blueprint = create_blueprint('tourney', __name__) |
25
|
|
|
|
26
|
|
|
|
27
|
1 |
|
@blueprint.route('/tourneys') |
28
|
1 |
|
@templated |
29
|
|
|
def tourney_index(): |
30
|
|
|
"""List all tournaments for the current party.""" |
31
|
|
|
if g.party_id is None: |
32
|
|
|
# No party is configured for the current site. |
33
|
|
|
abort(404) |
34
|
|
|
|
35
|
|
|
party = party_service.find_party(g.party_id) |
36
|
|
|
if not party: |
37
|
|
|
abort(404) |
38
|
|
|
|
39
|
|
|
categories = category_service.get_categories_for_party(party.id) |
40
|
|
|
tourneys = tourney_service.get_tourneys_for_party(party.id) |
41
|
|
|
|
42
|
|
|
categories_with_tourneys = get_categories_with_tourneys( |
43
|
|
|
categories, tourneys |
44
|
|
|
) |
45
|
|
|
|
46
|
|
|
return { |
47
|
|
|
'categories_with_tourneys': categories_with_tourneys, |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
|
51
|
1 |
|
def get_categories_with_tourneys(categories, tourneys): |
52
|
|
|
categories.sort(key=lambda c: c.position) |
53
|
|
|
tourneys.sort(key=lambda t: t.title) |
54
|
|
|
|
55
|
|
|
tourneys_by_category_id = defaultdict(list) |
56
|
|
|
for tourney in tourneys: |
57
|
|
|
tourneys_by_category_id[tourney.category_id].append(tourney) |
58
|
|
|
|
59
|
|
|
categories_with_tourneys = [] |
60
|
|
|
for category in categories: |
61
|
|
|
tourneys_in_category = tourneys_by_category_id[category.id] |
62
|
|
|
if tourneys_in_category: |
63
|
|
|
categories_with_tourneys.append((category, tourneys_in_category)) |
64
|
|
|
|
65
|
|
|
return categories_with_tourneys |
66
|
|
|
|
67
|
|
|
|
68
|
1 |
|
@blueprint.route('/tourneys/<tourney_id>') |
69
|
1 |
|
@templated |
70
|
|
|
def tourney_view(tourney_id): |
71
|
|
|
"""Show the tournament.""" |
72
|
|
|
tourney = tourney_service.find_tourney(tourney_id) |
73
|
|
|
if not tourney: |
74
|
|
|
abort(404) |
75
|
|
|
|
76
|
|
|
participants = participant_service.get_participants_for_tourney(tourney.id) |
77
|
|
|
|
78
|
|
|
tourney = dataclasses.replace( |
79
|
|
|
tourney, current_participant_count=len(participants) |
80
|
|
|
) |
81
|
|
|
|
82
|
|
|
return { |
83
|
|
|
'tourney': tourney, |
84
|
|
|
'participants': participants, |
85
|
|
|
} |
86
|
|
|
|