From b23ac72dafb7f6dc1bd1a9b1b54e07286adf0332 Mon Sep 17 00:00:00 2001 From: Joao Alfredo Gama Batista Date: Wed, 26 Feb 2014 11:25:24 -0500 Subject: [PATCH] [FIX] Partial refactoring based on MP comments --- document_multiple_records/__init__.py | 2 +- document_multiple_records/document.py | 54 ++++++++++++++----- document_multiple_records/document_view.xml | 2 +- .../static/src/js/document.js | 2 +- .../wizard/document_wizard.py | 17 +++--- .../wizard/document_wizard_view.xml | 10 ++-- 6 files changed, 59 insertions(+), 28 deletions(-) diff --git a/document_multiple_records/__init__.py b/document_multiple_records/__init__.py index d44daea7..579abcbc 100644 --- a/document_multiple_records/__init__.py +++ b/document_multiple_records/__init__.py @@ -21,6 +21,6 @@ ############################################################################### from . import document -import wizard +from . import wizard # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: diff --git a/document_multiple_records/document.py b/document_multiple_records/document.py index 2fb2c5df..fdbd5f4f 100644 --- a/document_multiple_records/document.py +++ b/document_multiple_records/document.py @@ -27,25 +27,55 @@ class document_file(orm.Model): _inherit = 'ir.attachment' _columns = { - 'attachmentdocument_ids': fields.one2many('ir.attachment.document', 'attachment_id', 'Records'), + 'attachment_document_ids': fields.one2many('ir.attachment.document', + 'attachment_id', + 'Records'), } - def unlink(self, cr, uid, ids, context=None, check=True): + def create(self, cr, uid, data, context=None): + res = super(document_file, self).create(cr, uid, data, context=context) + if 'res_model' and 'res_id' in data: + ir_attachment_document_obj = self.pool.get('ir.attachment.document') + doc_data = { + 'attachment_id': res, + 'res_model': data['res_model'], + 'res_id': data['res_id'], + 'res_name': data.get('res_name') or self.pool.get(data['res_model']).browse(cr, uid, data['res_id'], context=context).name + } + + ir_attachment_document_obj.create(cr, uid, doc_data, + context=context) + + return res + + def unlink(self, cr, uid, ids, context=None): ir_attach_doc_obj = self.pool.get('ir.attachment.document') if context is None: context = {} - for line in self.browse(cr, uid, ids, context=context): - if line.attachmentdocument_ids: - # Get the first document - first_id = min(line.attachmentdocument_ids) - result = self.write(cr, uid, ids, {'res_id': first_id.res_id, - 'res_model': first_id.res_model, - 'res_name': first_id.res_name, }, context=context) - ir_attach_doc_obj.unlink(cr, uid, min(line.attachmentdocument_ids).id, context=context) + for line in self.browse(cr, uid, ids, context=context): + if line.attachment_document_ids and len(line.attachment_document_ids) > 1: + query = [ + ('attachment_id', '=', line.id), + ('res_model', '=', line.res_model), + ('res_id', '=', line.res_id) + ] + id_to_unlink = ir_attach_doc_obj.search( + cr, uid, query, context=context) + + data = { + 'res_id': False, + 'res_model': False, + 'res_name': False + } + self.write(cr, uid, ids, data, context=context) + ir_attach_doc_obj.unlink(cr, uid, id_to_unlink, + context=context) + else: - result = super(document_file, self).unlink(cr, uid, ids, context=context) - return result + super(document_file, self).unlink(cr, uid, line.id, + context=context) + return True class ir_attachment_document(orm.Model): diff --git a/document_multiple_records/document_view.xml b/document_multiple_records/document_view.xml index aefa125d..0b564086 100644 --- a/document_multiple_records/document_view.xml +++ b/document_multiple_records/document_view.xml @@ -9,7 +9,7 @@ - + diff --git a/document_multiple_records/static/src/js/document.js b/document_multiple_records/static/src/js/document.js index 0f2a7872..c12ca5d0 100644 --- a/document_multiple_records/static/src/js/document.js +++ b/document_multiple_records/static/src/js/document.js @@ -24,7 +24,7 @@ var _t = instance.web._t, var action = { name: _t("Add existing document/attachment"), type: 'ir.actions.act_window', - res_model: 'ir.attachment.wizard', + res_model: 'ir.attachment.existing.doc', view_mode: 'form', view_type: 'form', views: [[false, 'form']], diff --git a/document_multiple_records/wizard/document_wizard.py b/document_multiple_records/wizard/document_wizard.py index 58d10e83..ebc5dcda 100644 --- a/document_multiple_records/wizard/document_wizard.py +++ b/document_multiple_records/wizard/document_wizard.py @@ -25,12 +25,14 @@ from openerp.tools.translate import _ class document_wizard(orm.Model): - _name = "ir.attachment.wizard" - _description = "Attachment wizard" + _name = "ir.attachment.existing.doc" + _description = "Add existing document/attachment wizard" _columns = { 'attachment_ids': fields.many2many('ir.attachment', 'document_attachment_rel', - 'wizard_id', 'attachment_id', 'Attachments'), + 'wizard_id', + 'attachment_id', + 'Attachments'), } def action_apply(self, cr, uid, ids, context=None): @@ -38,17 +40,17 @@ class document_wizard(orm.Model): context = {} ir_attach_obj = self.pool.get('ir.attachment') ir_attach_doc_obj = self.pool.get('ir.attachment.document') - ir_model_obj = self.pool.get(context['model']) + ir_model_obj = self.pool.get(context.get('model') or context.get('active_model')) - name = ir_model_obj.browse(cr, uid, context['ids'], context=context)[0]['name'] + name = ir_model_obj.browse(cr, uid, context.get('ids') or context.get('active_ids'), context=context)[0]['name'] data = self.read(cr, uid, ids, [], context=context)[0] if not data['attachment_ids']: raise orm.except_orm(_('Error'), _('You have to select at least 1 Document. And try again')) for attach in ir_attach_obj.browse(cr, uid, data['attachment_ids'], context=context): data_attach = { - 'res_model': context['model'], - 'res_id': context['ids'][0], + 'res_model': context.get('model') or context.get('active_model'), + 'res_id': context.get('ids') and context.get('ids')[0] or context.get('active_id'), 'res_name': name, 'attachment_id': attach.id, } @@ -61,4 +63,3 @@ class document_wizard(orm.Model): return {'type': 'ir.actions.act_window_close'} # vim:expandtab:smartindent:toabstop=4:softtabstop=4:shiftwidth=4: - diff --git a/document_multiple_records/wizard/document_wizard_view.xml b/document_multiple_records/wizard/document_wizard_view.xml index 9af0d333..9e4bb836 100644 --- a/document_multiple_records/wizard/document_wizard_view.xml +++ b/document_multiple_records/wizard/document_wizard_view.xml @@ -3,10 +3,10 @@ - Add Document - ir.attachment.wizard + Add existing document/attachment + ir.attachment.existing.doc -
+ @@ -28,8 +28,8 @@ - Add Document - ir.attachment.wizard + Add existing document/attachment + ir.attachment.existing.doc form tree,form