12345678910111213141516171819202122232425262728293031323334353637 |
- """
- Provides routes related to Consumption objects.
- """
- from flask import jsonify
- from sqlalchemy.exc import SQLAlchemyError
- from piket_server.flask import app, db
- from piket_server.models import Consumption
- @app.route("/consumptions/<int:consumption_id>", methods=["DELETE"])
- def reverse_consumption(consumption_id: int):
- """ Reverse a consumption. """
- consumption = Consumption.query.get_or_404(consumption_id)
- if consumption.reversed:
- return (
- jsonify(
- {
- "error": "Consumption already reversed",
- "consumption": consumption.as_dict,
- }
- ),
- 409,
- )
- try:
- consumption.reversed = True
- db.session.add(consumption)
- db.session.commit()
- except SQLAlchemyError:
- return jsonify({"error": "Database error."}), 500
- return jsonify(consumption=consumption.as_dict), 200
|