From fba4ab090c1cea84c31f15a1a9a9fce40fcbb363 Mon Sep 17 00:00:00 2001
From: lois <lois@procolix.eu>
Date: Thu, 20 Mar 2025 16:56:25 +0100
Subject: [PATCH] Remove duplicate code

---
 panel/src/panel/views.py | 63 ++++------------------------------------
 1 file changed, 5 insertions(+), 58 deletions(-)

diff --git a/panel/src/panel/views.py b/panel/src/panel/views.py
index 79cd30d1..fc3e87f0 100644
--- a/panel/src/panel/views.py
+++ b/panel/src/panel/views.py
@@ -33,10 +33,6 @@ class ConfigurationForm(LoginRequiredMixin, FormView):
     success_url = reverse_lazy('configuration_form')
     form_class = forms.Form
 
-    def get_context_data(self, **kwargs):
-        context = super().get_context_data(**kwargs)
-        return context
-
     def get_object(self):
         """Get or create the configuration object for the current user"""
         obj, created = models.Configuration.objects.get_or_create(
@@ -85,6 +81,7 @@ class ConfigurationForm(LoginRequiredMixin, FormView):
         initial.update(self.convert_enums_to_names(config_dict))
         return initial
 
+class Save(ConfigurationForm):
     def form_valid(self, form):
         obj = self.get_object()
         obj.value = form.to_python().model_dump_json()
@@ -92,57 +89,7 @@ class ConfigurationForm(LoginRequiredMixin, FormView):
 
         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()
@@ -182,10 +129,10 @@ class DeploymentStatus(LoginRequiredMixin, FormView):
             "apply",
             "test",
         ]
-        result = subprocess.run(
+        deployment_result = subprocess.run(
             cmd,
             cwd=settings.repo_dir,
             env=env,
         )
-        print(result)
-        return result
\ No newline at end of file
+        print(deployment_result)
+        return deployment_result
\ No newline at end of file