From 5af2a26f97a0c749c3c47327e18b7c34a0c53c0c Mon Sep 17 00:00:00 2001 From: blag Date: Sun, 25 May 2025 23:06:05 -0600 Subject: [PATCH] Add uniqueness setting and optionally connect signal --- dbtemplates/apps.py | 9 +++++++++ dbtemplates/conf.py | 1 + .../0003_template_template_name_idx.py | 18 ++++++++++++++++++ dbtemplates/models.py | 3 +++ 4 files changed, 31 insertions(+) create mode 100644 dbtemplates/migrations/0003_template_template_name_idx.py diff --git a/dbtemplates/apps.py b/dbtemplates/apps.py index 9f75273..b02e6c6 100644 --- a/dbtemplates/apps.py +++ b/dbtemplates/apps.py @@ -1,4 +1,6 @@ from django.apps import AppConfig +from django.conf import settings +from django.db.models.signals import m2m_changed from django.utils.translation import gettext_lazy as _ @@ -7,3 +9,10 @@ class DBTemplatesConfig(AppConfig): verbose_name = _('Database templates') default_auto_field = 'django.db.models.AutoField' + + def ready(self): + from .models import Template + from .signal_handlers import verify_template_name_uniqueness_across_all_selected_sites + + if getattr(settings, 'DBTEMPLATES_UNIQUE', False): + m2m_changed.connect(verify_template_name_uniqueness_across_all_selected_sites, sender=Template.sites.through) diff --git a/dbtemplates/conf.py b/dbtemplates/conf.py index 010db5b..eb8cda3 100644 --- a/dbtemplates/conf.py +++ b/dbtemplates/conf.py @@ -16,6 +16,7 @@ class DbTemplatesConf(AppConf): AUTO_POPULATE_CONTENT = True MEDIA_PREFIX = None CACHE_BACKEND = None + UNIQUE = False def configure_media_prefix(self, value): if value is None: diff --git a/dbtemplates/migrations/0003_template_template_name_idx.py b/dbtemplates/migrations/0003_template_template_name_idx.py new file mode 100644 index 0000000..7e66bed --- /dev/null +++ b/dbtemplates/migrations/0003_template_template_name_idx.py @@ -0,0 +1,18 @@ +# Generated by Django 5.1 on 2025-05-26 19:59 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('dbtemplates', '0002_alter_template_creation_date_and_more'), + ('sites', '0002_alter_domain_unique'), + ] + + operations = [ + migrations.AddIndex( + model_name='template', + index=models.Index(fields=['name'], name='template_name_idx'), + ), + ] diff --git a/dbtemplates/models.py b/dbtemplates/models.py index ca04070..670c30e 100644 --- a/dbtemplates/models.py +++ b/dbtemplates/models.py @@ -36,6 +36,9 @@ class AbstractTemplateMixin(models.Model): verbose_name = _('template') verbose_name_plural = _('templates') ordering = ('name',) + indexes = [ + models.Index(fields=["name"], name="template_name_idx"), + ] def __str__(self): return self.name