forked from Fediversity/Fediversity
Rebase onto main
This commit is contained in:
parent
e330ebcd3b
commit
08e597af1c
1 changed files with 59 additions and 1 deletions
|
@ -91,6 +91,64 @@ class Save(ConfigurationForm):
|
||||||
return super().form_valid(form)
|
return super().form_valid(form)
|
||||||
|
|
||||||
class DeploymentStatus(ConfigurationForm):
|
class DeploymentStatus(ConfigurationForm):
|
||||||
|
def form_valid(self, form):
|
||||||
|
obj = self.get_object()
|
||||||
|
obj.value = form.to_python().model_dump_json()
|
||||||
|
obj.save()
|
||||||
|
|
||||||
|
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
|
||||||
|
|
||||||
def form_valid(self, form):
|
def form_valid(self, form):
|
||||||
obj = self.get_object()
|
obj = self.get_object()
|
||||||
obj.value = form.to_python().model_dump_json()
|
obj.value = form.to_python().model_dump_json()
|
||||||
|
@ -136,4 +194,4 @@ class DeploymentStatus(ConfigurationForm):
|
||||||
env=env,
|
env=env,
|
||||||
)
|
)
|
||||||
print(deployment_result)
|
print(deployment_result)
|
||||||
return deployment_result
|
return deployment_result
|
||||||
|
|
Loading…
Add table
Reference in a new issue