[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

This commit is contained in:
EL HADJI DEM 2014-03-18 14:50:56 -04:00 committed by Sandy Carter
parent b30711948f
commit 2889c10a50
6 changed files with 79 additions and 52 deletions

View File

@ -41,10 +41,15 @@ Contributors
], ],
'data': [ 'data': [
'document_view.xml', 'document_view.xml',
'security/ir.model.access.csv',
'wizard/document_wizard_view.xml', 'wizard/document_wizard_view.xml',
], ],
'js': ['static/src/js/document.js'], 'js': [
'qweb': ['static/src/xml/document.xml'], 'static/src/js/document.js'
],
'qweb': [
'static/src/xml/document.xml'
],
'test': [], 'test': [],
'demo': [ 'demo': [
], ],

View File

@ -33,49 +33,47 @@ class document_file(orm.Model):
} }
def create(self, cr, uid, data, context=None): 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) res = super(document_file, self).create(cr, uid, data, context=context)
if 'res_model' and 'res_id' in data: # Create attachment_document_ids with res_model, res_id and res_name
ir_attachment_document_obj = self.pool.get('ir.attachment.document') if 'res_model' and 'res_id' in original_data:
doc_data = { doc_data = {
'attachment_id': res, 'attachment_id': res,
'res_model': data['res_model'], 'res_model': original_data['res_model'],
'res_id': data['res_id'], 'res_id': original_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_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, ir_attachment_document_obj.create(cr, uid, doc_data,
context=context) context=context)
return res 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') ir_attach_doc_obj = self.pool.get('ir.attachment.document')
if context is None: if context is None:
context = {} context = {}
# Deleting from dropdown list in the form view
for line in self.browse(cr, uid, ids, context=context): if context.get('res_model') and context.get('res_id'):
if line.attachment_document_ids and len(line.attachment_document_ids) > 1: for line in self.browse(cr, uid, ids, context=context):
query = [ if line.attachment_document_ids:
('attachment_id', '=', line.id), query = [
('res_model', '=', line.res_model), ('res_model', '=', context.get('res_model')),
('res_id', '=', line.res_id) ('res_id', '=', context.get('res_id')),
] ('attachment_id', '=', ids),
id_to_unlink = ir_attach_doc_obj.search( ]
cr, uid, query, context=context) 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)
data = { else:
'res_id': False, # Normal delete
'res_model': False, result = super(document_file, self).unlink(cr, uid, ids, context=context)
'res_name': False return result
}
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
class ir_attachment_document(orm.Model): class ir_attachment_document(orm.Model):

View File

@ -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
1 id name model_id:id group_id:id perm_read perm_write perm_create perm_unlink
2 access_ir_attachment_document_group_user ir.attachment.document user model_ir_attachment_document base.group_document_user 1 1 1 1

View File

@ -17,12 +17,12 @@ var _t = instance.web._t,
var ids = ( view.fields_view.type != "form" )? view.groups.get_selection().ids : [ view.datarecord.id ]; 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 // you can pass in other data using the context dictionary variable
var context = { var context = {
'model': view.dataset.model, 'model': view.dataset.model,
'ids': ids, 'ids': ids,
}; };
// the action dictionary variable sends data in the "self.do_action" method // the action dictionary variable sends data in the "self.do_action" method
var action = { var action = {
name: _t("Add existing document/attachment"), name: _t("Add existing document"),
type: 'ir.actions.act_window', type: 'ir.actions.act_window',
res_model: 'ir.attachment.existing.doc', res_model: 'ir.attachment.existing.doc',
view_mode: 'form', view_mode: 'form',
@ -32,22 +32,48 @@ var _t = instance.web._t,
context: context, context: context,
}; };
// self.do_action accepts the action parameter and opens the new view // 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; var self = this;
this.dataset = dataset; this.dataset = dataset;
this.model_id = model_id; this.model_id = model_id;
if (args && args[0].error) { 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) { if (!model_id) {
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);
} }
} 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);
}
}
}); });
}; };

View File

@ -4,7 +4,7 @@
<templates id="template" xml:space="preserve"> <templates id="template" xml:space="preserve">
<t t-name="AddDocfromserver"> <t t-name="AddDocfromserver">
<li class="open"><span><b>Add existing document/attachment...</b></span></li> <li class="open"><span><b>Add existing document...</b></span></li>
</t> </t>
</templates> </templates>

View File

@ -55,11 +55,7 @@ class document_wizard(orm.Model):
'attachment_id': attach.id, 'attachment_id': attach.id,
} }
#Created attachment_document_ids #Created attachment_document_ids
if attach.res_model: ir_attach_doc_obj.create(cr, uid, data_attach, context=context)
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)
return {'type': 'ir.actions.act_window_close'} return {'type': 'ir.actions.act_window_close'}
# vim:expandtab:smartindent:toabstop=4:softtabstop=4:shiftwidth=4: # vim:expandtab:smartindent:toabstop=4:softtabstop=4:shiftwidth=4: