Create a gist now

Instantly share code, notes, and snippets.

@djowett /validators.py Secret
Last active Dec 28, 2015

Embed
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)
@djowett

This comment has been minimized.

Show comment
Hide comment
@djowett

djowett Nov 22, 2013

Credits:
I got a good head start from Daniel Widerin's Image filesize validator

Owner

djowett commented Nov 22, 2013

Credits:
I got a good head start from Daniel Widerin's Image filesize validator

@djowett

This comment has been minimized.

Show comment
Hide comment
@djowett

djowett Nov 26, 2013

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!

Owner

djowett commented Nov 26, 2013

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!

@djowett

This comment has been minimized.

Show comment
Hide comment
@djowett

This comment has been minimized.

Show comment
Hide comment
@djowett

djowett Nov 9, 2015

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):

 <adapter 
    factory=".validator.ImageDimensionsValidator"  
    provides="z3c.form.interfaces.IValidator" />

Thanks @martior for that!

Owner

djowett commented Nov 9, 2015

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):

 <adapter 
    factory=".validator.ImageDimensionsValidator"  
    provides="z3c.form.interfaces.IValidator" />

Thanks @martior for that!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment