Cleanup code and fix possible bugs

create():
    Avoid duplicating data locally
    Fix boolean comparison problem
    Use name_get rather than name field
unlink():
    Use more specific context identifiers
    Remove redundant loop and if statement
model:
    Add ondelete='cascade'
This commit is contained in:
Sandy Carter 2014-04-16 20:24:17 -04:00
parent 94f1444213
commit 3f9ea85e45
2 changed files with 25 additions and 32 deletions

View File

@ -34,26 +34,18 @@ 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
if ('res_name', 'res_id', 'res_name') in data.keys():
data['res_model'] = False
data['res_id'] = False
data['res_name'] = False
res = super(document_file, self).create(cr, uid, data, context=context)
# Create attachment_document_ids with res_model, res_id and res_name
if 'res_model' and 'res_id' in original_data:
doc_data = {
if 'res_model' in data and 'res_id' in data:
ir_attachment_document_obj.create(cr, uid, {
'attachment_id': res,
'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)
'res_model': data['res_model'],
'res_id': data['res_id'],
'res_name': data.get(
'res_name', self.pool.get(data['res_model']).browse(
cr, uid, data['res_id'],
context=context).name_get()[0][1]),
}, context=context)
return res
def unlink(self, cr, uid, ids, context=None, check=True):
@ -61,16 +53,18 @@ class document_file(orm.Model):
if context is None:
context = {}
# 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)
res_model = context.get('multiple_records_res_model')
res_id = context.get('multiple_records_res_id')
if res_model and res_id:
query = [
('res_model', '=', res_model),
('res_id', '=', res_id),
('attachment_id', 'in', ids),
]
ids_to_unlink = ir_attach_doc_obj.search(
cr, uid, query, context=context)
result = ir_attach_doc_obj.unlink(
cr, uid, ids_to_unlink, context=context)
else:
# Normal delete
result = super(document_file, self).unlink(cr, uid, ids, context=context)
@ -90,7 +84,6 @@ class ir_attachment_document(orm.Model):
'res_name': fields.char('Resource Name', type='char',
size=128,
readonly=True),
'attachment_id': fields.many2one('ir.attachment', 'Attachment'),
'attachment_id': fields.many2one(
'ir.attachment', 'Attachment', ondelete='cascade'),
}
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:

View File

@ -49,8 +49,8 @@ var _t = instance.web._t,
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],
'multiple_records_res_model': self.model_view,
'multiple_records_res_id': ids[0],
};
var $e = $(e.currentTarget);
if (confirm(_t("Do you really want to delete this attachment ?"))) {