Back to index

plone3  3.1.7
ram.py
Go to the documentation of this file.
00001 from plone.app.form.validators import null_validator
00002 from zope.app.cache.interfaces.ram import IRAMCache
00003 
00004 from zope.interface import Interface
00005 from zope.component import adapts
00006 from zope.component import getMultiAdapter
00007 from zope.component import getUtility
00008 from zope.formlib import form
00009 from zope.interface import implements
00010 from zope.schema import Int
00011 
00012 from Acquisition import aq_inner
00013 
00014 from Products.CMFDefault.formlib.schema import SchemaAdapterBase
00015 from Products.CMFPlone import PloneMessageFactory as _
00016 from Products.CMFPlone.interfaces import IPloneSiteRoot
00017 from Products.Five.browser.pagetemplatefile import ZopeTwoPageTemplateFile
00018 from Products.statusmessages.interfaces import IStatusMessage
00019 
00020 from plone.app.controlpanel.form import ControlPanelForm
00021 from plone.app.controlpanel.form import _template
00022 
00023 from plone.protect import CheckAuthenticator
00024 
00025 
00026 class IRAMCacheSchema(Interface):
00027 
00028     maxEntries = Int(title=_(u'A maximum number of cached values.'),
00029                      default=1000,
00030                      required=True)
00031 
00032     maxAge = Int(title=_(u'Maximum age for cached values in seconds.'),
00033                  default=3600,
00034                  required=True)
00035 
00036     cleanupInterval = Int(title=_(u"An interval between cache cleanups "
00037                                    "in seconds."),
00038                  default=300,
00039                  required=True)
00040 
00041 
00042 class RAMCacheControlPanelAdapter(SchemaAdapterBase):
00043     adapts(IPloneSiteRoot)
00044     implements(IRAMCacheSchema)
00045 
00046     def get_max_entries(self):
00047         return getUtility(IRAMCache).maxEntries
00048     
00049     def set_max_entries(self, value):
00050         getUtility(IRAMCache).maxEntries = value
00051     
00052     maxEntries = property(get_max_entries, set_max_entries)
00053     
00054     def get_max_age(self):
00055         return getUtility(IRAMCache).maxAge
00056     
00057     def set_max_age(self, value):
00058         getUtility(IRAMCache).maxAge = value
00059     
00060     maxAge = property(get_max_age, set_max_age)
00061     
00062     def get_cleanup_intervall(self):
00063         return getUtility(IRAMCache).cleanupInterval
00064     
00065     def set_cleanup_intervall(self, value):
00066         getUtility(IRAMCache).cleanupInterval = value
00067     
00068     cleanupInterval = property(get_cleanup_intervall, set_cleanup_intervall)
00069 
00070 
00071 class RAMCacheControlPanel(ControlPanelForm):
00072 
00073     base_template = _template
00074     template = ZopeTwoPageTemplateFile('ram.pt')
00075 
00076     form_fields = form.FormFields(IRAMCacheSchema)
00077 
00078     label = _("RAM Cache Settings")
00079     description = None
00080     form_name = _("RAM Cache Settings")
00081 
00082     def getStatistics(self):
00083         return getUtility(IRAMCache).getStatistics()
00084 
00085     def restricted_actions(self):
00086         return [a for a in self.actions.actions
00087                   if a.__name__ in ('form.actions.save', 'form.actions.cancel')]
00088 
00089     @form.action(_(u'Clear cache'), validator=null_validator, name=u'clearall')
00090     def handle_clearall_action(self, action, data):
00091         CheckAuthenticator(self.request)
00092         context = aq_inner(self.context)
00093         getUtility(IRAMCache).invalidateAll()
00094         self.status = _(u'Cleared the cache.')
00095 
00096     @form.action(_(u'label_save', default=u'Save'), name=u'save')
00097     def handle_edit_action(self, action, data):
00098         CheckAuthenticator(self.request)
00099         if form.applyChanges(self.context, self.form_fields, data,
00100                              self.adapters):
00101             self.status = _("Changes saved.")
00102             self._on_save(data)
00103         else:
00104             self.status = _("No changes made.")
00105 
00106     @form.action(_(u'label_cancel', default=u'Cancel'),
00107                  validator=null_validator,
00108                  name=u'cancel')
00109     def handle_cancel_action(self, action, data):
00110         IStatusMessage(self.request).addStatusMessage(_("Changes canceled."),
00111                                                       type="info")
00112         url = getMultiAdapter((self.context, self.request),
00113                               name='absolute_url')()
00114         self.request.response.redirect(url + '/plone_control_panel')
00115         return ''