forked from Fediversity/Fediversity
Add seperate view and url for deploy
This commit is contained in:
parent
081a38eb6e
commit
d5fdc35e75
3 changed files with 61 additions and 3 deletions
panel/src/panel
|
@ -5,7 +5,7 @@
|
||||||
|
|
||||||
{{ form.as_p }}
|
{{ form.as_p }}
|
||||||
<button id="deploy-button" class="button"
|
<button id="deploy-button" class="button"
|
||||||
hx-post="{% url 'configuration_form' %}"
|
hx-post="{% url 'deployment_status' %}"
|
||||||
hx-trigger="click"
|
hx-trigger="click"
|
||||||
hx-indicator="#spinner-container"
|
hx-indicator="#spinner-container"
|
||||||
hx-disabled-elt="this"
|
hx-disabled-elt="this"
|
||||||
|
|
|
@ -26,4 +26,5 @@ urlpatterns = [
|
||||||
path("account/", views.AccountDetail.as_view(), name='account_detail'),
|
path("account/", views.AccountDetail.as_view(), name='account_detail'),
|
||||||
path("services/", views.ServiceList.as_view(), name='service_list'),
|
path("services/", views.ServiceList.as_view(), name='service_list'),
|
||||||
path("configuration/", views.ConfigurationForm.as_view(), name='configuration_form'),
|
path("configuration/", views.ConfigurationForm.as_view(), name='configuration_form'),
|
||||||
|
path("deployment/status/", views.DeploymentStatus.as_view(), name='deployment_status'),
|
||||||
]
|
]
|
||||||
|
|
|
@ -85,6 +85,64 @@ class ConfigurationForm(LoginRequiredMixin, FormView):
|
||||||
initial.update(self.convert_enums_to_names(config_dict))
|
initial.update(self.convert_enums_to_names(config_dict))
|
||||||
return initial
|
return initial
|
||||||
|
|
||||||
|
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()
|
||||||
|
@ -112,7 +170,6 @@ class ConfigurationForm(LoginRequiredMixin, FormView):
|
||||||
cmd = [
|
cmd = [
|
||||||
"nix",
|
"nix",
|
||||||
"develop",
|
"develop",
|
||||||
# workaround to pass in info to nixops4 thru env vars, tho impure :(
|
|
||||||
"--extra-experimental-features",
|
"--extra-experimental-features",
|
||||||
"configurable-impure-env",
|
"configurable-impure-env",
|
||||||
"--command",
|
"--command",
|
||||||
|
@ -126,4 +183,4 @@ class ConfigurationForm(LoginRequiredMixin, FormView):
|
||||||
env=env,
|
env=env,
|
||||||
)
|
)
|
||||||
|
|
||||||
return super().form_valid(form)
|
return super().form_valid(form)
|
Loading…
Add table
Reference in a new issue