Created
July 12, 2012 11:41
-
-
Save iivvoo/3097641 to your computer and use it in GitHub Desktop.
migrate "traditional" image fields to FilerImageField
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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
alter table cmsplugin_fancyimagemodel RENAME COLUMN image TO oldimage;
alter table cmsplugin_fancyimagemodel ALTER COLUMN "oldimage" drop not null;
alter table "cmsplugin_fancyimagemodel" ADD COLUMN "image_id" INTEGER REFERENCES "filer_image" ("file_ptr_id") DEFERRABLE INITIALLY DEFERRED;