mirror of
https://github.com/OCA/knowledge.git
synced 2025-07-22 20:12:04 -06:00
[UPD]Change the filter and add the context
Add a context to keep the model and model_id. Change the domain filter with the new field [ADD] Add a new field function This new function allows to get the related documents with the model and the model_id. We add this new field because the domain in the view or in the js doesn't match the wanted result [UPD] Add missing comma [ADD] Add unittests
This commit is contained in:
parent
930c8d7fc9
commit
9366ebf798
@ -51,6 +51,29 @@ class document_file(orm.Model):
|
||||
data[attachment.id] = False
|
||||
return data
|
||||
|
||||
def _get_related_model_documents(
|
||||
self, cr, uid, ids, field_name, args, context=None):
|
||||
""" This function allows to get only related documents with a specific
|
||||
model and model_id.
|
||||
"""
|
||||
context = context or {}
|
||||
ir_attachment_doc_obj = self.pool.get('ir.attachment.document')
|
||||
res = [('id', 'in', [])]
|
||||
# Format query with the res_model and res_id to search
|
||||
# in ir.attachment.document model.
|
||||
query = [
|
||||
('res_model', '=', context.get('model')),
|
||||
('res_id', '=', context.get('model_id'))]
|
||||
ir_attachment_doc_ids = ir_attachment_doc_obj.search(
|
||||
cr, uid, query, context=context)
|
||||
|
||||
ir_attachment_ids = self.search(
|
||||
cr, uid, [
|
||||
('attachment_document_ids.id', 'in', ir_attachment_doc_ids)],
|
||||
context=context)
|
||||
res = [('id', 'in', ir_attachment_ids)]
|
||||
return res
|
||||
|
||||
_columns = {
|
||||
'attachment_document_ids': fields.one2many('ir.attachment.document',
|
||||
'attachment_id',
|
||||
@ -58,6 +81,9 @@ class document_file(orm.Model):
|
||||
'res_name': fields.function(_name_get_resname, type='char',
|
||||
size=128, string='Resource Name',
|
||||
store=True),
|
||||
'related_document': fields.function(
|
||||
lambda self, *a, **kw: True, type='boolean',
|
||||
fnct_search=_get_related_model_documents),
|
||||
}
|
||||
|
||||
def create(self, cr, uid, data, context=None):
|
||||
|
@ -63,6 +63,11 @@ var _t = instance.web._t,
|
||||
var self = this;
|
||||
this.dataset = dataset;
|
||||
this.model_id = model_id;
|
||||
// Add the model and the model_id in the context
|
||||
var context = {
|
||||
'model': dataset.model,
|
||||
'model_id': model_id
|
||||
};
|
||||
if (args && args[0].error) {
|
||||
this.do_warn(_t('Uploading Error'), args[0].error);
|
||||
}
|
||||
@ -70,8 +75,8 @@ var _t = instance.web._t,
|
||||
this.on_attachments_loaded([]);
|
||||
}
|
||||
else {
|
||||
var dom = [ ['attachment_document_ids.res_model', '=', dataset.model], ['attachment_document_ids.res_id', '=', model_id], ['type', 'in', ['binary', 'url']] ];
|
||||
var ds = new instance.web.DataSetSearch(this, 'ir.attachment', dataset.get_context(), dom);
|
||||
var dom = [ ['related_document', '=', true] ];
|
||||
var ds = new instance.web.DataSetSearch(this, 'ir.attachment', context, dom);
|
||||
ds.read_slice(['name', 'url', 'type', 'create_uid', 'create_date', 'write_uid', 'write_date'], {}).done(this.on_attachments_loaded);
|
||||
}
|
||||
}
|
||||
|
29
document_multiple_records/tests/__init__.py
Normal file
29
document_multiple_records/tests/__init__.py
Normal file
@ -0,0 +1,29 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
##############################################################################
|
||||
#
|
||||
# OpenERP, Open Source Management Solution
|
||||
# Copyright (C) 2015 Savoir-faire Linux (<http://www.savoirfairelinux.com>).
|
||||
#
|
||||
# This program is free software: you can redistribute it and/or modify
|
||||
# it under the terms of the GNU Affero General Public License as
|
||||
# published by the Free Software Foundation, either version 3 of the
|
||||
# License, or (at your option) any later version.
|
||||
#
|
||||
# This program is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU Affero General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU Affero General Public License
|
||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
#
|
||||
##############################################################################
|
||||
|
||||
from . import (
|
||||
test_ir_attachment,
|
||||
)
|
||||
|
||||
checks = [
|
||||
test_ir_attachment,
|
||||
]
|
116
document_multiple_records/tests/test_ir_attachment.py
Normal file
116
document_multiple_records/tests/test_ir_attachment.py
Normal file
@ -0,0 +1,116 @@
|
||||
# -*- encoding: utf-8 -*-
|
||||
###############################################################################
|
||||
#
|
||||
# OpenERP, Open Source Management Solution
|
||||
# Copyright (C) 2015 Savoir-faire Linux (<http://www.savoirfairelinux.com>)
|
||||
#
|
||||
# This program is free software: you can redistribute it and/or modify
|
||||
# it under the terms of the GNU Affero General Public License as
|
||||
# published by the Free Software Foundation, either version 3 of the
|
||||
# License, or (at your option) any later version.
|
||||
#
|
||||
# This program is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU Affero General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU Affero General Public License
|
||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
#
|
||||
###############################################################################
|
||||
|
||||
from openerp.tests.common import TransactionCase
|
||||
|
||||
|
||||
class test_ir_attachment(TransactionCase):
|
||||
|
||||
def setUp(self):
|
||||
super(test_ir_attachment, self).setUp()
|
||||
|
||||
# Get registries
|
||||
self.ira_model = self.registry('ir.attachment')
|
||||
self.user_model = self.registry("res.users")
|
||||
self.partner_model = self.registry("res.partner")
|
||||
self.context = self.user_model.context_get(self.cr, self.uid)
|
||||
|
||||
# Create first partner
|
||||
self.partner_id_1 = self.partner_model.create(self.cr, self.uid, {
|
||||
'name': 'Test first partner'
|
||||
}, context=self.context)
|
||||
|
||||
# Create second partner
|
||||
self.partner_id_2 = self.partner_model.create(self.cr, self.uid, {
|
||||
'name': 'Test second partner'
|
||||
}, context=self.context)
|
||||
|
||||
blob1 = 'blob1'
|
||||
blob1_b64 = blob1.encode('base64')
|
||||
|
||||
self.vals = {
|
||||
'name': 'Test documemt',
|
||||
'datas': blob1_b64,
|
||||
'res_model': 'res.partner',
|
||||
'res_id': self.partner_id_1,
|
||||
'res_name': 'Test first partner',
|
||||
'attachment_document_ids': [(0, 0, {
|
||||
'res_model': 'res.partner',
|
||||
'res_id': self.partner_id_1,
|
||||
'res_name': 'Test first partner',
|
||||
}),
|
||||
(0, 0, {
|
||||
'res_model': 'res.partner',
|
||||
'res_id': self.partner_id_2,
|
||||
'res_name': 'Test second partner',
|
||||
})
|
||||
]
|
||||
}
|
||||
|
||||
def test_create_document(self):
|
||||
"""Test create document and check each value in vals variable """
|
||||
cr, uid, vals, context = self.cr, self.uid, self.vals, self.context
|
||||
ira_id = self.ira_model.create(cr, uid, vals, context=context)
|
||||
ira_obj = self.ira_model.browse(cr, uid, ira_id, context=context)
|
||||
self.assertEqual(ira_obj.name, vals['name'])
|
||||
self.assertEqual(ira_obj.res_id, vals['res_id'])
|
||||
self.assertEqual(ira_obj.res_name, vals['res_name'])
|
||||
self.assertEqual(ira_obj.res_model, vals['res_model'])
|
||||
self.assertEqual(
|
||||
ira_obj.attachment_document_ids[0].res_id, self.partner_id_1)
|
||||
self.assertEqual(
|
||||
ira_obj.attachment_document_ids[0].res_name, 'Test first partner')
|
||||
self.assertEqual(
|
||||
ira_obj.attachment_document_ids[0].res_model, 'res.partner')
|
||||
|
||||
def test_unlink_document(self):
|
||||
"""Test normal unlink document """
|
||||
cr, uid, vals = self.cr, self.uid, self.vals.copy()
|
||||
context = self.context
|
||||
ira_id = self.ira_model.create(cr, uid, vals, context=context)
|
||||
self.ira_model.unlink(cr, uid, ira_id, context=context)
|
||||
|
||||
def test_unlink_document_drop_down(self):
|
||||
"""Test unlink from drop-down list in the form view """
|
||||
cr, uid, vals = self.cr, self.uid, self.vals.copy()
|
||||
context = self.context
|
||||
context['multiple_records_res_model'] = 'res.partner'
|
||||
context['multiple_records_res_id'] = self.partner_id_1
|
||||
ira_id = self.ira_model.create(cr, uid, vals, context=context)
|
||||
self.ira_model.unlink(cr, uid, [ira_id], context=context)
|
||||
|
||||
def test_name_get_resname(self):
|
||||
"""Test _name_get_resname function """
|
||||
cr, uid, vals, context = self.cr, self.uid, self.vals, self.context
|
||||
ira_id = self.ira_model.create(cr, uid, vals, context=context)
|
||||
self.assertEquals(self.ira_model._name_get_resname(
|
||||
cr, uid, ira_id, obj=None, method=None,
|
||||
context=context)[ira_id], vals['res_name'])
|
||||
|
||||
def test_get_related_model_documents(self):
|
||||
cr, uid, vals = self.cr, self.uid, self.vals.copy()
|
||||
context = self.context
|
||||
context['model'] = 'res.partner'
|
||||
context['model_id'] = self.partner_id_1
|
||||
ira_id = self.ira_model.create(cr, uid, vals, context=context)
|
||||
res = self.ira_model._get_related_model_documents(
|
||||
cr, uid, ira_id, field_name=None, args=None, context=context)
|
||||
self.assertEqual(res[0], ('id', 'in', [ira_id]))
|
Loading…
Reference in New Issue
Block a user