Fundraising in PDF.
[wolnelektury.git] / src / catalogue / migrations / 0009_auto_20160127_1019.py
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.
3 #
4 from django.db import migrations, models
5
6 from sortify import sortify
7
8
9 def update_sort_keys(apps, schema_editor):
10     Tag = apps.get_model('catalogue', 'Tag')
11     Book = apps.get_model('catalogue', 'Book')
12     Picture = apps.get_model('picture', 'Picture')
13     ContentType = apps.get_model('contenttypes', 'ContentType')
14     for author in Tag.objects.filter(category='author'):
15         name_parts = author.name.split()
16         sort_key = ' '.join([name_parts[-1]] + name_parts[:-1])
17         author.sort_key = sortify(sort_key.lower())
18         author.save()
19     for model in Book, Picture:
20         ct = ContentType.objects.get_for_model(model)
21         for obj in model.objects.all():
22             authors = Tag.objects.filter(category='author', items__content_type=ct, items__object_id=obj.id)
23             author = authors[0]
24             obj.sort_key_author = author.sort_key
25             obj.save()
26
27
28 class Migration(migrations.Migration):
29
30     dependencies = [
31         ('catalogue', '0008_auto_20151221_1225'),
32         ('picture', '0007_auto_20160125_1709'),
33     ]
34
35     operations = [
36         migrations.RunPython(update_sort_keys),
37     ]