from five import grok from zope import schema from plone.directives import form, dexterity from plone.app.textfield import RichText from plone.namedfile.field import NamedImage from ngren.theme import _ from plone.dexterity.content import Item, Container from plone.app.layout.viewlets.common import ViewletBase from Products.Five.browser.pagetemplatefile import ViewPageTemplateFile from Products.CMFCore.utils import getToolByName from frontpagecontent import IFrontpageContent from zope.app.component.hooks import getSite class IFeatured(form.Schema): """A Banner which will be displayed in the Gallery Container """ title = schema.TextLine( title=_(u"Title"), required=True ) headline = schema.TextLine( title=_(u"Headline Text"), required=True ) picture = NamedImage(title=_(u"Banner"), description=_(u"Please upload an image"), required=False) text = schema.TextLine( title=_(u"Text"), required=False) link = schema.TextLine(title=_(u"Link"), required=False) class Featured( Item ): grok.implements(IFeatured) def __init__(self, *args): super(Featured, self).__init__(*args) self.exclude_from_nav = True class FeaturedViewlet(ViewletBase): index = ViewPageTemplateFile('templates/featuredviewlet.pt') def update(self): self.featured_items = [] portal = getSite() catalog = getToolByName(portal, "portal_catalog") path = "/".join(portal.getPhysicalPath()) frontpagecontents = catalog.searchResults( object_provides=IFrontpageContent.__identifier__, path={'query':path, 'depth':1}, sort_on='getObjPositionInParent') if len(frontpagecontents) > 0: path2 = "/".join(frontpagecontents[0].getObject().getPhysicalPath()) featured = catalog.searchResults( object_provides=IFeatured.__identifier__, path={'query':path2, 'depth':1}, sort_on='getObjPositionInParent', review_state='published') self.featured_items = [i.getObject() for i in featured] if len(self.featured_items) > 2: self.featured_items = self.featured_items[:2]