コード例 #1
0
ファイル: zcml.py プロジェクト: Zojax/zojax.content.type
def contentHandler(_context, schema, name, title, class_=None,
                   description='', permission='zope.View',
                   contenttype=None, ctclass=None,
                   type=[], contains=(), containers=(), addform=None):

    if class_ is None:
        type = type + [IInactiveType,]

    if IInactiveType in type and IActiveType in type:
        type.remove(IActiveType)

    # make content type Active if no type is set
    if not type:
        type.append(IActiveType)

    for tp in type:
        if tp.isOrExtends(IContentType):
            raise ValueError(
                'Content type type can not extend IContentType interface.', tp)

    # check schema
    if class_ is not None and not schema.implementedBy(class_):
        raise ValueError(
            'Content class should implement content schema.', class_, schema)

    # create content type
    if ctclass is not None:
        if not IContentType.implementedBy(ctclass):
            raise ValueError('Custom content type implementation '\
                                 'should implement IContentType interface.')
        ct_factory = ctclass
    else:
        ct_factory = ContentType

    ct = ct_factory(
        name, schema, class_, title, description, permission, addform)

    # set types
    interface.alsoProvides(ct, type)

    for tp in type:
        utility(_context, tp, ct, name=name)

    # create unique interface for content type
    if contenttype is None:
        iname = name
        for ch in ('.', '-'):
            iname = iname.replace(ch, '_')

        contenttype = InterfaceClass(iname, (IContentType,),
                                     __doc__='Content Type: %s' %name,
                                     __module__='zojax.content')

        # Add the content type to the `zojax.content` module.
        setattr(zojax.content, iname, contenttype)

    # register content type as utility
    utility(_context, contenttype, ct)

    # create named utility for content type
    utility(_context, IContentType, ct, name=name)

    # adapter that return IContentType object from class instances
    adapter(_context, (ClassToTypeAdapter(name),), IContentType, (schema,))

    if class_ is not None:
        clsifaces = list(interface.implementedBy(class_))
        if not IContent.implementedBy(class_):
            clsifaces.append(IContent)
        clsifaces.extend(type)
        interface.classImplements(class_, clsifaces)
        
    # process constraints
    _context.action(
        discriminator = ('zojax.content:contentTypeConstraints', name),
        callable = contentTypeConstraints,
        args = (name, contains, containers, _context),
        order = 999998)

    # added custom interface to contenttype object
    _context.action(
        discriminator = ('zojax.content:contenttypeInterface', contenttype),
        callable = contenttypeInterface,
        args = (ct, contenttype))