1dbee6085288a932db1e89c289ec1028d5d8d428
[wolnelektury.git] / src / catalogue / migrations / 0016_auto_20171031_1232.py
1 # -*- coding: utf-8 -*-
2 from __future__ import unicode_literals
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.extra_info
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, lambda apps, schema_editor: None),
37     ]