diff options
author | Étienne Loks <etienne.loks@iggdrasil.net> | 2017-08-12 12:54:13 +0200 |
---|---|---|
committer | Étienne Loks <etienne.loks@iggdrasil.net> | 2017-08-12 12:54:13 +0200 |
commit | cf10508b07f73cb26c8d47ce2d916853efb111c0 (patch) | |
tree | c1c525f5e1f7b244688c282f169fac2e326fa4de /ishtar_common | |
parent | e182eeb29cf9a7dbd936e88ee0db1b8658a0c5ce (diff) | |
download | Ishtar-cf10508b07f73cb26c8d47ce2d916853efb111c0.tar.bz2 Ishtar-cf10508b07f73cb26c8d47ce2d916853efb111c0.zip |
DocumentTemplate: add slug, manage natural key. Utils: create_slug function
Diffstat (limited to 'ishtar_common')
-rw-r--r-- | ishtar_common/migrations/0007_documenttemplate_slug.py | 28 | ||||
-rw-r--r-- | ishtar_common/models.py | 27 | ||||
-rw-r--r-- | ishtar_common/utils.py | 14 |
3 files changed, 66 insertions, 3 deletions
diff --git a/ishtar_common/migrations/0007_documenttemplate_slug.py b/ishtar_common/migrations/0007_documenttemplate_slug.py new file mode 100644 index 000000000..2d0258dff --- /dev/null +++ b/ishtar_common/migrations/0007_documenttemplate_slug.py @@ -0,0 +1,28 @@ +# -*- coding: utf-8 -*- +from __future__ import unicode_literals + +from django.db import models, migrations +from ishtar_common.utils import create_slug + + +def dt_create_slug(apps, schema): + DocumentTemplate = apps.get_model('ishtar_common', 'documenttemplate') + for dt in DocumentTemplate.objects.all(): + dt.slug = create_slug(DocumentTemplate, dt.name) + dt.save() + + +class Migration(migrations.Migration): + + dependencies = [ + ('ishtar_common', '0006_auto_20170811_2129'), + ] + + operations = [ + migrations.AddField( + model_name='documenttemplate', + name='slug', + field=models.SlugField(null=True, max_length=100, blank=True, unique=True, verbose_name='Slug'), + ), + migrations.RunPython(dt_create_slug), + ] diff --git a/ishtar_common/models.py b/ishtar_common/models.py index 25270e633..b3fba3b1f 100644 --- a/ishtar_common/models.py +++ b/ishtar_common/models.py @@ -65,7 +65,7 @@ from django.contrib.gis.db import models from simple_history.models import HistoricalRecords as BaseHistoricalRecords from ishtar_common.model_merging import merge_model_objects -from ishtar_common.utils import get_cache, disable_for_loaddata +from ishtar_common.utils import get_cache, disable_for_loaddata, create_slug from ishtar_common.data_importer import Importer, ImportFormater, \ IntegerFormater, FloatFormater, UnicodeFormater, DateFormater, \ TypeFormater, YearFormater, StrToBoolean, FileFormater @@ -415,6 +415,11 @@ def post_save_cache(sender, **kwargs): sender.refresh_cache() +class SlugModelManager(models.Manager): + def get_by_natural_key(self, slug): + return self.get(slug=slug) + + class TypeManager(models.Manager): def get_by_natural_key(self, txt_idx): return self.get(txt_idx=txt_idx) @@ -1594,10 +1599,13 @@ class DocumentTemplate(models.Model): CLASSNAMES = (('archaeological_operations.models.AdministrativeAct', _(u"Administrative Act")),) name = models.CharField(_(u"Name"), max_length=100) + slug = models.SlugField(_(u"Slug"), blank=True, null=True, max_length=100, + unique=True) template = models.FileField(_(u"Template"), upload_to="upload/templates/") associated_object_name = models.CharField( _(u"Associated object"), max_length=100, choices=CLASSNAMES) available = models.BooleanField(_(u"Available"), default=True) + objects = SlugModelManager() class Meta: verbose_name = _(u"Document template") @@ -1607,6 +1615,14 @@ class DocumentTemplate(models.Model): def __unicode__(self): return self.name + def natural_key(self): + return (self.slug, ) + + def save(self, *args, **kwargs): + if not self.slug: + self.slug = create_slug(DocumentTemplate, self.name) + return super(DocumentTemplate, self).save(*args, **kwargs) + @classmethod def get_tuples(cls, dct={}, empty_first=True): dct['available'] = True @@ -1935,8 +1951,8 @@ class ImporterType(models.Model): """ name = models.CharField(_(u"Name"), blank=True, null=True, max_length=100) - slug = models.SlugField(_(u"Slug"), unique=True, blank=True, null=True, - max_length=100) + slug = models.SlugField(_(u"Slug"), unique=True, max_length=100, + blank=True, null=True) description = models.CharField(_(u"Description"), blank=True, null=True, max_length=500) users = models.ManyToManyField('IshtarUser', verbose_name=_(u"Users"), @@ -2029,6 +2045,11 @@ class ImporterType(models.Model): newclass = type(name, (Importer,), args) return newclass + def save(self, *args, **kwargs): + if not self.slug: + self.slug = create_slug(ImporterType, self.name) + return super(ImporterType, self).save(*args, **kwargs) + def get_associated_model(parent_model, keys): model = None diff --git a/ishtar_common/utils.py b/ishtar_common/utils.py index 19ff16394..9eda2d22f 100644 --- a/ishtar_common/utils.py +++ b/ishtar_common/utils.py @@ -241,3 +241,17 @@ def post_save_point(sender, **kwargs): instance.skip_history_when_saving = True instance.save() return + + +def create_slug(model, name, slug_attr='slug', max_length=100): + 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(): + final_slug = slug + break + slug = base_slug[:(max_length - 1 - len(str(idx)))] + "-" + str(idx) + idx += 1 + return final_slug |