musician webapp edit, add helptext

This commit is contained in:
Jorge Pastor 2024-04-25 22:20:26 +02:00
parent 3c3e15d1c5
commit fa1a130370
6 changed files with 103 additions and 9 deletions

View File

@ -2,6 +2,8 @@ from django import forms
from django.contrib.auth.forms import AuthenticationForm from django.contrib.auth.forms import AuthenticationForm
from django.core.exceptions import ValidationError from django.core.exceptions import ValidationError
from django.utils.translation import gettext_lazy as _ 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 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.systemusers.models import WebappUsers, SystemUser
from orchestra.contrib.musician.validators import ValidateZoneMixin from orchestra.contrib.musician.validators import ValidateZoneMixin
from orchestra.contrib.webapps.models import WebApp, WebAppOption 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 from . import api
@ -203,8 +207,13 @@ class SystemUsersChangePasswordForm(ChangePasswordForm):
fields = ("password",) fields = ("password",)
model = SystemUser model = SystemUser
class WebappOptionCreateForm(forms.ModelForm): class WebappOptionCreateForm(forms.ModelForm):
OPTIONS_HELP_TEXT = {
op.name: force_str(op.help_text) for op in AppOption.get_plugins()
}
class Meta: class Meta:
model = WebAppOption model = WebAppOption
fields = ("name", "value") fields = ("name", "value")
@ -213,9 +222,33 @@ class WebappOptionCreateForm(forms.ModelForm):
self.webapp = kwargs.pop('webapp') self.webapp = kwargs.pop('webapp')
super().__init__(*args, **kwargs) 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): def save(self, commit=True):
instance = super().save(commit=False) instance = super().save(commit=False)
instance.webapp = self.webapp instance.webapp = self.webapp
if commit: if commit:
super().save(commit=True) super().save(commit=True)
return instance 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()

View File

@ -30,10 +30,12 @@
<td>{{ option.name }}</td> <td>{{ option.name }}</td>
<td>{{ option.value }}</td> <td>{{ option.value }}</td>
<td class="text-right"> <td class="text-right">
<a href="{% url 'musician:webapp-update-option' object.pk option.pk %}">
<i class="ml-3 fas fa-edit"></i></a>
<a href="{% url 'musician:webapp-delete-option' object.pk option.pk %}"> <a href="{% url 'musician:webapp-delete-option' object.pk option.pk %}">
<i class="ml-3 text-danger fas fa-trash"></i></a> <i class="ml-3 text-danger fas fa-trash"></i></a>
</td> </td>
</tr> </tr>
{% endfor %} {% endfor %}
</tbody> </tbody>
</table> </table>

View File

@ -5,7 +5,7 @@
<a class="btn-arrow-left" href="{% url 'musician:webapp-detail' view.kwargs.pk %}">{% trans "Go back" %}</a> <a class="btn-arrow-left" href="{% url 'musician:webapp-detail' view.kwargs.pk %}">{% trans "Go back" %}</a>
<h1 class="service-name"> <h1 class="service-name">
{% 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 %}
<span class="font-weight-light">{{ form.webapp.name }}</span> <span class="font-weight-light">{{ form.webapp.name }}</span>
</h1> </h1>

View File

@ -35,8 +35,11 @@
<span class="sr-only">{{ website.is_active|yesno }}</span> <span class="sr-only">{{ website.is_active|yesno }}</span>
</td> </td>
<td> <td>
<a class="btn btn-outline-warning" href="#"> <!-- <a class="btn btn-outline-warning" href="#">
<i class="fas fa-tools"></i></a> <i class="fas fa-tools"></i></a> -->
<button type="button" class="btn btn-outline-warning" data-toggle="modal" data-target="#exampleModal">
<i class="fas fa-tools"></i>
</button>
</td> </td>
</tr> </tr>
<!-- Fila oculta de webapp --> <!-- Fila oculta de webapp -->
@ -49,7 +52,7 @@
<td>Webapp Dir</td> <td>Webapp Dir</td>
<td>/home/{{ content.webapp.account }}/webapps/{{ content.webapp }}</td> <td>/home/{{ content.webapp.account }}/webapps/{{ content.webapp }}</td>
<td class="text-right"> <td class="text-right">
<a class="btn btn-outline-secondary" href="#"> <a class="btn btn-outline-secondary" href="{% url 'musician:webapp-list'%}">
<i class="fas fa-tools"></i></a> <i class="fas fa-tools"></i></a>
</td> </td>
</tr> </tr>
@ -100,4 +103,24 @@
{% include "musician/components/table_paginator.html" %} {% include "musician/components/table_paginator.html" %}
</table> </table>
{% endblock %}
<!-- Modal -->
<div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">Sorry!</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">&times;</span>
</button>
</div>
<div class="modal-body">
{% trans "This section is under development." %}
</div>
</div>
</div>
</div>
{% endblock %}

View File

@ -50,4 +50,5 @@ urlpatterns = [
path('webapps/<int:pk>/', views.WebappDetailView.as_view(), name='webapp-detail'), path('webapps/<int:pk>/', views.WebappDetailView.as_view(), name='webapp-detail'),
path('webapps/<int:pk>/add-option/', views.WebappAddOptionView.as_view(), name='webapp-add-option'), path('webapps/<int:pk>/add-option/', views.WebappAddOptionView.as_view(), name='webapp-add-option'),
path('webapps/<int:pk>/option/<int:option_pk>/delete/', views.WebappDeleteOptionView.as_view(), name='webapp-delete-option'), path('webapps/<int:pk>/option/<int:option_pk>/delete/', views.WebappDeleteOptionView.as_view(), name='webapp-delete-option'),
path('webapps/<int:pk>/option/<int:option_pk>/update/', views.WebappUpdateOptionView.as_view(), name='webapp-update-option'),
] ]

View File

@ -41,7 +41,7 @@ from .auth import logout as auth_logout
from .forms import (LoginForm, MailboxChangePasswordForm, MailboxCreateForm, from .forms import (LoginForm, MailboxChangePasswordForm, MailboxCreateForm,
MailboxSearchForm, MailboxUpdateForm, MailForm, MailboxSearchForm, MailboxUpdateForm, MailForm,
RecordCreateForm, RecordUpdateForm, WebappUsersChangePasswordForm, RecordCreateForm, RecordUpdateForm, WebappUsersChangePasswordForm,
SystemUsersChangePasswordForm, WebappOptionCreateForm) SystemUsersChangePasswordForm, WebappOptionCreateForm, WebappOptionUpdateForm)
from .mixins import (CustomContextMixin, ExtendedPaginationMixin, from .mixins import (CustomContextMixin, ExtendedPaginationMixin,
UserTokenRequiredMixin) UserTokenRequiredMixin)
from .models import Address as AddressService from .models import Address as AddressService
@ -704,4 +704,39 @@ class WebappDeleteOptionView(CustomContextMixin, UserTokenRequiredMixin, DeleteV
return qs return qs
def get_success_url(self): def get_success_url(self):
return reverse_lazy("musician:webapp-detail", kwargs={"pk": self.kwargs["pk"]}) 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