from Products.Five import BrowserView
from Products.CMFCore.utils import getToolByName

class SyndicationView(BrowserView):

    def syndicationAllowed(self, site=False):
        """Check whether syndication is allows on the context

        Based on Plone's rssAllowed.py PythonScript
        """
        ps = getToolByName(self.context, "portal_syndication")

        allowed = True
        if not site and not ps.isSyndicationAllowed(self.context):
            allowed = False
        if site and not ps.isSiteSyndicationAllowed():
            allowed = False
        # really we should be raising an HTTP error, something that
        # rss news readers would understand
        if not allowed:
            raise ValueError, "Site syndication via RSS feeds is not " \
                  "allowed. Ask the sites system administrator to go to " \
                  "portal_syndication > Policies and enable syndication. "\
                  "Each folder then needs to have syndication enabled."
        return allowed

    def syndicatableContent(self):
        ps = getToolByName(self.context, "portal_syndication")
        return ps.getSyndicatableContent(self.context)

    def updateBase(self):
        """Return the date of the last update in HTML4 form as a string"""
        ps = getToolByName(self.context, "portal_syndication")
        return ps.getHTML4UpdateBase(self.context)
