diff options
-rw-r--r-- | Makefile.example | 2 | ||||
-rw-r--r-- | archaeological_operations/forms.py | 2 | ||||
-rw-r--r-- | archaeological_operations/tests.py | 5 | ||||
-rw-r--r-- | ishtar_common/admin.py | 8 | ||||
-rw-r--r-- | ishtar_common/fixtures/initial_data-fr.json | 20 | ||||
-rw-r--r-- | ishtar_common/fixtures/initial_importtypes-fr.json | 27 | ||||
-rw-r--r-- | ishtar_common/forms.py | 2 | ||||
-rw-r--r-- | ishtar_common/migrations/0093_auto_20190429_0950.py | 50 | ||||
-rw-r--r-- | ishtar_common/migrations/0094_auto_20190429_1041.py | 39 | ||||
-rw-r--r-- | ishtar_common/models.py | 27 | ||||
-rw-r--r-- | ishtar_common/models_imports.py | 4 |
11 files changed, 148 insertions, 38 deletions
diff --git a/Makefile.example b/Makefile.example index 70eb78424..fc62e84d2 100644 --- a/Makefile.example +++ b/Makefile.example @@ -140,12 +140,12 @@ fixtures_common: fixtures_common_importers fixtures_spatialrefsystem ishtar_common.titletype \ ishtar_common.supporttype \ ishtar_common.format \ - ishtar_common.documenttemplate \ > '../ishtar_common/fixtures/initial_data-'$(default_data)'.json' fixtures_common_importers: cd $(project); $(PYTHON) ./manage.py dumpdata --indent 4 --natural-primary --natural-foreign \ ishtar_common.importermodel \ + ishtar_common.documenttemplate \ ishtar_common.importertype \ ishtar_common.regexp \ ishtar_common.valueformater \ diff --git a/archaeological_operations/forms.py b/archaeological_operations/forms.py index e3cdfeb4e..5cc544171 100644 --- a/archaeological_operations/forms.py +++ b/archaeological_operations/forms.py @@ -1673,7 +1673,7 @@ class DocumentGenerationAdminActForm(IshtarForm): self.obj = kwargs.pop('obj') super(DocumentGenerationAdminActForm, self).__init__(*args, **kwargs) self.fields['document_template'].choices = DocumentTemplate.get_tuples( - dct={'associated_object_name': + dct={'associated_model__klass': 'archaeological_operations.models.AdministrativeAct', 'acttypes__intented_to': self.document_type}) diff --git a/archaeological_operations/tests.py b/archaeological_operations/tests.py index 8eb2f361a..eba0d296a 100644 --- a/archaeological_operations/tests.py +++ b/archaeological_operations/tests.py @@ -2159,9 +2159,12 @@ class RegisterTest(TestCase, OperationInitTest): '../archaeological_operations/tests/document_reference.odt', 'rb') template = SimpleUploadedFile(tpl.name, tpl.read()) + model, __ = ImporterModel.objects.get_or_create( + klass='archaeological_operations.models.AdministrativeAct' + ) doc = DocumentTemplate.objects.create( name="Test", - associated_object_name=DocumentTemplate.CLASSNAMES[0][0], + associated_model=model, available=True, template=template ) diff --git a/ishtar_common/admin.py b/ishtar_common/admin.py index ca9de5162..0e9e7a4bd 100644 --- a/ishtar_common/admin.py +++ b/ishtar_common/admin.py @@ -1365,7 +1365,9 @@ class UserProfileAdmin(admin.ModelAdmin): admin_site.register(models.UserProfile, UserProfileAdmin) -basic_models = [models.DocumentTemplate] +class DocumentTemplateAdmin(admin.ModelAdmin): + list_display = ["name", "associated_model", "available", "for_labels"] + list_filter = ["available", "associated_model"] -for model in basic_models: - admin_site.register(model) + +admin_site.register(models.DocumentTemplate, DocumentTemplateAdmin) diff --git a/ishtar_common/fixtures/initial_data-fr.json b/ishtar_common/fixtures/initial_data-fr.json index f1ebe4c71..7e6b8446c 100644 --- a/ishtar_common/fixtures/initial_data-fr.json +++ b/ishtar_common/fixtures/initial_data-fr.json @@ -1541,25 +1541,5 @@ "comment": "105 \u00d7 148 mm", "available": true } -}, -{ - "model": "ishtar_common.documenttemplate", - "fields": { - "name": "Document de r\u00e9f\u00e9rence", - "slug": "document-de-reference", - "template": "upload/templates/document_reference.odt", - "associated_object_name": "archaeological_operations.models.AdministrativeAct", - "available": true - } -}, -{ - "model": "ishtar_common.documenttemplate", - "fields": { - "name": "Accus\u00e9 de r\u00e9ception d'un dossier", - "slug": "accuse-de-reception-dun-dossier", - "template": "upload/templates/ISHTAR_FILE_ACT_AR_Neutre_1.odt", - "associated_object_name": "archaeological_operations.models.AdministrativeAct", - "available": true - } } ] diff --git a/ishtar_common/fixtures/initial_importtypes-fr.json b/ishtar_common/fixtures/initial_importtypes-fr.json index a2ca74b68..5ae394040 100644 --- a/ishtar_common/fixtures/initial_importtypes-fr.json +++ b/ishtar_common/fixtures/initial_importtypes-fr.json @@ -126,6 +126,33 @@ } }, { + "model": "ishtar_common.importermodel", + "fields": { + "name": "Acte administratif", + "klass": "archaeological_operations.models.AdministrativeAct" + } +}, +{ + "model": "ishtar_common.documenttemplate", + "fields": { + "name": "Document de r\u00e9f\u00e9rence", + "slug": "document-de-reference", + "template": "upload/templates/document_reference.odt", + "associated_model": ["archaeological_operations.models.AdministrativeAct"], + "available": true + } +}, +{ + "model": "ishtar_common.documenttemplate", + "fields": { + "name": "Accus\u00e9 de r\u00e9ception d'un dossier", + "slug": "accuse-de-reception-dun-dossier", + "template": "upload/templates/ISHTAR_FILE_ACT_AR_Neutre_1.odt", + "associated_model": ["archaeological_operations.models.AdministrativeAct"], + "available": true + } +}, +{ "model": "ishtar_common.importertype", "fields": { "name": "MCC - Op\u00e9rations", diff --git a/ishtar_common/forms.py b/ishtar_common/forms.py index 3a696911d..f992ace38 100644 --- a/ishtar_common/forms.py +++ b/ishtar_common/forms.py @@ -890,7 +890,7 @@ class DocumentGenerationForm(forms.Form): super(DocumentGenerationForm, self).__init__(*args, **kwargs) self.fields['document_template'].choices = \ models.DocumentTemplate.get_tuples( - dct={'associated_object_name': self._associated_object_name}) + dct={'associated_model__klass': self._associated_object_name}) def save(self, object_pk): try: diff --git a/ishtar_common/migrations/0093_auto_20190429_0950.py b/ishtar_common/migrations/0093_auto_20190429_0950.py new file mode 100644 index 000000000..d2bc6a395 --- /dev/null +++ b/ishtar_common/migrations/0093_auto_20190429_0950.py @@ -0,0 +1,50 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.11.18 on 2019-04-29 09:50 +from __future__ import unicode_literals + +from django.db import migrations, models +import django.db.models.deletion + + +CLASS_NAMES = { + 'archaeological_operations.models.AdministrativeAct': "Acte administratif" +} + + +def migrate_to_model_fk(apps, schema_editor): + ImporterModel = apps.get_model('ishtar_common', 'ImporterModel') + DocumentTemplate = apps.get_model('ishtar_common', 'DocumentTemplate') + importer_models = {} + for doc in DocumentTemplate.objects.all(): + if doc.associated_object_name not in importer_models: + if doc.associated_object_name in CLASS_NAMES: + name = CLASS_NAMES[doc.associated_object_name] + else: + name = "-" + importer_models[doc.associated_object_name], __ = \ + ImporterModel.objects.get_or_create( + klass=doc.associated_object_name, + defaults={"name": name} + ) + doc.associated_model = importer_models[doc.associated_object_name] + doc.save() + + +class Migration(migrations.Migration): + + dependencies = [ + ('ishtar_common', '0092_ishtarsiteprofile_display_srs'), + ] + + operations = [ + migrations.AlterModelOptions( + name='importermodel', + options={'ordering': ('name',), 'verbose_name': 'Model', 'verbose_name_plural': 'Models'}, + ), + migrations.AddField( + model_name='documenttemplate', + name='associated_model', + field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, to='ishtar_common.ImporterModel'), + ), + migrations.RunPython(migrate_to_model_fk), + ] diff --git a/ishtar_common/migrations/0094_auto_20190429_1041.py b/ishtar_common/migrations/0094_auto_20190429_1041.py new file mode 100644 index 000000000..6b068b2dc --- /dev/null +++ b/ishtar_common/migrations/0094_auto_20190429_1041.py @@ -0,0 +1,39 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.11.18 on 2019-04-29 10:41 +from __future__ import unicode_literals + +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + ('ishtar_common', '0093_auto_20190429_0950'), + ] + + operations = [ + migrations.AlterModelOptions( + name='documenttemplate', + options={'ordering': ['associated_model', 'name'], 'verbose_name': 'Patron de document', 'verbose_name_plural': 'Patrons de document'}, + ), + migrations.RemoveField( + model_name='documenttemplate', + name='associated_object_name', + ), + migrations.AddField( + model_name='documenttemplate', + name='for_labels', + field=models.BooleanField(default=False, verbose_name='Used for labels'), + ), + migrations.AddField( + model_name='documenttemplate', + name='label_per_page', + field=models.IntegerField(blank=True, help_text='Only relevant for label template', null=True, verbose_name='Number of label per page'), + ), + migrations.AlterField( + model_name='documenttemplate', + name='associated_model', + field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='ishtar_common.ImporterModel'), + ), + ] diff --git a/ishtar_common/models.py b/ishtar_common/models.py index 74151fa31..43267f6df 100644 --- a/ishtar_common/models.py +++ b/ishtar_common/models.py @@ -3037,12 +3037,12 @@ class Dashboard(object): avrg = self.get_average(vals) return self.get_average([(x - avrg) ** 2 for x in vals]) - def get_standard_deviation(self, vals=[]): + def get_standard_deviation(self, vals=None): if not vals: vals = self.numbers[:] return round(self.get_variance(vals) ** 0.5, 3) - def get_median(self, vals=[]): + def get_median(self, vals=None): if not vals: vals = self.numbers[:] len_vals = len(vals) @@ -3053,7 +3053,7 @@ class Dashboard(object): return (vals[int(len_vals / 2) - 1] + vals[int(len_vals / 2)]) / 2.0 - def get_mode(self, vals={}): + def get_mode(self, vals=None): if not vals: vals = dict(zip(self.periods, self.numbers[:])) mx = max(vals.values()) @@ -3063,22 +3063,24 @@ class Dashboard(object): class DocumentTemplate(models.Model): - CLASSNAMES = (('archaeological_operations.models.AdministrativeAct', - _("Administrative Act")),) name = models.CharField(_("Name"), max_length=100) slug = models.SlugField(_("Slug"), blank=True, null=True, max_length=100, unique=True) + associated_model = models.ForeignKey(ImporterModel) template = models.FileField( _("Template"), upload_to="templates/%Y/", help_text=max_size_help()) - associated_object_name = models.CharField( - _("Associated object"), max_length=100, choices=CLASSNAMES) available = models.BooleanField(_("Available"), default=True) + for_labels = models.BooleanField(_("Used for labels"), default=False) + label_per_page = models.IntegerField( + _("Number of label per page"), blank=True, null=True, + help_text=_("Only relevant for label template") + ) objects = SlugModelManager() class Meta: verbose_name = _("Document template") verbose_name_plural = _("Document templates") - ordering = ['associated_object_name', 'name'] + ordering = ['associated_model', 'name'] def __str__(self): return self.name @@ -3086,13 +3088,20 @@ class DocumentTemplate(models.Model): def natural_key(self): return (self.slug,) + def clean(self): + if self.for_labels and not self.label_per_page: + raise ValidationError(_("For label template, you must provide " + "number of label per page.")) + 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): + def get_tuples(cls, dct=None, empty_first=True): + if not dct: + dct = {} dct['available'] = True if empty_first: yield ('', '----------') diff --git a/ishtar_common/models_imports.py b/ishtar_common/models_imports.py index 85d91d255..e3f5f2492 100644 --- a/ishtar_common/models_imports.py +++ b/ishtar_common/models_imports.py @@ -97,8 +97,8 @@ class ImporterModel(models.Model): objects = ImportModelManager() class Meta: - verbose_name = _(u"Importer - Model") - verbose_name_plural = _(u"Importer - Models") + verbose_name = _(u"Model") + verbose_name_plural = _(u"Models") ordering = ('name',) def __str__(self): |