From 0dc4352432ad0043965b8bd8eb52b0036e5962af Mon Sep 17 00:00:00 2001 From: jorgepastorr Date: Thu, 23 May 2024 21:46:45 +0200 Subject: [PATCH] musician website directives clear --- .../contrib/musician/tidy_forms/websites.py | 38 +++++++++---------- .../contrib/musician/tidy_views/websites.py | 1 - orchestra/contrib/websites/directives.py | 1 + 3 files changed, 19 insertions(+), 21 deletions(-) diff --git a/orchestra/contrib/musician/tidy_forms/websites.py b/orchestra/contrib/musician/tidy_forms/websites.py index ac402f14..9fd38b3b 100644 --- a/orchestra/contrib/musician/tidy_forms/websites.py +++ b/orchestra/contrib/musician/tidy_forms/websites.py @@ -93,26 +93,24 @@ class WesiteDirectiveCreateForm(forms.ModelForm): - # 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 clean(self): + # Recoge todos los paths de directive y contents + locations = set() + for content in Content.objects.filter(website=self.website): + location = content.path + if location is not None: + locations.add(location) + for directive_obj in WebsiteDirective.objects.filter(website=self.website): + location = directive_obj.value + if location is not None: + locations.add(normurlpath(location.split()[0])) + # Comprueva que no se repitan + directive = self.cleaned_data + value = normurlpath(directive.get('value')) + if value: + value = value.split()[0] + if value in locations: + self.add_error('value', f"Location '{value}' already in use by other content/directive.") def save(self, commit=True): instance = super().save(commit=False) diff --git a/orchestra/contrib/musician/tidy_views/websites.py b/orchestra/contrib/musician/tidy_views/websites.py index e536f90d..9d135dcd 100644 --- a/orchestra/contrib/musician/tidy_views/websites.py +++ b/orchestra/contrib/musician/tidy_views/websites.py @@ -133,7 +133,6 @@ class WebsiteAddDirectiveView(CustomContextMixin, UserTokenRequiredMixin, Create 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): diff --git a/orchestra/contrib/websites/directives.py b/orchestra/contrib/websites/directives.py index d1a3f635..ac44dc9a 100644 --- a/orchestra/contrib/websites/directives.py +++ b/orchestra/contrib/websites/directives.py @@ -4,6 +4,7 @@ from functools import lru_cache from django.core.exceptions import ValidationError from django.utils.translation import gettext_lazy as _ +from django.utils.encoding import force_str from orchestra import plugins from orchestra.utils.python import import_class