2010-12-02 02:39:15 +01:00
|
|
|
from models import Buyable, BuyableType, Purchase, Order
|
2010-10-13 02:57:59 +02:00
|
|
|
from django.contrib import admin
|
2011-10-08 05:05:27 +02:00
|
|
|
from django import forms
|
|
|
|
from PIL import Image
|
|
|
|
from django.core.files.uploadedfile import InMemoryUploadedFile
|
2010-10-13 02:57:59 +02:00
|
|
|
|
2011-10-08 05:05:27 +02:00
|
|
|
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)
|
2010-10-13 02:57:59 +02:00
|
|
|
admin.site.register(BuyableType)
|
|
|
|
admin.site.register(Purchase)
|
2010-12-02 02:39:15 +01:00
|
|
|
admin.site.register(Order)
|
2010-10-13 02:57:59 +02:00
|
|
|
|