def unique_file_gallerythumb(instance, file_name):
    # split the file_name into dir and filename
    # then split the filename into name and ext
    dirname, filename = os.path.split(file_name)
    prefix, suffix = os.path.splitext(filename)
    smallfilename, extension = os.path.splitext(prefix)

    # fd is file handle, but we arent too bothered by it
    # filename is the new random filename but its absolute
    # so we need to split it again.
    fd, filename = tempfile.mkstemp(suffix, smallfilename+"_",
dirname)
    dirname, save_filename = os.path.split(filename)
    return "thumbnails/" + save_filename

class UserProfile(models.Model):
    user = models.ForeignKey(User, unique=True)
    galleryname = models.CharField(max_length=30, help_text="Give the
gallery a friendly name. not bob. something like Anne-Marie's Pics!")
    gallerymessage = models.CharField(max_length=250, help_text="Leave
a quick message for the happy couple. no smut, thanks.")
    gallerythumb =
models.ImageField(upload_to=unique_file_gallerythumb)

    def save(self):
        pdb.set_trace()
        #resize image.
        image = Image.open(self.gallerythumb)
        #create thumbnail
        Thumb_Size = (120,120)
        #image = Image.open(self.photo)

        if image.mode not in ('L', 'RGB'):
            image = image.convert('RGB')

        image.thumbnail(Thumb_Size, Image.ANTIALIAS)

        temp_handle = StringIO()
        image.save(temp_handle, 'jpeg')
        temp_handle.seek(0)

        suf = SimpleUploadedFile(os.path.split(self.gallerythumb.name)
[-1], temp_handle.read(), content_type='image/jpg')
        self.gallerythumb.save(suf.name+'.jpg', suf, save=False)

        #enter info to database
        super(UserProfile, self).save()



##################
two problems with the above code.

1. def unique_file_gallerythumb(instance, file_name):
if you interogate the file_name in debug, it comtains two suffixes.
wedding.jpg.jpg is the uploaded file. quite strange. hance im using
splitext twice.

2. the line below creates an empty copy of the image in the root
folder, the one that the settings file lives in. empty as in zero kb.
self.gallerythumb.save(suf.name+'.jpg', suf, save=False)


anyone see any obvious reasons why the above would be happening?

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to