~homeworkprod/byceps

ref: 342e5296ae417372e544ce931b442e98301c17a1 byceps/byceps/blueprints/admin/tourney/category/views.py -rw-r--r-- 5.1 KiB
342e5296 — Jochen Kupperschmidt Require body blocks in base templates 2 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
"""
byceps.blueprints.admin.tourney.category.views
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

:Copyright: 2006-2021 Jochen Kupperschmidt
:License: Revised BSD (see `LICENSE` file for details)
"""

from flask import abort, request
from flask_babel import gettext

from .....services.party import service as party_service
from .....services.tourney import category_service
from .....util.authorization import register_permission_enum
from .....util.framework.blueprint import create_blueprint
from .....util.framework.flash import flash_error, flash_success
from .....util.framework.templating import templated
from .....util.views import permission_required, redirect_to, respond_no_content

from ..authorization import TourneyCategoryPermission

from .forms import CreateForm, UpdateForm


blueprint = create_blueprint('tourney_category_admin', __name__)


register_permission_enum(TourneyCategoryPermission)


@blueprint.route('/for_party/<party_id>')
@permission_required(TourneyCategoryPermission.view)
@templated
def index(party_id):
    """List tourney categories for that party."""
    party = _get_party_or_404(party_id)

    categories = category_service.get_categories_for_party(party.id)

    return {
        'party': party,
        'categories': categories,
    }


@blueprint.route('/for_party/<party_id>/create')
@permission_required(TourneyCategoryPermission.administrate)
@templated
def create_form(party_id, erroneous_form=None):
    """Show form to create a category."""
    party = _get_party_or_404(party_id)

    form = erroneous_form if erroneous_form else CreateForm()

    return {
        'party': party,
        'form': form,
    }


@blueprint.route('/for_party/<party_id>', methods=['POST'])
@permission_required(TourneyCategoryPermission.administrate)
def create(party_id):
    """Create a category."""
    party = _get_party_or_404(party_id)

    form = CreateForm(request.form)
    if not form.validate():
        return create_form(party_id, form)

    title = form.title.data.strip()

    category = category_service.create_category(party.id, title)

    flash_success(
        gettext(
            'Category "%(category_title)s" has been created.',
            category_title=category.title,
        )
    )
    return redirect_to('.index', party_id=category.party_id)


@blueprint.route('/categories/<uuid:category_id>/update')
@permission_required(TourneyCategoryPermission.administrate)
@templated
def update_form(category_id, erroneous_form=None):
    """Show form to update a category."""
    category = _get_category_or_404(category_id)

    party = party_service.get_party(category.party_id)

    form = erroneous_form if erroneous_form else UpdateForm(obj=category)

    return {
        'category': category,
        'party': party,
        'form': form,
    }


@blueprint.route('/categories/<uuid:category_id>', methods=['POST'])
@permission_required(TourneyCategoryPermission.administrate)
def update(category_id):
    """Update a category."""
    category = _get_category_or_404(category_id)

    form = UpdateForm(request.form)
    if not form.validate():
        return update_form(category_id, form)

    category_service.update_category(category.id, form.title.data.strip())

    flash_success(
        gettext(
            'Category "%(category_title)s" has been updated.',
            category_title=category.title,
        )
    )
    return redirect_to('.index', party_id=category.party_id)


@blueprint.route('/categories/<uuid:category_id>/up', methods=['POST'])
@permission_required(TourneyCategoryPermission.administrate)
@respond_no_content
def move_up(category_id):
    """Move a category upwards by one position."""
    category = _get_category_or_404(category_id)

    try:
        category_service.move_category_up(category.id)
    except ValueError:
        flash_error(
            gettext(
                'Category "%(category_title)s" is already at the top.',
                category_title=category.title,
            )
        )
    else:
        flash_success(
            gettext(
                'Category "%(category_title)s" has been moved upwards by one position.'
            )
        )


@blueprint.route('/categories/<uuid:category_id>/down', methods=['POST'])
@permission_required(TourneyCategoryPermission.administrate)
@respond_no_content
def move_down(category_id):
    """Move a category downwards by one position."""
    category = _get_category_or_404(category_id)

    try:
        category_service.move_category_down(category.id)
    except ValueError:
        flash_error(
            gettext(
                'Category "%(category_title)s" is already at the bottom.',
                category_title=category.title,
            )
        )
    else:
        flash_success(
            gettext(
                'Category "%(category_title)s" has been moved downwards by one position.'
            )
        )


def _get_party_or_404(party_id):
    party = party_service.find_party(party_id)

    if party is None:
        abort(404)

    return party


def _get_category_or_404(category_id):
    category = category_service.find_category(category_id)

    if category is None:
        abort(404)

    return category