Back to index

plone3  3.1.7
KSSRegistry.py
Go to the documentation of this file.
00001 from Globals import InitializeClass
00002 from AccessControl import ClassSecurityInfo
00003 
00004 from zope.interface import implements
00005 
00006 from Products.CMFCore.utils import getToolByName
00007 
00008 from Products.PageTemplates.PageTemplateFile import PageTemplateFile
00009 
00010 from Products.ResourceRegistries import config
00011 from Products.ResourceRegistries import permissions
00012 from Products.ResourceRegistries.interfaces import IKSSRegistry
00013 from Products.ResourceRegistries.tools.BaseRegistry import BaseRegistryTool
00014 from Products.ResourceRegistries.tools.BaseRegistry import Resource
00015 
00016 from packer import CSSPacker
00017 
00018 
00019 class KineticStylesheet(Resource):
00020     security = ClassSecurityInfo()
00021 
00022     def __init__(self, id, **kwargs):
00023         Resource.__init__(self, id, **kwargs)
00024         self._data['compression'] = kwargs.get('compression', 'safe')
00025 
00026     security.declarePublic('getCompression')
00027     def getCompression(self):
00028         # as this is a new property, old instance might not have that value, so
00029         # return 'safe' as default
00030         compression = self._data.get('compression', 'safe')
00031         if compression in config.KSS_COMPRESSION_METHODS:
00032             return compression
00033         return 'none'
00034 
00035     security.declareProtected(permissions.ManagePortal, 'setCompression')
00036     def setCompression(self, compression):
00037         self._data['compression'] = compression
00038 
00039 InitializeClass(KineticStylesheet)
00040 
00041 
00042 class KSSRegistryTool(BaseRegistryTool):
00043     """A Plone registry for managing the linking to kss files."""
00044 
00045     id = config.KSSTOOLNAME
00046     meta_type = config.KSSTOOLTYPE
00047     title = 'KSS Registry'
00048 
00049     security = ClassSecurityInfo()
00050 
00051     implements(IKSSRegistry)
00052     __implements__ = BaseRegistryTool.__implements__
00053 
00054     #
00055     # ZMI stuff
00056     #
00057 
00058     manage_kssForm = PageTemplateFile('www/kssconfig', config.GLOBALS)
00059     manage_kssComposition = PageTemplateFile('www/ksscomposition', config.GLOBALS)
00060 
00061     manage_options = (
00062         {
00063             'label': 'KSS Registry',
00064             'action': 'manage_kssForm',
00065         },
00066         {
00067             'label': 'Merged KSS Composition',
00068             'action': 'manage_kssComposition',
00069         },
00070     ) + BaseRegistryTool.manage_options
00071 
00072     attributes_to_compare = ('getExpression', 'getCookable', 'getCacheable')
00073     filename_base = 'ploneStyles'
00074     filename_appendix = '.kss'
00075     merged_output_prefix = u''
00076     cache_duration = config.KSS_CACHE_DURATION
00077     resource_class = KineticStylesheet
00078 
00079     #
00080     # Private Methods
00081     #
00082 
00083     security.declarePrivate('clearKineticStylesheets')
00084     def clearKineticStylesheets(self):
00085         self.clearResources()
00086 
00087     def _compressKSS(self, content, level='safe'):
00088         if level == 'full':
00089             return CSSPacker('full').pack(content)
00090         elif level == 'safe':
00091             return CSSPacker('safe').pack(content)
00092         else:
00093             return content
00094 
00095     security.declarePrivate('finalizeContent')
00096     def finalizeContent(self, resource, content):
00097         """Finalize the resource content."""
00098         compression = resource.getCompression()
00099         if compression != 'none' and not self.getDebugMode():
00100             orig_url = "%s/%s?original=1" % (self.absolute_url(), resource.getId())
00101             content = "/* %s */\n%s" % (orig_url,
00102                                      self._compressKSS(content, compression))
00103 
00104         return content
00105 
00106     #
00107     # ZMI Methods
00108     #
00109 
00110     security.declareProtected(permissions.ManagePortal, 'manage_addKineticStylesheet')
00111     def manage_addKineticStylesheet(self, id, expression='', media='',
00112                              rel='stylesheet', title='', rendering='import',
00113                              enabled=False, cookable=True, compression='safe',
00114                              cacheable=True, REQUEST=None):
00115         """Register a kineticstylesheet from a TTW request."""
00116         self.registerKineticStylesheet(id, expression, enabled,
00117                                        cookable, compression, cacheable)
00118         if REQUEST:
00119             REQUEST.RESPONSE.redirect(REQUEST['HTTP_REFERER'])
00120 
00121     security.declareProtected(permissions.ManagePortal, 'manage_saveKineticStylesheets')
00122     def manage_saveKineticStylesheets(self, REQUEST=None):
00123         """Save kineticstylesheets from the ZMI.
00124 
00125         Updates the whole sequence. For editing and reordering.
00126         """
00127         debugmode = REQUEST.get('debugmode',False)
00128         self.setDebugMode(debugmode)
00129         autogroupingmode = REQUEST.get('autogroupingmode', False)
00130         self.setAutoGroupingMode(autogroupingmode)
00131         records = REQUEST.get('kineticstylesheets')
00132         records.sort(lambda a, b: a.sort - b.sort)
00133         self.resources = ()
00134         kineticstylesheets = []
00135         for r in records:
00136             kss = KineticStylesheet(r.get('id'),
00137                                     expression=r.get('expression', ''),
00138                                     enabled=r.get('enabled', False),
00139                                     cookable=r.get('cookable', False),
00140                                     cacheable=r.get('cacheable', False),
00141                                     compression=r.get('compression', 'safe'))
00142             kineticstylesheets.append(kss)
00143         self.resources = tuple(kineticstylesheets)
00144         self.cookResources()
00145         if REQUEST:
00146             REQUEST.RESPONSE.redirect(REQUEST['HTTP_REFERER'])
00147 
00148     security.declareProtected(permissions.ManagePortal, 'manage_removeKineticStylesheet')
00149     def manage_removeKineticStylesheet(self, id, REQUEST=None):
00150         """Remove kineticstylesheet from the ZMI."""
00151         self.unregisterResource(id)
00152         if REQUEST:
00153             REQUEST.RESPONSE.redirect(REQUEST['HTTP_REFERER'])
00154 
00155     #
00156     # Protected Methods
00157     #
00158 
00159     security.declareProtected(permissions.ManagePortal, 'registerKineticStylesheet')
00160     def registerKineticStylesheet(self, id, expression='', enabled=1,
00161                                   cookable=True, compression='safe',
00162                                   cacheable=True, skipCooking=False):
00163         """Register a kineticstylesheet."""
00164         kineticstylesheet = KineticStylesheet(id,
00165                                 expression=expression,
00166                                 enabled=enabled,
00167                                 cookable=cookable,
00168                                 compression=compression,
00169                                 cacheable=cacheable)
00170         self.storeResource(kineticstylesheet, skipCooking=skipCooking)
00171 
00172     security.declareProtected(permissions.ManagePortal, 'updateKineticStylesheet')
00173     def updateKineticStylesheet(self, id, **data):
00174         kineticstylesheet = self.getResourcesDict().get(id, None)
00175         if kineticstylesheet is None:
00176             raise ValueError, 'Invalid resource id %s' % (id)
00177         
00178         if data.get('expression', None) is not None:
00179             kineticstylesheet.setExpression(data['expression'])
00180         if data.get('enabled', None) is not None:
00181             kineticstylesheet.setEnabled(data['enabled'])
00182         if data.get('cookable', None) is not None:
00183             kineticstylesheet.setCookable(data['cookable'])
00184         if data.get('compression', None) is not None:
00185             kineticstylesheet.setCompression(data['compression'])
00186         if data.get('cacheable', None) is not None:
00187             kineticstylesheet.setCacheable(data['cacheable'])
00188 
00189     security.declareProtected(permissions.ManagePortal, 'getCompressionOptions')
00190     def getCompressionOptions(self):
00191         """Compression methods for use in ZMI forms."""
00192         return config.KSS_COMPRESSION_METHODS
00193 
00194     security.declareProtected(permissions.View, 'getContentType')
00195     def getContentType(self):
00196         """Return the registry content type."""
00197         plone_utils = getToolByName(self, 'plone_utils')
00198         encoding = plone_utils.getSiteEncoding()
00199         return 'text/css;charset=%s' % encoding
00200 
00201 
00202 InitializeClass(KSSRegistryTool)