13
13
SuccessResult ,
14
14
WebHookResult ,
15
15
)
16
- from forestadmin .rpc_common .serializers .utils import camel_to_snake_case , enum_to_str_or_value , snake_to_camel_case
16
+ from forestadmin .rpc_common .serializers .utils import enum_to_str_or_value
17
17
18
18
19
19
class ActionSerializer :
@@ -31,22 +31,23 @@ async def serialize(action_name: str, collection: Collection) -> dict:
31
31
# ]
32
32
33
33
return {
34
- "scope" : action .scope .value ,
35
- "generateFile " : action .generate_file or False ,
36
- "staticForm " : action .static_form or False ,
34
+ "scope" : action .scope .value . lower () ,
35
+ "is_generate_file " : action .generate_file or False ,
36
+ "static_form " : action .static_form or False ,
37
37
"description" : action .description ,
38
- "submitButtonLabel " : action .submit_button_label ,
38
+ "submit_button_label " : action .submit_button_label ,
39
39
"form" : ActionFormSerializer .serialize (form ) if form is not None else [],
40
+ "execute" : {}, # TODO: I'm pretty sure this is not needed
40
41
}
41
42
42
43
@staticmethod
43
44
def deserialize (action : dict ) -> dict :
44
45
return {
45
- "scope" : ActionsScope (action ["scope" ]),
46
- "generate_file" : action ["generateFile " ],
47
- "static_form" : action ["staticForm " ],
46
+ "scope" : ActionsScope (action ["scope" ]. capitalize () ),
47
+ "generate_file" : action ["is_generate_file " ],
48
+ "static_form" : action ["static_form " ],
48
49
"description" : action ["description" ],
49
- "submit_button_label" : action ["submitButtonLabel " ],
50
+ "submit_button_label" : action ["submit_button_label " ],
50
51
"form" : ActionFormSerializer .deserialize (action ["form" ]) if action ["form" ] is not None else [],
51
52
}
52
53
@@ -57,35 +58,35 @@ def serialize(form) -> list[dict]:
57
58
serialized_form = []
58
59
59
60
for field in form :
61
+ tmp_field = {}
60
62
if field ["type" ] == ActionFieldType .LAYOUT :
61
63
if field ["component" ] == "Page" :
62
- serialized_form .append (
63
- {
64
- ** field ,
65
- "type" : "Layout" ,
66
- "elements" : ActionFormSerializer .serialize (field ["elements" ]),
67
- }
68
- )
64
+ tmp_field = {
65
+ ** field ,
66
+ "type" : "Layout" ,
67
+ "elements" : ActionFormSerializer .serialize (field ["elements" ]),
68
+ }
69
69
70
70
if field ["component" ] == "Row" :
71
- serialized_form .append (
72
- {
73
- ** field ,
74
- "type" : "Layout" ,
75
- "fields" : ActionFormSerializer .serialize (field ["fields" ]),
76
- }
77
- )
71
+ tmp_field = {
72
+ ** field ,
73
+ "type" : "Layout" ,
74
+ "fields" : ActionFormSerializer .serialize (field ["fields" ]),
75
+ }
78
76
else :
79
77
tmp_field = {
80
- ** {snake_to_camel_case ( k ) : v for k , v in field .items ()},
78
+ ** {k : v for k , v in field .items ()},
81
79
"type" : enum_to_str_or_value (field ["type" ]),
82
80
}
83
81
if field ["type" ] == ActionFieldType .FILE :
84
82
tmp_field .update (ActionFormSerializer ._serialize_file_field (tmp_field ))
85
- print (tmp_field .keys ())
86
83
if field ["type" ] == ActionFieldType .FILE_LIST :
87
84
tmp_field .update (ActionFormSerializer ._serialize_file_list_field (tmp_field ))
88
- serialized_form .append (tmp_field )
85
+
86
+ if "if_" in tmp_field :
87
+ tmp_field ["if_condition" ] = tmp_field ["if_" ]
88
+ del tmp_field ["if_" ]
89
+ serialized_form .append (tmp_field )
89
90
90
91
return serialized_form
91
92
@@ -105,7 +106,6 @@ def _serialize_file_field(field: dict) -> dict:
105
106
ret ["defaultValue" ] = ActionFormSerializer ._serialize_file_obj (field ["defaultValue" ])
106
107
if field .get ("value" ):
107
108
ret ["value" ] = ActionFormSerializer ._serialize_file_obj (field ["value" ])
108
- return ret
109
109
110
110
@staticmethod
111
111
def _serialize_file_obj (f : File ) -> dict :
@@ -148,34 +148,35 @@ def deserialize(form: list) -> list[dict]:
148
148
deserialized_form = []
149
149
150
150
for field in form :
151
+ tmp_field = {}
151
152
if field ["type" ] == "Layout" :
152
153
if field ["component" ] == "Page" :
153
- deserialized_form .append (
154
- {
155
- ** field ,
156
- "type" : ActionFieldType ("Layout" ),
157
- "elements" : ActionFormSerializer .deserialize (field ["elements" ]),
158
- }
159
- )
154
+ tmp_field = {
155
+ ** field ,
156
+ "type" : ActionFieldType ("Layout" ),
157
+ "elements" : ActionFormSerializer .deserialize (field ["elements" ]),
158
+ }
160
159
161
160
if field ["component" ] == "Row" :
162
- deserialized_form .append (
163
- {
164
- ** field ,
165
- "type" : ActionFieldType ("Layout" ),
166
- "fields" : ActionFormSerializer .deserialize (field ["fields" ]),
167
- }
168
- )
161
+ tmp_field = {
162
+ ** field ,
163
+ "type" : ActionFieldType ("Layout" ),
164
+ "fields" : ActionFormSerializer .deserialize (field ["fields" ]),
165
+ }
169
166
else :
170
167
tmp_field = {
171
- ** {camel_to_snake_case ( k ) : v for k , v in field .items ()},
168
+ ** {k : v for k , v in field .items ()},
172
169
"type" : ActionFieldType (field ["type" ]),
173
170
}
174
171
if tmp_field ["type" ] == ActionFieldType .FILE :
175
172
tmp_field .update (ActionFormSerializer ._deserialize_file_field (tmp_field ))
176
173
if tmp_field ["type" ] == ActionFieldType .FILE_LIST :
177
174
tmp_field .update (ActionFormSerializer ._deserialize_file_list_field (tmp_field ))
178
- deserialized_form .append (tmp_field )
175
+
176
+ if "if_condition" in tmp_field :
177
+ tmp_field ["if_" ] = tmp_field ["if_condition" ]
178
+ del tmp_field ["if_condition" ]
179
+ deserialized_form .append (tmp_field )
179
180
180
181
return deserialized_form
181
182
0 commit comments