예제 #1
0
def getCacheForObject(obj):
    """Returns the cache associated with `obj` or ``None``."""
    adapter = ICacheable(obj, None)
    if adapter is None:
        return None
    cache_id = adapter.getCacheId()
    if not cache_id:
        return None
    return getUtility(ICache, cache_id)
예제 #2
0
class CacheableView(BrowserView):

    __used_for__ = IAnnotatable

    form = ViewPageTemplateFile("cacheableedit.pt")

    def __init__(self, *args):
        super(CacheableView, self).__init__(*args)
        self.cacheable = ICacheable(self.context)
        setUpEditWidgets(self, ICacheable, self.cacheable)

    def current_cache_id(self):
        "Returns the current cache ID."
        return self.cacheable.getCacheId()

    def current_cache_url(self):
        "Returns the current cache provider's URL."
        cache = getCacheForObject(self.context)
        absolute_url = zapi.getMultiView((cache, self.request),
                                         name='absolute_url')
        try:
            return absolute_url()
        except TypeError:
            # In case the cache object is a global one and does not have a
            # location, then we just return None. 
            return None

    def invalidate(self):
        "Invalidate the current cached value."

        cache = getCacheForObject(self.context)
        location = getLocationForCache(self.context)
        if cache and location:
            cache.invalidate(location)
            return self.form(message=_("cache-invalidated", u"Invalidated."))
        else:
            return self.form(message=_("no-cache-associated",
                                       u"No cache associated with object."))

    def action(self):
        "Change the cacheId"
        try:
            cacheId = self.cacheId_widget.getInputValue()
        except WidgetInputError, e:
            #return self.form(errors=e.errors)
            return repr(e.errors)
        else:
예제 #3
0
 def __init__(self, *args):
     super(CacheableView, self).__init__(*args)
     self.cacheable = ICacheable(self.context)
     setUpEditWidgets(self, ICacheable, self.cacheable)