14
14
15
15
from openforms .formio .service import FormioData
16
16
from openforms .forms .models .form_variable import FormVariable
17
- from openforms .typing import DataMapping , JSONEncodable , JSONObject , JSONSerializable
17
+ from openforms .typing import DataMapping , JSONEncodable , JSONObject , JSONSerializable , JSONValue
18
18
from openforms .utils .date import format_date_value , parse_datetime , parse_time
19
19
from openforms .variables .constants import FormVariableDataTypes
20
20
from openforms .variables .service import VariablesRegistry , get_static_variables
@@ -253,6 +253,22 @@ def set_values(self, data: DataMapping) -> None:
253
253
continue
254
254
variable .value = new_value
255
255
256
+ # TODO-5139: can this be combined with SubmissionValueVariablesState.get_data?
257
+ def to_python (self ) -> FormioData :
258
+ """
259
+ Collect the total picture of data/variable values.
260
+
261
+ The dynamic values are augmented with the static variables.
262
+
263
+ :return: A datamapping (key: variable key, value: native python object for the
264
+ value) ready for (template context) evaluation.
265
+ """
266
+ dynamic_values = {
267
+ key : variable .to_python () for key , variable in self .variables .items ()
268
+ }
269
+ static_values = self .static_data ()
270
+ return FormioData ({** dynamic_values , ** static_values })
271
+
256
272
257
273
class SubmissionValueVariableManager (models .Manager ):
258
274
def bulk_create_or_update_from_data (
@@ -361,7 +377,7 @@ class Meta:
361
377
def __str__ (self ):
362
378
return _ ("Submission value variable {key}" ).format (key = self .key )
363
379
364
- def to_python (self ) -> Any :
380
+ def to_python (self , value : JSONValue = None ) -> Any :
365
381
"""
366
382
Deserialize the value into the appropriate python type.
367
383
@@ -371,7 +387,10 @@ def to_python(self) -> Any:
371
387
to correctly interpret the data. For the time being, this is not needed yet
372
388
as we focus on NL first.
373
389
"""
374
- if self .value is None :
390
+ if value is None :
391
+ value = self .value
392
+
393
+ if value is None :
375
394
return None
376
395
377
396
# it's possible a submission value variable exists without the form variable
@@ -390,7 +409,7 @@ def to_python(self) -> Any:
390
409
"submission_id" : self .submission_id ,
391
410
},
392
411
)
393
- return self . value
412
+ return value
394
413
395
414
# we expect JSON types to have been properly stored (and thus not as string!)
396
415
data_type = self .form_variable .data_type
@@ -402,37 +421,37 @@ def to_python(self) -> Any:
402
421
FormVariableDataTypes .float ,
403
422
FormVariableDataTypes .array ,
404
423
):
405
- return self . value
424
+ return value
406
425
407
- if self . value and data_type == FormVariableDataTypes .date :
408
- if isinstance (self . value , date ):
409
- return self . value
410
- formatted_date = format_date_value (self . value )
426
+ if value and data_type == FormVariableDataTypes .date :
427
+ if isinstance (value , date ):
428
+ return value
429
+ formatted_date = format_date_value (value )
411
430
naive_date = parse_date (formatted_date )
412
431
if naive_date is not None :
413
432
aware_date = timezone .make_aware (datetime .combine (naive_date , time .min ))
414
433
return aware_date .date ()
415
434
416
- maybe_naive_datetime = parse_datetime (self . value )
435
+ maybe_naive_datetime = parse_datetime (value )
417
436
if maybe_naive_datetime is None :
418
437
return
419
438
420
439
if timezone .is_aware (maybe_naive_datetime ):
421
440
return maybe_naive_datetime .date ()
422
441
return timezone .make_aware (maybe_naive_datetime ).date ()
423
442
424
- if self . value and data_type == FormVariableDataTypes .datetime :
425
- if isinstance (self . value , datetime ):
426
- return self . value
427
- maybe_naive_datetime = parse_datetime (self . value )
443
+ if value and data_type == FormVariableDataTypes .datetime :
444
+ if isinstance (value , datetime ):
445
+ return value
446
+ maybe_naive_datetime = parse_datetime (value )
428
447
if maybe_naive_datetime is None :
429
448
return
430
449
431
450
if timezone .is_aware (maybe_naive_datetime ):
432
451
return maybe_naive_datetime
433
452
return timezone .make_aware (maybe_naive_datetime )
434
453
435
- if self . value and data_type == FormVariableDataTypes .time :
436
- return parse_time (self . value )
454
+ if value and data_type == FormVariableDataTypes .time :
455
+ return parse_time (value )
437
456
438
- return self . value
457
+ return value
0 commit comments