from Acquisition import aq_inner
from five import grok
from plone import api

from plone.app.layout.viewlets.interfaces import IPortalFooter
from plone.uuid.interfaces import IUUID

from ade25.banner.interfaces import IBannerEnabled
from ade25.banner.contentbanner import IContentBanner


class BannerViewlet(grok.Viewlet):
    grok.context(IBannerEnabled)
    grok.viewletmanager(IPortalFooter)
    grok.require('zope2.View')
    grok.name('ade25.banner.PageBannerViewlet')

    def update(self):
        self.has_banners = len(self.banners()) > 0
        self.show_carousel = len(self.banners()) > 1

    def static_banner(self):
        items = self.banners()
        first_item = items[0].getObject()
        return IUUID(first_item, None)

    def render_item(self, uid):
        item = api.content.get(UID=uid)
        template = item.restrictedTraverse('@@banner-view')()
        return template

    def banners(self):
        context = aq_inner(self.context)
        catalog = api.portal.get_tool(name='portal_catalog')
        items = catalog(object_provides=IContentBanner.__identifier__,
                        path=dict(query='/'.join(context.getPhysicalPath()),
                                  depth=1),
                        review_state='published',
                        sort_on='getObjPositionInParent')
        return items

@@ -25,16 +25,27 @@
 aq_inner
+, aq_parent
 %0Afrom fi
@@ -82,16 +82,83 @@
 rt api%0A%0A
+from plone.app.layout.navigation.interfaces import INavigationRoot%0A
 from plo
@@ -1039,16 +1039,153 @@
 ontext)%0A
+        assignment_context = aq_parent(context)%0A        if INavigationRoot.providedBy(context):%0A            assignment_context = context%0A
         
@@ -1361,33 +1361,118 @@
 oin(
-context.getPhysicalPath()
+%0A                                    assignment_context.getPhysicalPath()%0A                                    
 ),%0A 

Add support for default pages with banners