X-Git-Url: https://git.mdrn.pl/audio.git/blobdiff_plain/4ed2ea5a00aa1293135923b513925c46b963be93..0678dd50bd89ef1ad0f0d95c1e7907e31187a0d3:/src/archive/migrations/0010_populate_license.py diff --git a/src/archive/migrations/0010_populate_license.py b/src/archive/migrations/0010_populate_license.py new file mode 100644 index 0000000..dc59bdc --- /dev/null +++ b/src/archive/migrations/0010_populate_license.py @@ -0,0 +1,34 @@ +# Generated by Django 3.0.4 on 2020-05-24 01:40 + +import json +from django.db import migrations + + +def populate_license(apps, schema_editor): + License = apps.get_model('archive.License') + Audiobook = apps.get_model('archive.Audiobook') + licenses = {} + for a in Audiobook.objects.all(): + if a.mp3_tags: + tags = json.loads(a.mp3_tags) + uri = tags.get('license') + if not uri: + continue + if uri not in licenses: + licenses[uri], created = License.objects.get_or_create(uri=uri, defaults={"name": "?"}) + a.license = licenses[uri] + a.save() + + +class Migration(migrations.Migration): + + dependencies = [ + ('archive', '0009_auto_20200524_0139'), + ] + + operations = [ + migrations.RunPython( + populate_license, + migrations.RunPython.noop + ) + ]