musician website create directives filter group name and helptext

This commit is contained in:
Jorge Pastor 2024-05-22 20:45:16 +02:00
parent 1ebf30db33
commit 475df0a2e5
7 changed files with 91 additions and 5 deletions

View File

@ -58,3 +58,9 @@ MUSICIAN_EDIT_ENABLE_PHP_OPTIONS = Setting('MUSICIAN_EDIT_ENABLE_PHP_OPTIONS', (
'post_max_size', 'post_max_size',
'upload_max_filesize', 'upload_max_filesize',
)) ))
MUSICIAN_WEBSITES_ENABLE_GROUP_DIRECTIVE = Setting('MUSICIAN_WEBSITES_ENABLE_GROUP_DIRECTIVE', (
'HTTPD',
),
help_text="Valid groups: HTTPD, ModSecurity, SSL, SaaS"
)

View File

@ -92,6 +92,6 @@
{% endfor %} {% endfor %}
</tbody> </tbody>
</table> </table>
<!-- <a class="btn btn-primary mt-4 mb-4" href="{% url 'musician:webapp-add-option' object.pk %}">{% trans "Add new option" %}</a></td> --> <a class="btn btn-primary mt-4 mb-4" href="{% url 'musician:website-add-directive' object.pk %}">{% trans "Add new directive" %}</a></td>
{% endblock %} {% endblock %}

View File

@ -3,11 +3,14 @@ from orchestra.forms.widgets import DynamicHelpTextSelect
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.contrib.websites.directives import SiteDirective
from orchestra.contrib.websites.models import Website, Content, WebsiteDirective from orchestra.contrib.websites.models import Website, Content, WebsiteDirective
from orchestra.contrib.webapps.models import WebApp from orchestra.contrib.webapps.models import WebApp
from orchestra.contrib.domains.models import Domain from orchestra.contrib.domains.models import Domain
from orchestra.contrib.musician.settings import MUSICIAN_WEBSITES_ENABLE_GROUP_DIRECTIVE
class WebsiteUpdateForm(forms.ModelForm): class WebsiteUpdateForm(forms.ModelForm):
@ -47,11 +50,70 @@ class WesiteContentCreateForm(forms.ModelForm):
cleaned_data = super().clean() cleaned_data = super().clean()
path = self.cleaned_data.get("path") path = self.cleaned_data.get("path")
path = "/" if path == "" else path path = "/" if path == "" else path
print(f"mypath: {path}")
if Content.objects.filter(website=self.website, path=path).exists(): if Content.objects.filter(website=self.website, path=path).exists():
self.add_error('path',_("This Path already exists on this Website.")) self.add_error('path',_("This Path already exists on this Website."))
return cleaned_data return cleaned_data
def save(self, commit=True):
instance = super().save(commit=False)
instance.website = self.website
if commit:
super().save(commit=True)
self.website.save()
return instance
from collections import defaultdict
from orchestra.contrib.websites.utils import normurlpath
class WesiteDirectiveCreateForm(forms.ModelForm):
DIRECTIVES_HELP_TEXT = {
op.name: force_str(op.help_text) for op in SiteDirective.get_plugins()
}
class Meta:
model = WebsiteDirective
fields = ("name", "value")
def __init__(self, *args, **kwargs):
self.website = kwargs.pop('website')
# self.user = kwargs.pop('user')
super().__init__(*args, **kwargs)
target = 'this.id.replace("name", "value")'
self.fields['name'].widget.attrs = DynamicHelpTextSelect(target, self.DIRECTIVES_HELP_TEXT).attrs
self.fields['name'].choices = self.get_allow_choices()
def get_allow_choices(self):
groups = MUSICIAN_WEBSITES_ENABLE_GROUP_DIRECTIVE
yield (None, '-------')
options = SiteDirective.get_option_groups()
for grp in groups:
if grp in options.keys():
yield (grp, [(op.name, op.verbose_name) for op in options[grp]])
# def clean(self):
# TODO: comprovar que la ruta no exista
# locations = set()
# for form in self.content_formset.forms:
# location = form.cleaned_data.get('path')
# delete = form.cleaned_data.get('DELETE')
# if not delete and location is not None:
# locations.add(normurlpath(location))
# values = defaultdict(list)
# for form in self.forms:
# wdirective = form.instance
# directive = form.cleaned_data
# if directive.get('name') is not None:
# try:
# wdirective.directive_instance.validate_uniqueness(directive, values, locations)
# except ValidationError as err:
# for k,v in err.error_dict.items():
# form.add_error(k, v)
def save(self, commit=True): def save(self, commit=True):
instance = super().save(commit=False) instance = super().save(commit=False)
instance.website = self.website instance.website = self.website

View File

@ -12,7 +12,8 @@ from orchestra.contrib.musician.mixins import (CustomContextMixin, ExtendedPagin
UserTokenRequiredMixin) UserTokenRequiredMixin)
from orchestra.contrib.websites.models import Website, Content, WebsiteDirective from orchestra.contrib.websites.models import Website, Content, WebsiteDirective
from orchestra.contrib.musician.tidy_forms.websites import WebsiteUpdateForm, WesiteContentCreateForm from orchestra.contrib.musician.tidy_forms.websites import ( WebsiteUpdateForm, WesiteContentCreateForm,
WesiteDirectiveCreateForm)
class WebsiteListView(CustomContextMixin, UserTokenRequiredMixin, ListView): class WebsiteListView(CustomContextMixin, UserTokenRequiredMixin, ListView):
@ -122,3 +123,19 @@ class WebsiteAddContentView(CustomContextMixin, UserTokenRequiredMixin, CreateVi
def get_success_url(self): def get_success_url(self):
return reverse_lazy("musician:website-detail", kwargs={"pk": self.kwargs["pk"]}) return reverse_lazy("musician:website-detail", kwargs={"pk": self.kwargs["pk"]})
class WebsiteAddDirectiveView(CustomContextMixin, UserTokenRequiredMixin, CreateView):
model = WebsiteDirective
form_class = WesiteDirectiveCreateForm
template_name = "musician/websites/website_create_option_form.html"
def get_form_kwargs(self):
kwargs = super().get_form_kwargs()
website = get_object_or_404(Website, account=self.request.user, pk=self.kwargs["pk"])
kwargs['website'] = website
# kwargs["user"] = self.request.user
return kwargs
def get_success_url(self):
return reverse_lazy("musician:website-detail", kwargs={"pk": self.kwargs["pk"]})

View File

@ -49,6 +49,7 @@ urlpatterns = [
path('websites/<int:pk>/', views.WebsiteDetailView.as_view(), name='website-detail'), path('websites/<int:pk>/', views.WebsiteDetailView.as_view(), name='website-detail'),
path('websites/<int:pk>/edit/', views.WebsiteUpdateView.as_view(), name='website-update'), path('websites/<int:pk>/edit/', views.WebsiteUpdateView.as_view(), name='website-update'),
path('websites/<int:pk>/add-content/', views.WebsiteAddContentView.as_view(), name='website-add-content'), path('websites/<int:pk>/add-content/', views.WebsiteAddContentView.as_view(), name='website-add-content'),
path('websites/<int:pk>/add-directive/', views.WebsiteAddDirectiveView.as_view(), name='website-add-directive'),
path('websites/<int:pk>/content/<int:content_pk>/delete/', views.WebsiteDeleteContentView.as_view(), name='website-delete-content'), path('websites/<int:pk>/content/<int:content_pk>/delete/', views.WebsiteDeleteContentView.as_view(), name='website-delete-content'),
path('websites/<int:pk>/directive/<int:directive_pk>/delete/', views.WebsiteDeleteDirectiveView.as_view(), name='website-delete-directive'), path('websites/<int:pk>/directive/<int:directive_pk>/delete/', views.WebsiteDeleteDirectiveView.as_view(), name='website-delete-directive'),