An Image Dimension Validator for Plone 4+ that works with (the Dexterity based) plone.app.contenttypesThis generally demonstrates how to add validators to behaviours from plone.app.contenttypes
# -*- coding: utf-8 -*- | |
from plone.namedfile.interfaces import INamedBlobImageField | |
from plone.app.contenttypes.behaviors.leadimage import ILeadImage | |
from plone.app.contenttypes.interfaces import INewsItem | |
from zope.interface import Invalid | |
from z3c.form import validator | |
from five import grok | |
# Strict Dimensions check | |
REQUIRED_HEIGHT = 320 | |
REQUIRED_WIDTH = 2000 | |
# Filesize limit | |
MAXSIZE_KB = 200 | |
class ImageDimensionsValidator(validator.FileUploadValidator): | |
def validate(self, value): | |
super(ImageDimensionsValidator, self).validate(value) | |
if INewsItem.providedBy(self.context): | |
return None | |
if value: | |
# See: plone.namedfile.file.NamedBlobImage | |
width, height = value.getImageSize() | |
if REQUIRED_HEIGHT and REQUIRED_WIDTH: | |
if (width != REQUIRED_WIDTH or | |
height != REQUIRED_HEIGHT): | |
raise Invalid("Image has wrong dimensions - it should be %d x %d pixels (h x w)" | |
", but is %d x %d" % | |
(REQUIRED_HEIGHT, REQUIRED_WIDTH, height, width)) | |
elif REQUIRED_HEIGHT: | |
if (height != REQUIRED_HEIGHT): | |
raise Invalid("Image is the wrong height - it should be %d pixels" % | |
(REQUIRED_HEIGHT)) | |
elif REQUIRED_WIDTH: | |
if (width != REQUIRED_WIDTH): | |
raise Invalid("Image is the wrong width - it should be %d pixels" % | |
(REQUIRED_WIDTH)) | |
# Lastly check filesize | |
if value.getSize() > (MAXSIZE_KB * 1024): | |
raise Invalid("Image filesize is too large. Maximum permitted: %dKB " % MAXSIZE_KB) | |
validator.WidgetValidatorDiscriminators(ImageDimensionsValidator, | |
context=ILeadImage, | |
field=ILeadImage['image']) | |
grok.global_adapter(ImageDimensionsValidator) |
This comment has been minimized.
This comment has been minimized.
The single issue is that the test for News Items doesn't work when News Item's are being created, because the INewsItem interface isn't applied to the object at that stage. Let me know if you know how to fix it! |
This comment has been minimized.
This comment has been minimized.
My blog article on this: http://www.jowettenterprises.com/blog/an-image-dimension-validator-for-plone-4 |
This comment has been minimized.
This comment has been minimized.
If you want to use ZCML for configuration (and you probably should), then this adding this to the configure.zcml in the same directory should do you nicely (assuming you name the above file validator.py):
Thanks @martior for that! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This comment has been minimized.
djowett commentedNov 22, 2013
Credits:
I got a good head start from Daniel Widerin's Image filesize validator