Phone not required.
[wolnelektury.git] / src / catalogue / migrations / 0016_auto_20171031_1232.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
7 def refresh_books(apps, schema_editor):
8     Book = apps.get_model('catalogue', 'Book')
9     TagRelation = apps.get_model('catalogue', 'TagRelation')
10     db_alias = schema_editor.connection.alias
11     for book in Book.objects.using(db_alias).all():
12         book.cached_author = ', '.join(
13             TagRelation.objects.filter(content_type__model='book', object_id=book.id, tag__category='author')
14             .values_list('tag__name', flat=True))
15         book.has_audience = 'audience' in book.get_extra_info_json()
16         book.save()
17
18
19 class Migration(migrations.Migration):
20
21     dependencies = [
22         ('catalogue', '0015_book_recommended'),
23     ]
24
25     operations = [
26         migrations.AddField(
27             model_name='book',
28             name='cached_author',
29             field=models.CharField(db_index=True, max_length=240, blank=True),
30         ),
31         migrations.AddField(
32             model_name='book',
33             name='has_audience',
34             field=models.BooleanField(default=False),
35         ),
36         migrations.RunPython(refresh_books, migrations.RunPython.noop),
37     ]