remove even more query chars
[wolnelektury.git] / src / picture / migrations / 0005_auto_20141022_1001.py
1 # -*- coding: utf-8 -*-
2 from __future__ import unicode_literals
3
4 from django.core.files.base import ContentFile
5 from django.db import models, migrations
6 from django.template.loader import render_to_string
7
8
9 def rebuild_extra_info(apps, schema_editor):
10     Picture = apps.get_model("picture", "Picture")
11     from librarian.picture import PictureInfo
12     from librarian import dcparser
13     for pic in Picture.objects.all():
14         info = dcparser.parse(pic.xml_file.path, PictureInfo)
15         pic.extra_info = info.to_dict()
16         areas_json = pic.areas_json
17         for field in areas_json[u'things'].values():
18             field[u'object'] = field[u'object'].capitalize()
19         pic.areas_json = areas_json
20         html_text = unicode(render_to_string('picture/picture_info.html', {
21                     'things': pic.areas_json['things'],
22                     'themes': pic.areas_json['themes'],
23                     }))
24         pic.html_file.save("%s.html" % pic.slug, ContentFile(html_text))
25         pic.save()
26
27
28 class Migration(migrations.Migration):
29
30     dependencies = [
31         ('picture', '0004_auto_20141016_1337'),
32     ]
33
34     operations = [
35         migrations.RunPython(rebuild_extra_info),
36     ]