1 | """ |
||
2 | byceps.blueprints.admin.tourney.category.views |
||
3 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
||
4 | |||
5 | :Copyright: 2006-2021 Jochen Kupperschmidt |
||
6 | :License: Revised BSD (see `LICENSE` file for details) |
||
7 | """ |
||
8 | |||
9 | 1 | from flask import abort, request |
|
10 | 1 | from flask_babel import gettext |
|
11 | |||
12 | 1 | from .....services.party import service as party_service |
|
13 | 1 | from .....services.tourney import category_service |
|
14 | 1 | from .....util.framework.blueprint import create_blueprint |
|
15 | 1 | from .....util.framework.flash import flash_error, flash_success |
|
16 | 1 | from .....util.framework.templating import templated |
|
17 | 1 | from .....util.views import permission_required, redirect_to, respond_no_content |
|
18 | |||
19 | 1 | from .forms import CreateForm, UpdateForm |
|
20 | |||
21 | |||
22 | 1 | blueprint = create_blueprint('tourney_category_admin', __name__) |
|
23 | |||
24 | |||
25 | 1 | @blueprint.get('/for_party/<party_id>') |
|
26 | 1 | @permission_required('tourney_category.view') |
|
27 | 1 | @templated |
|
28 | def index(party_id): |
||
29 | """List tourney categories for that party.""" |
||
30 | party = _get_party_or_404(party_id) |
||
31 | |||
32 | categories = category_service.get_categories_for_party(party.id) |
||
33 | |||
34 | return { |
||
35 | 'party': party, |
||
36 | 'categories': categories, |
||
37 | } |
||
38 | |||
39 | |||
40 | 1 | @blueprint.get('/for_party/<party_id>/create') |
|
41 | 1 | @permission_required('tourney_category.administrate') |
|
42 | 1 | @templated |
|
43 | 1 | def create_form(party_id, erroneous_form=None): |
|
44 | """Show form to create a category.""" |
||
45 | party = _get_party_or_404(party_id) |
||
46 | |||
47 | form = erroneous_form if erroneous_form else CreateForm() |
||
48 | |||
49 | return { |
||
50 | 'party': party, |
||
51 | 'form': form, |
||
52 | } |
||
53 | |||
54 | |||
55 | 1 | View Code Duplication | @blueprint.post('/for_party/<party_id>') |
56 | 1 | @permission_required('tourney_category.administrate') |
|
57 | def create(party_id): |
||
58 | """Create a category.""" |
||
59 | party = _get_party_or_404(party_id) |
||
60 | |||
61 | form = CreateForm(request.form) |
||
62 | if not form.validate(): |
||
63 | return create_form(party_id, form) |
||
64 | |||
65 | title = form.title.data.strip() |
||
66 | |||
67 | category = category_service.create_category(party.id, title) |
||
68 | |||
69 | flash_success( |
||
70 | gettext( |
||
71 | 'Category "%(category_title)s" has been created.', |
||
72 | category_title=category.title, |
||
73 | ) |
||
74 | ) |
||
75 | return redirect_to('.index', party_id=category.party_id) |
||
76 | |||
77 | |||
78 | 1 | @blueprint.get('/categories/<uuid:category_id>/update') |
|
79 | 1 | @permission_required('tourney_category.administrate') |
|
80 | 1 | @templated |
|
81 | 1 | def update_form(category_id, erroneous_form=None): |
|
82 | """Show form to update a category.""" |
||
83 | category = _get_category_or_404(category_id) |
||
84 | |||
85 | party = party_service.get_party(category.party_id) |
||
86 | |||
87 | form = erroneous_form if erroneous_form else UpdateForm(obj=category) |
||
88 | |||
89 | return { |
||
90 | 'category': category, |
||
91 | 'party': party, |
||
92 | 'form': form, |
||
93 | } |
||
94 | |||
95 | |||
96 | 1 | View Code Duplication | @blueprint.post('/categories/<uuid:category_id>') |
1 ignored issue
–
show
Duplication
introduced
by
Loading history...
|
|||
97 | 1 | @permission_required('tourney_category.administrate') |
|
98 | def update(category_id): |
||
99 | """Update a category.""" |
||
100 | category = _get_category_or_404(category_id) |
||
101 | |||
102 | form = UpdateForm(request.form) |
||
103 | if not form.validate(): |
||
104 | return update_form(category_id, form) |
||
105 | |||
106 | category_service.update_category(category.id, form.title.data.strip()) |
||
107 | |||
108 | flash_success( |
||
109 | gettext( |
||
110 | 'Category "%(category_title)s" has been updated.', |
||
111 | category_title=category.title, |
||
112 | ) |
||
113 | ) |
||
114 | return redirect_to('.index', party_id=category.party_id) |
||
115 | |||
116 | |||
117 | 1 | @blueprint.post('/categories/<uuid:category_id>/up') |
|
118 | 1 | @permission_required('tourney_category.administrate') |
|
119 | 1 | @respond_no_content |
|
120 | def move_up(category_id): |
||
121 | """Move a category upwards by one position.""" |
||
122 | category = _get_category_or_404(category_id) |
||
123 | |||
124 | try: |
||
125 | category_service.move_category_up(category.id) |
||
126 | except ValueError: |
||
127 | flash_error( |
||
128 | gettext( |
||
129 | 'Category "%(category_title)s" is already at the top.', |
||
130 | category_title=category.title, |
||
131 | ) |
||
132 | ) |
||
133 | else: |
||
134 | flash_success( |
||
135 | gettext( |
||
136 | 'Category "%(category_title)s" has been moved upwards by one position.' |
||
137 | ) |
||
138 | ) |
||
139 | |||
140 | |||
141 | 1 | @blueprint.post('/categories/<uuid:category_id>/down') |
|
142 | 1 | @permission_required('tourney_category.administrate') |
|
143 | 1 | @respond_no_content |
|
144 | def move_down(category_id): |
||
145 | """Move a category downwards by one position.""" |
||
146 | category = _get_category_or_404(category_id) |
||
147 | |||
148 | try: |
||
149 | category_service.move_category_down(category.id) |
||
150 | except ValueError: |
||
151 | flash_error( |
||
152 | gettext( |
||
153 | 'Category "%(category_title)s" is already at the bottom.', |
||
154 | category_title=category.title, |
||
155 | ) |
||
156 | ) |
||
157 | else: |
||
158 | flash_success( |
||
159 | gettext( |
||
160 | 'Category "%(category_title)s" has been moved downwards by one position.' |
||
161 | ) |
||
162 | ) |
||
163 | |||
164 | |||
165 | 1 | def _get_party_or_404(party_id): |
|
166 | party = party_service.find_party(party_id) |
||
167 | |||
168 | if party is None: |
||
169 | abort(404) |
||
170 | |||
171 | return party |
||
172 | |||
173 | |||
174 | 1 | def _get_category_or_404(category_id): |
|
175 | category = category_service.find_category(category_id) |
||
176 | |||
177 | if category is None: |
||
178 | abort(404) |
||
179 | |||
180 | return category |
||
181 |