diff options
-rw-r--r-- | archaeological_operations/tests.py | 23 | ||||
-rw-r--r-- | ishtar_common/migrations/0083_document_index_external_id.py | 22 | ||||
-rw-r--r-- | ishtar_common/models.py | 15 |
3 files changed, 59 insertions, 1 deletions
diff --git a/archaeological_operations/tests.py b/archaeological_operations/tests.py index f8925f610..956afcaaa 100644 --- a/archaeological_operations/tests.py +++ b/archaeological_operations/tests.py @@ -43,7 +43,8 @@ from ishtar_common.models import OrganizationType, Organization, ItemKey, \ Town, ImporterColumn, Person, Author, SourceType, AuthorType, \ DocumentTemplate, PersonType, TargetKeyGroup, JsonDataField, \ JsonDataSection, ImportTarget, FormaterType, CustomForm, ExcludedField, \ - UserProfile, ProfileType, Area, CustomFormJsonField, get_current_profile + UserProfile, ProfileType, Area, CustomFormJsonField, get_current_profile, \ + Document from archaeological_files.models import File, FileType from archaeological_context_records.models import Unit, ContextRecord @@ -1306,6 +1307,26 @@ class OperationTest(TestCase, OperationInitTest): for key in ('32303', 'red', 'Red'): self.assertNotIn(key, operation.search_vector) + def test_document(self): + operation = self.operations[0] + q = Document.objects.values('index').order_by('-index') + if q.count(): + c_index = q.all()[0]['index'] + else: + c_index = 0 + doc = Document.objects.create(title="Image!") + operation.documents.add(doc) + operation.save() + + doc = Document.objects.get(pk=doc.pk) + self.assertEqual(c_index + 1, doc.index) + self.assertEqual(doc.external_id, unicode(doc.index)) + + doc2 = Document.objects.create(title="Image2!") + operation.documents.add(doc2) + doc2 = Document.objects.get(pk=doc2.pk) + self.assertEqual(c_index + 2, doc2.index) + class CustomFormTest(TestCase, OperationInitTest): fixtures = FILE_FIXTURES diff --git a/ishtar_common/migrations/0083_document_index_external_id.py b/ishtar_common/migrations/0083_document_index_external_id.py new file mode 100644 index 000000000..9d339b20e --- /dev/null +++ b/ishtar_common/migrations/0083_document_index_external_id.py @@ -0,0 +1,22 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.11.10 on 2019-01-18 17:51 +from __future__ import unicode_literals + +from django.db import migrations + + +def gen_index(apps, schema_editor): + from ishtar_common.models import Document + for doc in Document.objects.all(): + doc.save() + + +class Migration(migrations.Migration): + + dependencies = [ + ('ishtar_common', '0082_auto_20190118_1203'), + ] + + operations = [ + migrations.RunPython(gen_index) + ] diff --git a/ishtar_common/models.py b/ishtar_common/models.py index 4078b5840..769013fa2 100644 --- a/ishtar_common/models.py +++ b/ishtar_common/models.py @@ -3762,6 +3762,7 @@ post_delete.connect(post_save_cache, sender=LicenseType) class Document(BaseHistorizedItem, OwnPerms, ImageModel): + EXTERNAL_ID_KEY = 'document_external_id' # order is important: put the image in the first match found # other will be symbolic links RELATED_MODELS = [ @@ -4146,9 +4147,23 @@ class Document(BaseHistorizedItem, OwnPerms, ImageModel): def _generate_cache_related_label(self): return self.related_label() + def set_index(self): + if self.index: + return + q = Document.objects.values('index').filter( + index__isnull=False).order_by("-index") + if not q.count(): + self.index = 1 + return + cid = q.all()[0]['index'] + if not cid: + cid = 0 + self.index = cid + 1 + def save(self, *args, **kwargs): no_path_change = 'no_path_change' in kwargs \ and kwargs.pop('no_path_change') + self.set_index() super(Document, self).save(*args, **kwargs) if self.image and not no_path_change and \ |