Digitale bierlijst

exports.py 1.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. """
  2. Provides routes for managing Export objects.
  3. """
  4. from flask import jsonify
  5. from sqlalchemy.exc import SQLAlchemyError
  6. from piket_server.flask import app, db
  7. from piket_server.models import Export, Settlement
  8. @app.route("/exports", methods=["GET"])
  9. def get_exports():
  10. """ Return a list of the created Exports. """
  11. result = Export.query.all()
  12. return jsonify(exports=[e.as_dict for e in result])
  13. @app.route("/exports/<int:export_id>", methods=["GET"])
  14. def get_export(export_id: int):
  15. """ Return an overview for the given Export. """
  16. e = Export.query.get_or_404(export_id)
  17. ss = [s.as_dict for s in e.settlements]
  18. return jsonify(export=e.as_dict, settlements=ss)
  19. @app.route("/exports", methods=["POST"])
  20. def add_export():
  21. """ Create an Export, and link all un-exported Settlements to it. """
  22. # Assert that there are Settlements to be exported.
  23. s_count = Settlement.query.filter_by(export=None).count()
  24. if s_count == 0:
  25. return jsonify(error="No un-exported Settlements."), 403
  26. e = Export()
  27. db.session.add(e)
  28. db.session.commit()
  29. Settlement.query.filter_by(export=None).update({"export_id": e.export_id})
  30. db.session.commit()
  31. ss = [s.as_dict for s in e.settlements]
  32. return jsonify(export=e.as_dict, settlements=ss), 201