Author: Pedro Lucas Porcellis <pedrolucasporcellis@gmail.com>
Close a cycle action
src/blueprints/cycles.py | 11 +++++++++++ src/templates/cycles.html | 1 + src/types/cycle.py | 4 ++++
diff --git a/src/blueprints/cycles.py b/src/blueprints/cycles.py index ad66828b70306c37239e5b8d0e73358a5abe1cde..a3185f115c70c13fbc38d083cda33722254155f7 100644 --- a/src/blueprints/cycles.py +++ b/src/blueprints/cycles.py @@ -31,12 +31,23 @@ return "Not found", 404 return render_template('cycle.html', cycle = cycle, current_order = current_order, products = products, title = "Carrinho - Feira Virtual") +@cycles.route('/<id>/close', methods = ['GET']) +def close(id): + if current_user.is_admin(): + cycle = Cycle.query.get(id) + cycle.close() + + return redirect(url_for('cycles.index')) + else: + return "Not found", 404 + def create(params): start_at = params.get('start_at') end_at = params.get('end_at') cycle = Cycle.query.filter_by(status = CycleStatus.published.value).first() + # TODO: Better handling an ongoing cycle when create another one if cycle: return "there's an ongoing cycle" diff --git a/src/templates/cycles.html b/src/templates/cycles.html index bf6a5fcd2bc05dbb18ebbc40db48e7f85f4b0098..c6607b52187021b8def0d3820cdf93cc4effa201 100644 --- a/src/templates/cycles.html +++ b/src/templates/cycles.html @@ -4,5 +4,6 @@<h2>{{ current_cycle.title }}</h2> <p>{{ current_cycle.description }}</p> <a href="{{url_for('cycles.show', id = current_cycle.id)}}">Ver ciclo</a> + <a href="{{url_for('cycles.close', id = current_cycle.id)}}">Fechar ciclo</a> </div> </section> diff --git a/src/types/cycle.py b/src/types/cycle.py index f4757a29ef139e6a2261f3379652baf8e6fc80f1..36ed20b3aa774511b462a1ad00ace839b90455bf 100644 --- a/src/types/cycle.py +++ b/src/types/cycle.py @@ -46,6 +46,10 @@ def create(self): db.session.add(self) db.session.commit() + def close(self): + self.status = CycleStatus.closed.value + db.session.commit() + def to_json(self): return { "name": self.name,