forked from Fediversity/Fediversity
Rebase onto main
This commit is contained in:
parent
08e597af1c
commit
772901085b
1 changed files with 2 additions and 52 deletions
|
@ -98,57 +98,7 @@ class DeploymentStatus(ConfigurationForm):
|
|||
|
||||
return super().form_valid(form)
|
||||
|
||||
class DeploymentStatus(LoginRequiredMixin, FormView):
|
||||
template_name = 'configuration_form.html'
|
||||
success_url = reverse_lazy('configuration_form')
|
||||
form_class = forms.Form
|
||||
|
||||
def get_object(self):
|
||||
"""Get or create the configuration object for the current user"""
|
||||
obj, created = models.Configuration.objects.get_or_create(
|
||||
operator=self.request.user,
|
||||
)
|
||||
|
||||
return obj
|
||||
|
||||
def convert_enums_to_names(self, data_dict):
|
||||
"""
|
||||
Recursively convert all enum values in a dictionary to their string names.
|
||||
This handles nested dictionaries and lists as well.
|
||||
|
||||
Needed for converting a Pydantic `BaseModel` instance to a `Form` input.
|
||||
"""
|
||||
if isinstance(data_dict, dict):
|
||||
result = {}
|
||||
for key, value in data_dict.items():
|
||||
if isinstance(value, Enum):
|
||||
# Convert Enum to its name
|
||||
result[key] = value.name
|
||||
elif isinstance(value, (dict, list)):
|
||||
# Recursively process nested structures
|
||||
result[key] = self.convert_enums_to_names(value)
|
||||
else:
|
||||
# Keep other values as is
|
||||
result[key] = value
|
||||
return result
|
||||
elif isinstance(data_dict, list):
|
||||
# Process each item in the list
|
||||
return [self.convert_enums_to_names(item) for item in data_dict]
|
||||
elif isinstance(data_dict, Enum):
|
||||
# Convert single Enum value
|
||||
return data_dict.name
|
||||
else:
|
||||
# Return non-dict, non-list, non-Enum values as is
|
||||
return data_dict
|
||||
|
||||
def get_initial(self):
|
||||
initial = super().get_initial()
|
||||
config = self.get_object()
|
||||
config_dict = config.parsed_value.model_dump()
|
||||
|
||||
initial.update(self.convert_enums_to_names(config_dict))
|
||||
return initial
|
||||
|
||||
class DeploymentStatus(ConfigurationForm):
|
||||
def form_valid(self, form):
|
||||
obj = self.get_object()
|
||||
obj.value = form.to_python().model_dump_json()
|
||||
|
@ -194,4 +144,4 @@ class DeploymentStatus(LoginRequiredMixin, FormView):
|
|||
env=env,
|
||||
)
|
||||
print(deployment_result)
|
||||
return deployment_result
|
||||
return deployment_result
|
Loading…
Add table
Reference in a new issue