1
1
from attrs import define , field
2
- import cattrs
2
+ from cattr import Converter
3
3
4
4
from datetime import datetime
5
- from typing import Optional , Dict , Any
5
+ from typing import Optional
6
6
7
7
from notubiz .api ._helpers import parse_date , get_attribute , get_title , get_description
8
8
from notubiz .api .dataclasses .document import Document
9
9
10
10
@define
11
11
class AgendaItem :
12
+ # Auto-filled fields
12
13
id : int
13
14
last_modified : datetime
14
- title : str
15
- description : str
16
- start_date : Optional [datetime ]
17
- end_date : Optional [datetime ]
18
- is_heading : bool
19
15
documents : list [Document ]
20
- agenda_items : list ['AgendaItem' ] = field (factory = list )
21
-
22
- class AgendaItems :
23
- @staticmethod
24
- def from_json (json_object : any ) -> list [AgendaItem ]:
25
- c = cattrs .Converter ()
26
-
27
- c .register_structure_hook (datetime , lambda date_string , _ : parse_date (date_string ))
28
- c .register_structure_hook (AgendaItem , agenda_item_structure_hook )
29
-
30
- agenda_items = [c .structure (item , AgendaItem ) for item in json_object ]
31
16
32
- return agenda_items
33
-
17
+ # Filled manually
18
+ title : str = field (init = False )
19
+ description : str = field (init = False )
20
+ start_date : Optional [datetime ] = field (init = False )
21
+ end_date : Optional [datetime ] = field (init = False )
22
+ is_heading : bool = field (init = False )
23
+ agenda_items : list ['AgendaItem' ] = field (factory = list )
34
24
35
25
def get_start_date (attributes ) -> datetime :
36
26
try :
@@ -44,22 +34,30 @@ def get_end_date(attributes) -> datetime:
44
34
except Exception : # The nested agenda items seem to have no end dates.
45
35
return None
46
36
47
- def agenda_item_structure_hook (data : Dict [str , Any ], cls : type ) -> AgendaItem :
48
- type_data = data .get ("type_data" , {})
49
- attributes = type_data ["attributes" ]
37
+ def agenda_item_hook (data : dict [str , any ], cls : type ) -> AgendaItem :
38
+ # Auto-fill fields
50
39
51
- documents = [Document .from_json (item ) for item in data ["documents" ]]
52
- agenda_items = AgendaItems .from_json (data ["agenda_items" ])
40
+ converter = Converter ()
41
+ converter .register_structure_hook (datetime , lambda date_string , _ : parse_date (date_string ))
42
+ converter .register_structure_hook (AgendaItem , agenda_item_hook )
43
+ documents = [converter .structure (item , Document ) for item in data .get ("documents" , [])]
53
44
54
- return AgendaItem (
55
- id = data ["id" ],
56
- last_modified = parse_date (data ["last_modified" ]),
57
- title = get_title (attributes ),
58
- description = get_description (attributes ),
59
- start_date = get_start_date (attributes ),
60
- end_date = get_end_date (attributes ),
61
- is_heading = data ["type_data" ]["heading" ],
62
- documents = documents ,
63
- agenda_items = agenda_items
45
+ agenda_item = AgendaItem (
46
+ id = data .get ("id" ),
47
+ last_modified = parse_date (data .get ("last_modified" )),
48
+ documents = documents
64
49
)
65
50
51
+ # Manually add some fields
52
+ type_data = data .get ("type_data" , {})
53
+ attributes = type_data ["attributes" ]
54
+
55
+ agenda_item .title = get_title (attributes )
56
+ agenda_item .description = get_description (attributes )
57
+ agenda_item .start_date = get_start_date (attributes )
58
+ agenda_item .end_date = get_end_date (attributes )
59
+ agenda_item .is_heading = type_data .get ("heading" , False )
60
+
61
+ agenda_item .agenda_items = [converter .structure (item , AgendaItem ) for item in data .get ("agenda_items" , [])]
62
+
63
+ return agenda_item
0 commit comments