byceps.blueprints.site.tourney.views   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 87
Duplicated Lines 0 %

Test Coverage

Coverage 36.59%

Importance

Changes 0
Metric Value
eloc 51
dl 0
loc 87
ccs 15
cts 41
cp 0.3659
rs 10
c 0
b 0
f 0
wmc 11

3 Functions

Rating   Name   Duplication   Size   Complexity  
B get_categories_with_tourneys() 0 15 6
A tourney_index() 0 21 3
A tourney_view() 0 19 2
1
"""
2
byceps.blueprints.site.tourney.views
3
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
4
5
:Copyright: 2014-2024 Jochen Kupperschmidt
6
:License: Revised BSD (see `LICENSE` file 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 byceps.services.party import party_service
15 1
from byceps.services.tourney import (
16
    tourney_category_service,
17
    tourney_participant_service,
18
    tourney_service,
19
)
20 1
from byceps.util.framework.blueprint import create_blueprint
21 1
from byceps.util.framework.templating import templated
22
23
24 1
blueprint = create_blueprint('tourney', __name__)
25
26
27 1
@blueprint.get('/tourneys')
28 1
@templated
29 1
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 = tourney_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.get('/tourneys/<tourney_id>')
69 1
@templated
70 1
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 = tourney_participant_service.get_participants_for_tourney(
77
        tourney.id
78
    )
79
80
    tourney = dataclasses.replace(
81
        tourney, current_participant_count=len(participants)
82
    )
83
84
    return {
85
        'tourney': tourney,
86
        'participants': participants,
87
    }
88