added aspect ratio checking for admin (+note)
This commit is contained in:
parent
c42babf7c3
commit
0bd4f3b4e0
|
@ -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)
|
||||
|
|
|
@ -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="<strong>The Image needs to have a 1:1 aspect ratio.</strong>")
|
||||
deposit = models.DecimalField(max_digits=8, decimal_places=2)
|
||||
description = models.TextField()
|
||||
buyableType = models.ManyToManyField(BuyableType)
|
||||
|
|
Loading…
Reference in New Issue