from zope.interface import implements
from Products.validation.interfaces.IValidator import IValidator
class LocalContentTypesValidator(object):
implements(IValidator)
def __init__(self, name, title='', description='')
self.name = name
self.title = title or name
self.description = description
def __call__(value, *args, **kwargs):
instance = kwargs.get('instance', None)
field = kwargs.get('field', None)
# Get your list of content types from the aq_parent of the instance
# Verify that the value (*usually* a python file object)
# I suspect you have to do some content type sniffing here
# Return True if the content type is allowed, or an error message
在注册表中注册validotor的实例:
from Products.validation.config import validation
validation.register(LocalContentTypesValidator('checkLocalContentTypes'))
from Products.ATContentTypes.content.file import ATFile, ATFileSchema
Schema = ATFileSchema.schema.copy()
Schema['file'].validators = schema['file'].validators + (
'checkLocalContentTypes',)
class ContentTypeRestrictedFile(ATFile):
schema = Schema
# Everything else you need for a custom Archetype
或者,如果希望将此应用于部署中的所有文件对象,则只需更改ATFile架构本身:
from Products.ATContentTypes.content.file import ATFileSchema
ATFileSchema['file'].validators = ATFileSchema['file'].validators + (
'checkLocalContentTypes',)
如果没有额外的开发,你必须用验证器扩展文件类型来限制允许的mime类型。在
在没有详细说明的情况下(你自己试试,在这里问更多的问题,所以如果你被卡住了),下面是我遇到这个问题时要实施的各种活动部件:
创建新的
IValidator
类以检查允许的内容类型:在注册表中注册validotor的实例:
创建ATContentTypes ATFile类的新子类,使用基类架构的副本,将验证器添加到其验证链中:
或者,如果希望将此应用于部署中的所有文件对象,则只需更改ATFile架构本身:
将字段添加到文件夹或自定义子类以存储本地允许的内容类型列表。我可能会用^{} 来做这个。最近有很多关于这方面的文档,例如^{} 。在
当然,您必须做出一个策略决定,让人们在这里限制mime类型:通配符、自由格式文本、词汇表等。
相关问题 更多 >
编程相关推荐