diff --git a/apps/collection/migrations/0027_auto_20191217_1852.py b/apps/collection/migrations/0027_auto_20191217_1852.py new file mode 100644 index 00000000..5cbc4e0d --- /dev/null +++ b/apps/collection/migrations/0027_auto_20191217_1852.py @@ -0,0 +1,18 @@ +# Generated by Django 2.2.7 on 2019-12-17 18:52 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('collection', '0026_merge_20191217_1151'), + ] + + operations = [ + migrations.AlterField( + model_name='collection', + name='slug', + field=models.SlugField(max_length=255, null=True, unique=True, verbose_name='Collection slug'), + ), + ] diff --git a/apps/collection/models.py b/apps/collection/models.py index d273a9cd..a509b8d1 100644 --- a/apps/collection/models.py +++ b/apps/collection/models.py @@ -13,6 +13,8 @@ from utils.models import ( from utils.querysets import RelatedObjectsCountMixin from utils.models import IntermediateGalleryModelMixin, GalleryMixin +from slugify import slugify + # Mixins class CollectionNameMixin(models.Model): @@ -79,7 +81,7 @@ class Collection(ProjectBaseMixin, CollectionDateMixin, description = TJSONField( _('description'), null=True, blank=True, default=None, help_text='{"en-GB":"some text"}') - slug = models.SlugField(max_length=50, unique=True, + slug = models.SlugField(max_length=255, unique=True, verbose_name=_('Collection slug'), editable=True, null=True) old_id = models.IntegerField(null=True, blank=True) @@ -141,6 +143,15 @@ class Collection(ProjectBaseMixin, CollectionDateMixin, return related_objects + def save(self, *args, **kwargs): + if not self.pk: + slugify_slug = slugify( + next(iter(self.name.values())), + word_boundary=True + ) + self.slug = slugify_slug + super(Collection, self).save(*args, **kwargs) + class GuideTypeQuerySet(models.QuerySet): """QuerySet for model GuideType.""" diff --git a/apps/collection/serializers/back.py b/apps/collection/serializers/back.py index 35917142..06630d8e 100644 --- a/apps/collection/serializers/back.py +++ b/apps/collection/serializers/back.py @@ -43,6 +43,9 @@ class CollectionBackOfficeSerializer(CollectionBaseSerializer): 'related_object_names', 'rank', ] + extra_kwargs = { + 'slug': {'read_only': True}, + } class CollectionBindObjectSerializer(serializers.Serializer): diff --git a/requirements/base.txt b/requirements/base.txt index 18bb1bc5..e17c0975 100644 --- a/requirements/base.txt +++ b/requirements/base.txt @@ -64,5 +64,8 @@ pycountry==19.8.18 # sql-tree django-mptt==0.9.1 +# slugify +python-slugify==4.0.0 + # Export to Excel XlsxWriter==1.2.6 \ No newline at end of file