mirror of
https://github.com/OCA/knowledge.git
synced 2025-07-17 12:42:56 -06:00
[IMP] Download document from DMS
This commit is contained in:
parent
9b566cfb83
commit
61bdde4eee
@ -75,7 +75,9 @@ Contributors
|
|||||||
'metadata_view.xml',
|
'metadata_view.xml',
|
||||||
'security/ir.model.access.csv',
|
'security/ir.model.access.csv',
|
||||||
],
|
],
|
||||||
'js': [],
|
'js': [
|
||||||
|
'static/src/js/document.js'
|
||||||
|
],
|
||||||
'qweb': [],
|
'qweb': [],
|
||||||
'test': [],
|
'test': [],
|
||||||
'demo': [],
|
'demo': [],
|
||||||
|
@ -24,16 +24,30 @@ from openerp.osv import orm, fields
|
|||||||
from openerp.addons.connector.session import ConnectorSession
|
from openerp.addons.connector.session import ConnectorSession
|
||||||
from openerp.addons.connector.queue.job import job
|
from openerp.addons.connector.queue.job import job
|
||||||
import base64
|
import base64
|
||||||
|
from openerp import SUPERUSER_ID
|
||||||
|
from openerp.tools.translate import _
|
||||||
import logging
|
import logging
|
||||||
_logger = logging.getLogger(__name__)
|
_logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
class ir_attachment(orm.Model):
|
class ir_attachment_download(orm.TransientModel):
|
||||||
_inherit = 'ir.attachment'
|
_name = 'ir.attachment.download'
|
||||||
|
|
||||||
_columns = {
|
_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):
|
def create(self, cr, uid, values, context=None):
|
||||||
metadata_obj = self.pool.get('metadata')
|
metadata_obj = self.pool.get('metadata')
|
||||||
@ -77,6 +91,84 @@ class ir_attachment(orm.Model):
|
|||||||
user_login)
|
user_login)
|
||||||
return res
|
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
|
@job
|
||||||
def create_doc_in_edm(session, model_name, value, res,
|
def create_doc_in_edm(session, model_name, value, res,
|
||||||
|
42
cmis_write/static/src/js/document.js
Normal file
42
cmis_write/static/src/js/document.js
Normal file
@ -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);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
});
|
||||||
|
};
|
Loading…
Reference in New Issue
Block a user