diff options
author | Étienne Loks <etienne.loks@iggdrasil.net> | 2018-08-16 12:38:41 +0200 |
---|---|---|
committer | Étienne Loks <etienne.loks@iggdrasil.net> | 2018-08-16 12:38:41 +0200 |
commit | fe78f0db71ac7a9528925492c8521496dc6c36ed (patch) | |
tree | fa0614969e604e65396fabf9a48cbd7c97a410e8 | |
parent | 5d0750488690ec698857aa40e4e16761d3aae613 (diff) | |
download | Ishtar-fe78f0db71ac7a9528925492c8521496dc6c36ed.tar.bz2 Ishtar-fe78f0db71ac7a9528925492c8521496dc6c36ed.zip |
Manage ope prefixes with the profile
-rw-r--r-- | archaeological_context_records/models.py | 8 | ||||
-rw-r--r-- | archaeological_context_records/tests.py | 6 | ||||
-rw-r--r-- | archaeological_finds/models_finds.py | 18 | ||||
-rw-r--r-- | archaeological_operations/models.py | 17 | ||||
-rw-r--r-- | archaeological_operations/tests.py | 11 | ||||
-rw-r--r-- | ishtar_common/migrations/0066_auto_20180816_1114.py | 46 | ||||
-rw-r--r-- | ishtar_common/models.py | 13 |
7 files changed, 96 insertions, 23 deletions
diff --git a/archaeological_context_records/models.py b/archaeological_context_records/models.py index 3cb0313dd..e3c7ff4e5 100644 --- a/archaeological_context_records/models.py +++ b/archaeological_context_records/models.py @@ -35,7 +35,7 @@ from ishtar_common.models import Document, GeneralType, \ BaseHistorizedItem, HistoricalRecords, OwnPerms, ShortMenuItem, \ GeneralRelationType, GeneralRecordRelations, post_delete_record_relation,\ post_save_cache, ValueGetter, BulkUpdatedItem, \ - RelationItem, Town + RelationItem, Town, get_current_profile from archaeological_operations.models import Operation, Period, Parcel, \ ArchaeologicalSite @@ -436,6 +436,8 @@ class ContextRecord(BulkUpdatedItem, BaseHistorizedItem, return kwargs['transaction_id'] = transaction_id + profile = get_current_profile() + sql = """ UPDATE "archaeological_context_records_contextrecord" AS cr SET cached_label = @@ -469,8 +471,8 @@ class ContextRecord(BulkUpdatedItem, BaseHistorizedItem, SELECT id FROM archaeological_context_records_contextrecord WHERE {where} ); - """.format(main_ope_prefix=settings.ISHTAR_OPE_PREFIX, - ope_prefix=settings.ISHTAR_DEF_OPE_PREFIX, + """.format(main_ope_prefix=profile.operation_prefix, + ope_prefix=profile.default_operation_prefix, join=settings.JOINT, where=where) with connection.cursor() as c: c.execute(sql, args) diff --git a/archaeological_context_records/tests.py b/archaeological_context_records/tests.py index bf8864d56..2f676c194 100644 --- a/archaeological_context_records/tests.py +++ b/archaeological_context_records/tests.py @@ -28,7 +28,7 @@ from django.core.urlresolvers import reverse from django.test.client import Client from ishtar_common.models import IshtarSiteProfile, ImporterModel, \ - UserProfile, ProfileType, Town, Area + UserProfile, ProfileType, Town, Area, get_current_profile from archaeological_operations.tests import OperationInitTest, \ ImportTest, FILE_TOWNS_FIXTURES, FILE_FIXTURES, OPERATION_TOWNS_FIXTURES @@ -293,7 +293,9 @@ class ContextRecordTest(ContextRecordInit, TestCase): cr.operation.code_patriarche = "PATRIARCHE" cr.operation.save() cr = models.ContextRecord.objects.get(pk=cr.pk) - self.assertIn(settings.ISHTAR_OPE_PREFIX.lower() + "patriarch", + + profile = get_current_profile() + self.assertIn(profile.operation_prefix.lower() + "patriarch", cr.search_vector) def test_upstream_cache_update(self): diff --git a/archaeological_finds/models_finds.py b/archaeological_finds/models_finds.py index 0fb8229e4..40bcf52c7 100644 --- a/archaeological_finds/models_finds.py +++ b/archaeological_finds/models_finds.py @@ -349,14 +349,15 @@ class BaseFind(BulkUpdatedItem, BaseHistorizedItem, OwnPerms): def _ope_code(self): if not self.context_record.operation: return '' + profile = get_current_profile() ope = self.context_record.operation c_id = [] if ope.code_patriarche: - c_id.append(settings.ISHTAR_OPE_PREFIX + + c_id.append(profile.operation_prefix + ope.code_patriarche) elif ope.year and ope.operation_code: c_id.append( - settings.ISHTAR_DEF_OPE_PREFIX + + profile.default_operation_prefix + unicode(ope.year or '') + u"-" + unicode(ope.operation_code or '')) else: @@ -463,6 +464,8 @@ class BaseFind(BulkUpdatedItem, BaseHistorizedItem, OwnPerms): return kwargs['transaction_id'] = transaction_id + profile = get_current_profile() + sql = """ UPDATE "archaeological_finds_basefind" AS bf SET cache_short_id = @@ -535,8 +538,8 @@ class BaseFind(BulkUpdatedItem, BaseHistorizedItem, OwnPerms): SELECT mybf.id FROM archaeological_finds_basefind mybf {filters} ); - """.format(main_ope_prefix=settings.ISHTAR_OPE_PREFIX, - ope_prefix=settings.ISHTAR_DEF_OPE_PREFIX, + """.format(main_ope_prefix=profile.operation_prefix, + ope_prefix=profile.default_operation_prefix, join=settings.JOINT, filters=filters, zeros=settings.ISHTAR_FINDS_INDEX_ZERO_LEN * "0") with connection.cursor() as c: @@ -1312,6 +1315,9 @@ class Find(BulkUpdatedItem, ValueGetter, BaseHistorizedItem, OwnPerms, args = [int(find_id)] else: return + + profile = get_current_profile() + sql = """ UPDATE "archaeological_finds_find" AS f SET cached_label = @@ -1346,8 +1352,8 @@ class Find(BulkUpdatedItem, ValueGetter, BaseHistorizedItem, OwnPerms, SELECT myf.id FROM archaeological_finds_find myf {filters} ); - """.format(main_ope_prefix=settings.ISHTAR_OPE_PREFIX, - ope_prefix=settings.ISHTAR_DEF_OPE_PREFIX, + """.format(main_ope_prefix=profile.operation_prefix, + ope_prefix=profile.default_operation_prefix, join=settings.JOINT, filters=filters, zeros=settings.ISHTAR_FINDS_INDEX_ZERO_LEN * "0") with connection.cursor() as c: diff --git a/archaeological_operations/models.py b/archaeological_operations/models.py index 828df91e5..9d6264a4d 100644 --- a/archaeological_operations/models.py +++ b/archaeological_operations/models.py @@ -36,7 +36,7 @@ from ishtar_common.models import BaseHistorizedItem, Dashboard, \ HistoricalRecords, IshtarUser, LightHistorizedItem, \ OperationType, Organization, OwnPerms, Person, PersonType, \ post_delete_record_relation, post_save_cache, RelationItem, \ - ShortMenuItem, SourceType, Town, ValueGetter + ShortMenuItem, SourceType, Town, ValueGetter, get_current_profile from ishtar_common.utils import cached_label_changed, \ force_cached_label_changed, mode @@ -831,15 +831,16 @@ class Operation(ClosedItem, BaseHistorizedItem, OwnPerms, ValueGetter, return BaseFind.objects.filter(context_record__operation=self) def get_reference(self, full=False): + profile = get_current_profile() ref = "" - if settings.COUNTRY == 'fr' and self.code_patriarche: - ref = settings.ISHTAR_OPE_PREFIX + unicode(self.code_patriarche) + if self.code_patriarche: + ref = profile.operation_prefix + unicode(self.code_patriarche) if not full: return ref if self.year and self.operation_code: if ref: ref += u" - " - ref += settings.ISHTAR_DEF_OPE_PREFIX + ref += profile.default_operation_prefix ref += u"-".join((unicode(self.year), unicode(self.operation_code))) return ref or "00" @@ -931,15 +932,17 @@ class Operation(ClosedItem, BaseHistorizedItem, OwnPerms, ValueGetter, return "" lbl = unicode(self.operation_code) year = self.year or 0 - lbl = settings.ISHTAR_DEF_OPE_PREFIX \ - + u"%d-%s%s" % (year, (3 - len(lbl)) * "0", lbl) + profile = get_current_profile() + lbl = profile.default_operation_prefix \ + + u"%d-%s%s" % (year, (3 - len(lbl)) * "0", lbl) return lbl @property def full_code_patriarche(self): if not self.code_patriarche: return '' - return settings.ISHTAR_OPE_PREFIX + self.code_patriarche + profile = get_current_profile() + return profile.operation_prefix + self.code_patriarche def clean(self): if not self.operation_code: diff --git a/archaeological_operations/tests.py b/archaeological_operations/tests.py index f50e58eaa..a3cd5fd8d 100644 --- a/archaeological_operations/tests.py +++ b/archaeological_operations/tests.py @@ -43,7 +43,7 @@ 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 + UserProfile, ProfileType, Area, CustomFormJsonField, get_current_profile from archaeological_files.models import File, FileType from archaeological_context_records.models import Unit, ContextRecord @@ -1103,8 +1103,10 @@ class OperationTest(TestCase, OperationInitTest): # verify the relevance of the update cr = ContextRecord.objects.get(pk=cr.pk) ope_id, parcel_sec, parcel_nb, cr_label = cr.cached_label.split(' | ') - self.assertEqual(ope_id, '{}2011-1'.format( - settings.ISHTAR_DEF_OPE_PREFIX)) + profile = get_current_profile() + + self.assertEqual( + ope_id, '{}2011-1'.format(profile.default_operation_prefix)) base_find = BaseFind.objects.get(pk=base_find.pk) op_code, idx = base_find.cache_short_id.split(' | ') @@ -1126,7 +1128,8 @@ class OperationTest(TestCase, OperationInitTest): operation.save() cr = ContextRecord.objects.get(pk=cr.pk) ope_id, parcel_sec, parcel_nb, cr_label = cr.cached_label.split(' | ') - self.assertEqual(ope_id, '{}666'.format(settings.ISHTAR_OPE_PREFIX)) + + self.assertEqual(ope_id, '{}666'.format(profile.operation_prefix)) base_find = BaseFind.objects.get(pk=base_find.pk) op_code, idx = base_find.cache_short_id.split(' | ') diff --git a/ishtar_common/migrations/0066_auto_20180816_1114.py b/ishtar_common/migrations/0066_auto_20180816_1114.py new file mode 100644 index 000000000..3d2d8ca23 --- /dev/null +++ b/ishtar_common/migrations/0066_auto_20180816_1114.py @@ -0,0 +1,46 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.11.10 on 2018-08-16 11:14 +from __future__ import unicode_literals + +from django.db import migrations, models +from django.conf import settings + + +def migrate_codes(apps, schema_editor): + IshtarSiteProfile = apps.get_model('ishtar_common', 'IshtarSiteProfile') + for p in IshtarSiteProfile.objects.all(): + if settings.ISHTAR_OPE_PREFIX: + p.operation_prefix = settings.ISHTAR_OPE_PREFIX + if settings.ISHTAR_DEF_OPE_PREFIX: + p.default_operation_prefix = settings.ISHTAR_DEF_OPE_PREFIX + p.save() + + +class Migration(migrations.Migration): + + dependencies = [ + ('ishtar_common', '0065_author_cached_label'), + ] + + operations = [ + migrations.AlterModelOptions( + name='document', + options={'ordering': ('title',), 'permissions': (('view_document', 'Peut voir tous les Documents'), ('view_own_document', 'Peut voir ses propres Documents'), ('add_own_document', 'Peut ajouter son propre Document'), ('change_own_document', 'Peut modifier ses propres documents'), ('delete_own_document', 'Peut supprimer ses propres Documents')), 'verbose_name': 'Document', 'verbose_name_plural': 'Documents'}, + ), + migrations.AddField( + model_name='ishtarsiteprofile', + name='default_operation_prefix', + field=models.CharField(blank=True, default='OP', max_length=20, null=True, verbose_name='Default operation code prefix'), + ), + migrations.AddField( + model_name='ishtarsiteprofile', + name='operation_prefix', + field=models.CharField(blank=True, default='OA', max_length=20, null=True, verbose_name='Main operation code prefix'), + ), + migrations.AddField( + model_name='ishtarsiteprofile', + name='operation_region_code', + field=models.CharField(blank=True, max_length=5, null=True, verbose_name='Operation region code'), + ), + migrations.RunPython(migrate_codes) + ] diff --git a/ishtar_common/models.py b/ishtar_common/models.py index 8bab21360..bd800181f 100644 --- a/ishtar_common/models.py +++ b/ishtar_common/models.py @@ -1210,7 +1210,6 @@ class FullSearch(models.Model): [data]) row = cursor.fetchone() search_vectors.append(row[0]) - # self.search_vector = merge_tsvectors(search_vectors) new_search_vector = merge_tsvectors(search_vectors) changed = old_search != new_search_vector if save and changed: @@ -1720,6 +1719,18 @@ class IshtarSiteProfile(models.Model, Cached): help_text=_(u"Homepage of Ishtar - if not defined a default homepage " u"will appear. Use the markdown syntax. {random_image} " u"can be used to display a random image.")) + operation_prefix = models.CharField( + _(u"Main operation code prefix"), default=u'OA', null=True, blank=True, + max_length=20 + ) + default_operation_prefix = models.CharField( + _(u"Default operation code prefix"), default=u'OP', null=True, + blank=True, max_length=20 + ) + operation_region_code = models.CharField( + _(u"Operation region code"), null=True, blank=True, + max_length=5 + ) file_external_id = models.TextField( _(u"File external id"), default=u"{year}-{numeric_reference}", |