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): def create(self, cr, uid, data, context=None):
ir_attachment_document_obj = self.pool.get('ir.attachment.document') 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) res = super(document_file, self).create(cr, uid, data, context=context)
# Create attachment_document_ids with res_model, res_id and res_name # Create attachment_document_ids with res_model, res_id and res_name
if 'res_model' and 'res_id' in original_data: if 'res_model' in data and 'res_id' in data:
doc_data = { ir_attachment_document_obj.create(cr, uid, {
'attachment_id': res, 'attachment_id': res,
'res_model': original_data['res_model'], 'res_model': data['res_model'],
'res_id': original_data['res_id'], 'res_id': data['res_id'],
'res_name': original_data.get('res_name') 'res_name': data.get(
or self.pool.get(original_data['res_model']).browse(cr, uid, 'res_name', self.pool.get(data['res_model']).browse(
original_data['res_id'], cr, uid, data['res_id'],
context=context).name context=context).name_get()[0][1]),
} }, context=context)
ir_attachment_document_obj.create(cr, uid, doc_data,
context=context)
return res return res
def unlink(self, cr, uid, ids, context=None, check=True): def unlink(self, cr, uid, ids, context=None, check=True):
@ -61,16 +53,18 @@ class document_file(orm.Model):
if context is None: if context is None:
context = {} context = {}
# Deleting from dropdown list in the form view # Deleting from dropdown list in the form view
if context.get('res_model') and context.get('res_id'): res_model = context.get('multiple_records_res_model')
for line in self.browse(cr, uid, ids, context=context): res_id = context.get('multiple_records_res_id')
if line.attachment_document_ids: if res_model and res_id:
query = [ query = [
('res_model', '=', context.get('res_model')), ('res_model', '=', res_model),
('res_id', '=', context.get('res_id')), ('res_id', '=', res_id),
('attachment_id', '=', ids), ('attachment_id', 'in', ids),
] ]
id_to_unlink = ir_attach_doc_obj.search(cr, uid, query, context=context) ids_to_unlink = ir_attach_doc_obj.search(
result = ir_attach_doc_obj.unlink(cr, uid, id_to_unlink, context=context) cr, uid, query, context=context)
result = ir_attach_doc_obj.unlink(
cr, uid, ids_to_unlink, context=context)
else: else:
# Normal delete # Normal delete
result = super(document_file, self).unlink(cr, uid, ids, context=context) 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', 'res_name': fields.char('Resource Name', type='char',
size=128, size=128,
readonly=True), 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 ]; var ids = ( view.fields_view.type != "form" )? view.groups.get_selection().ids : [ view.datarecord.id ];
// Context dictionary variable // Context dictionary variable
var context = { var context = {
'res_model': self.model_view, 'multiple_records_res_model': self.model_view,
'res_id': ids[0], 'multiple_records_res_id': ids[0],
}; };
var $e = $(e.currentTarget); var $e = $(e.currentTarget);
if (confirm(_t("Do you really want to delete this attachment ?"))) { if (confirm(_t("Do you really want to delete this attachment ?"))) {