diff options
Diffstat (limited to 'ishtar_common')
| -rw-r--r-- | ishtar_common/migrations/0238_biographicalnote_slug.py | 35 | ||||
| -rw-r--r-- | ishtar_common/models.py | 33 | ||||
| -rw-r--r-- | ishtar_common/utils.py | 7 | 
3 files changed, 73 insertions, 2 deletions
| diff --git a/ishtar_common/migrations/0238_biographicalnote_slug.py b/ishtar_common/migrations/0238_biographicalnote_slug.py new file mode 100644 index 000000000..faa25d699 --- /dev/null +++ b/ishtar_common/migrations/0238_biographicalnote_slug.py @@ -0,0 +1,35 @@ +# Generated by Django 2.2.24 on 2024-02-26 11:40 + +from django.db import migrations, models +from ishtar_common.utils import create_slug + + +def set_slug(apps, __): +    BiographicalNote = apps.get_model('ishtar_common', 'BiographicalNote') +    for bn in BiographicalNote.objects.all(): +        bn.slug = create_slug(BiographicalNote, bn.denomination, max_length=250, pk=bn.pk) +        bn.skip_history_when_saving = True +        bn.save() + + +class Migration(migrations.Migration): + +    dependencies = [ +        ('ishtar_common', '0237_data_migration_licenses_shootingangle'), +    ] + +    operations = [ +        migrations.AddField( +            model_name='biographicalnote', +            name='slug', +            field=models.SlugField(blank=True, help_text='The slug is the standardized version of the name. It contains only lowercase letters, numbers and hyphens. Each slug must be unique.', max_length=300, null=True, verbose_name='Textual ID'), +        ), +        migrations.RunPython(set_slug), +        migrations.AlterField( +            model_name='biographicalnote', +            name='slug', +            field=models.SlugField(blank=True, +                                   help_text='The slug is the standardized version of the name. It contains only lowercase letters, numbers and hyphens. Each slug must be unique.', +                                   max_length=300, verbose_name='Textual ID'), +        ), +    ] diff --git a/ishtar_common/models.py b/ishtar_common/models.py index a5e666b13..e05bcad2d 100644 --- a/ishtar_common/models.py +++ b/ishtar_common/models.py @@ -3237,6 +3237,16 @@ def text_format(text, text_format):  class BiographicalNote(BaseHistorizedItem, ValueGetter, MainItem):      SLUG = "biographicalnote"      denomination = models.TextField(_("Denomination")) +    slug = models.SlugField( +        _("Textual ID"), +        max_length=300, +        help_text=_( +            "The slug is the standardized version of the name. It contains " +            "only lowercase letters, numbers and hyphens. Each slug must " +            "be unique." +        ), +        blank=True, +    )      last_name = models.TextField(_("Last name"), blank=True, default="")      first_name = models.TextField(_("First name"), blank=True, default="")      birth_year = models.PositiveIntegerField(_("Year of birth"), blank=True, null=True) @@ -3267,6 +3277,29 @@ class BiographicalNote(BaseHistorizedItem, ValueGetter, MainItem):      def __str__(self):          return self.denomination +    def history_compress(self): +        return self.slug + +    @classmethod +    def history_decompress(cls, full_value, create=False): +        if not full_value: +            return [] +        res = [] +        for value in full_value: +            try: +                res.append(cls.objects.get(slug=value)) +            except cls.DoesNotExist: +                continue +        return res + +    def set_slug(self): +        self.slug = create_slug(self.__class__, self.denomination, max_length=250, pk=self.pk) + +    def save(self, *args, **kwargs): +        if not self.slug: +            self.set_slug() +        return super().save(*args, **kwargs) +  GDPR_ACTIVITY = (      ("DC", _("Directory consultation")), diff --git a/ishtar_common/utils.py b/ishtar_common/utils.py index 69736a434..a408abb24 100644 --- a/ishtar_common/utils.py +++ b/ishtar_common/utils.py @@ -1271,13 +1271,16 @@ def max_value_current_year(value):      return MaxValueValidator(datetime.date.today().year)(value) -def create_slug(model, name, slug_attr="slug", max_length=100): +def create_slug(model, name, slug_attr="slug", max_length=100, pk=None):      base_slug = slugify(name)      slug = base_slug[:max_length]      final_slug = None      idx = 1      while not final_slug: -        if slug and not model.objects.filter(**{slug_attr: slug}).exists(): +        q = model.objects.filter(**{slug_attr: slug}) +        if pk: +            q = q.exclude(pk=pk) +        if slug and not q.exists():              final_slug = slug              break          slug = base_slug[: (max_length - 1 - len(str(idx)))] + "-" + str(idx) | 
