comparison libervia/server/server.py @ 1132:0cafb79ced6d

server: use site names in _moveFirstLevelToDict to better distinguish values for default site at first level + better handling of default menu
author Goffi <goffi@goffi.org>
date Tue, 18 Sep 2018 21:16:53 +0200
parents 9cf592d1e6aa
children ef565839dada
comparison
equal deleted inserted replaced
1131:9cf592d1e6aa 1132:0cafb79ced6d
1751 def _front_url_filter(self, ctx, relative_url): 1751 def _front_url_filter(self, ctx, relative_url):
1752 template_data = ctx[u'template_data'] 1752 template_data = ctx[u'template_data']
1753 return os.path.join(u'/', C.TPL_RESOURCE, template_data.site or u'sat', 1753 return os.path.join(u'/', C.TPL_RESOURCE, template_data.site or u'sat',
1754 C.TEMPLATE_TPL_DIR, template_data.theme, relative_url) 1754 C.TEMPLATE_TPL_DIR, template_data.theme, relative_url)
1755 1755
1756 def _moveFirstLevelToDict(self, options, key): 1756 def _moveFirstLevelToDict(self, options, key, keys_to_keep):
1757 """Read a config option and put value at first level into u'' dict 1757 """Read a config option and put value at first level into u'' dict
1758 1758
1759 This is useful to put values for Libervia official site directly in dictionary, 1759 This is useful to put values for Libervia official site directly in dictionary,
1760 and to use site_name as keys when external sites are used. 1760 and to use site_name as keys when external sites are used.
1761 options will be modified in place 1761 options will be modified in place
1762 @param options(dict): options to modify
1763 @param key(unicode): setting key to modify
1764 @param keys_to_keep(list(unicode)): keys allowed in first level
1762 """ 1765 """
1763 try: 1766 try:
1764 conf = options[key] 1767 conf = options[key]
1765 except KeyError: 1768 except KeyError:
1766 return 1769 return
1767 if not isinstance(conf, dict): 1770 if not isinstance(conf, dict):
1768 options[key] = {u'': conf} 1771 options[key] = {u'': conf}
1769 return 1772 return
1770 default_dict = conf.setdefault(u'', {}) 1773 default_dict = conf.get(u'', {})
1771 to_delete = [] 1774 to_delete = []
1772 for key, value in conf.iteritems(): 1775 for key, value in conf.iteritems():
1773 # "/" can't be present in host name, and help to differenciate from 1776 if key not in keys_to_keep:
1774 # non vhost dict (which may be used in redirections)
1775 if not isinstance(value, dict) or u'/' in key:
1776 default_dict[key] = value 1777 default_dict[key] = value
1777 to_delete.append(key) 1778 to_delete.append(key)
1778 for key in to_delete: 1779 for key in to_delete:
1779 del conf[key] 1780 del conf[key]
1781 if default_dict:
1782 conf[u''] = default_dict
1780 1783
1781 def backendReady(self, __): 1784 def backendReady(self, __):
1782 self.media_dir = self.bridge.getConfig("", "media_dir") 1785 self.media_dir = self.bridge.getConfig("", "media_dir")
1783 self.local_dir = self.bridge.getConfig("", "local_dir") 1786 self.local_dir = self.bridge.getConfig("", "local_dir")
1784 self.cache_root_dir = os.path.join(self.local_dir, C.CACHE_DIR) 1787 self.cache_root_dir = os.path.join(self.local_dir, C.CACHE_DIR)
1785 1788 self.renderer = template.Renderer(self, self._front_url_filter)
1786 self._moveFirstLevelToDict(self.options, "url_redirections_dict") 1789 sites_names = self.renderer.sites_paths.keys()
1787 self._moveFirstLevelToDict(self.options, "menu_json") 1790
1791 self._moveFirstLevelToDict(self.options, "url_redirections_dict", sites_names)
1792 self._moveFirstLevelToDict(self.options, "menu_json", sites_names)
1793 if not u'' in self.options["menu_json"]:
1794 self.options["menu_json"][u''] = C.DEFAULT_MENU
1788 1795
1789 # we create virtual hosts and import Libervia pages into them 1796 # we create virtual hosts and import Libervia pages into them
1790 self.renderer = template.Renderer(self, self._front_url_filter)
1791 self.vhost_root = vhost.NameVirtualHost() 1797 self.vhost_root = vhost.NameVirtualHost()
1792 default_site_path = os.path.dirname(libervia.__file__) 1798 default_site_path = os.path.dirname(libervia.__file__)
1793 # self.sat_root is official Libervia site 1799 # self.sat_root is official Libervia site
1794 self.sat_root = default_root = LiberviaRootResource( 1800 self.sat_root = default_root = LiberviaRootResource(
1795 host=self, host_name=u'', site_name=u'', site_path=default_site_path, 1801 host=self, host_name=u'', site_name=u'', site_path=default_site_path,