naro

Django LFS - regenerate image scales

Aug 25th, 2013
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.11 KB | None | 0 0
  1. import sys
  2. from django.conf import settings
  3. from django.core.management.base import BaseCommand
  4. from django.core.files.base import ContentFile
  5. from lfs.catalog.models import Image
  6.  
  7.  
  8. class Command(BaseCommand):
  9.     help = "Removes and recreates all scales for all LFS Images"
  10.     args = ''
  11.  
  12.     def handle(self, *args, **options):
  13.  
  14.         if settings.MEDIA_ROOT == '':
  15.             print "MEDIA_ROOT is not set, nothing to do"
  16.             return
  17.  
  18.         progress = 0
  19.         total = Image.objects.count()
  20.         for img in Image.objects.all():
  21.             filename = img.image.name.split('/')[-1]
  22.             try:
  23.                 content = ContentFile(img.image.read())
  24.             except IOError:
  25.                 print >>sys.stderr, "Invalid file:", img.pk, filename
  26.                 continue
  27.             img.image.delete()  # remove all scales and files in media dir
  28.             img.image.save(filename, content)
  29.             del content
  30.             del filename
  31.             progress += 1
  32.             if progress % 500 == 0:
  33.                 print >>sys.stderr, "Progress: %s/%s" % (progress, total)
Advertisement
Add Comment
Please, Sign In to add comment