0047_attachment_alias.py
42 lines
| 1.3 KiB
| text/x-python
|
PythonLexer
neko259
|
r1590 | # -*- coding: utf-8 -*- | ||
# Generated by Django 1.9.5 on 2016-05-21 07:43 | ||||
from __future__ import unicode_literals | ||||
from boards.signals import generate_thumb | ||||
from boards.utils import get_extension | ||||
from django.db import migrations, models | ||||
class Migration(migrations.Migration): | ||||
def images_to_attachments(apps, schema_editor): | ||||
PostImage = apps.get_model('boards', 'PostImage') | ||||
Attachment = apps.get_model('boards', 'Attachment') | ||||
count = 0 | ||||
images = PostImage.objects.all() | ||||
for image in images: | ||||
file_type = get_extension(image.image.name) | ||||
attachment = Attachment.objects.create( | ||||
file=image.image.file, mimetype=file_type, hash=image.hash, | ||||
alias=image.alias) | ||||
generate_thumb(attachment) | ||||
count += 1 | ||||
print('Processed {} of {} images'.format(count, len(images))) | ||||
for post in image.post_images.all(): | ||||
post.attachments.add(attachment) | ||||
image.image.close() | ||||
dependencies = [ | ||||
('boards', '0046_auto_20160520_2307'), | ||||
] | ||||
operations = [ | ||||
migrations.AddField( | ||||
model_name='attachment', | ||||
name='alias', | ||||
field=models.TextField(blank=True, null=True, unique=True), | ||||
), | ||||
migrations.RunPython(images_to_attachments), | ||||
] | ||||