You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
k4ever/k4ever/buyable/admin.py

36 lines
969 B

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
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']
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)