from plone import api
from plone.app.layout.navigation.interfaces import INavigationRoot
from ploneintranet.notifications.sql import saconfig
from zExceptions import NotFound
from zope.component import adapter
from zope.publisher.interfaces.browser import IBrowserRequest
from zope.traversing.namespace import SimpleHandler


@adapter(INavigationRoot, IBrowserRequest)
class NotificationTraverser(SimpleHandler):
    """notification view traverser."""

    def traverse(self, uid, ignored):
        session = saconfig.session()
        Notification = saconfig.Notification
        notification = (
            session.query(Notification).filter(Notification.uid == uid).first()
        )
        if not notification:
            raise NotFound
        view = api.content.get_view("view", notification)
        return view
