diff options
author | Étienne Loks <etienne.loks@iggdrasil.net> | 2025-06-18 16:51:11 +0200 |
---|---|---|
committer | Étienne Loks <etienne.loks@iggdrasil.net> | 2025-06-18 16:51:11 +0200 |
commit | 9b3d54257b5024393284f2b1002acdb09615a1f1 (patch) | |
tree | 68d1e5941c5874e8bbd4c1aff0430fcbf542411d | |
parent | ee67a4dd81390da2841d6e7c7bd81a39d27e099a (diff) | |
download | Ishtar-9b3d54257b5024393284f2b1002acdb09615a1f1.tar.bz2 Ishtar-9b3d54257b5024393284f2b1002acdb09615a1f1.zip |
✨ Media exporter: naming template
-rw-r--r-- | archaeological_operations/tests.py | 18 | ||||
-rw-r--r-- | ishtar_common/models_imports.py | 14 | ||||
-rw-r--r-- | ishtar_common/tests.py | 1 |
3 files changed, 27 insertions, 6 deletions
diff --git a/archaeological_operations/tests.py b/archaeological_operations/tests.py index 385337213..068ec3057 100644 --- a/archaeological_operations/tests.py +++ b/archaeological_operations/tests.py @@ -4232,6 +4232,24 @@ class OperationExportMediaTest(TestCase, TestPermissionQuery, OperationInitTest) exclude=[('operation_image_00002.png', True)] ) + def test_naming(self): + self.exporter.naming = "DOC-{{item_type}}-{{item.cached_label|slug}}" + self.exporter.cascade = True + self.exporter.save() + cr, find = self._add_find() + cr.documents.add(self.documents[0]) + find.documents.add(self.documents[0]) + + c = Client() + c.login(username=self.username, password=self.password) + response = c.get(self.export_url) + self._test_files( + response, + [(f'DOC-operation-{slugify(self.operation.cached_label)}.png', True), + (f'DOC-contextrecord-{slugify(cr.cached_label)}.png', True), + (f'DOC-find-{slugify(find.cached_label)}.png', True)] + ) + class LabelTest(TestCase, OperationInitTest): fixtures = FILE_FIXTURES diff --git a/ishtar_common/models_imports.py b/ishtar_common/models_imports.py index 23fb4f38e..e09ca2502 100644 --- a/ishtar_common/models_imports.py +++ b/ishtar_common/models_imports.py @@ -81,7 +81,8 @@ from ishtar_common.utils import ( reverse_coordinates, update_data, OwnPerms, - SheetItem + SheetItem, + jinja_evaluation ) from ishtar_common.data_importer import ( Importer, @@ -3060,8 +3061,9 @@ class MediaExporter(models.Model): naming = models.TextField( _("Naming"), blank=True, null=False, default="", help_text=_( - "Can use jinja template logic to have conditionnal naming. If left empty, " - "each media will be named with incremented numbers." + 'Jinja template. 3 variables are available: "document" for the document, ' + '"item" for associated item and "item_type" for the item type (example: ' + '"find"). If left empty, each media will be named with incremented numbers.' ) ) @@ -3167,8 +3169,10 @@ class MediaExporter(models.Model): base_name = media.path.split(os.path.sep)[-1] ext = base_name.split(".")[-1] if self.naming: - # TODO: naming - name = base_name + name = jinja_evaluation( + self.naming, + {"document": document, "item": item, "item_type": item_type} + ) + f".{ext}" else: if media_attr == "associated_file": name = f"{item_type}_file" diff --git a/ishtar_common/tests.py b/ishtar_common/tests.py index 8fd5ac9e1..977c1bc26 100644 --- a/ishtar_common/tests.py +++ b/ishtar_common/tests.py @@ -84,7 +84,6 @@ from archaeological_finds.serializers import FIND_MODEL_LIST from archaeological_warehouse.serializers import WAREHOUSE_MODEL_LIST from ishtar_common.serializers_utils import serialization_info from ishtar_common.utils import ( - post_save_geo, update_data, move_dict_data, rename_and_simplify_media_name, |