funkwhale/api/funkwhale_api/music/migrations/0019_populate_mimetypes.py

32 wiersze
877 B
Python
Czysty Zwykły widok Historia

# -*- coding: utf-8 -*-
from __future__ import unicode_literals
2018-06-10 08:39:47 +00:00
from django.db import migrations
from funkwhale_api.music.utils import guess_mimetype
def populate_mimetype(apps, schema_editor):
TrackFile = apps.get_model("music", "TrackFile")
2018-06-09 13:36:16 +00:00
for tf in TrackFile.objects.filter(
audio_file__isnull=False, mimetype__isnull=True
).only("audio_file"):
try:
tf.mimetype = guess_mimetype(tf.audio_file)
except Exception as e:
2018-06-09 13:36:16 +00:00
print("Error on track file {}: {}".format(tf.pk, e))
continue
2018-06-09 13:36:16 +00:00
print("Track file {}: {}".format(tf.pk, tf.mimetype))
tf.save(update_fields=["mimetype"])
def rewind(apps, schema_editor):
pass
class Migration(migrations.Migration):
2018-06-09 13:36:16 +00:00
dependencies = [("music", "0018_auto_20180218_1554")]
2018-06-09 13:36:16 +00:00
operations = [migrations.RunPython(populate_mimetype, rewind)]