36 lines
969 B
Python
36 lines
969 B
Python
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)
|
|
|