2013-02-28 86 views
2

我有一个灵巧型,具有图像字段定义是这样的:图片字段验证为特定的宽度/高度

image = NamedBlobImage(
    title=_(u'Lead Image'), 
    description=_(u"Upload a Image of Size 230x230."), 
    required=True, 
) 

我如何添加一个验证器来检查上传的图片文件?例如,如果图像宽度超过500像素,则警告用户上传另一个文件。提示或示例代码表示赞赏。

回答

4

你想设置一个约束功能:

from zope.interface import Invalid 
from foo.bar import MessageFactory as _ 


def imageSizeConstraint(value): 
    # value implements the plone.namedfile.interfaces.INamedBlobImageField interface 
    width, height = value.getImageSize() 
    if width > 500 or height > 500: 
     raise Invalid(_(u"Your image is too large")) 

然后设置功能您NamedBlobImageconstraint

image = NamedBlobImage(
    title=_(u'Lead Image'), 
    description=_(u"Upload a Image of Size 230x230."), 
    constraint=imageSizeConstraint, 
    required=True, 
) 

更多信息,请参见Dexterity manual on validation,还有plone.namedfile interface definitions

相关问题