diff options
author | Étienne Loks <etienne.loks@iggdrasil.net> | 2018-05-11 12:33:54 +0200 |
---|---|---|
committer | Étienne Loks <etienne.loks@iggdrasil.net> | 2018-06-12 08:44:18 +0200 |
commit | 83d4e47b7e0ee3946e07d1c5de04597a06613cf0 (patch) | |
tree | 67981f597f7b7a98b782d2db02687d83f1024772 /ishtar_common | |
parent | 3d12b0ac2e97ab43a555106f404ed7cf442caf17 (diff) | |
download | Ishtar-83d4e47b7e0ee3946e07d1c5de04597a06613cf0.tar.bz2 Ishtar-83d4e47b7e0ee3946e07d1c5de04597a06613cf0.zip |
Migrate single image to M2M (refs #4076)
Diffstat (limited to 'ishtar_common')
-rw-r--r-- | ishtar_common/models.py | 3 | ||||
-rw-r--r-- | ishtar_common/utils.py | 17 | ||||
-rw-r--r-- | ishtar_common/utils_migrations.py | 48 |
3 files changed, 61 insertions, 7 deletions
diff --git a/ishtar_common/models.py b/ishtar_common/models.py index 0e8d96ddb..53f4e1c11 100644 --- a/ishtar_common/models.py +++ b/ishtar_common/models.py @@ -865,6 +865,9 @@ class ItemKey(models.Model): def get_image_path(instance, filename): + # when using migrations instance is not a real ImageModel instance + if not hasattr(instance, '_get_image_path'): + return u"upload/{}".format(filename) return instance._get_image_path(filename) diff --git a/ishtar_common/utils.py b/ishtar_common/utils.py index 52ea98547..2af5212ca 100644 --- a/ishtar_common/utils.py +++ b/ishtar_common/utils.py @@ -424,7 +424,7 @@ def get_field_labels_from_path(model, path): return labels -def create_default_areas(models=None): +def create_default_areas(models=None, verbose=False): # can be used on migrations if models are provided if not models: from ishtar_common.models import Area, Town, Department, State @@ -444,7 +444,8 @@ def create_default_areas(models=None): areas['state-{}'.format(state.pk)] = area if created: idx += 1 - print("\n* {} state areas added".format(idx)) + if verbose: + print("\n* {} state areas added".format(idx)) idx, idx2 = 0, 0 for dep in Department.objects.all(): @@ -464,10 +465,11 @@ def create_default_areas(models=None): idx2 += 1 area.parent = areas[state_slug] area.save() - print( - "* {} department areas added with {} associations to state".format( - idx, idx2) - ) + if verbose: + print( + "* {} department areas added with {} associations to state".format( + idx, idx2) + ) idx = 0 for town in Town.objects.all(): @@ -484,7 +486,8 @@ def create_default_areas(models=None): areas[code_dep_dom].towns.add(town) idx += 1 - print("* {} town associated to department area".format(idx)) + if verbose: + print("* {} town associated to department area".format(idx)) def get_relations_for_graph(rel_model, obj_pk, above_relations=None, diff --git a/ishtar_common/utils_migrations.py b/ishtar_common/utils_migrations.py new file mode 100644 index 000000000..9f514ea48 --- /dev/null +++ b/ishtar_common/utils_migrations.py @@ -0,0 +1,48 @@ +# -*- coding: utf-8 -*- +from __future__ import unicode_literals + +from django.core.files import File +import os + + +def migrate_simple_image_to_m2m(base_model, image_model, rel_model, + verbose=False): + missing, moved = 0, 0 + for item in base_model.objects.all(): + if not item.image: + continue + + image_instance = image_model.objects.create() + + try: + image_instance.image.save( + os.path.basename(item.image.path), + File(open(item.image.path)) + ) + image_instance.thumbnail.save( + os.path.basename(item.thumbnail.path), + File(open(item.thumbnail.path)) + ) + except IOError: + # image not on hard-drive + item.image = None + item.thumbnail = None + item.save() + image_instance.delete() + missing += 1 + continue + + image_instance.save() + + rel_model.objects.create(item=item, image=image_instance, is_main=True) + + # clean + item.image = None + item.thumbnail = None + item.save() + moved += 1 + if verbose: + print("") + print(base_model) + print("{} missing".format(missing)) + print("{} moved".format(moved)) |