2012-07-29 7 views
5

Chcę, aby widok był stosowany do widoku kilku typów zawartości w tym samym jajeczku z python. Co robiłem jest zastosowanie interfejsu markera poprzez przeglądarkę/configure.zcmlLepszy sposób oznaczania typów zawartości za pomocą wspólnego interfejsu

<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> 

I w moim szablonu Grok opartej mam:

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) 

To działa. Ale chciałbym wiedzieć, czy jest lepszy sposób na zrobienie tego.

Odpowiedz

0

Nie, to, co robisz, jest w rzeczywistości najlepszym sposobem podejścia do tego problemu. Używanie interfejsów znaczników jest tak czy inaczej w każdym przypadku. :-)

Alternatywą byłoby, aby ponownie zarejestrować viewlet dla interfejsów lub klas wszystkich typów treści Zamiast:

<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" 
    /> 

ale to jest dużo bardziej gadatliwy.

0

Jako alternatywę, to działa również:

dodałem interfejsów/annualprogram.py (użyłem pastowania stworzyć mój produkt). W nim mam:

from zope.interface import Interface  

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

Wtedy w moim institution.py I dodaje:

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

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

Następnie zastosowano to samo do innych typów zawartości, które musiały się IAnnualProgram.

Działa to, ale niekoniecznie lepiej.

Powiązane problemy