import StringIO
from PIL import Image
from django.core.files.uploadedfile import InMemoryUploadedFile
from django.db import models


class CoreModel(models.Model):
    created = models.DateTimeField(auto_now_add=True, null=True)

    def __repr__(self):
        return '<%s:%s %s>' % (self.__class__.__name__, self.pk, str(self))

    class Meta:
        abstract = True


# Requires the model to have one image field called original_photo and a constant called SIZES
def resize_model_photos(sender, **kwargs):
    # Update fields is used when saving the photo so that we can short circuit an infinite loop with the signal
    if not kwargs['update_fields'] or not 'original_photo' in kwargs['update_fields']:
        return

    if not kwargs['instance'].original_photo:
        for size_name, size in sender.SIZES.iteritems():
            setattr(kwargs['instance'], size_name, '')
        return

    processed = process_thumbnail(kwargs['instance'], sender.SIZES)
    if processed:
        kwargs['instance'].save()


def process_thumbnail(instance, sizes, crop=False):
    photo_path = str(instance.original_photo.path)  # this returns the full system path to the original file
    original_image = Image.open(photo_path)  # open the image using PIL

    # pull a few variables out of that full path
    extension = photo_path.rsplit('.', 1)[1]  # the file extension
    filename = photo_path.rsplit('/', 1)[1].rsplit('.', 1)[0]  # the file name only (minus path or extension)

    # use the file extension to determine if the image is valid before proceeding
    if extension not in ['jpg', 'jpeg', 'gif', 'png']:
        return False

    for size_name, size in sizes.iteritems():
        im = original_image.copy()

        (x_size, y_size) = im.size
        original_ratio = float(x_size) / float(y_size)
        width = size['width']
        height = size['height']
        new_ratio = float(width / height)
        if new_ratio > original_ratio:
            im = im.resize((width, int(width / original_ratio)), Image.ANTIALIAS)
            if crop:
                clip_amount = int((int(width / original_ratio) - height) / 2)
                im = im.crop((0, clip_amount, width,height + clip_amount))
        else:
            im = im.resize((int(height * original_ratio), height), Image.ANTIALIAS)
            if crop:
                clip_amount = int((int(height * original_ratio) - width) / 2)
                im = im.crop((clip_amount, 0, width + clip_amount, height))

        name = "%s.jpg" % filename
        tempfile_io = StringIO.StringIO()
        if im.mode != "RGB":
            im = im.convert("RGB")
        im.save(tempfile_io, 'JPEG')
        temp_file = InMemoryUploadedFile(tempfile_io, None, name, 'image/jpeg', tempfile_io.len, None)
        getattr(instance, size_name).save(name, temp_file)

    return True
@@ -9,16 +9,26 @@
 tringIO%0A
+import os%0A
 from PIL
@@ -1080,16 +1080,18 @@
 se):%0A   
+ #
  photo_p
@@ -1220,26 +1220,39 @@
 ge.open(
+instance.original_
 photo
-_path
 )  # ope
@@ -1331,114 +1331,146 @@
     
-extension = photo_path.rsplit('.', 1)%5B1%5D  # the file extension%0A    filename = photo_path.rsplit('/', 1)%5B1%5D
+filename = os.path.basename(instance.original_photo.name).rsplit('.', 1)%5B0%5D%0A    extension = os.path.basename(instance.original_photo.name)
 .rsp
@@ -1481,17 +1481,17 @@
 '.', 1)%5B
-0
+1
 %5D  # the
@@ -1500,33 +1500,8 @@
 ile 
-name only (minus path or 
 exte
@@ -1505,17 +1505,16 @@
 xtension
-)
 %0A%0A    # 

fix getting filename and extensions to not use absolute paths