Last active
September 13, 2021 13:03
-
-
Save gitllermopalafox/e3b0559d994df4e510d0 to your computer and use it in GitHub Desktop.
Django model dependent image size validation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Model dependent image size validation | |
# forms.py | |
from django import forms | |
from .models import * | |
from django.core.files.images import get_image_dimensions | |
class ValidateImageForm(forms.ModelForm): | |
class Meta: | |
model = MyModel # Model name | |
field = "image" # Field name | |
MinW = 1280 # Min. Width | |
MaxW = 1500 # Max. Width | |
checkH = False # If it's going to validate the height | |
MinH = 400 # Min. Height | |
MaxH = 1000 # Max. Height | |
text_minw = u"The image width is lower than %i" % MinW # Error text for min. width | |
text_maxw = u"The image width is larger than %i" % MaxW # Error text for max. width | |
text_minh = u"The image height is lower than %i" % MinH # Error text for min. height | |
text_maxh = u"The image height is larger than %i" % MaxH # Error text for max. height | |
#clean_(name of field) | |
def clean_image(self): | |
image = self.cleaned_data.get(self.Meta.field) | |
if not image: | |
raise forms.ValidationError(u"No image") | |
else: | |
w, h = get_image_dimensions(image) | |
if w < self.Meta.MinW: | |
raise forms.ValidationError(self.Meta.text_minw) | |
if w > self.Meta.MaxW: | |
raise forms.ValidationError(self.Meta.texo_maxw) | |
if h < self.Meta.MinH and self.Meta.checkH == True: | |
raise forms.ValidationError(self.Meta.text_minw) | |
if h > self.Meta.MaxH and self.Meta.checkH == True: | |
raise forms.ValidationError(self.Meta.text_maxw) | |
return image | |
# Inheritance | |
class ValidateDemoForm(ValidateImageForm): | |
class Meta(ValidateImageForm.Meta): | |
model = Demo | |
MaxW = 1900 | |
MinW = 1280 | |
text_minw = u"The image width is lower than %i" % MinW | |
text_maxw = u"The image width is larger than %i" % MaxW | |
# If the model has a thumbnail too. | |
class ValidateDemoForm(ValidateImageForm): | |
class Meta(ValidateImageForm.Meta): | |
model = Demo | |
MaxW = 1900 | |
MinW = 1280 | |
text_minw = u"The image width is lower than %i" % MinW | |
text_maxw = u"The image width is larger than %i" % MaxW | |
#Validate the thumbnail field | |
def clean_thumb(self): | |
thumb = self.cleaned_data.get("thumb") | |
if not thumb: | |
raise forms.ValidationError(u"No image") | |
else: | |
w, h = get_image_dimensions(thumb) | |
if w < 300: | |
raise forms.ValidationError(u"The image width is lower than 300px") | |
if w > 300: | |
raise forms.ValidationError(u"The image width is larger than 300px") | |
if h < 197: | |
raise forms.ValidationError(u"The image height is lower than 197px") | |
if h > 197: | |
raise forms.ValidationError(u"The image height is larger than 197px") | |
return thumb |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
thanks