From fa1a130370f29e0aecf0ed2b922c2e0843f61745 Mon Sep 17 00:00:00 2001 From: jorgepastorr Date: Thu, 25 Apr 2024 22:20:26 +0200 Subject: [PATCH] musician webapp edit, add helptext --- orchestra/contrib/musician/forms.py | 35 ++++++++++++++++- .../templates/musician/webapp_detail.html | 4 +- .../musician/webapp_option_form.html | 2 +- .../templates/musician/website_list.html | 31 +++++++++++++-- orchestra/contrib/musician/urls.py | 1 + orchestra/contrib/musician/views.py | 39 ++++++++++++++++++- 6 files changed, 103 insertions(+), 9 deletions(-) diff --git a/orchestra/contrib/musician/forms.py b/orchestra/contrib/musician/forms.py index d6dfd3bd..068591b7 100644 --- a/orchestra/contrib/musician/forms.py +++ b/orchestra/contrib/musician/forms.py @@ -2,6 +2,8 @@ from django import forms from django.contrib.auth.forms import AuthenticationForm from django.core.exceptions import ValidationError from django.utils.translation import gettext_lazy as _ +from django.utils.encoding import force_str +from orchestra.forms.widgets import DynamicHelpTextSelect from django.contrib.auth.hashers import make_password @@ -10,6 +12,8 @@ from orchestra.contrib.mailboxes.models import Address, Mailbox from orchestra.contrib.systemusers.models import WebappUsers, SystemUser from orchestra.contrib.musician.validators import ValidateZoneMixin from orchestra.contrib.webapps.models import WebApp, WebAppOption +from orchestra.contrib.webapps.options import AppOption +from orchestra.contrib.webapps.types import AppType from . import api @@ -203,8 +207,13 @@ class SystemUsersChangePasswordForm(ChangePasswordForm): fields = ("password",) model = SystemUser + class WebappOptionCreateForm(forms.ModelForm): + OPTIONS_HELP_TEXT = { + op.name: force_str(op.help_text) for op in AppOption.get_plugins() + } + class Meta: model = WebAppOption fields = ("name", "value") @@ -213,9 +222,33 @@ class WebappOptionCreateForm(forms.ModelForm): self.webapp = kwargs.pop('webapp') super().__init__(*args, **kwargs) + target = 'this.id.replace("name", "value")' + self.fields['name'].widget.attrs = DynamicHelpTextSelect(target, self.OPTIONS_HELP_TEXT).attrs + plugin = AppType.get(self.webapp.type) + self.fields['name'].widget.choices = plugin.get_group_options_choices() + def save(self, commit=True): instance = super().save(commit=False) instance.webapp = self.webapp if commit: super().save(commit=True) - return instance \ No newline at end of file + return instance + +class WebappOptionUpdateForm(forms.ModelForm): + + OPTIONS_HELP_TEXT = { + op.name: force_str(op.help_text) for op in AppOption.get_plugins() + } + + class Meta: + model = WebAppOption + fields = ("name", "value") + + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + self.webapp = self.instance.webapp + target = 'this.id.replace("name", "value")' + self.fields['name'].widget.attrs = DynamicHelpTextSelect(target, self.OPTIONS_HELP_TEXT).attrs + plugin = AppType.get(self.webapp.type) + self.fields['name'].widget.choices = plugin.get_group_options_choices() + diff --git a/orchestra/contrib/musician/templates/musician/webapp_detail.html b/orchestra/contrib/musician/templates/musician/webapp_detail.html index 7b35c0d1..e9616d7b 100644 --- a/orchestra/contrib/musician/templates/musician/webapp_detail.html +++ b/orchestra/contrib/musician/templates/musician/webapp_detail.html @@ -30,10 +30,12 @@ {{ option.name }} {{ option.value }} + + - + {% endfor %} diff --git a/orchestra/contrib/musician/templates/musician/webapp_option_form.html b/orchestra/contrib/musician/templates/musician/webapp_option_form.html index 4ca2390c..2ee18d9b 100644 --- a/orchestra/contrib/musician/templates/musician/webapp_option_form.html +++ b/orchestra/contrib/musician/templates/musician/webapp_option_form.html @@ -5,7 +5,7 @@ {% trans "Go back" %}

- {% if form.instance.pk %}{% trans "Update record of" %}{% else %}{% trans "Add record to" %}{% endif %} + {% if form.instance.pk %}{% trans "Update Option of" %}{% else %}{% trans "Add Option to" %}{% endif %} {{ form.webapp.name }}

diff --git a/orchestra/contrib/musician/templates/musician/website_list.html b/orchestra/contrib/musician/templates/musician/website_list.html index ea2f2b1e..390bf010 100644 --- a/orchestra/contrib/musician/templates/musician/website_list.html +++ b/orchestra/contrib/musician/templates/musician/website_list.html @@ -35,8 +35,11 @@ {{ website.is_active|yesno }} - - + + @@ -49,7 +52,7 @@ Webapp Dir /home/{{ content.webapp.account }}/webapps/{{ content.webapp }} - + @@ -100,4 +103,24 @@ {% include "musician/components/table_paginator.html" %} -{% endblock %} + + + + + +{% endblock %} \ No newline at end of file diff --git a/orchestra/contrib/musician/urls.py b/orchestra/contrib/musician/urls.py index fb626bbd..b71daad4 100644 --- a/orchestra/contrib/musician/urls.py +++ b/orchestra/contrib/musician/urls.py @@ -50,4 +50,5 @@ urlpatterns = [ path('webapps//', views.WebappDetailView.as_view(), name='webapp-detail'), path('webapps//add-option/', views.WebappAddOptionView.as_view(), name='webapp-add-option'), path('webapps//option//delete/', views.WebappDeleteOptionView.as_view(), name='webapp-delete-option'), + path('webapps//option//update/', views.WebappUpdateOptionView.as_view(), name='webapp-update-option'), ] diff --git a/orchestra/contrib/musician/views.py b/orchestra/contrib/musician/views.py index 87559aa0..0b580ad5 100644 --- a/orchestra/contrib/musician/views.py +++ b/orchestra/contrib/musician/views.py @@ -41,7 +41,7 @@ from .auth import logout as auth_logout from .forms import (LoginForm, MailboxChangePasswordForm, MailboxCreateForm, MailboxSearchForm, MailboxUpdateForm, MailForm, RecordCreateForm, RecordUpdateForm, WebappUsersChangePasswordForm, - SystemUsersChangePasswordForm, WebappOptionCreateForm) + SystemUsersChangePasswordForm, WebappOptionCreateForm, WebappOptionUpdateForm) from .mixins import (CustomContextMixin, ExtendedPaginationMixin, UserTokenRequiredMixin) from .models import Address as AddressService @@ -704,4 +704,39 @@ class WebappDeleteOptionView(CustomContextMixin, UserTokenRequiredMixin, DeleteV return qs def get_success_url(self): - return reverse_lazy("musician:webapp-detail", kwargs={"pk": self.kwargs["pk"]}) \ No newline at end of file + return reverse_lazy("musician:webapp-detail", kwargs={"pk": self.kwargs["pk"]}) + + +class WebappUpdateOptionView(CustomContextMixin, UserTokenRequiredMixin, UpdateView): + model = WebAppOption + form_class = WebappOptionUpdateForm + template_name = "musician/webapp_option_form.html" + pk_url_kwarg = "option_pk" + + def get_queryset(self): + qs = WebAppOption.objects.filter(webapp__account=self.request.user, webapp=self.kwargs["pk"]) + return qs + + def get_success_url(self): + return reverse_lazy("musician:webapp-detail", kwargs={"pk": self.kwargs["pk"]}) + +# from django.forms import inlineformset_factory +# class WebappUpdateOptionView(CustomContextMixin, UserTokenRequiredMixin, UpdateView): +# model = WebApp +# template_name = "musician/webapp_option_form.html" +# fields = '__all__' + +# def get_context_data(self, **kwargs): +# context = super().get_context_data(**kwargs) +# webapp = self.object # Obtener el objeto de libro + +# # Crear el inline formset para los autores +# OptionFormSet = inlineformset_factory(WebApp, WebAppOption, fields=('name','value'), extra=1) + +# # Obtener el formset prellenado con los autores del libro +# formset = OptionFormSet(instance=webapp) + +# # Agregar el formset al contexto +# context['option_formset'] = formset + +# return context \ No newline at end of file