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.
4 from django.db import migrations, models
6 from sortify import sortify
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())
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)
24 obj.sort_key_author = author.sort_key
28 class Migration(migrations.Migration):
31 ('catalogue', '0008_auto_20151221_1225'),
32 ('picture', '0007_auto_20160125_1709'),
36 migrations.RunPython(update_sort_keys),