mirror of
https://code.eliotberriot.com/funkwhale/funkwhale.git
synced 2025-10-06 08:49:55 +02:00
Attachments
This commit is contained in:
parent
421b441dbe
commit
c84396e669
50 changed files with 879 additions and 261 deletions
|
@ -0,0 +1,20 @@
|
|||
# Generated by Django 2.2.6 on 2019-11-12 09:56
|
||||
|
||||
from django.db import migrations, models
|
||||
import django.db.models.deletion
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('common', '0004_auto_20191111_1338'),
|
||||
('music', '0041_auto_20191021_1705'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AddField(
|
||||
model_name='album',
|
||||
name='attachment_cover',
|
||||
field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.SET_NULL, to='common.Attachment', blank=True),
|
||||
),
|
||||
]
|
|
@ -0,0 +1,41 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
from __future__ import unicode_literals
|
||||
|
||||
from django.db import migrations
|
||||
|
||||
|
||||
def create_attachments(apps, schema_editor):
|
||||
Album = apps.get_model("music", "Album")
|
||||
Attachment = apps.get_model("common", "Attachment")
|
||||
|
||||
album_attachment_mapping = {}
|
||||
def get_mimetype(path):
|
||||
if path.lower().endswith('.png'):
|
||||
return "image/png"
|
||||
return "image/jpeg"
|
||||
|
||||
for album in Album.objects.filter(attachment_cover=None).exclude(cover="").exclude(cover=None):
|
||||
album_attachment_mapping[album] = Attachment(
|
||||
file=album.cover,
|
||||
size=album.cover.size,
|
||||
mimetype=get_mimetype(album.cover.path),
|
||||
)
|
||||
|
||||
Attachment.objects.bulk_create(album_attachment_mapping.values(), batch_size=2000)
|
||||
# map each attachment to the corresponding album
|
||||
# and bulk save
|
||||
for album, attachment in album_attachment_mapping.items():
|
||||
album.attachment_cover = attachment
|
||||
|
||||
Album.objects.bulk_update(album_attachment_mapping.keys(), fields=['attachment_cover'], batch_size=2000)
|
||||
|
||||
|
||||
def rewind(apps, schema_editor):
|
||||
pass
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [("music", "0042_album_attachment_cover")]
|
||||
|
||||
operations = [migrations.RunPython(create_attachments, rewind)]
|
Loading…
Add table
Add a link
Reference in a new issue