From 53bad5a10d2f0763ee6e782a6dc155606a28931b Mon Sep 17 00:00:00 2001 From: "Silvio J. Gutierrez" Date: Thu, 9 Nov 2023 14:13:49 -0500 Subject: [PATCH 1/2] Forbid callable form field defaults --- reactivated/serialization/__init__.py | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/reactivated/serialization/__init__.py b/reactivated/serialization/__init__.py index 595e7295a..2a680dbcb 100644 --- a/reactivated/serialization/__init__.py +++ b/reactivated/serialization/__init__.py @@ -334,12 +334,16 @@ def get_serialized_value( class_or_instance: Union[Type[django_forms.BaseForm], django_forms.BaseForm], schema: Thing, ) -> JSON: + if Type.__name__ == "WorkoutProgramForm": + assert False value = ( class_or_instance if isinstance(class_or_instance, django_forms.BaseForm) else class_or_instance() ) + name = f"{value.__class__.__module__}.{value.__class__.__qualname__}" + for field in value: if ( isinstance(field.field, django_forms.ModelChoiceField) @@ -347,6 +351,14 @@ def get_serialized_value( ): field.field.queryset = field.field.queryset.none() + if ( + callable(field.field.initial) + and schema.definitions.get("is_static_context") is True # type: ignore[comparison-overlap] + ): + assert ( + False + ), f"Callables are not supported in initial/default values for field {field.name} in form {name}" + form = value context = form.get_context() # type: ignore[attr-defined] @@ -362,9 +374,7 @@ def get_serialized_value( value.fields = {**hidden_fields, **visible_fields} serialized = serialize(value, schema, suppress_custom_serializer=True) - serialized[ - "name" - ] = f"{value.__class__.__module__}.{value.__class__.__qualname__}" + serialized["name"] = name serialized["prefix"] = form.prefix or "" serialized["iterator"] = list(hidden_fields.keys()) + list( visible_fields.keys() From 9a4fb2a5e607755e88b0534d0737e5f89b9246ff Mon Sep 17 00:00:00 2001 From: "Silvio J. Gutierrez" Date: Thu, 9 Nov 2023 14:15:58 -0500 Subject: [PATCH 2/2] Cleanup --- reactivated/serialization/__init__.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/reactivated/serialization/__init__.py b/reactivated/serialization/__init__.py index 2a680dbcb..42772be08 100644 --- a/reactivated/serialization/__init__.py +++ b/reactivated/serialization/__init__.py @@ -334,8 +334,6 @@ def get_serialized_value( class_or_instance: Union[Type[django_forms.BaseForm], django_forms.BaseForm], schema: Thing, ) -> JSON: - if Type.__name__ == "WorkoutProgramForm": - assert False value = ( class_or_instance if isinstance(class_or_instance, django_forms.BaseForm)