Upgrades.
[wolnelektury.git] / src / picture / migrations / 0005_auto_20141022_1001.py
1 # This file is part of Wolnelektury, licensed under GNU Affero GPLv3 or later.
2 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
3 #
4 import json
5 from django.core.files.base import ContentFile
6 from django.db import models, migrations
7 from django.template.loader import render_to_string
8
9
10 def rebuild_extra_info(apps, schema_editor):
11     Picture = apps.get_model("picture", "Picture")
12     from librarian.picture import PictureInfo
13     from librarian import dcparser
14     for pic in Picture.objects.all():
15         info = dcparser.parse(pic.xml_file.path, PictureInfo)
16         pic.extra_info = json.dumps(info.to_dict())
17         areas_json = json.loads(pic.areas_json)
18         for field in areas_json['things'].values():
19             field['object'] = field['object'].capitalize()
20         pic.areas_json = json.dumps(areas_json)
21         html_text = render_to_string('picture/picture_info.html', {
22                     'things': areas_json['things'],
23                     'themes': areas_json['themes'],
24                     })
25         pic.html_file.save("%s.html" % pic.slug, ContentFile(html_text))
26         pic.save()
27
28
29 class Migration(migrations.Migration):
30
31     dependencies = [
32         ('picture', '0004_auto_20141016_1337'),
33     ]
34
35     operations = [
36         migrations.RunPython(rebuild_extra_info),
37     ]