diff --git a/orchestra/contrib/musician/settings.py b/orchestra/contrib/musician/settings.py index 8872536e..18c878a9 100644 --- a/orchestra/contrib/musician/settings.py +++ b/orchestra/contrib/musician/settings.py @@ -58,3 +58,9 @@ MUSICIAN_EDIT_ENABLE_PHP_OPTIONS = Setting('MUSICIAN_EDIT_ENABLE_PHP_OPTIONS', ( 'post_max_size', 'upload_max_filesize', )) + +MUSICIAN_WEBSITES_ENABLE_GROUP_DIRECTIVE = Setting('MUSICIAN_WEBSITES_ENABLE_GROUP_DIRECTIVE', ( + 'HTTPD', + ), + help_text="Valid groups: HTTPD, ModSecurity, SSL, SaaS" +) diff --git a/orchestra/contrib/musician/templates/musician/websites/website_detail.html b/orchestra/contrib/musician/templates/musician/websites/website_detail.html index d3c3b0ce..34e7036f 100644 --- a/orchestra/contrib/musician/templates/musician/websites/website_detail.html +++ b/orchestra/contrib/musician/templates/musician/websites/website_detail.html @@ -92,6 +92,6 @@ {% endfor %} - +{% trans "Add new directive" %} {% endblock %} diff --git a/orchestra/contrib/musician/tidy_forms/websites.py b/orchestra/contrib/musician/tidy_forms/websites.py index d1cd2ef5..ac402f14 100644 --- a/orchestra/contrib/musician/tidy_forms/websites.py +++ b/orchestra/contrib/musician/tidy_forms/websites.py @@ -3,11 +3,14 @@ from orchestra.forms.widgets import DynamicHelpTextSelect from django.core.exceptions import ValidationError 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.webapps.models import WebApp from orchestra.contrib.domains.models import Domain +from orchestra.contrib.musician.settings import MUSICIAN_WEBSITES_ENABLE_GROUP_DIRECTIVE class WebsiteUpdateForm(forms.ModelForm): @@ -47,11 +50,70 @@ class WesiteContentCreateForm(forms.ModelForm): cleaned_data = super().clean() path = self.cleaned_data.get("path") path = "/" if path == "" else path - print(f"mypath: {path}") if Content.objects.filter(website=self.website, path=path).exists(): self.add_error('path',_("This Path already exists on this Website.")) 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): instance = super().save(commit=False) instance.website = self.website diff --git a/orchestra/contrib/musician/tidy_views/websites.py b/orchestra/contrib/musician/tidy_views/websites.py index e0de6e0b..e536f90d 100644 --- a/orchestra/contrib/musician/tidy_views/websites.py +++ b/orchestra/contrib/musician/tidy_views/websites.py @@ -12,7 +12,8 @@ from orchestra.contrib.musician.mixins import (CustomContextMixin, ExtendedPagin UserTokenRequiredMixin) 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): @@ -121,4 +122,20 @@ class WebsiteAddContentView(CustomContextMixin, UserTokenRequiredMixin, CreateVi def get_success_url(self): 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"]}) \ No newline at end of file diff --git a/orchestra/contrib/musician/urls.py b/orchestra/contrib/musician/urls.py index 75950373..607d344b 100644 --- a/orchestra/contrib/musician/urls.py +++ b/orchestra/contrib/musician/urls.py @@ -49,6 +49,7 @@ urlpatterns = [ path('websites//', views.WebsiteDetailView.as_view(), name='website-detail'), path('websites//edit/', views.WebsiteUpdateView.as_view(), name='website-update'), path('websites//add-content/', views.WebsiteAddContentView.as_view(), name='website-add-content'), + path('websites//add-directive/', views.WebsiteAddDirectiveView.as_view(), name='website-add-directive'), path('websites//content//delete/', views.WebsiteDeleteContentView.as_view(), name='website-delete-content'), path('websites//directive//delete/', views.WebsiteDeleteDirectiveView.as_view(), name='website-delete-directive'), diff --git a/orchestra/contrib/websites/directives.py b/orchestra/contrib/websites/directives.py index 6192d164..d1a3f635 100644 --- a/orchestra/contrib/websites/directives.py +++ b/orchestra/contrib/websites/directives.py @@ -55,7 +55,7 @@ class SiteDirective(plugins.Plugin, metaclass=plugins.PluginMount): yield (option.name, option.verbose_name) for group, options in options.items(): yield (group, [(op.name, op.verbose_name) for op in options]) - + def validate_uniqueness(self, directive, values, locations): """ Validates uniqueness location, name and value """ errors = defaultdict(list) diff --git a/orchestra/contrib/websites/forms.py b/orchestra/contrib/websites/forms.py index f8e04d70..93c918c6 100644 --- a/orchestra/contrib/websites/forms.py +++ b/orchestra/contrib/websites/forms.py @@ -67,7 +67,7 @@ class WebsiteDirectiveInlineFormSet(forms.models.BaseInlineFormSet): 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