在Plon中添加文件夹的自定义限制

2024-05-02 07:06:32 发布

您现在位置:Python中文网/ 问答频道 /正文

我可以为Plone 4.1中的文件夹内容添加自定义限制吗。限制文件夹只包含扩展名为*.doc,*.pdf的文件 我了解Plone中提供的文件/文件夹/页面/图像等常规限制


Tags: 文件图像文件夹内容docpdfplone页面
1条回答
网友
1楼 · 发布于 2024-05-02 07:06:32

如果没有额外的开发,你必须用验证器扩展文件类型来限制允许的mime类型。在

在没有详细说明的情况下(你自己试试,在这里问更多的问题,所以如果你被卡住了),下面是我遇到这个问题时要实施的各种活动部件:

  1. 创建新的IValidator类以检查允许的内容类型:

    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
    
  2. 在注册表中注册validotor的实例:

    from Products.validation.config import validation
    validation.register(LocalContentTypesValidator('checkLocalContentTypes'))
    
  3. 创建ATContentTypes ATFile类的新子类,使用基类架构的副本,将验证器添加到其验证链中:

    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',)
    
  4. 将字段添加到文件夹或自定义子类以存储本地允许的内容类型列表。我可能会用^{}来做这个。最近有很多关于这方面的文档,例如^{}。在

    当然,您必须做出一个策略决定,让人们在这里限制mime类型:通配符、自由格式文本、词汇表等。

相关问题 更多 >