From 88665fbcdcf31467e27258a3af6435aac265c582 Mon Sep 17 00:00:00 2001 From: EL HADJI DEM Date: Fri, 21 Mar 2014 18:41:38 -0400 Subject: [PATCH 1/9] [ADD] add cmis_read module;It allows to allows you to use the CMIS backend to search in the DMS repository --- cmis_read/__init__.py | 25 ++++ cmis_read/__openerp__.py | 74 +++++++++++ cmis_read/i18n/cmis_read.pot | 119 +++++++++++++++++ cmis_read/security/ir.model.access.csv | 2 + cmis_read/static/src/js/document.js | 44 +++++++ cmis_read/static/src/xml/document.xml | 10 ++ cmis_read/wizard/__init__.py | 25 ++++ cmis_read/wizard/document_wizard.py | 152 ++++++++++++++++++++++ cmis_read/wizard/document_wizard_view.xml | 34 +++++ 9 files changed, 485 insertions(+) create mode 100644 cmis_read/__init__.py create mode 100644 cmis_read/__openerp__.py create mode 100644 cmis_read/i18n/cmis_read.pot create mode 100644 cmis_read/security/ir.model.access.csv create mode 100644 cmis_read/static/src/js/document.js create mode 100644 cmis_read/static/src/xml/document.xml create mode 100644 cmis_read/wizard/__init__.py create mode 100644 cmis_read/wizard/document_wizard.py create mode 100644 cmis_read/wizard/document_wizard_view.xml diff --git a/cmis_read/__init__.py b/cmis_read/__init__.py new file mode 100644 index 00000000..44b55c29 --- /dev/null +++ b/cmis_read/__init__.py @@ -0,0 +1,25 @@ +# -*- encoding: utf-8 -*- +############################################################################## +# +# OpenERP, Open Source Management Solution +# This module copyright (C) 2014 Savoir-faire Linux +# (). +# +# 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 . +# +############################################################################## + +from . import wizard + +# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: diff --git a/cmis_read/__openerp__.py b/cmis_read/__openerp__.py new file mode 100644 index 00000000..9ff4e2a9 --- /dev/null +++ b/cmis_read/__openerp__.py @@ -0,0 +1,74 @@ +# -*- encoding: utf-8 -*- +############################################################################## +# +# OpenERP, Open Source Management Solution +# This module copyright (C) 2014 Savoir-faire Linux +# (). +# +# 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 . +# +############################################################################## + +{ + 'name': 'CMIS Read', + 'version': '0.1', + 'category': 'Knowledge Management', + 'summary': 'Store Document File in a Remote CMIS Server', + 'description': """ +This module allows you to use the CMIS backend to search in the DMS repository +and attach documents to OpenERP records. + +Configuration +============= + +Create a new CMIS backend with the host, login and password. + +Usage +===== + +* On one OpenERP record, click "Add from DMS". +* Type your query and then click on "Search". +* Filter your results if necessary +* Select the documents you want to attach +* Selected documents will be enqueued for importing + +Contributors +------------ +* El Hadji Dem (elhadji.dem@savoirfairelinux.com) +""", + 'author': 'Savoir-faire Linux', + 'website': 'www.savoirfairelinux.com', + 'license': 'AGPL-3', + 'depends': [ + 'document', + 'cmis' + ], + 'data': [ + 'security/ir.model.access.csv', + 'wizard/document_wizard_view.xml', + ], + 'js': [ + 'static/src/js/document.js' + ], + 'qweb': [ + 'static/src/xml/document.xml' + ], + 'test': [], + 'demo': [ + ], + 'installable': True, + 'auto_install': False, +} + +# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: diff --git a/cmis_read/i18n/cmis_read.pot b/cmis_read/i18n/cmis_read.pot new file mode 100644 index 00000000..84d3e6be --- /dev/null +++ b/cmis_read/i18n/cmis_read.pot @@ -0,0 +1,119 @@ +# Translation of OpenERP Server. +# This file contains the translation of the following modules: +# * cmis_read +# +msgid "" +msgstr "" +"Project-Id-Version: OpenERP Server 7.0\n" +"Report-Msgid-Bugs-To: \n" +"POT-Creation-Date: 2014-03-21 14:55+0000\n" +"PO-Revision-Date: 2014-03-21 10:55-0500\n" +"Last-Translator: EL Hadji DEM \n" +"Language-Team: \n" +"MIME-Version: 1.0\n" +"Content-Type: text/plain; charset=UTF-8\n" +"Content-Transfer-Encoding: 8bit\n" +"Plural-Forms: \n" +"X-Generator: Poedit 1.5.4\n" + +#. module: cmis_read +#: field:ir.attachment.dms,file_id:0 +msgid "File ID" +msgstr "" + +#. module: cmis_read +#: field:ir.attachment.dms.wizard,attachment_ids:0 +msgid "Attachments" +msgstr "" + +#. module: cmis_read +#: view:ir.attachment.dms.wizard:0 +msgid "Search Document" +msgstr "" + +#. module: cmis_read +#: field:ir.attachment.dms,name:0 help:ir.attachment.dms,name:0 +#: field:ir.attachment.dms.wizard,name:0 help:ir.attachment.dms.wizard,name:0 +msgid "File name" +msgstr "" + +#. module: cmis_read +#: code:addons/cmis_read/wizard/document_wizard.py:59 +#, python-format +msgid "You have to fill in the file name. And try again" +msgstr "" + +#. module: cmis_read +#: help:ir.attachment.dms,file_id:0 +msgid "File Id" +msgstr "" + +#. module: cmis_read +#: model:_description:0 model:ir.model,name:cmis_read.model_ir_attachment_dms +msgid "ir.attachment.dms" +msgstr "" + +#. module: cmis_read +#: field:ir.attachment.dms,owner:0 help:ir.attachment.dms,owner:0 +msgid "Owner" +msgstr "" + +#. module: cmis_read +#: view:ir.attachment.dms.wizard:0 +msgid "" +"This action allows you to search by file name and to add the document you " +"select" +msgstr "" + +#. module: cmis_read +#. openerp-web +#: code:addons/cmis_read/static/src/js/document.js:26 +#, python-format +msgid "Search Document from DMS" +msgstr "" + +#. module: cmis_read +#. openerp-web +#: code:addons/cmis_read/static/src/xml/document.xml:7 +#, python-format +msgid "Add Doc from DMS..." +msgstr "" + +#. module: cmis_read +#: code:addons/cmis_read/wizard/document_wizard.py:58 +#: code:addons/cmis_read/wizard/document_wizard.py:86 +#, python-format +msgid "Error" +msgstr "" + +#. module: cmis_read +#: view:ir.attachment.dms.wizard:0 +msgid "Cancel" +msgstr "" + +#. module: cmis_read +#: view:ir.attachment.dms.wizard:0 +msgid "Apply" +msgstr "" + +#. module: cmis_read +#: model:_description:0 +#: model:ir.model,name:cmis_read.model_ir_attachment_dms_wizard +msgid "ir.attachment.dms.wizard" +msgstr "" + +#. module: cmis_read +#: view:ir.attachment.dms.wizard:0 +msgid "Search" +msgstr "" + +#. module: cmis_read +#: view:ir.attachment.dms.wizard:0 +msgid "or" +msgstr "" + +#. module: cmis_read +#: code:addons/cmis_read/wizard/document_wizard.py:87 +#, python-format +msgid "You have to select at least 1 Document. And try again" +msgstr "" diff --git a/cmis_read/security/ir.model.access.csv b/cmis_read/security/ir.model.access.csv new file mode 100644 index 00000000..d08023be --- /dev/null +++ b/cmis_read/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_dms_group_user,ir.attachment.dms user,model_ir_attachment_dms,base.group_document_user,1,1,1,1 diff --git a/cmis_read/static/src/js/document.js b/cmis_read/static/src/js/document.js new file mode 100644 index 00000000..9765a44e --- /dev/null +++ b/cmis_read/static/src/js/document.js @@ -0,0 +1,44 @@ +openerp.cmis_read = function(instance, m) { +var _t = instance.web._t, + QWeb = instance.web.qweb; + + instance.web.Sidebar.include({ + redraw: function() { + var self = this; + this._super.apply(this, arguments); + self.$el.find('.oe_sidebar_add_attachment').after(QWeb.render('AddDocfromdms', {widget: self})) + self.$el.find('.oe_sidebar_add_dms_doc').on('click', function (e) { + self.on_cmis_doc(); + }); + }, + on_cmis_doc: function(state) { + var self = this; + var view = self.getParent(); + var ids = ( view.fields_view.type != "form" )? view.groups.get_selection().ids : [ view.datarecord.id ]; + var ds = new instance.web.DataSet(this, 'ir.attachment', context); + // you can pass in other data using the context dictionary variable + var context = { + 'model': view.dataset.model, + 'ids': ids, + }; + // the action dictionary variable sends data in the "self.do_action" method + var action = { + name: _t("Search Document from DMS"), + type: 'ir.actions.act_window', + res_model: 'ir.attachment.dms.wizard', + view_mode: 'form', + view_type: 'form', + views: [[false, 'form']], + target: 'new', + context: context, + }; + // self.do_action accepts the action parameter and opens the new view + self.do_action(action, { + // refresh list of documents + on_close: function () { + self.do_attachement_update(self.dataset, self.model_id); + } + }); + } + }); +}; diff --git a/cmis_read/static/src/xml/document.xml b/cmis_read/static/src/xml/document.xml new file mode 100644 index 00000000..6ce1df0a --- /dev/null +++ b/cmis_read/static/src/xml/document.xml @@ -0,0 +1,10 @@ + + + + + +
  • Add Doc from DMS...
  • +
    + +
    diff --git a/cmis_read/wizard/__init__.py b/cmis_read/wizard/__init__.py new file mode 100644 index 00000000..108a1a20 --- /dev/null +++ b/cmis_read/wizard/__init__.py @@ -0,0 +1,25 @@ +# -*- encoding: utf-8 -*- +############################################################################## +# +# OpenERP, Open Source Management Solution +# This module copyright (C) 2014 Savoir-faire Linux +# (). +# +# 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 . +# +############################################################################## + +from . import document_wizard + +# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: diff --git a/cmis_read/wizard/document_wizard.py b/cmis_read/wizard/document_wizard.py new file mode 100644 index 00000000..4af2b563 --- /dev/null +++ b/cmis_read/wizard/document_wizard.py @@ -0,0 +1,152 @@ +# -*- encoding: utf-8 -*- +############################################################################## +# +# OpenERP, Open Source Management Solution +# This module copyright (C) 2014 Savoir-faire Linux +# (). +# +# 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 . +# +############################################################################## + +from openerp.osv import orm, fields, osv +from openerp.tools.translate import _ +from openerp.addons.connector.session import ConnectorSession +from openerp.addons.connector.queue.job import job +import logging +_logger = logging.getLogger(__name__) + + +class ir_attachment_dms(osv.TransientModel): + _name = 'ir.attachment.dms' + + _columns = { + 'name': fields.char('File name', size=150, readonly=True, help="File name"), + 'owner': fields.char('Owner', size=150, readonly=True, help="Owner"), + 'file_id': fields.char('File ID', size=150, readonly=True, help="File Id"), + } + + +class ir_attachment_edm_wizard(orm.Model): + _name = 'ir.attachment.dms.wizard' + + _columns = { + 'name': fields.char('File name', size=150, help="File name"), + 'attachment_ids': fields.many2many('ir.attachment.dms', + 'document_attachment_dms_rel', + 'wizard_id', 'attachment_id', 'Attachments'), + } + + # Search documents from dms. + def search_doc(self, cr, uid, ids, context=None): + if context is None: + context = {} + this = self.browse(cr, uid, ids, context=context)[0] + data = self.read(cr, uid, ids, [], context=context)[0] + if not data['name']: + raise orm.except_orm(_('Error'), + _('You have to fill in the file name. And try again')) + if not hasattr(ids, '__iter__'): + ids = [ids] + session = ConnectorSession(cr, uid, context=context) + file_name = data['name'] + for backend_id in ids: + search_doc_from_dms(session, 'ir.attachment', backend_id, file_name) + return { + 'type': 'ir.actions.act_window', + 'res_model': 'ir.attachment.dms.wizard', + 'view_mode': 'form', + 'view_type': 'form', + 'res_id': this.id, + 'views': [(False, 'form')], + 'target': 'new', + } + + # Adding documents from Document Management (EDM) to OE. + def action_apply(self, cr, uid, ids, context=None): + if context is None: + context = {} + model = context['model'] + res_id = context['ids'][0] + ir_model_obj = self.pool.get(context['model']) + name = ir_model_obj.browse(cr, uid, context['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')) + if not hasattr(ids, '__iter__'): + ids = [ids] + session = ConnectorSession(cr, uid, context=context) + file_name = data['name'] + for backend_id in ids: + # Create doc in OE from DMS. + create_doc_from_dms.delay(session, 'ir.attachment', backend_id, data, name, model, res_id) + return {'type': 'ir.actions.act_window_close'} + + +def search_doc_from_dms(session, model_name, backend_id, file_name): + ir_attach_dms_obj = session.pool.get('ir.attachment.dms') + cmis_backend_obj = session.pool.get('cmis.backend') + if session.context is None: + session.context = {} + #login with the cmis account + client = cmis_backend_obj._auth(session.cr, session.uid, context=session.context) + repo = client.getDefaultRepository() + + # Search name of doc and delete it if the document is already existed + attachment_ids = ir_attach_dms_obj.search(session.cr, session.uid, []) + ir_attach_dms_obj.unlink(session.cr, session.uid, attachment_ids, context=session.context) + # Get results from name of document + results = repo.query("SELECT cmis:name', cmis:createdBy, cmis:objectId FROM cmis:document WHERE cmis:name LIKE '%" + file_name + "%'") + for result in results: + info = result.getProperties() + if info['cmis:contentStreamLength'] != 0: + data_attach = { + 'name': info['cmis:name'], + 'owner': info['cmis:createdBy'], + 'file_id': info['cmis:objectId'], + } + ir_attach_dms_obj.create(session.cr, session.uid, data_attach, context=session.context) + + +@job +def create_doc_from_dms(session, model_name, backend_id, data, name, model, res_id, filters=None): + ir_attach_obj = session.pool.get('ir.attachment') + ir_attach_dms_obj = session.pool.get('ir.attachment.dms') + cmis_backend_obj = session.pool.get('cmis.backend') + if session.context is None: + session.context = {} + #login with the cmis account + client = cmis_backend_obj._auth(session.cr, session.uid, context=session.context) + repo = client.getDefaultRepository() + + for attach in ir_attach_dms_obj.browse(session.cr, session.uid, data['attachment_ids'], + context=session.context): + # Get results from id of document + results = repo.query("SELECT * FROM cmis:document WHERE cmis:objectId ='" + attach.file_id + "'") + for result in results: + info = result.getProperties() + data_attach = { + 'name': info['cmis:name'], + 'type': 'binary', + 'datas': result.getContentStream().read().encode("base64"), + 'res_model': model, + 'res_name': name, + 'res_id': session.context['ids'][0], + 'user_id': session.uid, + } + ir_attach_obj.create(session.cr, session.uid, data_attach, context=session.context) + return True + +# vim:expandtab:smartindent:toabstop=4:softtabstop=4:shiftwidth=4: diff --git a/cmis_read/wizard/document_wizard_view.xml b/cmis_read/wizard/document_wizard_view.xml new file mode 100644 index 00000000..d44b883a --- /dev/null +++ b/cmis_read/wizard/document_wizard_view.xml @@ -0,0 +1,34 @@ + + + + + + Search Document + ir.attachment.dms.wizard + +
    +
    +
    + +
    +
    From 6bceb30aa9ae09fab8694be727f619b770c1e4f9 Mon Sep 17 00:00:00 2001 From: EL HADJI DEM Date: Wed, 16 Apr 2014 17:19:35 -0400 Subject: [PATCH 2/9] [IMP] fix pep8 error, add control when reading from DMS --- cmis_read/wizard/document_wizard.py | 60 ++++++++++++++++++++--------- 1 file changed, 41 insertions(+), 19 deletions(-) diff --git a/cmis_read/wizard/document_wizard.py b/cmis_read/wizard/document_wizard.py index 4af2b563..c037080e 100644 --- a/cmis_read/wizard/document_wizard.py +++ b/cmis_read/wizard/document_wizard.py @@ -32,9 +32,15 @@ class ir_attachment_dms(osv.TransientModel): _name = 'ir.attachment.dms' _columns = { - 'name': fields.char('File name', size=150, readonly=True, help="File name"), - 'owner': fields.char('Owner', size=150, readonly=True, help="Owner"), - 'file_id': fields.char('File ID', size=150, readonly=True, help="File Id"), + 'name': fields.char('File name', size=150, + readonly=True, + help="File name"), + 'owner': fields.char('Owner', size=150, + readonly=True, + help="Owner"), + 'file_id': fields.char('File ID', size=150, + readonly=True, + help="File Id"), } @@ -45,7 +51,8 @@ class ir_attachment_edm_wizard(orm.Model): 'name': fields.char('File name', size=150, help="File name"), 'attachment_ids': fields.many2many('ir.attachment.dms', 'document_attachment_dms_rel', - 'wizard_id', 'attachment_id', 'Attachments'), + 'wizard_id', 'attachment_id', + 'Attachments'), } # Search documents from dms. @@ -56,13 +63,15 @@ class ir_attachment_edm_wizard(orm.Model): data = self.read(cr, uid, ids, [], context=context)[0] if not data['name']: raise orm.except_orm(_('Error'), - _('You have to fill in the file name. And try again')) + _('You have to fill in the file name.' + + 'And try again')) if not hasattr(ids, '__iter__'): ids = [ids] session = ConnectorSession(cr, uid, context=context) file_name = data['name'] for backend_id in ids: - search_doc_from_dms(session, 'ir.attachment', backend_id, file_name) + search_doc_from_dms(session, 'ir.attachment', + backend_id, file_name) return { 'type': 'ir.actions.act_window', 'res_model': 'ir.attachment.dms.wizard', @@ -80,18 +89,21 @@ class ir_attachment_edm_wizard(orm.Model): model = context['model'] res_id = context['ids'][0] ir_model_obj = self.pool.get(context['model']) - name = ir_model_obj.browse(cr, uid, context['ids'], context=context)[0]['name'] + name = ir_model_obj.browse(cr, uid, context['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')) + _('You have to select at least 1 Document.' + + 'And try again')) if not hasattr(ids, '__iter__'): ids = [ids] session = ConnectorSession(cr, uid, context=context) file_name = data['name'] for backend_id in ids: # Create doc in OE from DMS. - create_doc_from_dms.delay(session, 'ir.attachment', backend_id, data, name, model, res_id) + create_doc_from_dms.delay(session, 'ir.attachment', backend_id, + data, name, model, res_id) return {'type': 'ir.actions.act_window_close'} @@ -101,14 +113,18 @@ def search_doc_from_dms(session, model_name, backend_id, file_name): if session.context is None: session.context = {} #login with the cmis account - client = cmis_backend_obj._auth(session.cr, session.uid, context=session.context) + client = cmis_backend_obj._auth(session.cr, session.uid, + context=session.context) repo = client.getDefaultRepository() # Search name of doc and delete it if the document is already existed attachment_ids = ir_attach_dms_obj.search(session.cr, session.uid, []) - ir_attach_dms_obj.unlink(session.cr, session.uid, attachment_ids, context=session.context) + ir_attach_dms_obj.unlink(session.cr, session.uid, + attachment_ids, context=session.context) # Get results from name of document - results = repo.query("SELECT cmis:name', cmis:createdBy, cmis:objectId FROM cmis:document WHERE cmis:name LIKE '%" + file_name + "%'") + results = repo.query(" SELECT cmis:name, cmis:createdBy, cmis:objectId, \ + cmis:contentStreamLength FROM cmis:document \ + WHERE cmis:name LIKE '%" + file_name + "%'") for result in results: info = result.getProperties() if info['cmis:contentStreamLength'] != 0: @@ -117,36 +133,42 @@ def search_doc_from_dms(session, model_name, backend_id, file_name): 'owner': info['cmis:createdBy'], 'file_id': info['cmis:objectId'], } - ir_attach_dms_obj.create(session.cr, session.uid, data_attach, context=session.context) + ir_attach_dms_obj.create(session.cr, session.uid, data_attach, + context=session.context) @job -def create_doc_from_dms(session, model_name, backend_id, data, name, model, res_id, filters=None): +def create_doc_from_dms(session, model_name, backend_id, data, name, + model, res_id, filters=None): ir_attach_obj = session.pool.get('ir.attachment') ir_attach_dms_obj = session.pool.get('ir.attachment.dms') cmis_backend_obj = session.pool.get('cmis.backend') if session.context is None: session.context = {} #login with the cmis account - client = cmis_backend_obj._auth(session.cr, session.uid, context=session.context) + client = cmis_backend_obj._auth(session.cr, session.uid, + context=session.context) repo = client.getDefaultRepository() - for attach in ir_attach_dms_obj.browse(session.cr, session.uid, data['attachment_ids'], + for attach in ir_attach_dms_obj.browse(session.cr, session.uid, + data['attachment_ids'], context=session.context): # Get results from id of document - results = repo.query("SELECT * FROM cmis:document WHERE cmis:objectId ='" + attach.file_id + "'") + results = repo.query(" SELECT * FROM cmis:document WHERE \ + cmis:objectId ='" + attach.file_id + "'") for result in results: info = result.getProperties() data_attach = { 'name': info['cmis:name'], 'type': 'binary', - 'datas': result.getContentStream().read().encode("base64"), + 'datas': result.getContentStream().read().encode('base64'), 'res_model': model, 'res_name': name, 'res_id': session.context['ids'][0], 'user_id': session.uid, } - ir_attach_obj.create(session.cr, session.uid, data_attach, context=session.context) + ir_attach_obj.create(session.cr, session.uid, + data_attach, context=session.context) return True # vim:expandtab:smartindent:toabstop=4:softtabstop=4:shiftwidth=4: From b4978b509e4d1f2258cdd66adc85f09616a251e9 Mon Sep 17 00:00:00 2001 From: EL HADJI DEM Date: Mon, 5 May 2014 18:02:49 -0400 Subject: [PATCH 3/9] [IMP] Take comments from LP --- cmis_read/security/ir.model.access.csv | 2 +- cmis_read/wizard/document_wizard.py | 32 ++++++++++++-------------- 2 files changed, 16 insertions(+), 18 deletions(-) diff --git a/cmis_read/security/ir.model.access.csv b/cmis_read/security/ir.model.access.csv index d08023be..ac92b9a2 100644 --- a/cmis_read/security/ir.model.access.csv +++ b/cmis_read/security/ir.model.access.csv @@ -1,2 +1,2 @@ id,name,model_id:id,group_id:id,perm_read,perm_write,perm_create,perm_unlink -access_ir_attachment_dms_group_user,ir.attachment.dms user,model_ir_attachment_dms,base.group_document_user,1,1,1,1 +access_ir_attachment_dms_wizard,access_ir_attachment_dms_wizard,model_ir_attachment_dms_wizard,base.group_document_user,1,1,1,1 diff --git a/cmis_read/wizard/document_wizard.py b/cmis_read/wizard/document_wizard.py index c037080e..1f331d61 100644 --- a/cmis_read/wizard/document_wizard.py +++ b/cmis_read/wizard/document_wizard.py @@ -20,7 +20,7 @@ # ############################################################################## -from openerp.osv import orm, fields, osv +from openerp.osv import orm, fields from openerp.tools.translate import _ from openerp.addons.connector.session import ConnectorSession from openerp.addons.connector.queue.job import job @@ -28,7 +28,7 @@ import logging _logger = logging.getLogger(__name__) -class ir_attachment_dms(osv.TransientModel): +class ir_attachment_dms(orm.TransientModel): _name = 'ir.attachment.dms' _columns = { @@ -99,11 +99,10 @@ class ir_attachment_edm_wizard(orm.Model): if not hasattr(ids, '__iter__'): ids = [ids] session = ConnectorSession(cr, uid, context=context) - file_name = data['name'] for backend_id in ids: # Create doc in OE from DMS. create_doc_from_dms.delay(session, 'ir.attachment', backend_id, - data, name, model, res_id) + data, name, model, res_id, uid) return {'type': 'ir.actions.act_window_close'} @@ -112,12 +111,11 @@ def search_doc_from_dms(session, model_name, backend_id, file_name): cmis_backend_obj = session.pool.get('cmis.backend') if session.context is None: session.context = {} - #login with the cmis account - client = cmis_backend_obj._auth(session.cr, session.uid, - context=session.context) - repo = client.getDefaultRepository() + # login with the cmis account + repo = cmis_backend_obj._auth(session.cr, session.uid, + context=session.context) - # Search name of doc and delete it if the document is already existed + # Search name of doc and delete it if the document is already existed attachment_ids = ir_attach_dms_obj.search(session.cr, session.uid, []) ir_attach_dms_obj.unlink(session.cr, session.uid, attachment_ids, context=session.context) @@ -139,17 +137,15 @@ def search_doc_from_dms(session, model_name, backend_id, file_name): @job def create_doc_from_dms(session, model_name, backend_id, data, name, - model, res_id, filters=None): + model, res_id, uid, filters=None): ir_attach_obj = session.pool.get('ir.attachment') ir_attach_dms_obj = session.pool.get('ir.attachment.dms') cmis_backend_obj = session.pool.get('cmis.backend') if session.context is None: session.context = {} - #login with the cmis account - client = cmis_backend_obj._auth(session.cr, session.uid, - context=session.context) - repo = client.getDefaultRepository() - + # login with the cmis account + repo = cmis_backend_obj._auth( + session.cr, session.uid, context=session.context) for attach in ir_attach_dms_obj.browse(session.cr, session.uid, data['attachment_ids'], context=session.context): @@ -160,13 +156,15 @@ def create_doc_from_dms(session, model_name, backend_id, data, name, info = result.getProperties() data_attach = { 'name': info['cmis:name'], + 'description': info['cmis:description'], 'type': 'binary', 'datas': result.getContentStream().read().encode('base64'), 'res_model': model, 'res_name': name, - 'res_id': session.context['ids'][0], - 'user_id': session.uid, + 'res_id': res_id, + 'user_id': uid, } + session.context['bool_read_doc'] = True ir_attach_obj.create(session.cr, session.uid, data_attach, context=session.context) return True From eda65f7451a4bd134850e169c8c03e77b57bb7b9 Mon Sep 17 00:00:00 2001 From: EL HADJI DEM Date: Tue, 13 May 2014 15:32:13 -0400 Subject: [PATCH 4/9] [fixed comments from LP, for uniitests, we have to install Alfresco,] --- cmis_read/wizard/document_wizard.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/cmis_read/wizard/document_wizard.py b/cmis_read/wizard/document_wizard.py index 1f331d61..a2d9d47a 100644 --- a/cmis_read/wizard/document_wizard.py +++ b/cmis_read/wizard/document_wizard.py @@ -119,10 +119,12 @@ def search_doc_from_dms(session, model_name, backend_id, file_name): attachment_ids = ir_attach_dms_obj.search(session.cr, session.uid, []) ir_attach_dms_obj.unlink(session.cr, session.uid, attachment_ids, context=session.context) + # Escape the name for characters not supported in filenames + file_name = file_name.replace('/', '_') # Get results from name of document - results = repo.query(" SELECT cmis:name, cmis:createdBy, cmis:objectId, \ - cmis:contentStreamLength FROM cmis:document \ - WHERE cmis:name LIKE '%" + file_name + "%'") + results = repo.query(" SELECT cmis:name, cmis:createdBy, cmis:objectId, " + "cmis:contentStreamLength FROM cmis:document " + "WHERE cmis:name LIKE '%" + file_name + "%'") for result in results: info = result.getProperties() if info['cmis:contentStreamLength'] != 0: From 37f63bbdafe630283c0d1a2c9e2802b0c379e66d Mon Sep 17 00:00:00 2001 From: EL HADJI DEM Date: Tue, 13 May 2014 17:59:02 -0400 Subject: [PATCH 5/9] [IMP] fix bug when we read document in OE --- cmis_read/wizard/document_wizard.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/cmis_read/wizard/document_wizard.py b/cmis_read/wizard/document_wizard.py index a2d9d47a..249c86c3 100644 --- a/cmis_read/wizard/document_wizard.py +++ b/cmis_read/wizard/document_wizard.py @@ -166,7 +166,8 @@ def create_doc_from_dms(session, model_name, backend_id, data, name, 'res_id': res_id, 'user_id': uid, } - session.context['bool_read_doc'] = True + # Don't create doc again in DMS + session.context['bool_testdoc'] = True ir_attach_obj.create(session.cr, session.uid, data_attach, context=session.context) return True From 5d83f783c29d7f36e8e65a386dad3a7d6e1cd82c Mon Sep 17 00:00:00 2001 From: EL HADJI DEM Date: Thu, 15 May 2014 16:37:16 -0400 Subject: [PATCH 6/9] [IMP] Add constraint for avoidind SQL Injection --- cmis_read/wizard/document_wizard.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/cmis_read/wizard/document_wizard.py b/cmis_read/wizard/document_wizard.py index 249c86c3..d4e331b8 100644 --- a/cmis_read/wizard/document_wizard.py +++ b/cmis_read/wizard/document_wizard.py @@ -120,7 +120,10 @@ def search_doc_from_dms(session, model_name, backend_id, file_name): ir_attach_dms_obj.unlink(session.cr, session.uid, attachment_ids, context=session.context) # Escape the name for characters not supported in filenames - file_name = file_name.replace('/', '_') + # for avoiding SQL Injection + file_name = file_name.replace("'", "\\'") + file_name = file_name.replace("%", "\%") + file_name = file_name.replace("_", "\_") # Get results from name of document results = repo.query(" SELECT cmis:name, cmis:createdBy, cmis:objectId, " "cmis:contentStreamLength FROM cmis:document " @@ -166,8 +169,7 @@ def create_doc_from_dms(session, model_name, backend_id, data, name, 'res_id': res_id, 'user_id': uid, } - # Don't create doc again in DMS - session.context['bool_testdoc'] = True + session.context['bool_read_doc'] = True ir_attach_obj.create(session.cr, session.uid, data_attach, context=session.context) return True From 5f34a5029974a2875863c6c11cb170b43142ff0c Mon Sep 17 00:00:00 2001 From: EL HADJI DEM Date: Thu, 15 May 2014 16:57:09 -0400 Subject: [PATCH 7/9] [IMP] Added a function to sanitize filename --- cmis_read/wizard/document_wizard.py | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/cmis_read/wizard/document_wizard.py b/cmis_read/wizard/document_wizard.py index d4e331b8..de7a4a40 100644 --- a/cmis_read/wizard/document_wizard.py +++ b/cmis_read/wizard/document_wizard.py @@ -106,6 +106,15 @@ class ir_attachment_edm_wizard(orm.Model): return {'type': 'ir.actions.act_window_close'} +def sanitize_input_filename_field(file_name): + # Escape the name for characters not supported in filenames + # for avoiding SQL Injection + file_name = file_name.replace("'", "\\'") + file_name = file_name.replace("%", "\%") + file_name = file_name.replace("_", "\_") + return file_name + + def search_doc_from_dms(session, model_name, backend_id, file_name): ir_attach_dms_obj = session.pool.get('ir.attachment.dms') cmis_backend_obj = session.pool.get('cmis.backend') @@ -120,10 +129,7 @@ def search_doc_from_dms(session, model_name, backend_id, file_name): ir_attach_dms_obj.unlink(session.cr, session.uid, attachment_ids, context=session.context) # Escape the name for characters not supported in filenames - # for avoiding SQL Injection - file_name = file_name.replace("'", "\\'") - file_name = file_name.replace("%", "\%") - file_name = file_name.replace("_", "\_") + file_name = sanitize_input_filename_field(file_name) # Get results from name of document results = repo.query(" SELECT cmis:name, cmis:createdBy, cmis:objectId, " "cmis:contentStreamLength FROM cmis:document " From 997ab32e2d87ec4a4797793cd3365b764fbd02bd Mon Sep 17 00:00:00 2001 From: EL HADJI DEM Date: Tue, 27 May 2014 15:26:52 -0400 Subject: [PATCH 8/9] [IMP] Change the view where we search document from DMS and print the results below the search field --- cmis_read/wizard/document_wizard.py | 64 +++++++++++++--------- cmis_read/wizard/document_wizard_view.xml | 65 ++++++++++++++--------- 2 files changed, 77 insertions(+), 52 deletions(-) diff --git a/cmis_read/wizard/document_wizard.py b/cmis_read/wizard/document_wizard.py index de7a4a40..6ba9c997 100644 --- a/cmis_read/wizard/document_wizard.py +++ b/cmis_read/wizard/document_wizard.py @@ -24,35 +24,18 @@ from openerp.osv import orm, fields from openerp.tools.translate import _ from openerp.addons.connector.session import ConnectorSession from openerp.addons.connector.queue.job import job +from openerp import SUPERUSER_ID import logging _logger = logging.getLogger(__name__) -class ir_attachment_dms(orm.TransientModel): - _name = 'ir.attachment.dms' - - _columns = { - 'name': fields.char('File name', size=150, - readonly=True, - help="File name"), - 'owner': fields.char('Owner', size=150, - readonly=True, - help="Owner"), - 'file_id': fields.char('File ID', size=150, - readonly=True, - help="File Id"), - } - - class ir_attachment_edm_wizard(orm.Model): _name = 'ir.attachment.dms.wizard' _columns = { 'name': fields.char('File name', size=150, help="File name"), - 'attachment_ids': fields.many2many('ir.attachment.dms', - 'document_attachment_dms_rel', - 'wizard_id', 'attachment_id', - 'Attachments'), + 'attachment_ids': fields.one2many('ir.attachment.dms', + 'wizard_id'), } # Search documents from dms. @@ -71,7 +54,7 @@ class ir_attachment_edm_wizard(orm.Model): file_name = data['name'] for backend_id in ids: search_doc_from_dms(session, 'ir.attachment', - backend_id, file_name) + backend_id, file_name, this.id) return { 'type': 'ir.actions.act_window', 'res_model': 'ir.attachment.dms.wizard', @@ -84,6 +67,7 @@ class ir_attachment_edm_wizard(orm.Model): # Adding documents from Document Management (EDM) to OE. def action_apply(self, cr, uid, ids, context=None): + ir_attachment_dms_obj = self.pool.get('ir.attachment.dms') if context is None: context = {} model = context['model'] @@ -92,13 +76,20 @@ class ir_attachment_edm_wizard(orm.Model): name = ir_model_obj.browse(cr, uid, context['ids'], context=context)[0]['name'] data = self.read(cr, uid, ids, [], context=context)[0] + + if not hasattr(ids, '__iter__'): + ids = [ids] + session = ConnectorSession(cr, uid, context=context) + # Just take the lines we select in the tree view + selected_data = [one_attachment.id for one_attachment in + ir_attachment_dms_obj.browse( + cr, uid, data['attachment_ids'], context) + if one_attachment.selectable_ok] + data['attachment_ids'] = selected_data if not data['attachment_ids']: raise orm.except_orm(_('Error'), _('You have to select at least 1 Document.' + 'And try again')) - if not hasattr(ids, '__iter__'): - ids = [ids] - session = ConnectorSession(cr, uid, context=context) for backend_id in ids: # Create doc in OE from DMS. create_doc_from_dms.delay(session, 'ir.attachment', backend_id, @@ -106,6 +97,26 @@ class ir_attachment_edm_wizard(orm.Model): return {'type': 'ir.actions.act_window_close'} +class ir_attachment_dms(orm.TransientModel): + _name = 'ir.attachment.dms' + + _columns = { + 'name': fields.char('File name', size=150, + readonly=True, + help="File name"), + 'owner': fields.char('Owner', size=150, + readonly=True, + help="Owner"), + 'file_id': fields.char('File ID', size=150, + readonly=True, + help="File Id"), + 'wizard_id': fields.many2one('ir.attachment.dms.wizard', + string='Wizard', + required=True), + 'selectable_ok': fields.boolean('Selected', help="Selected."), + } + + def sanitize_input_filename_field(file_name): # Escape the name for characters not supported in filenames # for avoiding SQL Injection @@ -115,7 +126,7 @@ def sanitize_input_filename_field(file_name): return file_name -def search_doc_from_dms(session, model_name, backend_id, file_name): +def search_doc_from_dms(session, model_name, backend_id, file_name, wizard_id): ir_attach_dms_obj = session.pool.get('ir.attachment.dms') cmis_backend_obj = session.pool.get('cmis.backend') if session.context is None: @@ -141,6 +152,7 @@ def search_doc_from_dms(session, model_name, backend_id, file_name): 'name': info['cmis:name'], 'owner': info['cmis:createdBy'], 'file_id': info['cmis:objectId'], + 'wizard_id': wizard_id, } ir_attach_dms_obj.create(session.cr, session.uid, data_attach, context=session.context) @@ -175,7 +187,7 @@ def create_doc_from_dms(session, model_name, backend_id, data, name, 'res_id': res_id, 'user_id': uid, } - session.context['bool_read_doc'] = True + session.context['bool_testdoc'] = True ir_attach_obj.create(session.cr, session.uid, data_attach, context=session.context) return True diff --git a/cmis_read/wizard/document_wizard_view.xml b/cmis_read/wizard/document_wizard_view.xml index d44b883a..b63c0db4 100644 --- a/cmis_read/wizard/document_wizard_view.xml +++ b/cmis_read/wizard/document_wizard_view.xml @@ -2,32 +2,45 @@ - - Search Document - ir.attachment.dms.wizard - -
    -
    + + + Search Document + ir.attachment.dms.wizard + +
    +
    + This action allows you to search by file name and to add the document you select +
    + +
    + +
    +
    + +
    +
    + +
    +
    + + + + Ir Attachment Dms + ir.attachment.dms + + + + + + +
    From 6957f53ac55aa6523d3558f7363b51e36ccacea1 Mon Sep 17 00:00:00 2001 From: EL HADJI DEM Date: Thu, 12 Jun 2014 15:56:52 -0400 Subject: [PATCH 9/9] [IMP] Call the defined function in cmis module to clean query --- cmis_read/wizard/document_wizard.py | 19 ++++--------------- 1 file changed, 4 insertions(+), 15 deletions(-) diff --git a/cmis_read/wizard/document_wizard.py b/cmis_read/wizard/document_wizard.py index 6ba9c997..70421dc4 100644 --- a/cmis_read/wizard/document_wizard.py +++ b/cmis_read/wizard/document_wizard.py @@ -117,15 +117,6 @@ class ir_attachment_dms(orm.TransientModel): } -def sanitize_input_filename_field(file_name): - # Escape the name for characters not supported in filenames - # for avoiding SQL Injection - file_name = file_name.replace("'", "\\'") - file_name = file_name.replace("%", "\%") - file_name = file_name.replace("_", "\_") - return file_name - - def search_doc_from_dms(session, model_name, backend_id, file_name, wizard_id): ir_attach_dms_obj = session.pool.get('ir.attachment.dms') cmis_backend_obj = session.pool.get('cmis.backend') @@ -139,12 +130,10 @@ def search_doc_from_dms(session, model_name, backend_id, file_name, wizard_id): attachment_ids = ir_attach_dms_obj.search(session.cr, session.uid, []) ir_attach_dms_obj.unlink(session.cr, session.uid, attachment_ids, context=session.context) - # Escape the name for characters not supported in filenames - file_name = sanitize_input_filename_field(file_name) - # Get results from name of document - results = repo.query(" SELECT cmis:name, cmis:createdBy, cmis:objectId, " - "cmis:contentStreamLength FROM cmis:document " - "WHERE cmis:name LIKE '%" + file_name + "%'") + results = cmis_backend_obj.safe_query( + "SELECT cmis:name, cmis:createdBy, cmis:objectId, " + "cmis:contentStreamLength FROM cmis:document " + "WHERE cmis:name LIKE '%%%s%%'", file_name, repo) for result in results: info = result.getProperties() if info['cmis:contentStreamLength'] != 0: