1
+
2
+ from attrs import define , field
3
+ import cattrs
4
+ from cattrs import transform_error
5
+
6
+ from typing import Optional
7
+
8
+ from datetime import datetime
9
+ from notubiz .api ._helpers import parse_date , get_title , get_location
10
+
11
+ from notubiz import ApiClient
12
+
13
+ @define
14
+ class Planning :
15
+ # Auto-filled
16
+ start_date : datetime
17
+ end_date : Optional [datetime ]
18
+
19
+ @define
20
+ class Event :
21
+ # Auto-filled
22
+ id : int
23
+ type : str
24
+ permission_group : str
25
+ body : str
26
+ confidential : bool
27
+ announcement : bool
28
+ canceled : bool
29
+ inactive : bool
30
+ creation_date : datetime
31
+ last_modified : datetime
32
+ live : bool
33
+ archive_state : str
34
+ archive_state_last_modified : Optional [datetime ]
35
+ allow_subscriptions : bool
36
+ plannings : list [Planning ]
37
+
38
+ # Manually filled
39
+ title : str = field (init = False )
40
+ location : str = field (init = False )
41
+ gremium_id : int = field (init = False )
42
+
43
+ @staticmethod
44
+ def from_json (json_object : any ) -> 'Event' :
45
+ c = cattrs .Converter ()
46
+
47
+ c .register_structure_hook (datetime , lambda date_string , _ : parse_date (date_string ))
48
+
49
+ try :
50
+ meeting = c .structure (json_object , Event )
51
+ except Exception as exc :
52
+ print ("\n " .join (transform_error (exc )))
53
+ quit ()
54
+
55
+ attributes = json_object .get ("attributes" , [])
56
+ meeting .title = get_title (attributes )
57
+ meeting .location = get_location (attributes )
58
+ meeting .gremium_id = json_object ["gremium" ]["id" ]
59
+
60
+ return meeting
61
+
62
+ class EventApi :
63
+ api_client : ApiClient
64
+
65
+ def __init__ (self , api_client : ApiClient ):
66
+ self .api_client = api_client
67
+
68
+
69
+ def get (self , date_from : datetime , date_to : datetime , gremia : list [int ] = None ) -> list [Event ]:
70
+
71
+ json_events : list [dict ] = []
72
+ has_more_pages = True
73
+ page = 1 # Notubiz uses 1-based paging
74
+
75
+ # We loop over the pages until no more pages are left
76
+ while has_more_pages :
77
+
78
+ additional_payload = {
79
+ 'date_from' : date_from .strftime ("%Y-%m-%d %H:%M:%S" ),
80
+ 'date_to' : date_to .strftime ("%Y-%m-%d %H:%M:%S" ),
81
+ # For some reason this endpoint requires 'organisation_id' instead of 'organisation"
82
+ 'organisation_id' : self .api_client .configuration .organisation_id ,
83
+ 'gremia' : gremia ,
84
+ 'page' : page
85
+ }
86
+
87
+ json_events_page = self .api_client .get ("events/" , additional_payload )
88
+ json_events .extend (json_events_page ["events" ])
89
+
90
+ has_more_pages = json_events_page ["pagination" ]["has_more_pages" ]
91
+ page += 1
92
+
93
+ # Now run the deserialization based on the merged events
94
+ return [Event .from_json (json_event ) for json_event in json_events ]
0 commit comments