Fix volume updater.
[audio.git] / src / youtube / migrations / 0012_move_thumbnail_definitions.py
1 # Generated by Django 3.0.6 on 2020-07-03 17:18
2
3 from django.db import migrations
4 import yaml
5
6
7 def generate_versions(version_lists):
8     if not version_lists:
9         yield []
10     else:
11         for subversion in generate_versions(version_lists[1:]):
12             for version in version_lists[0]:
13                 yield [version] + subversion
14
15
16 def move_definitions(apps, schema_editor):
17     YouTube = apps.get_model('youtube', 'YouTube')
18     ThumbnailTemplate = apps.get_model('youtube', 'ThumbnailTemplate')
19     order = 1
20     for youtube in YouTube.objects.all():
21         src_def = yaml.load(youtube.thumbnail_definition)
22         version_lists = [
23             box['versions']
24             for box in src_def['boxes']
25         ]
26         for version in generate_versions(version_lists):
27             defn = yaml.dump(
28                 {"boxes": version},
29                 allow_unicode=True,
30                 default_flow_style=False
31             )
32             ThumbnailTemplate.objects.create(
33                 order=order,
34                 is_active=True,
35                 background=youtube.thumbnail_template,
36                 definition=defn,
37             )
38             order += 1
39
40
41 class Migration(migrations.Migration):
42
43     dependencies = [
44         ('youtube', '0011_thumbnailtemplate'),
45     ]
46
47     operations = [
48         migrations.RunPython(
49             move_definitions,
50             migrations.RunPython.noop
51         )
52     ]