web/webconfig.py
changeset 5444 f7fdb5dd82f6
parent 5443 f299ee54d7e0
child 5445 4467ed43d97d
equal deleted inserted replaced
5443:f299ee54d7e0 5444:f7fdb5dd82f6
    21 __docformat__ = "restructuredtext en"
    21 __docformat__ = "restructuredtext en"
    22 _ = unicode
    22 _ = unicode
    23 
    23 
    24 import os
    24 import os
    25 from os.path import join, exists, split
    25 from os.path import join, exists, split
       
    26 from warnings import warn
    26 
    27 
    27 from logilab.common.decorators import cached
    28 from logilab.common.decorators import cached
    28 
    29 
    29 from cubicweb.toolsutils import read_config
    30 from cubicweb.toolsutils import read_config
    30 from cubicweb.cwconfig import CubicWebConfiguration, register_persistent_options, merge_options
    31 from cubicweb.cwconfig import CubicWebConfiguration, register_persistent_options, merge_options
   206           }),
   207           }),
   207 
   208 
   208         ))
   209         ))
   209 
   210 
   210     def fckeditor_installed(self):
   211     def fckeditor_installed(self):
   211         return exists(self.ext_resources['FCKEDITOR_PATH'])
   212         return exists(self.uiprops['FCKEDITOR_PATH'])
   212 
   213 
   213     def eproperty_definitions(self):
   214     def eproperty_definitions(self):
   214         for key, pdef in super(WebConfiguration, self).eproperty_definitions():
   215         for key, pdef in super(WebConfiguration, self).eproperty_definitions():
   215             if key == 'ui.fckeditor' and not self.fckeditor_installed():
   216             if key == 'ui.fckeditor' and not self.fckeditor_installed():
   216                 continue
   217                 continue
   237             return repo
   238             return repo
   238 
   239 
   239     def vc_config(self):
   240     def vc_config(self):
   240         return self.repository().get_versions()
   241         return self.repository().get_versions()
   241 
   242 
   242     # mapping to external resources (id -> path) (`external_resources` file) ##
       
   243     ext_resources = {
       
   244         'FAVICON':  'DATADIR/favicon.ico',
       
   245         'LOGO':     'DATADIR/logo.png',
       
   246         'RSS_LOGO': 'DATADIR/rss.png',
       
   247         'HELP':     'DATADIR/help.png',
       
   248         'CALENDAR_ICON': 'DATADIR/calendar.gif',
       
   249         'SEARCH_GO':'DATADIR/go.png',
       
   250 
       
   251         'FCKEDITOR_PATH':  '/usr/share/fckeditor/',
       
   252 
       
   253         'IE_STYLESHEETS':    ['DATADIR/cubicweb.ie.css'],
       
   254         'STYLESHEETS':       ['DATADIR/cubicweb.css'],
       
   255         'STYLESHEETS_PRINT': ['DATADIR/cubicweb.print.css'],
       
   256 
       
   257         'JAVASCRIPTS':       ['DATADIR/jquery.js',
       
   258                               'DATADIR/jquery.corner.js',
       
   259                               'DATADIR/jquery.json.js',
       
   260                               'DATADIR/cubicweb.compat.js',
       
   261                               'DATADIR/cubicweb.python.js',
       
   262                               'DATADIR/cubicweb.htmlhelpers.js'],
       
   263         }
       
   264 
       
   265 
       
   266     def anonymous_user(self):
   243     def anonymous_user(self):
   267         """return a login and password to use for anonymous users. None
   244         """return a login and password to use for anonymous users. None
   268         may be returned for both if anonymous connections are not allowed
   245         may be returned for both if anonymous connections are not allowed
   269         """
   246         """
   270         try:
   247         try:
   276             user = unicode(user)
   253             user = unicode(user)
   277         return user, passwd
   254         return user, passwd
   278 
   255 
   279     def has_resource(self, rid):
   256     def has_resource(self, rid):
   280         """return true if an external resource is defined"""
   257         """return true if an external resource is defined"""
   281         return bool(self.ext_resources.get(rid))
   258         return bool(self.uiprops.get(rid))
   282 
   259 
   283     @cached
   260     @cached
   284     def locate_resource(self, rid):
   261     def locate_resource(self, rid):
   285         """return the directory where the given resource may be found"""
   262         """return the directory where the given resource may be found"""
   286         return self._fs_locate(rid, 'data')
   263         return self._fs_locate(rid, 'data')
   308     def load_configuration(self):
   285     def load_configuration(self):
   309         """load instance's configuration files"""
   286         """load instance's configuration files"""
   310         super(WebConfiguration, self).load_configuration()
   287         super(WebConfiguration, self).load_configuration()
   311         # load external resources definition
   288         # load external resources definition
   312         self._init_base_url()
   289         self._init_base_url()
   313         self._build_ext_resources()
   290         self._build_ui_properties()
   314 
   291 
   315     def _init_base_url(self):
   292     def _init_base_url(self):
   316         # normalize base url(s)
   293         # normalize base url(s)
   317         baseurl = self['base-url'] or self.default_base_url()
   294         baseurl = self['base-url'] or self.default_base_url()
   318         if baseurl and baseurl[-1] != '/':
   295         if baseurl and baseurl[-1] != '/':
   322         httpsurl = self['https-url']
   299         httpsurl = self['https-url']
   323         if httpsurl and httpsurl[-1] != '/':
   300         if httpsurl and httpsurl[-1] != '/':
   324             httpsurl += '/'
   301             httpsurl += '/'
   325             if not self.repairing:
   302             if not self.repairing:
   326                 self.global_set_option('https-url', httpsurl)
   303                 self.global_set_option('https-url', httpsurl)
   327 
   304         if self.debugmode:
   328     def _build_ext_resources(self):
   305             self.datadir_url = baseurl + 'data/'
   329         libresourcesfile = join(self.shared_dir(), 'data', 'external_resources')
   306         else:
   330         self.ext_resources.update(read_config(libresourcesfile))
   307             self.datadir_url = baseurl + 'data%s/' % self.instance_md5_version()
       
   308 
       
   309     def _build_ui_properties(self):
       
   310         # self.datadir_url[:-1] to remove trailing /
       
   311         from cubicweb.web.propertysheet import PropertySheet
       
   312         self.uiprops = PropertySheet(datadir_url=self.datadir_url[:-1])
       
   313         libuiprops = join(self.shared_dir(), 'data', 'uiprops.py')
       
   314         self.uiprops.load(libuiprops)
   331         for path in reversed([self.apphome] + self.cubes_path()):
   315         for path in reversed([self.apphome] + self.cubes_path()):
   332             resourcesfile = join(path, 'data', 'external_resources')
   316             self._load_ui_properties(join(path, 'data'))
   333             if exists(resourcesfile):
   317         self._load_ui_properties(self.apphome)
   334                 self.debug('loading %s', resourcesfile)
   318 
   335                 self.ext_resources.update(read_config(resourcesfile))
   319     def _load_ui_properties(self, path):
   336         resourcesfile = join(self.apphome, 'external_resources')
   320         resourcesfile = join(path, 'external_resources')
   337         if exists(resourcesfile):
   321         if exists(resourcesfile):
   338             self.debug('loading %s', resourcesfile)
   322             warn('[3.9] %s file is deprecated, use an uiprops.py file'
   339             self.ext_resources.update(read_config(resourcesfile))
   323                  % resourcesfile, DeprecationWarning)
   340         for resource in ('STYLESHEETS', 'STYLESHEETS_PRINT',
   324             for rid, val in read_config(resourcesfile).iteritems():
   341                          'IE_STYLESHEETS', 'JAVASCRIPTS'):
   325                 if rid in ('STYLESHEETS', 'STYLESHEETS_PRINT',
   342             val = self.ext_resources[resource]
   326                            'IE_STYLESHEETS', 'JAVASCRIPTS'):
   343             if isinstance(val, str):
   327                     val = [w.strip().replace('DATADIR/', self.datadir_url)
   344                 files = [w.strip() for w in val.split(',') if w.strip()]
   328                            for w in val.split(',') if w.strip()]
   345                 self.ext_resources[resource] = files
   329                     if rid == 'IE_STYLESHEETS':
       
   330                         rid = 'STYLESHEETS_IE'
       
   331                 else:
       
   332                     val = val.strip().replace('DATADIR/', self.datadir_url)
       
   333                 self.uiprops[rid] = val
       
   334         uipropsfile = join(path, 'uiprops.py')
       
   335         if exists(uipropsfile):
       
   336             self.debug('loading %s', uipropsfile)
       
   337             self.uiprops.load(uipropsfile)
   346 
   338 
   347     # static files handling ###################################################
   339     # static files handling ###################################################
   348 
   340 
   349     @property
   341     @property
   350     def static_directory(self):
   342     def static_directory(self):