[FIX] Partial refactoring based on MP comments

This commit is contained in:
Joao Alfredo Gama Batista 2014-02-26 11:25:24 -05:00 committed by Sandy Carter
parent e1bf00a575
commit b23ac72daf
6 changed files with 59 additions and 28 deletions

View File

@ -21,6 +21,6 @@
############################################################################### ###############################################################################
from . import document from . import document
import wizard from . import wizard
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:

View File

@ -27,25 +27,55 @@ class document_file(orm.Model):
_inherit = 'ir.attachment' _inherit = 'ir.attachment'
_columns = { _columns = {
'attachmentdocument_ids': fields.one2many('ir.attachment.document', 'attachment_id', 'Records'), 'attachment_document_ids': fields.one2many('ir.attachment.document',
'attachment_id',
'Records'),
} }
def unlink(self, cr, uid, ids, context=None, check=True): def create(self, cr, uid, data, context=None):
res = super(document_file, self).create(cr, uid, data, context=context)
if 'res_model' and 'res_id' in data:
ir_attachment_document_obj = self.pool.get('ir.attachment.document')
doc_data = {
'attachment_id': res,
'res_model': data['res_model'],
'res_id': 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
}
ir_attachment_document_obj.create(cr, uid, doc_data,
context=context)
return res
def unlink(self, cr, uid, ids, context=None):
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 = {}
for line in self.browse(cr, uid, ids, context=context):
if line.attachmentdocument_ids:
# Get the first document
first_id = min(line.attachmentdocument_ids)
result = self.write(cr, uid, ids, {'res_id': first_id.res_id, for line in self.browse(cr, uid, ids, context=context):
'res_model': first_id.res_model, if line.attachment_document_ids and len(line.attachment_document_ids) > 1:
'res_name': first_id.res_name, }, context=context) query = [
ir_attach_doc_obj.unlink(cr, uid, min(line.attachmentdocument_ids).id, context=context) ('attachment_id', '=', line.id),
('res_model', '=', line.res_model),
('res_id', '=', line.res_id)
]
id_to_unlink = ir_attach_doc_obj.search(
cr, uid, query, context=context)
data = {
'res_id': False,
'res_model': False,
'res_name': False
}
self.write(cr, uid, ids, data, context=context)
ir_attach_doc_obj.unlink(cr, uid, id_to_unlink,
context=context)
else: else:
result = super(document_file, self).unlink(cr, uid, ids, context=context) super(document_file, self).unlink(cr, uid, line.id,
return result context=context)
return True
class ir_attachment_document(orm.Model): class ir_attachment_document(orm.Model):

View File

@ -9,7 +9,7 @@
<field name="arch" type="xml"> <field name="arch" type="xml">
<group string="Indexed Content" position="after"> <group string="Indexed Content" position="after">
<group col="2" colspan="4"> <group col="2" colspan="4">
<field name="attachmentdocument_ids" nolabel="1"> <field name="attachment_document_ids" nolabel="1">
<tree string="AttachmentDocumentTree" create="false" version="7.0"> <tree string="AttachmentDocumentTree" create="false" version="7.0">
<field name="res_model"/> <field name="res_model"/>
<field name="res_id"/> <field name="res_id"/>

View File

@ -24,7 +24,7 @@ var _t = instance.web._t,
var action = { var action = {
name: _t("Add existing document/attachment"), name: _t("Add existing document/attachment"),
type: 'ir.actions.act_window', type: 'ir.actions.act_window',
res_model: 'ir.attachment.wizard', res_model: 'ir.attachment.existing.doc',
view_mode: 'form', view_mode: 'form',
view_type: 'form', view_type: 'form',
views: [[false, 'form']], views: [[false, 'form']],

View File

@ -25,12 +25,14 @@ from openerp.tools.translate import _
class document_wizard(orm.Model): class document_wizard(orm.Model):
_name = "ir.attachment.wizard" _name = "ir.attachment.existing.doc"
_description = "Attachment wizard" _description = "Add existing document/attachment wizard"
_columns = { _columns = {
'attachment_ids': fields.many2many('ir.attachment', 'attachment_ids': fields.many2many('ir.attachment',
'document_attachment_rel', 'document_attachment_rel',
'wizard_id', 'attachment_id', 'Attachments'), 'wizard_id',
'attachment_id',
'Attachments'),
} }
def action_apply(self, cr, uid, ids, context=None): def action_apply(self, cr, uid, ids, context=None):
@ -38,17 +40,17 @@ class document_wizard(orm.Model):
context = {} context = {}
ir_attach_obj = self.pool.get('ir.attachment') ir_attach_obj = self.pool.get('ir.attachment')
ir_attach_doc_obj = self.pool.get('ir.attachment.document') ir_attach_doc_obj = self.pool.get('ir.attachment.document')
ir_model_obj = self.pool.get(context['model']) ir_model_obj = self.pool.get(context.get('model') or context.get('active_model'))
name = ir_model_obj.browse(cr, uid, context['ids'], context=context)[0]['name'] name = ir_model_obj.browse(cr, uid, context.get('ids') or context.get('active_ids'), context=context)[0]['name']
data = self.read(cr, uid, ids, [], context=context)[0] data = self.read(cr, uid, ids, [], context=context)[0]
if not data['attachment_ids']: if not data['attachment_ids']:
raise orm.except_orm(_('Error'), 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'))
for attach in ir_attach_obj.browse(cr, uid, data['attachment_ids'], context=context): for attach in ir_attach_obj.browse(cr, uid, data['attachment_ids'], context=context):
data_attach = { data_attach = {
'res_model': context['model'], 'res_model': context.get('model') or context.get('active_model'),
'res_id': context['ids'][0], 'res_id': context.get('ids') and context.get('ids')[0] or context.get('active_id'),
'res_name': name, 'res_name': name,
'attachment_id': attach.id, 'attachment_id': attach.id,
} }
@ -61,4 +63,3 @@ class document_wizard(orm.Model):
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:

View File

@ -3,10 +3,10 @@
<data> <data>
<record id="document_form_view" model="ir.ui.view"> <record id="document_form_view" model="ir.ui.view">
<field name="name">Add Document</field> <field name="name">Add existing document/attachment</field>
<field name="model">ir.attachment.wizard</field> <field name="model">ir.attachment.existing.doc</field>
<field name="arch" type="xml"> <field name="arch" type="xml">
<form string="Add Document" version="7.0"> <form string="Add existing document/attachment" version="7.0">
<group string="Select document(s)" colspan="4"> <group string="Select document(s)" colspan="4">
<field name="attachment_ids" nolabel="1"> <field name="attachment_ids" nolabel="1">
<tree string="AttachmentDocumentWizardTree"> <tree string="AttachmentDocumentWizardTree">
@ -28,8 +28,8 @@
<!-- Actions --> <!-- Actions -->
<record model="ir.actions.act_window" id="action_view_document"> <record model="ir.actions.act_window" id="action_view_document">
<field name="name">Add Document</field> <field name="name">Add existing document/attachment</field>
<field name="res_model">ir.attachment.wizard</field> <field name="res_model">ir.attachment.existing.doc</field>
<field name="view_type">form</field> <field name="view_type">form</field>
<field name="view_mode">tree,form</field> <field name="view_mode">tree,form</field>
<field name="view_id" ref="document_form_view"/> <field name="view_id" ref="document_form_view"/>