From cc108a8dcda642397544593bdee583c31ee19de9 Mon Sep 17 00:00:00 2001 From: krek0 Date: Wed, 22 Apr 2026 13:44:18 +0200 Subject: [PATCH] Allow duplicate gallery names --- photologue/forms.py | 14 +++++++------- .../0006_allow_duplicate_gallery_titles.py | 18 ++++++++++++++++++ photologue/models.py | 2 +- 3 files changed, 26 insertions(+), 8 deletions(-) create mode 100644 photologue/migrations/0006_allow_duplicate_gallery_titles.py diff --git a/photologue/forms.py b/photologue/forms.py index 1fef985..f7eba00 100644 --- a/photologue/forms.py +++ b/photologue/forms.py @@ -120,12 +120,6 @@ class UploadForm(forms.Form): Submit("submit", _("Upload"), css_class="btn btn-success mt-2"), ) - def clean_new_gallery_title(self): - title = self.cleaned_data["new_gallery_title"] - if title and Gallery.objects.filter(title=title).exists(): - raise forms.ValidationError(_("A gallery with that title already exists.")) - return title - def clean(self): cleaned_data = super().clean() @@ -148,9 +142,15 @@ class UploadForm(forms.Form): if not gallery: # Create new gallery title = self.cleaned_data.get("new_gallery_title") + base_slug = slugify(title) + slug = base_slug + counter = 2 + while Gallery.objects.filter(slug=slug).exists(): + slug = f"{base_slug}-{counter}" + counter += 1 gallery = Gallery.objects.create( title=title, - slug=slugify(title), + slug=slug, date_start=self.cleaned_data["new_gallery_date_start"], date_end=self.cleaned_data["new_gallery_date_end"], description=self.cleaned_data["new_gallery_description"], diff --git a/photologue/migrations/0006_allow_duplicate_gallery_titles.py b/photologue/migrations/0006_allow_duplicate_gallery_titles.py new file mode 100644 index 0000000..f0cad93 --- /dev/null +++ b/photologue/migrations/0006_allow_duplicate_gallery_titles.py @@ -0,0 +1,18 @@ +# Generated by Django 6.0.4 on 2026-04-22 10:58 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('photologue', '0005_add_can_resolve_censorship_permission'), + ] + + operations = [ + migrations.AlterField( + model_name='gallery', + name='title', + field=models.CharField(max_length=250, verbose_name='title'), + ), + ] diff --git a/photologue/models.py b/photologue/models.py index a466c92..99a6449 100644 --- a/photologue/models.py +++ b/photologue/models.py @@ -145,7 +145,7 @@ class TagField(models.CharField): class Gallery(models.Model): - title = models.CharField(_("title"), max_length=250, unique=True) + title = models.CharField(_("title"), max_length=250) slug = models.SlugField( _("title slug"), unique=True,