Back to index

plone3  3.1.7
maintenance.py
Go to the documentation of this file.
00001 import os
00002 from cgi import escape
00003 
00004 from plone.app.form.validators import null_validator
00005 from plone.fieldsets.form import FieldsetsEditForm
00006 from zope.component import adapts
00007 from zope.formlib import form
00008 from zope.interface import Interface
00009 from zope.interface import implements
00010 from zope.schema import Int
00011 
00012 from AccessControl import getSecurityManager
00013 from AccessControl.Permissions import view_management_screens
00014 from Acquisition import aq_inner
00015 from Products.CMFCore.utils import getToolByName
00016 from Products.CMFDefault.formlib.schema import SchemaAdapterBase
00017 from Products.CMFPlone import PloneMessageFactory as _
00018 from Products.CMFPlone.interfaces import IPloneSiteRoot
00019 from Products.Five.browser.pagetemplatefile import ZopeTwoPageTemplateFile
00020 
00021 from plone.app.controlpanel.interfaces import IPloneControlPanelForm
00022 
00023 from plone.protect import CheckAuthenticator
00024 
00025 
00026 class IMaintenanceSchema(Interface):
00027 
00028     days = Int(title=_(u"Days of object history to keep after packing"),
00029         description=_(u"You should pack your database regularly. This number "
00030                        "indicates how many days of undo history you want to "
00031                        "keep. It is unrelated to versioning, so even if you "
00032                        "pack the database, the history of the content changes "
00033                        "will be kept. Recommended value is 7 days."),
00034         default=7,
00035         required=True)
00036 
00037 
00038 class MaintenanceControlPanelAdapter(SchemaAdapterBase):
00039 
00040     adapts(IPloneSiteRoot)
00041     implements(IMaintenanceSchema)
00042 
00043     def __init__(self, context):
00044         pprop = getToolByName(context, 'portal_properties')
00045         self.context = pprop.site_properties
00046 
00047     def get_days(self):
00048         return self.context.number_of_days_to_keep
00049 
00050     def set_days(self, value):
00051         if isinstance(value, basestring):
00052             value = int(value)
00053         self.context.number_of_days_to_keep = value
00054 
00055     days = property(get_days, set_days)
00056 
00057 
00058 class MaintenanceControlPanel(FieldsetsEditForm):
00059     """A simple form to pack the databases."""
00060 
00061     implements(IPloneControlPanelForm)
00062 
00063     template = ZopeTwoPageTemplateFile('maintenance.pt')
00064     form_fields = form.FormFields(IMaintenanceSchema)
00065     label = _(u'Maintenance')
00066     description = _(u"Zope server and site maintenance options.")
00067     form_name = _(u'Zope Database Packing')
00068 
00069     @form.action(_(u'Pack database now'), name=u'pack')
00070     def handle_edit_action(self, action, data):
00071         CheckAuthenticator(self.request)
00072         if not self.available():
00073             self.status = _(u'text_not_allowed_manage_server',
00074                             default=u'You are not allowed to manage the Zope server.')
00075             return
00076         form.applyChanges(self.context, self.form_fields, data, self.adapters)
00077         value = data.get('days', None)
00078         # skip the actual pack method in tests
00079         if value is not None and isinstance(value, int) and value >= 0:
00080             context = aq_inner(self.context)
00081             cpanel = context.unrestrictedTraverse('/Control_Panel')
00082             cpanel.manage_pack(days=value, REQUEST=None)
00083         self.status = _(u'Packed the database.')
00084 
00085     @form.action(_(u'Shut down'), validator=null_validator, name=u'shutdown')
00086     def handle_shutdown_action(self, action, data):
00087         CheckAuthenticator(self.request)
00088         if not self.available():
00089             self.status = _(u'text_not_allowed_manage_server',
00090                             default=u'You are not allowed to manage the Zope server.')
00091             return
00092         context = aq_inner(self.context)
00093         cpanel = context.unrestrictedTraverse('/Control_Panel')
00094         result = cpanel.manage_shutdown()
00095         return result
00096 
00097     @form.action(_(u'Restart'), validator=null_validator)
00098     def handle_restart_action(self, action, data):
00099         CheckAuthenticator(self.request)
00100         if not self.available():
00101             self.status = _(u'text_not_allowed_manage_server',
00102                             default=u'You are not allowed to manage the Zope server.')
00103             return
00104         context = aq_inner(self.context)
00105         cpanel = context.unrestrictedTraverse('/Control_Panel')
00106         url = self.request.get('URL')
00107         result = cpanel.manage_restart(url)
00108         return """<html>
00109         <head><meta HTTP-EQUIV=REFRESH CONTENT="10; URL=%s">
00110         </head>
00111         <body>Zope is restarting</body></html>
00112         """ % escape(url, 1)
00113 
00114     def available(self):
00115         root = aq_inner(self.context).getPhysicalRoot()
00116         sm = getSecurityManager()
00117         return sm.checkPermission(view_management_screens, root)
00118 
00119     def isRestartable(self):
00120         if os.environ.has_key('ZMANAGED'):
00121             return True
00122         return False
00123 
00124     def isDevelopmentMode(self):
00125         qi = getToolByName(self.context, 'portal_quickinstaller')
00126         return qi.isDevelopmentMode()
00127 
00128     def coreVersions(self):
00129         mt = getToolByName(self.context, 'portal_migration')
00130         versions = mt.coreVersions()
00131         versions['Instance'] = versions['Plone Instance']
00132         return versions
00133 
00134     def processTime(self):
00135         context = aq_inner(self.context)
00136         cpanel = context.unrestrictedTraverse('/Control_Panel')
00137         return cpanel.process_time()
00138 
00139     def dbSize(self):
00140         context = aq_inner(self.context)
00141         cpanel = context.unrestrictedTraverse('/Control_Panel')
00142         return cpanel.db_size()