1 # This file is part of Wolne Lektury, licensed under GNU Affero GPLv3 or later.
 
   2 # Copyright © Fundacja Wolne Lektury. See NOTICE for more information.
 
   5 from django.core.files.base import ContentFile
 
   6 from django.db import models, migrations
 
   7 from django.template.loader import render_to_string
 
  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'],
 
  25         pic.html_file.save("%s.html" % pic.slug, ContentFile(html_text))
 
  29 class Migration(migrations.Migration):
 
  32         ('picture', '0004_auto_20141016_1337'),
 
  36         migrations.RunPython(rebuild_extra_info),