[ADD] implement forgotten DataSet.get RPC method, test it a bit
bzr revid: xmo@openerp.com-20110323120806-uhas8xvxnpsa1dm2
This commit is contained in:
parent
65c8aaf78b
commit
801fae18ca
|
@ -176,6 +176,9 @@ class DataSet(openerpweb.Controller):
|
|||
domain, context, sort)
|
||||
def do_find(self, request, model, fields=False, offset=0, limit=False,
|
||||
domain=None, context=None, sort=None):
|
||||
""" Performs a search() followed by a read() (if needed) using the
|
||||
provided search criteria, only
|
||||
"""
|
||||
Model = request.session.model(model)
|
||||
ids = Model.search(domain or [], offset or 0, limit or False,
|
||||
sort or False, context or False)
|
||||
|
@ -184,6 +187,30 @@ class DataSet(openerpweb.Controller):
|
|||
return map(lambda id: {'id': id}, ids)
|
||||
return Model.read(ids, fields or False)
|
||||
|
||||
@openerpweb.jsonrequest
|
||||
def get(self, request, model, ids):
|
||||
self.do_get(request, model, ids)
|
||||
|
||||
def do_get(self, request, model, ids):
|
||||
""" Fetches and returns the records of the model ``model`` whose ids
|
||||
are in ``ids``.
|
||||
|
||||
The results are in the same order as the inputs, but elements may be
|
||||
missing (if there is no record left for the id)
|
||||
|
||||
:param request: the JSON-RPC2 request object
|
||||
:type request: openerpweb.JsonRequest
|
||||
:param model: the model to read from
|
||||
:type model: str
|
||||
:param ids: a list of identifiers
|
||||
:type ids: list
|
||||
"""
|
||||
Model = request.session.model(model)
|
||||
records = Model.read(ids)
|
||||
|
||||
record_map = dict((record['id'], record) for record in records)
|
||||
|
||||
return [record_map[id] for id in ids if record_map.get(id)]
|
||||
|
||||
class DataRecord(openerpweb.Controller):
|
||||
_cp_path = "/base/datarecord"
|
||||
|
|
|
@ -183,7 +183,8 @@ openerp.base.DataSet = openerp.base.Controller.extend({
|
|||
*/
|
||||
active_ids: function () {
|
||||
this.rpc('/base/dataset/get', {
|
||||
ids: this.get_active_ids()
|
||||
ids: this.get_active_ids(),
|
||||
model: this.model
|
||||
}, _.bind(function (records) {
|
||||
this.on_active_ids(_.map(
|
||||
records, function (record) {
|
||||
|
@ -210,7 +211,8 @@ openerp.base.DataSet = openerp.base.Controller.extend({
|
|||
*/
|
||||
active_id: function () {
|
||||
this.rpc('/base/dataset/get', {
|
||||
ids: [this.get_active_id()]
|
||||
ids: [this.get_active_id()],
|
||||
model: this.model
|
||||
}, _.bind(function (records) {
|
||||
var record = records[0];
|
||||
this.on_active_id(
|
||||
|
|
|
@ -35,3 +35,42 @@ class TestDataSetController(unittest2.TestCase):
|
|||
self.dataset.do_find(self.request, 'fake.model', ['id']),
|
||||
[{'id': 1}, {'id': 2}, {'id': 3}])
|
||||
self.assertFalse(self.read.called)
|
||||
|
||||
def test_get(self):
|
||||
self.read.return_value = [
|
||||
{'id': 1, 'name': 'baz'},
|
||||
{'id': 3, 'name': 'foo'},
|
||||
{'id': 2, 'name': 'bar'}
|
||||
]
|
||||
|
||||
result = self.dataset.do_get(
|
||||
self.request, 'fake.model', [3, 2, 1])
|
||||
self.read.assert_called_once_with(
|
||||
[3, 2, 1])
|
||||
self.assertFalse(self.search.called)
|
||||
|
||||
self.assertEqual(
|
||||
result,
|
||||
[
|
||||
{'id': 3, 'name': 'foo'},
|
||||
{'id': 2, 'name': 'bar'},
|
||||
{'id': 1, 'name': 'baz'}
|
||||
]
|
||||
)
|
||||
|
||||
def test_get_missing_result(self):
|
||||
self.read.return_value = [
|
||||
{'id': 1, 'name': 'baz'},
|
||||
{'id': 2, 'name': 'bar'}
|
||||
]
|
||||
|
||||
result = self.dataset.do_get(
|
||||
self.request, 'fake.model', [3, 2, 1])
|
||||
|
||||
self.assertEqual(
|
||||
result,
|
||||
[
|
||||
{'id': 2, 'name': 'bar'},
|
||||
{'id': 1, 'name': 'baz'}
|
||||
]
|
||||
)
|
||||
|
|
Loading…
Reference in New Issue