[MERGE] Forward-port saas-4 up to 5ceded9
[odoo/odoo.git] / openerp / addons / base / ir / ir_config_parameter.py
1 # -*- coding: utf-8 -*-
2 ##############################################################################
3 #
4 #    OpenERP, Open Source Management Solution
5 #    Copyright (C) 2011 OpenERP SA (<http://www.openerp.com>).
6 #
7 #    This program is free software: you can redistribute it and/or modify
8 #    it under the terms of the GNU Affero General Public License as
9 #    published by the Free Software Foundation, either version 3 of the
10 #    License, or (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 Affero General Public License for more details.
16 #
17 #    You should have received a copy of the GNU Affero General Public License
18 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 #
20 ##############################################################################
21 """
22 Store database-specific configuration parameters
23 """
24
25 import uuid
26 import datetime
27
28 from openerp import SUPERUSER_ID
29 from openerp.osv import osv, fields
30 from openerp.tools import misc, config
31
32 """
33 A dictionary holding some configuration parameters to be initialized when the database is created.
34 """
35 _default_parameters = {
36     "database.uuid": lambda: str(uuid.uuid1()),
37     "database.create_date": lambda: datetime.datetime.now().strftime(misc.DEFAULT_SERVER_DATETIME_FORMAT),
38     "web.base.url": lambda: "http://localhost:%s" % config.get('xmlrpc_port'),
39 }
40
41 class ir_config_parameter(osv.osv):
42     """Per-database storage of configuration key-value pairs."""
43
44     _name = 'ir.config_parameter'
45     _rec_name = 'key'
46
47     _columns = {
48         'key': fields.char('Key', size=256, required=True, select=1),
49         'value': fields.text('Value', required=True),
50     }
51
52     _sql_constraints = [
53         ('key_uniq', 'unique (key)', 'Key must be unique.')
54     ]
55
56     def init(self, cr, force=False):
57         """
58         Initializes the parameters listed in _default_parameters.
59         It overrides existing parameters if force is ``True``.
60         """
61         for key, func in _default_parameters.iteritems():
62             # force=True skips search and always performs the 'if' body (because ids=False)
63             ids = not force and self.search(cr, SUPERUSER_ID, [('key','=',key)])
64             if not ids:
65                 self.set_param(cr, SUPERUSER_ID, key, func())
66
67     def get_param(self, cr, uid, key, default=False, context=None):
68         """Retrieve the value for a given key.
69
70         :param string key: The key of the parameter value to retrieve.
71         :param string default: default value if parameter is missing.
72         :return: The value of the parameter, or ``default`` if it does not exist.
73         :rtype: string
74         """
75         ids = self.search(cr, uid, [('key','=',key)], context=context)
76         if not ids:
77             return default
78         param = self.browse(cr, uid, ids[0], context=context)
79         value = param.value
80         return value
81     
82     def set_param(self, cr, uid, key, value, context=None):
83         """Sets the value of a parameter.
84         
85         :param string key: The key of the parameter value to set.
86         :param string value: The value to set.
87         :return: the previous value of the parameter or False if it did
88                  not exist.
89         :rtype: string
90         """
91         ids = self.search(cr, uid, [('key','=',key)], context=context)
92         if ids:
93             param = self.browse(cr, uid, ids[0], context=context)
94             old = param.value
95             self.write(cr, uid, ids, {'value': value}, context=context)
96             return old
97         else:
98             self.create(cr, uid, {'key': key, 'value': value}, context=context)
99             return False
100
101 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: