From 2889c10a505ddfcf7bfa02ec69ca00e44bec7623 Mon Sep 17 00:00:00 2001 From: EL HADJI DEM Date: Tue, 18 Mar 2014 14:50:56 -0400 Subject: [PATCH] [IMP] Refresh automatically list of documents in the form view;from knownlege, delete the documents and references; when delete document from form view, juste delete the reference; informations about the model are saved in the attachment_document_ids --- document_multiple_records/__openerp__.py | 9 ++- document_multiple_records/document.py | 62 +++++++++---------- .../security/ir.model.access.csv | 2 + .../static/src/js/document.js | 50 +++++++++++---- .../static/src/xml/document.xml | 2 +- .../wizard/document_wizard.py | 6 +- 6 files changed, 79 insertions(+), 52 deletions(-) create mode 100644 document_multiple_records/security/ir.model.access.csv diff --git a/document_multiple_records/__openerp__.py b/document_multiple_records/__openerp__.py index f882fb38..1118ff48 100644 --- a/document_multiple_records/__openerp__.py +++ b/document_multiple_records/__openerp__.py @@ -41,10 +41,15 @@ Contributors ], 'data': [ 'document_view.xml', + 'security/ir.model.access.csv', 'wizard/document_wizard_view.xml', ], - 'js': ['static/src/js/document.js'], - 'qweb': ['static/src/xml/document.xml'], + 'js': [ + 'static/src/js/document.js' + ], + 'qweb': [ + 'static/src/xml/document.xml' + ], 'test': [], 'demo': [ ], diff --git a/document_multiple_records/document.py b/document_multiple_records/document.py index fdbd5f4f..48ebb228 100644 --- a/document_multiple_records/document.py +++ b/document_multiple_records/document.py @@ -33,49 +33,47 @@ class document_file(orm.Model): } def create(self, cr, uid, data, context=None): + ir_attachment_document_obj = self.pool.get('ir.attachment.document') + original_data = {key: data[key] for key in data.keys()} + # Don't save this information below + data['res_model'] = '' + data['res_id'] = '' + data['res_name'] = '' 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') + # Create attachment_document_ids with res_model, res_id and res_name + if 'res_model' and 'res_id' in original_data: 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 + 'res_model': original_data['res_model'], + 'res_id': original_data['res_id'], + 'res_name': original_data.get('res_name') + or self.pool.get(original_data['res_model']).browse(cr, uid, + original_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): + def unlink(self, cr, uid, ids, context=None, check=True): 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.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: - super(document_file, self).unlink(cr, uid, line.id, - context=context) - return True + # Deleting from dropdown list in the form view + if context.get('res_model') and context.get('res_id'): + for line in self.browse(cr, uid, ids, context=context): + if line.attachment_document_ids: + query = [ + ('res_model', '=', context.get('res_model')), + ('res_id', '=', context.get('res_id')), + ('attachment_id', '=', ids), + ] + id_to_unlink = ir_attach_doc_obj.search(cr, uid, query, context=context) + result = ir_attach_doc_obj.unlink(cr, uid, id_to_unlink, context=context) + else: + # Normal delete + result = super(document_file, self).unlink(cr, uid, ids, context=context) + return result class ir_attachment_document(orm.Model): diff --git a/document_multiple_records/security/ir.model.access.csv b/document_multiple_records/security/ir.model.access.csv new file mode 100644 index 00000000..a6d92b57 --- /dev/null +++ b/document_multiple_records/security/ir.model.access.csv @@ -0,0 +1,2 @@ +id,name,model_id:id,group_id:id,perm_read,perm_write,perm_create,perm_unlink +access_ir_attachment_document_group_user,ir.attachment.document user,model_ir_attachment_document,base.group_document_user,1,1,1,1 diff --git a/document_multiple_records/static/src/js/document.js b/document_multiple_records/static/src/js/document.js index 07c61b66..7eafe28a 100644 --- a/document_multiple_records/static/src/js/document.js +++ b/document_multiple_records/static/src/js/document.js @@ -17,12 +17,12 @@ var _t = instance.web._t, var ids = ( view.fields_view.type != "form" )? view.groups.get_selection().ids : [ view.datarecord.id ]; // you can pass in other data using the context dictionary variable var context = { - 'model': view.dataset.model, - 'ids': ids, + 'model': view.dataset.model, + 'ids': ids, }; // the action dictionary variable sends data in the "self.do_action" method var action = { - name: _t("Add existing document/attachment"), + name: _t("Add existing document"), type: 'ir.actions.act_window', res_model: 'ir.attachment.existing.doc', view_mode: 'form', @@ -32,22 +32,48 @@ var _t = instance.web._t, context: context, }; // self.do_action accepts the action parameter and opens the new view - self.do_action(action); + self.do_action(action, { + // refresh list of documents + on_close: function () { + self.do_attachement_update(self.dataset, self.model_id); + } + }); + }, - do_attachement_update: function(dataset, model_id, args) { + on_attachment_delete: function(e) { + e.preventDefault(); + e.stopPropagation(); + var self = this; + var view = self.getParent(); + self.model_view = view.dataset.model + var ids = ( view.fields_view.type != "form" )? view.groups.get_selection().ids : [ view.datarecord.id ]; + // Context dictionary variable + var context = { + 'res_model': self.model_view, + 'res_id': ids[0], + }; + var $e = $(e.currentTarget); + if (confirm(_t("Do you really want to delete this attachment ?"))) { + (new instance.web.DataSet(this, 'ir.attachment', context)).unlink([parseInt($e.attr('data-id'), 10)]).done(function() { + self.do_attachement_update(self.dataset, self.model_id); + }); + } + }, + do_attachement_update: function(dataset, model_id, args) { var self = this; this.dataset = dataset; this.model_id = model_id; if (args && args[0].error) { - this.do_warn(_t('Uploading Error'), args[0].error); + this.do_warn(_t('Uploading Error'), args[0].error); } if (!model_id) { - 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); - ds.read_slice(['name', 'url', 'type', 'create_uid', 'create_date', 'write_uid', 'write_date'], {}).done(this.on_attachments_loaded); + 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); + ds.read_slice(['name', 'url', 'type', 'create_uid', 'create_date', 'write_uid', 'write_date'], {}).done(this.on_attachments_loaded); + } + } }); }; diff --git a/document_multiple_records/static/src/xml/document.xml b/document_multiple_records/static/src/xml/document.xml index fc36ab7a..da9c6987 100644 --- a/document_multiple_records/static/src/xml/document.xml +++ b/document_multiple_records/static/src/xml/document.xml @@ -4,7 +4,7 @@ -
  • Add existing document/attachment...
  • +
  • Add existing document...
  • diff --git a/document_multiple_records/wizard/document_wizard.py b/document_multiple_records/wizard/document_wizard.py index ebc5dcda..68c4e7bb 100644 --- a/document_multiple_records/wizard/document_wizard.py +++ b/document_multiple_records/wizard/document_wizard.py @@ -55,11 +55,7 @@ class document_wizard(orm.Model): 'attachment_id': attach.id, } #Created attachment_document_ids - if attach.res_model: - ir_attach_doc_obj.create(cr, uid, data_attach, context=context) - # Updated attachment line - else: - ir_attach_obj.write(cr, uid, [attach.id], data_attach, context=context) + ir_attach_doc_obj.create(cr, uid, data_attach, context=context) return {'type': 'ir.actions.act_window_close'} # vim:expandtab:smartindent:toabstop=4:softtabstop=4:shiftwidth=4: