[Zope3-checkins] CVS: Zope3/lib/python/Zope/App/Caching/Views/Browser - CacheableView.py:1.1 __init__.py:1.1 configure.zcml:1.1 edit.pt:1.1

Albertas Agejevas alga@codeworks.lt
Wed, 9 Oct 2002 09:08:45 -0400


Update of /cvs-repository/Zope3/lib/python/Zope/App/Caching/Views/Browser
In directory cvs.zope.org:/tmp/cvs-serv4391/Views/Browser

Added Files:
	CacheableView.py __init__.py configure.zcml edit.pt 
Log Message:
Views for ICacheable

=== Added File Zope3/lib/python/Zope/App/Caching/Views/Browser/CacheableView.py ===
##############################################################################
#
# Copyright (c) 2001, 2002 Zope Corporation and Contributors.
# All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.0 (ZPL).  A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE.
#
##############################################################################
"""Management view for binding caches to content objects.

$Id: CacheableView.py,v 1.1 2002/10/09 13:08:44 alga Exp $
"""

from Zope.App.Caching.ICacheable import ICacheable
from Zope.App.OFS.Annotation.IAnnotatable import IAnnotatable
from Zope.App.Caching.Caching import getCacheForObj
from Zope.App.PageTemplate import ViewPageTemplateFile
#from Zope.App.Forms.Views.Browser.FormView import FormView
from Zope.Publisher.Browser.BrowserView import BrowserView
from Zope.App.Forms.Widget import CustomWidget
from Zope.App.Forms.Views.Browser import Widget
from Zope.ComponentArchitecture import getService, getAdapter
from Zope.Proxy.ContextWrapper import ContextWrapper
from Zope.Schema.Exceptions import StopValidation, ValidationError, \
     ValidationErrorsAll, ConversionErrorsAll
from Zope.App.Forms.Exceptions import WidgetInputError

class CacheableView(BrowserView):

    __used_for__ = IAnnotatable
    
    form = ViewPageTemplateFile("edit.pt")

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

        cache = getCacheForObj(self.context)
        if cache:
            cache.invalidate(self.context)
            return self.form(message="Invalidated.")
        else:
            return self.form(message="No cache associated with object.")

    def action(self):
        "Change the cacheId"
        try:
            cacheId = self._getCacheIdWidget().getData()
        except (ValidationErrorsAll, ConversionErrorsAll), e:
            return self.form(errors=e)
        except WidgetInputError, e:
            #return self.form(errors=e.errors)
            return repr(e.errors)
        else:
            getAdapter(self.context, ICacheable).setCacheId(cacheId)
            return self.form(message="Saved changes.")

    def renderCacheId(self):
        cacheId = getAdapter(self.context, ICacheable).getCacheId()
        return self._getCacheIdWidget().render(cacheId)
    
    def _getCacheIdWidget(self):
        cacheId = getAdapter(self.context, ICacheable).getCacheId()
        field = ICacheable.getDescriptionFor('cacheId')
        field = ContextWrapper(field, self.context)
        w = CustomWidget(Widget.ListWidget, size=1)
        return w(field, self.request)






=== Added File Zope3/lib/python/Zope/App/Caching/Views/Browser/__init__.py ===
##############################################################################
#
# Copyright (c) 2001, 2002 Zope Corporation and Contributors.
# All Rights Reserved.
# 
# This software is subject to the provisions of the Zope Public License,
# Version 2.0 (ZPL).  A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE.
# 
##############################################################################
"""Views for ICacheable.

$Id: __init__.py,v 1.1 2002/10/09 13:08:44 alga Exp $
"""



=== Added File Zope3/lib/python/Zope/App/Caching/Views/Browser/configure.zcml ===
<zopeConfigure
   xmlns="http://namespaces.zope.org/zope"
   xmlns:browser='http://namespaces.zope.org/browser'
>
  <browser:view for="Zope.App.OFS.Annotation.IAnnotatable."
                permission="Zope.Public"
                factory=".CacheableView.">

    <browser:page name="Caching.html"
                  attribute="form" />
    <browser:page name="ChangeCaching.html"
                  attribute="action" />
    <browser:page name="InvalidateCache.html"
                  attribute="invalidate" />
  </browser:view>
</zopeConfigure>


=== Added File Zope3/lib/python/Zope/App/Caching/Views/Browser/edit.pt ===
<html metal:use-macro="views/standard_macros/page">
  <body>
  <div metal:fill-slot="body">

    <p>This edit form allows you to associate a cache with this object.</p>

    <div tal:condition="python: options.has_key('errors') and
                                options['errors']">
      <span style="font-weight: bold">Errors:</span>
      <div tal:repeat="error options/errors | nothing">
        <span tal:replace="python: error[0].title" />:
        <span tal:replace="python: error[1].error_name" />
      </div>
    </div>
    <br />

    <form action="./" method="post" enctype="multipart/form-data">
      <input type="hidden" name="nextURL" value=""
          tal:attributes="value request/URL" />

      <table class="EditTable">
        <tr>
          <th class="EditAttributeName">Cache name</th>
          <td class="EditAttributeValue"
              tal:content="structure python: view.renderCacheId()">
            <input size="20" />
          </td>
        </tr>
      </table>

      <input type="submit" name="ChangeCaching.html:method" value="Save Changes" />
      <input type="submit" name="InvalidateCache.html:method"
             value="Invalidate Cached Value" />

    </form>
    <div tal:replace="options/message|nothing" />

  </div>
  </body>

</html>