summaryrefslogtreecommitdiff
path: root/ishtar_common/utils_migrations.py
blob: d161c1f2968c6c0b17578323651f3106476f1721 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# -*- 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))


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():
        image = IshtarImage.objects.get(pk=image_rel.image.pk)
        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')
    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']:
            setattr(doc, attr, getattr(source, attr))
        doc.save()
        for author in source.authors.all():
            doc.authors.add(author)
        item = base_model.objects.get(pk=getattr(source, item_attr).pk)
        item.documents.add(doc)


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)
        except:
            continue
        for item in model.objects.all():
            q = historical_model.objects.filter(
                id=item.pk).order_by('-history_date')
            if not q.count():
                return
            edit_date = q.all()[0].history_date
            if not edit_date:
                return
            item.last_modified = edit_date
            item.skip_history_when_saving = True
            item.save()


def migrate_main_image(apps, app_name, model_name):
    model = apps.get_model(app_name, model_name)
    for item in model.objects.filter(
            documents__image__isnull=False).exclude(
            main_image__isnull=False).all():
        q = item.documents.filter(
            image__isnull=False).exclude(image='')
        if not q.count():
            return
        # by default get the lowest pk
        item.main_image = q.order_by('pk').all()[0]
        item.skip_history_when_saving = True
        item.save()