from django.db import models

# Create your models here.

@@ -21,16 +21,38 @@
 t models
+%0Afrom PIL import Image
 %0A%0A# Crea
@@ -72,8 +72,1093 @@
 s here.%0A
+%0A# custom model manager%0Aclass PublishManager(models.Manager):%0A    def get_queryset(self):%0A        return super(PublishManager, self).get_queryset().filter(status='published')%0A%0A# helper functions%0Adef get_image_path(instance, filename):%0A    return '/'.join(%5B'uploads/images', instance.upload.slug, filename%5D)%0A%0A# models%0Aclass Upload(models.Model):%0A    STATUS_CHOICES = (%0A        ('draft', 'Draft'),%0A        ('published', 'Published'),%0A    )%0A%0A    image = models.ImageField(upload_to=get_image_path)%0A    status = models.Charfield(max_length=10, choices=STATUS_CHOICES, default='draft')%0A%0A%0A    published = PublishManager()%0A    objects = models.Manager()%0A%0A    # use PIL to change the image size%0A    # override save function%0A    def save(self, *args, *kwargs):%0A        super(Upload, self).save()%0A%0A        if self.image:%0A            image = Image.open(self.image)%0A            i_width, i_height = image.size%0A            max_size = (2000, 2000)%0A            if i_width %3E 2000 or i_height %3E 2000:%0A                image.thumbnail(max_size, Image.ANTIALIAS)%0A                image.save(self.image.path)

Add Uploads Model #1