From 3039fae5124c00a67283c9b707e4a411149d93b1 Mon Sep 17 00:00:00 2001 From: Étienne Loks Date: Fri, 19 Mar 2021 11:05:22 +0100 Subject: Format - black: ishtar_common --- ishtar_common/utils_migrations.py | 86 +++++++++++++++++++++++---------------- 1 file changed, 52 insertions(+), 34 deletions(-) (limited to 'ishtar_common/utils_migrations.py') diff --git a/ishtar_common/utils_migrations.py b/ishtar_common/utils_migrations.py index 57d75077e..13ebf245e 100644 --- a/ishtar_common/utils_migrations.py +++ b/ishtar_common/utils_migrations.py @@ -11,8 +11,7 @@ from django.core.files import File from django.db import connection -def migrate_simple_image_to_m2m(base_model, image_model, rel_model, - verbose=False): +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: @@ -22,12 +21,10 @@ def migrate_simple_image_to_m2m(base_model, image_model, rel_model, try: image_instance.image.save( - os.path.basename(item.image.path), - File(open(item.image.path)) + 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)) + os.path.basename(item.thumbnail.path), File(open(item.thumbnail.path)) ) except IOError: # image not on hard-drive @@ -55,26 +52,39 @@ def migrate_simple_image_to_m2m(base_model, image_model, rel_model, def migrate_images(apps, base_model, rel_model): - IshtarImage = apps.get_model('ishtar_common', 'IshtarImage') - Document = apps.get_model('ishtar_common', 'Document') - for image_rel in rel_model.objects.order_by('is_main').all(): + IshtarImage = apps.get_model("ishtar_common", "IshtarImage") + Document = apps.get_model("ishtar_common", "Document") + for image_rel in rel_model.objects.order_by("is_main").all(): image = IshtarImage.objects.get(pk=image_rel.image.pk) - doc = Document.objects.create(image=image.image, - thumbnail=image.thumbnail) + doc = Document.objects.create(image=image.image, thumbnail=image.thumbnail) item = base_model.objects.get(pk=image_rel.item.pk) item.documents.add(doc) def migrate_sources(apps, base_model, source_model, item_attr): - Document = apps.get_model('ishtar_common', 'Document') + Document = apps.get_model("ishtar_common", "Document") for source in source_model.objects.all(): doc = Document.objects.create() - for attr in ['title', 'index', 'external_id', 'reference', - 'internal_reference', 'source_type', 'support_type', - 'format_type', 'scale', 'associated_url', 'receipt_date', - 'creation_date', 'receipt_date_in_documentation', - 'item_number', 'description', 'comment', - 'additional_information', 'duplicate']: + for attr in [ + "title", + "index", + "external_id", + "reference", + "internal_reference", + "source_type", + "support_type", + "format_type", + "scale", + "associated_url", + "receipt_date", + "creation_date", + "receipt_date_in_documentation", + "item_number", + "description", + "comment", + "additional_information", + "duplicate", + ]: setattr(doc, attr, getattr(source, attr)) doc.save() for author in source.authors.all(): @@ -87,13 +97,11 @@ def reinit_last_modified(apps, app_name, models): for model_name in models: model = apps.get_model(app_name, model_name) try: - historical_model = apps.get_model( - app_name, 'Historical' + model_name) + historical_model = apps.get_model(app_name, "Historical" + model_name) except: continue for item in model.objects.all(): - q = historical_model.objects.filter( - id=item.pk).order_by('-history_date') + q = historical_model.objects.filter(id=item.pk).order_by("-history_date") if not q.count(): return edit_date = q.all()[0].history_date @@ -107,21 +115,22 @@ def reinit_last_modified(apps, app_name, models): def migrate_main_image(apps, app_name, model_name, verbose=False): model = apps.get_model(app_name, model_name) q = model.objects.filter(documents__image__isnull=False).exclude( - main_image__isnull=False) + main_image__isnull=False + ) ln = q.count() for idx, item in enumerate(q.all()): if verbose: if not idx: sys.stdout.write("\n") - sys.stdout.write(" * {}.{}: {}/{}\r".format(app_name, model_name, - idx + 1, ln)) + sys.stdout.write( + " * {}.{}: {}/{}\r".format(app_name, model_name, idx + 1, ln) + ) sys.stdout.flush() - q = item.documents.filter( - image__isnull=False).exclude(image='') + q = item.documents.filter(image__isnull=False).exclude(image="") if not q.count(): # no image continue # by default get the lowest pk - item.main_image = q.order_by('pk').all()[0] + item.main_image = q.order_by("pk").all()[0] item.skip_history_when_saving = True item._no_move = True item.save() @@ -141,9 +150,15 @@ def m2m_historization_init(obj): for hist in obj.history.all(): hist.history_m2m = hist_values d = hist.history_date - date = datetime.datetime(year=d.year, month=d.month, day=d.day, - hour=d.hour, minute=d.minute, second=d.second, - microsecond=d.microsecond) + date = datetime.datetime( + year=d.year, + month=d.month, + day=d.day, + hour=d.hour, + minute=d.minute, + second=d.second, + microsecond=d.microsecond, + ) hist.history_date = date hist.last_modified = date hist.save() @@ -151,9 +166,11 @@ def m2m_historization_init(obj): # not clean... but json fields seems to be not well managed by # cursor.execute cursor.execute( - "UPDATE \"" + obj.__class__._meta.db_table + "\" SET " - "history_m2m = '" + json.dumps(hist_values).replace("'", "''") + - "'::json WHERE id = %s", [obj.pk] + 'UPDATE "' + obj.__class__._meta.db_table + '" SET ' + "history_m2m = '" + + json.dumps(hist_values).replace("'", "''") + + "'::json WHERE id = %s", + [obj.pk], ) @@ -168,4 +185,5 @@ def set_uuid_helper(module, model_name): def set_uuid(apps, schema_editor): model = apps.get_model(module, model_name) migrate_uuid(model) + return set_uuid -- cgit v1.2.3