Digitale bierlijst

aardbei.py 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. from typing import Any, Dict, List, Tuple, Union
  2. from flask import request
  3. from piket_server.aardbei_sync import (
  4. AARDBEI_ENDPOINT,
  5. ActivityId,
  6. get_activity,
  7. AardbeiLink,
  8. AardbeiSyncError,
  9. create_missing,
  10. get_aardbei_people,
  11. match_activity,
  12. get_activities,
  13. link_matches,
  14. match_local_aardbei,
  15. update_names,
  16. )
  17. from piket_server.flask import app, db
  18. def common_prepare_aardbei_sync(
  19. token: str, endpoint: str
  20. ) -> Union[AardbeiSyncError, AardbeiLink]:
  21. aardbei_people = get_aardbei_people(token, endpoint)
  22. if isinstance(aardbei_people, AardbeiSyncError):
  23. return aardbei_people
  24. aardbei_activities = get_activities(token, endpoint)
  25. if isinstance(aardbei_activities, AardbeiSyncError):
  26. return aardbei_activities
  27. return match_local_aardbei(aardbei_people)
  28. @app.route("/aardbei/diff_people", methods=["POST"])
  29. def aardbei_diff() -> Tuple[Dict[str, Any], int]:
  30. data: Dict[str, str] = request.json
  31. link = common_prepare_aardbei_sync(
  32. data["token"], data.get("endpoint", AARDBEI_ENDPOINT)
  33. )
  34. if isinstance(link, AardbeiSyncError):
  35. return {"error": link.value}, 503
  36. return (
  37. {
  38. "num_changes": link.num_changes,
  39. "new_people": [member.person.full_name for member in link.remote_only],
  40. "link_existing": [match.local.full_name for match in link.matches],
  41. "altered_name": [match.local.full_name for match in link.matches],
  42. },
  43. 200,
  44. )
  45. @app.route("/aardbei/sync_people", methods=["POST"])
  46. def aardbei_apply() -> Union[Tuple[Dict[str, Any], int]]:
  47. data: Dict[str, str] = request.json
  48. link = common_prepare_aardbei_sync(
  49. data["token"], data.get("endpoint", AARDBEI_ENDPOINT)
  50. )
  51. if isinstance(link, AardbeiSyncError):
  52. return {"error": link.value}, 503
  53. link_matches(link.matches)
  54. create_missing(link.remote_only)
  55. update_names(link.altered_name)
  56. db.session.commit()
  57. return (
  58. {
  59. "num_changes": link.num_changes,
  60. "new_people": [member.person.full_name for member in link.remote_only],
  61. "link_existing": [match.local.full_name for match in link.matches],
  62. "altered_name": [match.local.full_name for match in link.altered_name],
  63. },
  64. 200,
  65. )
  66. @app.route("/aardbei/get_activities", methods=["POST"])
  67. def aardbei_get_activities() -> Tuple[Dict[str, object], int]:
  68. data: Dict[str, str] = request.json
  69. activities = get_activities(data["token"], data.get("endpoint", AARDBEI_ENDPOINT))
  70. if isinstance(activities, AardbeiSyncError):
  71. return {"error": activities.value}, 503
  72. return {"activities": [x.as_json_dict for x in activities]}, 200
  73. @app.route("/aardbei/apply_activity", methods=["POST"])
  74. def aardbei_apply_activity() -> Tuple[Dict[str, Any], int]:
  75. data: Dict[str, Union[str, int]] = request.json
  76. aid = data["activity_id"]
  77. token = data["token"]
  78. endpoint = data["endpoint"]
  79. if not isinstance(aid, int):
  80. return {"error": "nonnumeric_activity_id"}, 400
  81. if not isinstance(token, str):
  82. return {"error": "illtyped_token"}, 400
  83. if not isinstance(endpoint, str):
  84. return {"error": "illtyped_endpoint"}, 400
  85. activity = get_activity(activity_id=ActivityId(aid), token=token, endpoint=endpoint)
  86. if isinstance(activity, AardbeiSyncError):
  87. return {"error": activity.value}, 503
  88. match_activity(activity)
  89. db.session.commit()
  90. return (activity.as_json_dict, 200)