Digitale bierlijst

people.py 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. """
  2. Provides routes related to managing Person objects.
  3. """
  4. from flask import jsonify, request
  5. from sqlalchemy.exc import SQLAlchemyError
  6. from piket_server.models import Consumption, Person
  7. from piket_server.flask import app, db
  8. @app.route("/people", methods=["GET"])
  9. def get_people():
  10. """ Return a list of currently known people. """
  11. people = Person.query.order_by(Person.full_name).all()
  12. q = Person.query.order_by(Person.full_name)
  13. if request.args.get("active"):
  14. active_status = request.args.get("active", type=int)
  15. q = q.filter_by(active=active_status)
  16. people = q.all()
  17. result = [person.as_dict for person in people]
  18. return jsonify(people=result)
  19. @app.route("/people/<int:person_id>", methods=["GET"])
  20. def get_person(person_id: int):
  21. person = Person.query.get_or_404(person_id)
  22. return jsonify(person=person.as_dict)
  23. @app.route("/people", methods=["POST"])
  24. def add_person():
  25. """
  26. Add a new person.
  27. Required parameters:
  28. - name (str)
  29. """
  30. json = request.get_json()
  31. if not json:
  32. return jsonify({"error": "Could not parse JSON."}), 400
  33. data = json.get("person") or {}
  34. person = Person(
  35. full_name=data.get("full_name"),
  36. active=data.get("active", False),
  37. display_name=data.get("display_name", None),
  38. )
  39. try:
  40. db.session.add(person)
  41. db.session.commit()
  42. except SQLAlchemyError:
  43. return jsonify({"error": "Invalid arguments for Person."}), 400
  44. return jsonify(person=person.as_dict), 201
  45. @app.route("/people/<int:person_id>", methods=["PATCH"])
  46. def update_person(person_id: int):
  47. person = Person.query.get_or_404(person_id)
  48. data = request.json["person"]
  49. if "active" in data:
  50. person.active = data["active"]
  51. db.session.add(person)
  52. db.session.commit()
  53. return jsonify(person=person.as_dict)
  54. @app.route("/people/<int:person_id>/add_consumption", methods=["POST"])
  55. def add_consumption(person_id: int):
  56. person = Person.query.get_or_404(person_id)
  57. consumption = Consumption(person=person, consumption_type_id=1)
  58. try:
  59. db.session.add(consumption)
  60. db.session.commit()
  61. except SQLAlchemyError:
  62. return (
  63. jsonify(
  64. {"error": "Invalid Consumption parameters.", "person": person.as_dict}
  65. ),
  66. 400,
  67. )
  68. return jsonify(person=person.as_dict, consumption=consumption.as_dict), 201
  69. @app.route("/people/<int:person_id>/add_consumption/<int:ct_id>", methods=["POST"])
  70. def add_consumption2(person_id: int, ct_id: int):
  71. person = Person.query.get_or_404(person_id)
  72. consumption = Consumption(person=person, consumption_type_id=ct_id)
  73. try:
  74. db.session.add(consumption)
  75. db.session.commit()
  76. except SQLAlchemyError:
  77. return (
  78. jsonify(
  79. {"error": "Invalid Consumption parameters.", "person": person.as_dict}
  80. ),
  81. 400,
  82. )
  83. return jsonify(person=person.as_dict, consumption=consumption.as_dict), 201