Passed
Push — main ( 1baeb6...c8aa67 )
by Jochen
04:02
created

_get_db_category()   A

Complexity

Conditions 2

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 4.048

Importance

Changes 0
Metric Value
cc 2
eloc 5
nop 1
dl 0
loc 7
ccs 1
cts 5
cp 0.2
crap 4.048
rs 10
c 0
b 0
f 0
1
"""
2
byceps.services.tourney.category_service
3
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
4
5
:Copyright: 2006-2020 Jochen Kupperschmidt
6
:License: Modified BSD, see LICENSE for details.
7
"""
8
9 1
from typing import Optional, Sequence
10
11 1
from ...database import db
12 1
from ...typing import PartyID
13
14 1
from ..party.models.party import Party as DbParty
15
16 1
from .models.tourney_category import TourneyCategory as DbTourneyCategory
17 1
from .transfer.models import TourneyCategory, TourneyCategoryID
18
19
20 1
def create_category(party_id: PartyID, title: str) -> DbTourneyCategory:
21
    """Create a category for that party."""
22
    party = DbParty.query.get(party_id)
23
    if party is None:
24
        raise ValueError(f'Unknown party ID "{party_id}"')
25
26
    category = DbTourneyCategory(party.id, title)
27
    party.tourney_categories.append(category)
28
29
    db.session.commit()
30
31
    return _db_entity_to_category(category)
32
33
34 1
def update_category(category_id: TourneyCategoryID, title: str) -> None:
35
    """Update category."""
36
    category = _get_db_category(category_id)
37
38
    category.title = title
39
    db.session.commit()
40
41
42 1
def move_category_up(category_id: TourneyCategoryID) -> None:
43
    """Move a category upwards by one position."""
44
    category = _get_db_category(category_id)
45
46
    category_list = category.party.tourney_categories
47
48
    if category.position == 1:
49
        raise ValueError('Category already is at the top.')
50
51
    popped_category = category_list.pop(category.position - 1)
52
    category_list.insert(popped_category.position - 2, popped_category)
53
54
    db.session.commit()
55
56
57 1
def move_category_down(category_id: TourneyCategoryID) -> None:
58
    """Move a category downwards by one position."""
59
    category = _get_db_category(category_id)
60
61
    category_list = category.party.tourney_categories
62
63
    if category.position == len(category_list):
64
        raise ValueError('Category already is at the bottom.')
65
66
    popped_category = category_list.pop(category.position - 1)
67
    category_list.insert(popped_category.position, popped_category)
68
69
    db.session.commit()
70
71
72 1
def find_category(
73
    category_id: TourneyCategoryID,
74
) -> Optional[DbTourneyCategory]:
75
    """Return the category with that id, or `None` if not found."""
76
    category = _find_db_category(category_id)
77
78
    if category is None:
79
        return None
80
81
    return _db_entity_to_category(category)
82
83
84 1
def _find_db_category(category_id: TourneyCategoryID) -> Optional[DbTourneyCategory]:
85
    return DbTourneyCategory.query.get(category_id)
86
87
88 1
def _get_db_category(category_id: TourneyCategoryID) -> DbTourneyCategory:
89
    category = _find_db_category(category_id)
90
91
    if category is None:
92
        raise ValueError(f'Unknown category ID "{category_id}"')
93
94
    return category
95
96
97 1
def get_categories_for_party(party_id: PartyID) -> Sequence[DbTourneyCategory]:
98
    """Return the categories for this party."""
99
    return DbTourneyCategory.query \
100
        .filter_by(party_id=party_id) \
101
        .order_by(DbTourneyCategory.position) \
102
        .all()
103
104
105 1
def _db_entity_to_category(category: DbTourneyCategory) -> TourneyCategory:
106
    return TourneyCategory(
107
        category.id,
108
        category.party_id,
109
        category.position,
110
        category.title,
111
    )
112