Skip to content

Instantly share code, notes, and snippets.

@yakky
Forked from iivvoo/migrate_filer.py
Created July 12, 2012 11:42
Show Gist options
  • Save yakky/3097643 to your computer and use it in GitHub Desktop.
Save yakky/3097643 to your computer and use it in GitHub Desktop.
migrate "traditional" image fields to FilerImageField
# encoding: utf-8
from django.core.management.base import BaseCommand
##
## FancyImageModel is a plugin model holding a ImageField called "oldimage" and
## FilerImageField "image". It removed duplicates based on the basename of the original
## image. This may or may not work for you.
from imageplugin.models import FancyImageModel
from filer.models.imagemodels import Image
import os
class Command(BaseCommand):
args = ""
help = ""
def handle(self, *args, **options):
fancies = FancyImageModel.objects.all()
name_image_map = {}
for fancy in fancies:
if fancy.image is None:
print fancy.oldimage, "needs migration"
name = os.path.basename(fancy.oldimage.name)
i = name_image_map.get(name)
if i is None:
print "creating ", name
i = Image(
file=fancy.oldimage.name,
height=fancy.oldimage.height,
width=fancy.oldimage.width,
author="migrated",
original_filename=os.path.basename(fancy.oldimage.name),
)
i.save()
name_image_map[name] = i
else:
print "already have ", name
fancy.image = i
fancy.save()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment