2014-12-02 12:54:01 +01:00
|
|
|
# This file is part of Tryton. The COPYRIGHT file at the top level of
|
|
|
|
|
# this repository contains the full copyright notices and license terms.
|
2009-09-01 14:43:49 +02:00
|
|
|
from trytond.model import ModelStorage
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class ModelSingleton(ModelStorage):
|
|
|
|
|
"""
|
|
|
|
|
Define a singleton model in Tryton.
|
|
|
|
|
"""
|
|
|
|
|
|
2012-09-24 22:16:53 +02:00
|
|
|
@classmethod
|
|
|
|
|
def get_singleton(cls):
|
2010-05-08 14:22:26 +02:00
|
|
|
'''
|
2012-09-24 22:16:53 +02:00
|
|
|
Return the instance of the unique record if there is one.
|
2010-05-08 14:22:26 +02:00
|
|
|
'''
|
2012-09-24 22:16:53 +02:00
|
|
|
singletons = super(ModelSingleton, cls).search([], limit=1)
|
|
|
|
|
if singletons:
|
|
|
|
|
return singletons[0]
|
2009-09-01 14:43:49 +02:00
|
|
|
|
2012-09-24 22:16:53 +02:00
|
|
|
@classmethod
|
2013-01-06 22:18:13 +01:00
|
|
|
def create(cls, vlist):
|
|
|
|
|
assert len(vlist) == 1
|
2012-09-24 22:16:53 +02:00
|
|
|
singleton = cls.get_singleton()
|
2013-01-06 22:18:13 +01:00
|
|
|
if not singleton:
|
|
|
|
|
return super(ModelSingleton, cls).create(vlist)
|
|
|
|
|
cls.write([singleton], vlist[0])
|
|
|
|
|
return [singleton]
|
2009-09-01 14:43:49 +02:00
|
|
|
|
2012-09-24 22:16:53 +02:00
|
|
|
@classmethod
|
|
|
|
|
def read(cls, ids, fields_names=None):
|
|
|
|
|
singleton = cls.get_singleton()
|
|
|
|
|
if not singleton:
|
2010-12-03 17:23:51 +01:00
|
|
|
if not fields_names:
|
2013-03-20 22:49:16 +01:00
|
|
|
fields_names = cls._fields.keys()
|
2013-10-05 18:04:44 +02:00
|
|
|
fname_no_rec_name = [f for f in fields_names if '.' not in f]
|
|
|
|
|
res = cls.default_get(fname_no_rec_name,
|
|
|
|
|
with_rec_name=len(fname_no_rec_name) != len(fields_names))
|
2010-12-03 17:23:51 +01:00
|
|
|
for field_name in fields_names:
|
|
|
|
|
if field_name not in res:
|
2012-12-16 23:54:41 +01:00
|
|
|
res[field_name] = None
|
2013-10-05 18:04:44 +02:00
|
|
|
for field_name in res.keys():
|
|
|
|
|
if field_name not in fields_names:
|
|
|
|
|
del res[field_name]
|
2012-09-24 22:16:53 +02:00
|
|
|
res['id'] = ids[0]
|
|
|
|
|
return [res]
|
|
|
|
|
res = super(ModelSingleton, cls).read([singleton.id],
|
|
|
|
|
fields_names=fields_names)
|
|
|
|
|
res[0]['id'] = ids[0]
|
2009-09-01 14:43:49 +02:00
|
|
|
return res
|
|
|
|
|
|
2012-09-24 22:16:53 +02:00
|
|
|
@classmethod
|
2013-12-19 14:44:01 +01:00
|
|
|
def write(cls, records, values, *args):
|
2012-09-24 22:16:53 +02:00
|
|
|
singleton = cls.get_singleton()
|
|
|
|
|
if not singleton:
|
2013-12-19 14:44:01 +01:00
|
|
|
singleton, = cls.create([values])
|
|
|
|
|
actions = (records, values) + args
|
|
|
|
|
args = []
|
|
|
|
|
for values in actions[1:None:2]:
|
|
|
|
|
args.extend(([singleton], values))
|
|
|
|
|
return super(ModelSingleton, cls).write(*args)
|
2009-09-01 14:43:49 +02:00
|
|
|
|
2012-09-24 22:16:53 +02:00
|
|
|
@classmethod
|
|
|
|
|
def delete(cls, records):
|
|
|
|
|
singleton = cls.get_singleton()
|
|
|
|
|
if not singleton:
|
|
|
|
|
return
|
|
|
|
|
return super(ModelSingleton, cls).delete([singleton])
|
2009-09-01 14:43:49 +02:00
|
|
|
|
2012-09-24 22:16:53 +02:00
|
|
|
@classmethod
|
|
|
|
|
def copy(cls, records, default=None):
|
2009-09-01 14:43:49 +02:00
|
|
|
if default:
|
2012-09-24 22:16:53 +02:00
|
|
|
cls.write(records, default)
|
|
|
|
|
return records
|
2009-09-01 14:43:49 +02:00
|
|
|
|
2012-09-24 22:16:53 +02:00
|
|
|
@classmethod
|
|
|
|
|
def search(cls, domain, offset=0, limit=None, order=None, count=False):
|
|
|
|
|
res = super(ModelSingleton, cls).search(domain, offset=offset,
|
2010-08-19 22:01:12 +02:00
|
|
|
limit=limit, order=order, count=count)
|
2015-06-15 10:19:01 +02:00
|
|
|
if not res and not domain:
|
2009-10-03 00:24:19 +02:00
|
|
|
if count:
|
|
|
|
|
return 1
|
2012-09-24 22:16:53 +02:00
|
|
|
return [cls(1)]
|
2009-09-01 14:43:49 +02:00
|
|
|
return res
|
|
|
|
|
|
2012-09-24 22:16:53 +02:00
|
|
|
@classmethod
|
2014-06-08 00:33:31 +02:00
|
|
|
def default_get(cls, fields_names, with_rec_name=True):
|
2011-04-20 14:27:52 +02:00
|
|
|
if '_timestamp' in fields_names:
|
|
|
|
|
fields_names = list(fields_names)
|
|
|
|
|
fields_names.remove('_timestamp')
|
2012-09-24 22:16:53 +02:00
|
|
|
default = super(ModelSingleton, cls).default_get(fields_names,
|
2014-06-08 00:33:31 +02:00
|
|
|
with_rec_name=with_rec_name)
|
2012-09-24 22:16:53 +02:00
|
|
|
singleton = cls.get_singleton()
|
|
|
|
|
if singleton:
|
2009-09-01 14:43:49 +02:00
|
|
|
if with_rec_name:
|
|
|
|
|
fields_names = fields_names[:]
|
|
|
|
|
for field in fields_names[:]:
|
2012-09-24 22:16:53 +02:00
|
|
|
if cls._fields[field]._type in ('many2one',):
|
2009-09-01 14:43:49 +02:00
|
|
|
fields_names.append(field + '.rec_name')
|
2012-09-24 22:16:53 +02:00
|
|
|
default, = cls.read([singleton.id], fields_names=fields_names)
|
|
|
|
|
for field in (x for x in default.keys() if x not in fields_names):
|
|
|
|
|
del default[field]
|
|
|
|
|
return default
|