You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

197 lines
7.2 KiB

  1. # -*- coding: utf-8 -*-
  2. ##############################################################################
  3. #
  4. # Adapted by Nicolas Bessi. Copyright Camptocamp SA
  5. # Based on Florent Xicluna original code. Copyright Wingo SA
  6. #
  7. # This program is free software: you can redistribute it and/or modify
  8. # it under the terms of the GNU General Public License as published by
  9. # the Free Software Foundation, either version 3 of the License, or
  10. # (at your option) any later version.
  11. #
  12. # This program is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU General Public License
  18. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. #
  20. ##############################################################################
  21. import os
  22. import ConfigParser
  23. from lxml import etree
  24. from openerp.osv import fields, orm
  25. from openerp.tools.config import config as system_base_config
  26. from .system_info import get_server_environment
  27. from openerp.addons import server_environment_files
  28. _dir = os.path.dirname(server_environment_files.__file__)
  29. # Same dict as RawConfigParser._boolean_states
  30. _boolean_states = {'1': True, 'yes': True, 'true': True, 'on': True,
  31. '0': False, 'no': False, 'false': False, 'off': False}
  32. if not system_base_config.get('running_env', False):
  33. raise Exception(
  34. "The parameter 'running_env' has not be set neither in base config "
  35. "file option -c or in openerprc.\n"
  36. "We strongly recommend against using the rc file but instead use an "
  37. "explicit config file with this content:\n"
  38. "[options]\nrunning_env = dev"
  39. )
  40. ck_path = os.path.join(_dir, system_base_config['running_env'])
  41. if not os.path.exists(ck_path):
  42. raise Exception(
  43. "Provided server environment does not exist, "
  44. "please add a folder %s" % ck_path
  45. )
  46. def setboolean(obj, attr, _bool=None):
  47. """Replace the attribute with a boolean."""
  48. if _bool is None:
  49. _bool = _boolean_states
  50. res = _bool[getattr(obj, attr).lower()]
  51. setattr(obj, attr, res)
  52. return res
  53. # Borrowed from MarkupSafe
  54. def _escape(s):
  55. """Convert the characters &<>'" in string s to HTML-safe sequences."""
  56. return (str(s).replace('&', '&amp;')
  57. .replace('>', '&gt;')
  58. .replace('<', '&lt;')
  59. .replace("'", '&#39;')
  60. .replace('"', '&#34;'))
  61. def _listconf(env_path):
  62. """List configuration files in a folder."""
  63. files = [os.path.join(env_path, name)
  64. for name in sorted(os.listdir(env_path))
  65. if name.endswith('.conf')]
  66. return files
  67. def _load_config():
  68. """Load the configuration and return a ConfigParser instance."""
  69. default = os.path.join(_dir, 'default')
  70. running_env = os.path.join(_dir,
  71. system_base_config['running_env'])
  72. if os.path.isdir(default):
  73. conf_files = _listconf(default) + _listconf(running_env)
  74. else:
  75. conf_files = _listconf(running_env)
  76. config_p = ConfigParser.SafeConfigParser()
  77. # options are case-sensitive
  78. config_p.optionxform = str
  79. try:
  80. config_p.read(conf_files)
  81. except Exception, e:
  82. raise Exception('Cannot read config files "%s": %s' % (conf_files, e))
  83. return config_p
  84. serv_config = _load_config()
  85. class _Defaults(dict):
  86. __slots__ = ()
  87. def __setitem__(self, key, value):
  88. func = lambda *a: str(value)
  89. return dict.__setitem__(self, key, func)
  90. class ServerConfiguration(orm.TransientModel):
  91. """Display server configuration."""
  92. _name = 'server.config'
  93. _columns = {}
  94. _conf_defaults = _Defaults()
  95. def __init__(self, pool, cr):
  96. super(ServerConfiguration, self).__init__(pool, cr)
  97. self.running_env = system_base_config['running_env']
  98. # Only show passwords in development
  99. self.show_passwords = self.running_env in ('dev',)
  100. self._arch = None
  101. self._build_osv()
  102. def _group(self, items, prefix):
  103. """Return an XML chunk which represents a group of fields."""
  104. names = []
  105. for k, v in items:
  106. key = '%s\\%s' % (prefix, k)
  107. # Mask passwords
  108. if 'passw' in k and not self.show_passwords:
  109. v = '**********'
  110. self._columns[key] = fields.char(k, size=1024)
  111. self._conf_defaults[key] = v
  112. names.append(key)
  113. return ('<group col="2" colspan="4">' +
  114. ''.join(['<field name="%s" readonly="1"/>' %
  115. _escape(name) for name in names]) +
  116. '</group>')
  117. def _build_osv(self):
  118. """Build the view for the current configuration."""
  119. arch = ('<?xml version="1.0" encoding="utf-8"?>'
  120. '<form string="Configuration Form">'
  121. '<notebook colspan="4">')
  122. # OpenERP server configuration
  123. rcfile = system_base_config.rcfile
  124. items = sorted(system_base_config.options.items())
  125. arch += '<page string="OpenERP">'
  126. arch += '<separator string="%s" colspan="4"/>' % _escape(rcfile)
  127. arch += self._group(items, prefix='openerp')
  128. arch += '<separator colspan="4"/></page>'
  129. arch += '<page string="Environment based configurations">'
  130. for section in sorted(serv_config.sections()):
  131. items = sorted(serv_config.items(section))
  132. arch += '<separator string="[%s]" colspan="4"/>' % _escape(section)
  133. arch += self._group(items, prefix=section)
  134. arch += '<separator colspan="4"/></page>'
  135. # System information
  136. arch += '<page string="System">'
  137. arch += '<separator string="Server Environment" colspan="4"/>'
  138. arch += self._group(get_server_environment(), prefix='system')
  139. arch += '<separator colspan="4"/></page>'
  140. arch += '</notebook></form>'
  141. self._arch = etree.fromstring(arch)
  142. def fields_view_get(self, cr, uid, view_id=None, view_type='form',
  143. context=None, toolbar=False, submenu=False):
  144. """Overwrite the default method to render the custom view."""
  145. res = super(ServerConfiguration, self).fields_view_get(cr, uid,
  146. view_id,
  147. view_type,
  148. context,
  149. toolbar)
  150. if view_type == 'form':
  151. arch_node = self._arch
  152. xarch, xfields = self._view_look_dom_arch(cr, uid,
  153. arch_node,
  154. view_id,
  155. context=context)
  156. res['arch'] = xarch
  157. res['fields'] = xfields
  158. return res
  159. def default_get(self, cr, uid, fields_list, context=None):
  160. res = {}
  161. for key in self._conf_defaults:
  162. res[key] = self._conf_defaults[key]()
  163. return res