Digitale bierlijst

people.py 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. changed = False
  50. if "active" in data:
  51. person.active = data["active"]
  52. changed = True
  53. if "full_name" in data:
  54. person.full_name = data["full_name"]
  55. changed = True
  56. if "display_name" in data:
  57. person.display_name = data["display_name"]
  58. changed = True
  59. if changed:
  60. db.session.add(person)
  61. db.session.commit()
  62. return jsonify(person=person.as_dict)
  63. @app.route("/people/<int:person_id>/add_consumption", methods=["POST"])
  64. def add_consumption(person_id: int):
  65. person = Person.query.get_or_404(person_id)
  66. consumption = Consumption(person=person, consumption_type_id=1)
  67. try:
  68. db.session.add(consumption)
  69. db.session.commit()
  70. except SQLAlchemyError:
  71. return (
  72. jsonify(
  73. {"error": "Invalid Consumption parameters.", "person": person.as_dict}
  74. ),
  75. 400,
  76. )
  77. return jsonify(person=person.as_dict, consumption=consumption.as_dict), 201
  78. @app.route("/people/<int:person_id>/add_consumption/<int:ct_id>", methods=["POST"])
  79. def add_consumption2(person_id: int, ct_id: int):
  80. person = Person.query.get_or_404(person_id)
  81. consumption = Consumption(person=person, consumption_type_id=ct_id)
  82. try:
  83. db.session.add(consumption)
  84. db.session.commit()
  85. except SQLAlchemyError:
  86. return (
  87. jsonify(
  88. {"error": "Invalid Consumption parameters.", "person": person.as_dict}
  89. ),
  90. 400,
  91. )
  92. return jsonify(person=person.as_dict, consumption=consumption.as_dict), 201