[MERGE]: Merge with latest trunk-server
[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 from osv import osv,fields
26 import uuid
27 import datetime
28 from tools import misc, config
29
30 """
31 A dictionary holding some configuration parameters to be initialized when the database is created.
32 """
33 _default_parameters = {
34     "database.uuid": lambda: str(uuid.uuid1()),
35     "database.create_date": lambda: datetime.datetime.now().strftime(misc.DEFAULT_SERVER_DATETIME_FORMAT),
36     "web.base.url": lambda: "http://localhost:%s" % config.get('xmlrpc_port'),
37 }
38
39 class ir_config_parameter(osv.osv):
40     """Per-database storage of configuration key-value pairs."""
41
42     _name = 'ir.config_parameter'
43
44     _columns = {
45         'key': fields.char('Key', size=256, required=True, select=1),
46         'value': fields.text('Value', required=True),
47     }
48
49     _sql_constraints = [
50         ('key_uniq', 'unique (key)', 'Key must be unique.')
51     ]
52
53     def init(self, cr):
54         """
55         Initializes the parameters listed in _default_parameters.
56         """
57         for key, func in _default_parameters.iteritems():
58             ids = self.search(cr, 1, [('key','=',key)])
59             if not ids:
60                 self.set_param(cr, 1, key, func())
61
62     def get_param(self, cr, uid, key, default=False, context=None):
63         """Retrieve the value for a given key.
64
65         :param string key: The key of the parameter value to retrieve.
66         :param string default: default value if parameter is missing.
67         :return: The value of the parameter, or ``default`` if it does not exist.
68         :rtype: string
69         """
70         ids = self.search(cr, uid, [('key','=',key)], context=context)
71         if not ids:
72             return default
73         param = self.browse(cr, uid, ids[0], context=context)
74         value = param.value
75         return value
76     
77     def set_param(self, cr, uid, key, value, context=None):
78         """Sets the value of a parameter.
79         
80         :param string key: The key of the parameter value to set.
81         :param string value: The value to set.
82         :return: the previous value of the parameter or False if it did
83                  not exist.
84         :rtype: string
85         """
86         ids = self.search(cr, uid, [('key','=',key)], context=context)
87         if ids:
88             param = self.browse(cr, uid, ids[0], context=context)
89             old = param.value
90             self.write(cr, uid, ids, {'value': value}, context=context)
91             return old
92         else:
93             self.create(cr, uid, {'key': key, 'value': value}, context=context)
94             return False
95
96 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: