The best way to mark content types using a common interface

I want the viewlet to apply to represent multiple types of content in the same python egg. What I did was apply the marker interface through browser / configure.zcml

<configure
    xmlns="http://namespaces.zope.org/zope"
    xmlns:browser="http://namespaces.zope.org/browser"
    i18n_domain="AnnualProgramModule.content">

  <include package="plone.app.contentmenu" />

  <class class="..content.programyear.ProgramYear">
    <implements interface=".viewlets.IAnnualProgram" />
  </class>

  <class class="..content.institution.Institution">
    <implements interface=".viewlets.IAnnualProgram" />
  </class>

</configure>

And in my Grok based template, I have:

from zope.interface import Interface
from five import grok
from plone.app.layout.viewlets.interfaces import IAboveContentTitle
from AnnualProgramModule.content.interfaces import IInstitution

grok.templatedir('templates')

class IAnnualProgram(Interface):
    """Marker Interface for AnnualProgram content types
    """

class AnnualProgramViewlet(grok.Viewlet):
    grok.require('zope2.View')
    grok.viewletmanager(IAboveContentTitle)
    grok.context(IAnnualProgram)

class InstitutionViewlet(grok.Viewlet):
    grok.require('zope2.View')
    grok.context(IInstitution)
    grok.viewletmanager(IAboveContentTitle)

It works. But I am interested to know if there is a better way to do this.

+5
source share
2 answers

No, what you are doing is actually the best way to approach this. Using marker interfaces is how I will do this anyway. :-)

An alternative would be re-registering the widget for interfaces or classes of all different types of content:

<browser:viewlet
    name="yourpackage.annualprogram"
    for="..content.programyear.ProgramYear"
    manager="plone.app.layout.viewlets.interfaces.IAboveContentTitle"
    template="annualprogram.pt"
    permission="zope2.View"
    />

<browser:viewlet
    name="yourpackage.annualprogram"
    for="..content.interfaces.IInstitution"
    manager="plone.app.layout.viewlets.interfaces.IAboveContentTitle"
    template="annualprogram.pt"
    permission="zope2.View"
    />

but it is much more complicated.

0
source

:

/annualprogram.py( paster ). :

from zope.interface import Interface    

class IAnnualProgram(Interface):
"""A common marker interface for AnnualProgram ContentTypes"""

.py :

from AnnualProgramModule.content.interfaces import IAnnualProgram
.....

class Institution(folder.ATFolder):
    """Institution Profile"""
    implements(IInstitution, IAnnualProgram)

, IAnnualProgram.

, .

0

All Articles