From 0bd4f3b4e0c71d9ec6659d4b9a58c8fecd664fd0 Mon Sep 17 00:00:00 2001 From: seba Date: Sat, 8 Oct 2011 05:05:27 +0200 Subject: [PATCH] added aspect ratio checking for admin (+note) --- k4ever/buyable/admin.py | 31 ++++++++++++++++++++++++++++++- k4ever/buyable/models.py | 2 +- 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/k4ever/buyable/admin.py b/k4ever/buyable/admin.py index 37217d9..15ff686 100644 --- a/k4ever/buyable/admin.py +++ b/k4ever/buyable/admin.py @@ -1,7 +1,36 @@ from models import Buyable, BuyableType, Purchase, Order from django.contrib import admin +from django import forms +from PIL import Image +from django.core.files.uploadedfile import InMemoryUploadedFile -admin.site.register(Buyable) +class BuyableAdminForm(forms.ModelForm): + """ Special BuyableAdminForm which checks the buyable image for an 1:1 aspect ratio. """ + class Meta: + model = Buyable + + def clean_image(self): + img = self.cleaned_data['image'] + print img.file + print dir(img) + width, height = (0, 0) + if isinstance(img, InMemoryUploadedFile): + i = Image.open(img) + width, height = i.size + else: + width, height = img.width, img.height + + if width != height: + raise forms.ValidationError("Aspect ratio of image should be 1:1 (width x height was (%dx%d))"% (width, height)) + return self.cleaned_data['image'] + +class BuyableAdmin(admin.ModelAdmin): + form = BuyableAdminForm + + + + +admin.site.register(Buyable, BuyableAdmin) admin.site.register(BuyableType) admin.site.register(Purchase) admin.site.register(Order) diff --git a/k4ever/buyable/models.py b/k4ever/buyable/models.py index 363c1d2..22cf246 100644 --- a/k4ever/buyable/models.py +++ b/k4ever/buyable/models.py @@ -17,7 +17,7 @@ class Buyable(models.Model): """ Represents a buyable item. """ name = models.CharField(max_length=100) price = models.DecimalField(max_digits=8, decimal_places=2) - image = models.ImageField(upload_to='img/buyable/') + image = models.ImageField(upload_to='img/buyable/', help_text="The Image needs to have a 1:1 aspect ratio.") deposit = models.DecimalField(max_digits=8, decimal_places=2) description = models.TextField() buyableType = models.ManyToManyField(BuyableType)