f606c13d9ede4f0d1089412803a1846b01a4ea0f
[wolnelektury.git] / src / catalogue / migrations / 0016_auto_20171031_1232.py
1 from django.db import migrations, models
2
3
4 def refresh_books(apps, schema_editor):
5     Book = apps.get_model('catalogue', 'Book')
6     TagRelation = apps.get_model('catalogue', 'TagRelation')
7     db_alias = schema_editor.connection.alias
8     for book in Book.objects.using(db_alias).all():
9         book.cached_author = ', '.join(
10             TagRelation.objects.filter(content_type__model='book', object_id=book.id, tag__category='author')
11             .values_list('tag__name', flat=True))
12         book.has_audience = 'audience' in book.get_extra_info_json()
13         book.save()
14
15
16 class Migration(migrations.Migration):
17
18     dependencies = [
19         ('catalogue', '0015_book_recommended'),
20     ]
21
22     operations = [
23         migrations.AddField(
24             model_name='book',
25             name='cached_author',
26             field=models.CharField(db_index=True, max_length=240, blank=True),
27         ),
28         migrations.AddField(
29             model_name='book',
30             name='has_audience',
31             field=models.BooleanField(default=False),
32         ),
33         migrations.RunPython(refresh_books, migrations.RunPython.noop),
34     ]