From 61bdde4eee17d81c5a1fc77c5d6eb5d0f3fd3c43 Mon Sep 17 00:00:00 2001 From: EL HADJI DEM Date: Thu, 29 May 2014 18:12:47 -0400 Subject: [PATCH] [IMP] Download document from DMS --- cmis_write/__openerp__.py | 4 +- cmis_write/ir_attachment.py | 98 +++++++++++++++++++++++++++- cmis_write/static/src/js/document.js | 42 ++++++++++++ 3 files changed, 140 insertions(+), 4 deletions(-) create mode 100644 cmis_write/static/src/js/document.js diff --git a/cmis_write/__openerp__.py b/cmis_write/__openerp__.py index 6f0dc20c..d1e6ec1f 100644 --- a/cmis_write/__openerp__.py +++ b/cmis_write/__openerp__.py @@ -75,7 +75,9 @@ Contributors 'metadata_view.xml', 'security/ir.model.access.csv', ], - 'js': [], + 'js': [ + 'static/src/js/document.js' + ], 'qweb': [], 'test': [], 'demo': [], diff --git a/cmis_write/ir_attachment.py b/cmis_write/ir_attachment.py index 22531e8d..8426c96b 100644 --- a/cmis_write/ir_attachment.py +++ b/cmis_write/ir_attachment.py @@ -24,16 +24,30 @@ from openerp.osv import orm, fields from openerp.addons.connector.session import ConnectorSession from openerp.addons.connector.queue.job import job import base64 +from openerp import SUPERUSER_ID +from openerp.tools.translate import _ import logging _logger = logging.getLogger(__name__) -class ir_attachment(orm.Model): - _inherit = 'ir.attachment' +class ir_attachment_download(orm.TransientModel): + _name = 'ir.attachment.download' _columns = { - 'id_dms': fields.char('Id of Dms', size=256, help="Id of Dms."), + 'name': fields.char('Attachment Name', size=256, required=True, + help='Attachment Name'), + 'datas': fields.binary('File', readonly=True), + 'type': fields.char('Type', size=256, help='Type'), + 'file_type': fields.char('Content Type', help='Content Type'), + 'attachment_id': fields.many2one('ir.attachment', 'Attachment'), } + _defaults = { + 'type': 'binary', + } + + +class ir_attachment(orm.Model): + _inherit = 'ir.attachment' def create(self, cr, uid, values, context=None): metadata_obj = self.pool.get('metadata') @@ -77,6 +91,84 @@ class ir_attachment(orm.Model): user_login) return res + def action_download(self, cr, uid, ids, context=None): + if context is None: + context = {} + cmis_backend_obj = self.pool.get('cmis.backend') + # login with the cmis account + repo = cmis_backend_obj._auth(cr, uid, context=context) + cmis_backend_rec = self.read( + cr, uid, ids, ['id_dms'], context=context)[0] + id_dms = cmis_backend_rec['id_dms'] + # Get results from id of document + results = repo.query(" SELECT * FROM cmis:document WHERE \ + cmis:objectId ='" + id_dms + "'") + datas = results[0].getContentStream().read().encode('base64') + return datas + + def _data_set(self, cr, uid, id, name, value, arg, context=None): + # We dont handle setting data to null + if not value: + return True + if context is None: + context = {} + location = self.pool.get('ir.config_parameter').get_param( + cr, uid, 'ir_attachment.location') + file_size = len(value.decode('base64')) + if location: + attach = self.browse(cr, uid, id, context=context) + if attach.store_fname: + self._file_delete(cr, uid, location, attach.store_fname) + fname = self._file_write(cr, uid, location, value) + # SUPERUSER_ID as probably don't have write access, + # trigger during create + super(ir_attachment, self).write( + cr, SUPERUSER_ID, [id], + {'store_fname': fname, 'file_size': file_size}, + context=context) + else: + super(ir_attachment, self).write( + cr, SUPERUSER_ID, [id], + {'db_datas': value, 'file_size': file_size}, context=context) + return True + + def _data_get(self, cr, uid, ids, name, arg, context=None): + if context is None: + context = {} + result = {} + location = self.pool.get('ir.config_parameter').get_param( + cr, uid, 'ir_attachment.location') + bin_size = context.get('bin_size') + for attach in self.browse(cr, uid, ids, context=context): + if location and attach.store_fname: + result[attach.id] = self._file_read( + cr, uid, location, attach.store_fname, bin_size) + elif attach.id_dms: + datas = self.action_download( + cr, uid, attach.id, context=context) + result[attach.id] = datas + file_type, index_content = self._index( + cr, uid, datas.decode('base64'), attach.datas_fname, None) + self.write( + cr, uid, [attach.id], + {'file_type': file_type, 'index_content': index_content}, + context=context) + else: + raise orm.except_orm(_('Access error of document'), + _("Document is not available in DMS; " + "Please try again")) + return result + + _columns = { + 'id_dms': fields.char('Id of Dms', size=256, help="Id of Dms."), + 'download_id': fields.one2many('ir.attachment.download', + 'attachment_id', + 'Attachment download'), + 'datas': fields.function(_data_get, fnct_inv=_data_set, + string='File Content', + type="binary", nodrop=True), + } + @job def create_doc_in_edm(session, model_name, value, res, diff --git a/cmis_write/static/src/js/document.js b/cmis_write/static/src/js/document.js new file mode 100644 index 00000000..524594a9 --- /dev/null +++ b/cmis_write/static/src/js/document.js @@ -0,0 +1,42 @@ +openerp.cmis_write = function(instance, m) { +var _t = instance.web._t, + QWeb = instance.web.qweb; + + instance.web.Sidebar.include({ + start: function() { + var self = this; + this._super(this); + this.redraw(); + this.$el.on('click','.oe_dropdown_menu li a', function(event) { + var section = $(this).data('section'); + var index = $(this).data('index'); + var item = self.items[section][index]; + if (item.callback) { + item.callback.apply(self, [item]); + } else if (item.action) { + self.on_item_action_clicked(item); + } else if (!item.id_dms) { + alert(_t("Document is not available in DMS.Please try again !!!")); + } else if (item.url) { + return true; + } + event.preventDefault(); + }); + }, + 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); + } + if (!model_id) { + this.on_attachments_loaded([]); + } else { + var dom = [ ['res_model', '=', dataset.model], ['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', 'id_dms','type', 'create_uid', 'create_date', 'write_uid', 'write_date'], {}).done(this.on_attachments_loaded); + } + }, + }); +};