9533003a046faa6f9efae635d729231a9ea3e252
[audio.git] / src / archive / migrations / 0010_populate_license.py
1 # Generated by Django 3.0.4 on 2020-05-24 01:40
2
3 import json
4 from django.db import migrations
5
6
7 def populate_license(apps, schema_editor):
8     License = apps.get_model('archive.License')
9     Audiobook = apps.get_model('archive.Audiobook')
10     licenses = {}
11     for a in Audiobook.objects.all():
12         if a.mp3_tags:
13             tags = json.loads(a.mp3_published_tags)
14             uri = tags.get('license')
15             if not uri:
16                 continue
17             if uri not in licenses:
18                 licenses[uri], created = License.objects.get_or_create(uri=uri, defaults={"name": "?"})
19             a.license = licenses[uri]
20             a.save()
21
22
23 class Migration(migrations.Migration):
24
25     dependencies = [
26         ('archive', '0009_auto_20200524_0139'),
27     ]
28
29     operations = [
30         migrations.RunPython(
31             populate_license,
32             migrations.RunPython.noop
33         )
34     ]