Passed
Push — main ( 0b198e...cd8d81 )
by Jochen
05:10
created

byceps.blueprints.site.orga_team.views   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Test Coverage

Coverage 71.43%

Importance

Changes 0
Metric Value
eloc 39
dl 0
loc 67
ccs 20
cts 28
cp 0.7143
rs 10
c 0
b 0
f 0
wmc 7

2 Functions

Rating   Name   Duplication   Size   Complexity  
A _merge_public_orgas() 0 23 4
A index() 0 17 3
1
"""
2
byceps.blueprints.site.orga_team.views
3
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
4
5
:Copyright: 2006-2021 Jochen Kupperschmidt
6
:License: Revised BSD (see `LICENSE` file for details)
7
"""
8
9 1
from collections import defaultdict
10 1
from typing import Iterable, Iterator
11
12 1
from flask import abort, g
13
14 1
from ....services.orga_team import service as orga_team_service
15 1
from ....services.orga_team.transfer.models import PublicOrga
16 1
from ....services.user import service as user_service
17 1
from ....util.framework.blueprint import create_blueprint
18 1
from ....util.framework.templating import templated
19
20
21 1
blueprint = create_blueprint('orga_team', __name__)
22
23
24 1
@blueprint.get('/')
25 1
@templated
26
def index():
27
    """List all organizers for the current party."""
28 1
    if g.party_id is None:
29
        # No party is configured for the current site.
30
        abort(404)
31
32 1
    orgas = orga_team_service.get_public_orgas_for_party(g.party_id)
33 1
    orgas = _merge_public_orgas(orgas)
34 1
    orgas = sorted(
35
        orgas,
36
        key=lambda orga: user_service.get_sort_key_for_screen_name(orga.user),
37
    )
38
39 1
    return {
40
        'orgas': orgas,
41
    }
42
43
44 1
def _merge_public_orgas(
45
    public_orgas: Iterable[PublicOrga],
46
) -> Iterator[PublicOrga]:
47
    """Merge team names and duties of public orga objects that represent
48
    the same person.
49
    """
50 1
    orgas_by_user_id = defaultdict(list)
51 1
    for orga in public_orgas:
52
        orgas_by_user_id[orga.user.id].append(orga)
53
54 1
    for _, orgas in orgas_by_user_id.items():
55
        first = orgas[0]
56
        if len(orgas) > 1:
57
            team_names = ', '.join(orga.team_name for orga in orgas)
58
            duties = ', '.join(orga.duties for orga in orgas if orga.duties)
59
            yield PublicOrga(
60
                user=first.user,
61
                full_name=first.full_name,
62
                team_name=team_names,
63
                duties=duties,
64
            )
65
        else:
66
            yield first
67