1
- from attrs import define
1
+
2
+ from attrs import define , field
2
3
import cattrs
3
4
from cattrs import transform_error
4
- from cattrs .gen import make_dict_structure_fn , override
5
+
6
+ from typing import Optional
5
7
6
8
from datetime import datetime
9
+ from notubiz .api ._helpers import parse_date , get_title , get_location
7
10
8
- from notubiz .api .meeting import Meeting , meeting_structure_hook
9
- from notubiz .api .agenda_item import AgendaItem , agenda_item_structure_hook
10
11
from notubiz import ApiClient
11
12
12
13
@define
13
- class Event :
14
- title : str
15
- description : str
16
- location : str
17
- chairman : str
18
- clerk : str
19
- secretary : str
20
-
21
- # TODO: media
22
- # TODO: speakers
14
+ class Planning :
15
+ # Auto-filled
16
+ start_date : datetime
17
+ end_date : Optional [datetime ]
23
18
24
- agenda : list [AgendaItem ]
25
-
26
- # meeting_id : int
27
- # type : str
28
- # start_date : datetime
29
- # end_date : datetime
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 ]
30
37
31
- # meetings : list[Meeting]
38
+ # Manually filled
39
+ title : str = field (init = False )
40
+ location : str = field (init = False )
41
+ gremium_id : int = field (init = False )
32
42
33
43
@staticmethod
34
44
def from_json (json_object : any ) -> 'Event' :
35
45
c = cattrs .Converter ()
36
- c .register_structure_hook (Meeting , meeting_structure_hook )
37
- # c.register_structure_hook(AgendaItem, agenda_item_structure_hook)
38
- st_hook = make_dict_structure_fn (
39
- AgendaItem , c , agenda = override (struct_hook = lambda v , _ : agenda_item_structure_hook (v ["agendaitem" ])))
40
- c .register_structure_hook (AgendaItem , st_hook )
46
+
47
+ c .register_structure_hook (datetime , lambda date_string , _ : parse_date (date_string ))
41
48
42
49
try :
43
- meeting = c .structure (json_object [ "event" ][ 0 ] , Event )
50
+ meeting = c .structure (json_object , Event )
44
51
except Exception as exc :
45
52
print ("\n " .join (transform_error (exc )))
46
53
quit ()
47
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
+
48
60
return meeting
49
61
50
62
class EventApi :
51
63
api_client : ApiClient
52
-
53
64
54
65
def __init__ (self , api_client : ApiClient ):
55
66
self .api_client = api_client
56
67
57
- def get (self , event_id : int ) -> Event :
58
- json_object = self .api_client .get ("events/{}" .format (event_id ))
59
- return Event .from_json (json_object )
60
-
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