mirror of
https://github.com/OCA/knowledge.git
synced 2025-07-22 20:12:04 -06:00
[FIX] Partial refactoring based on MP comments
This commit is contained in:
parent
e1bf00a575
commit
b23ac72daf
@ -21,6 +21,6 @@
|
||||
###############################################################################
|
||||
|
||||
from . import document
|
||||
import wizard
|
||||
from . import wizard
|
||||
|
||||
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
|
||||
|
@ -27,25 +27,55 @@ class document_file(orm.Model):
|
||||
_inherit = 'ir.attachment'
|
||||
|
||||
_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')
|
||||
if context is None:
|
||||
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,
|
||||
'res_model': first_id.res_model,
|
||||
'res_name': first_id.res_name, }, context=context)
|
||||
ir_attach_doc_obj.unlink(cr, uid, min(line.attachmentdocument_ids).id, context=context)
|
||||
for line in self.browse(cr, uid, ids, context=context):
|
||||
if line.attachment_document_ids and len(line.attachment_document_ids) > 1:
|
||||
query = [
|
||||
('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:
|
||||
result = super(document_file, self).unlink(cr, uid, ids, context=context)
|
||||
return result
|
||||
super(document_file, self).unlink(cr, uid, line.id,
|
||||
context=context)
|
||||
return True
|
||||
|
||||
|
||||
class ir_attachment_document(orm.Model):
|
||||
|
@ -9,7 +9,7 @@
|
||||
<field name="arch" type="xml">
|
||||
<group string="Indexed Content" position="after">
|
||||
<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">
|
||||
<field name="res_model"/>
|
||||
<field name="res_id"/>
|
||||
|
@ -24,7 +24,7 @@ var _t = instance.web._t,
|
||||
var action = {
|
||||
name: _t("Add existing document/attachment"),
|
||||
type: 'ir.actions.act_window',
|
||||
res_model: 'ir.attachment.wizard',
|
||||
res_model: 'ir.attachment.existing.doc',
|
||||
view_mode: 'form',
|
||||
view_type: 'form',
|
||||
views: [[false, 'form']],
|
||||
|
@ -25,12 +25,14 @@ from openerp.tools.translate import _
|
||||
|
||||
|
||||
class document_wizard(orm.Model):
|
||||
_name = "ir.attachment.wizard"
|
||||
_description = "Attachment wizard"
|
||||
_name = "ir.attachment.existing.doc"
|
||||
_description = "Add existing document/attachment wizard"
|
||||
_columns = {
|
||||
'attachment_ids': fields.many2many('ir.attachment',
|
||||
'document_attachment_rel',
|
||||
'wizard_id', 'attachment_id', 'Attachments'),
|
||||
'wizard_id',
|
||||
'attachment_id',
|
||||
'Attachments'),
|
||||
}
|
||||
|
||||
def action_apply(self, cr, uid, ids, context=None):
|
||||
@ -38,17 +40,17 @@ class document_wizard(orm.Model):
|
||||
context = {}
|
||||
ir_attach_obj = self.pool.get('ir.attachment')
|
||||
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]
|
||||
if not data['attachment_ids']:
|
||||
raise orm.except_orm(_('Error'),
|
||||
_('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):
|
||||
data_attach = {
|
||||
'res_model': context['model'],
|
||||
'res_id': context['ids'][0],
|
||||
'res_model': context.get('model') or context.get('active_model'),
|
||||
'res_id': context.get('ids') and context.get('ids')[0] or context.get('active_id'),
|
||||
'res_name': name,
|
||||
'attachment_id': attach.id,
|
||||
}
|
||||
@ -61,4 +63,3 @@ class document_wizard(orm.Model):
|
||||
return {'type': 'ir.actions.act_window_close'}
|
||||
|
||||
# vim:expandtab:smartindent:toabstop=4:softtabstop=4:shiftwidth=4:
|
||||
|
||||
|
@ -3,10 +3,10 @@
|
||||
<data>
|
||||
|
||||
<record id="document_form_view" model="ir.ui.view">
|
||||
<field name="name">Add Document</field>
|
||||
<field name="model">ir.attachment.wizard</field>
|
||||
<field name="name">Add existing document/attachment</field>
|
||||
<field name="model">ir.attachment.existing.doc</field>
|
||||
<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">
|
||||
<field name="attachment_ids" nolabel="1">
|
||||
<tree string="AttachmentDocumentWizardTree">
|
||||
@ -28,8 +28,8 @@
|
||||
|
||||
<!-- Actions -->
|
||||
<record model="ir.actions.act_window" id="action_view_document">
|
||||
<field name="name">Add Document</field>
|
||||
<field name="res_model">ir.attachment.wizard</field>
|
||||
<field name="name">Add existing document/attachment</field>
|
||||
<field name="res_model">ir.attachment.existing.doc</field>
|
||||
<field name="view_type">form</field>
|
||||
<field name="view_mode">tree,form</field>
|
||||
<field name="view_id" ref="document_form_view"/>
|
||||
|
Loading…
Reference in New Issue
Block a user