Skip to content
Snippets Groups Projects
Commit 388b3efee2cd authored by Houzefa Abbasbhay's avatar Houzefa Abbasbhay :slight_smile:
Browse files

admin_doc -> document_attachment

Add a "model" field to document_attachment_type so multiple doc attachment tables can live together
The implementer is now responsible for creating menus (removed the dep on hr)
parent 56d187e4ac23
No related branches found
No related tags found
No related merge requests found
# -*- coding: utf-8 -*-
import admin_doc
import document_attachment
......@@ -13,7 +13,7 @@
'document',
],
'data': [
'admin_doc.xml',
'document_attachment.xml',
],
'test': [
],
......
from openerp.osv import fields, osv
class admin_doc_type(osv.Model):
"""Kind of an administrative document employees have to provide."""
_name = 'admin_doc.type'
_columns = {
'name': fields.char('Description', size=256, required=True),
}
class admin_doc(osv.Model):
"""An administrative document employees have to provide.
Inherit[s] from ir.attachment to store documents as attachments.
"""
_name = 'admin_doc'
_inherits = {
'ir.attachment': 'file_id',
}
_columns = {
'type_id': fields.many2one('admin_doc.type',
'Type',
required=True),
'file_id': fields.many2one('ir.attachment',
'File',
ondelete='cascade',
required=True),
}
def create(self, cr, uid, vals, context=None):
"""- Fill the "name" from the original filename.
- Associate the new attachment with the current employee.
"""
if not 'res_model' in context:
raise osv.except_osv(
_('Programming Error'),
_("You must send the 'res_model' through the m2m context"),
)
if not 'res_id' in context:
raise osv.except_osv(
_('Programming Error'),
_("You must send the 'res_id' through the m2m context"),
)
vals['name'] = vals['datas_fname']
vals['res_model'] = context['res_model']
vals['res_id'] = context['res_id']
return super(admin_doc, self).create(cr, uid, vals, context=context)
def write(self, cr, uid, ids, vals, context=None):
"""Fill the "name" from the original filename."""
if 'datas_fname' in vals:
vals['name'] = vals['datas_fname']
return super(admin_doc, self).write(cr, uid, ids, vals,
context=context)
def unlink(self, cr, uid, ids, context=None):
"""Delete the attachment associated with records being deleted."""
brs = self.browse(cr, uid, ids, context=context)
attachment_ids = [br.file_id.id for br in brs]
ret = super(admin_doc, self).unlink(cr, uid, ids, context=context)
attachment_obj = self.pool.get('ir.attachment')
attachment_obj.unlink(cr, uid, attachment_ids, context=context)
return ret
<?xml version="1.0" encoding="utf-8"?>
<openerp>
<data>
<!-- Views related to the admin_doc_type model. -->
<record id="admin_doc_type_tree_view" model="ir.ui.view">
<field name="name">admin_doc_type_tree_view</field>
<field name="model">admin_doc.type</field>
<field name="arch" type="xml">
<tree string="Administrative document types" version="7.0">
<field name="name" />
</tree>
</field>
</record>
<record id="admin_doc_type_form_view" model="ir.ui.view">
<field name="name">admin_doc_type_form_view</field>
<field name="model">admin_doc.type</field>
<field name="arch" type="xml">
<form string="Administrative document types" version="7.0">
<group cols="2">
<field name="name" />
</group>
</form>
</field>
</record>
<!-- Views related to the admin_doc model. -->
<record id="admin_doc_tree_view" model="ir.ui.view">
<field name="name">admin_doc_tree_view</field>
<field name="model">admin_doc</field>
<field name="arch" type="xml">
<tree string="Administrative documents" version="7.0">
<field name="type_id" />
<!-- Inherited from ir.attachment. -->
<field name="datas" filename="datas_fname" />
<field name="datas_fname" invisible="1"
class="oe_inline oe_right" />
</tree>
</field>
</record>
<record id="admin_doc_form_view" model="ir.ui.view">
<field name="name">admin_doc_form_view</field>
<field name="model">admin_doc</field>
<!-- <field name="inherit_id" ref="base.view_attachment_form" /> -->
<field name="arch" type="xml">
<form string="Administrative documents" version="7.0">
<group cols="2">
<field name="type_id" />
<!-- Inherited from ir.attachment. -->
<field name="datas" filename="datas_fname" />
<field name="datas_fname" invisible="1"
class="oe_inline oe_right" />
</group>
</form>
</field>
</record>
<!-- Menus. -->
<record id="admin_doc_type_action" model="ir.actions.act_window">
<field name="name">Administrative documents</field>
<field name="res_model">admin_doc.type</field>
<field name="view_type">form</field>
<field name="view_mode">tree,form</field>
</record>
<menuitem id="admin_doc_type_menu" name="Administrative documents"
parent="hr.menu_hr_configuration" sequence="10" action="admin_doc_type_action" />
</data>
</openerp>
from openerp.osv import fields, osv
class document_attachment_type(osv.Model):
"""Kind of document that can be attached."""
_name = 'document_attachment.type'
_columns = {
'name': fields.char(
'Description',
size=256,
required=True
),
'model': fields.char(
'Model',
size=128,
required=True,
),
}
class document_attachment(osv.Model):
"""A document that can be attached.
Inherit[s] from ir.attachment to store documents as attachments.
"""
_name = 'document_attachment'
_inherits = {
'ir.attachment': 'file_id',
}
_columns = {
'type_id': fields.many2one(
'document_attachment.type',
'Type',
required=True
),
'file_id': fields.many2one(
'ir.attachment',
'File',
ondelete='cascade',
required=True
),
}
def create(self, cr, uid, vals, context=None):
"""- Fill the "name" from the original filename.
- Associate the new attachment with the current object.
"""
if not 'res_model' in context:
raise osv.except_osv(
_('Programming Error'),
_("You must send the 'res_model' through the m2m context"),
)
if not 'res_id' in context:
raise osv.except_osv(
_('Programming Error'),
_("You must send the 'res_id' through the m2m context"),
)
vals['name'] = vals['datas_fname']
vals['res_model'] = context['res_model']
vals['res_id'] = context['res_id']
return super(document_attachment, self).create(
cr, uid, vals, context=context
)
def write(self, cr, uid, ids, vals, context=None):
"""Fill the "name" from the original filename."""
if 'datas_fname' in vals:
vals['name'] = vals['datas_fname']
return super(document_attachment, self).write(
cr, uid, ids, vals, context=context
)
def unlink(self, cr, uid, ids, context=None):
"""Delete the attachment associated with records being deleted."""
brs = self.browse(cr, uid, ids, context=context)
attachment_ids = [br.file_id.id for br in brs]
ret = super(document_attachment, self).unlink(
cr, uid, ids, context=context
)
attachment_obj = self.pool.get('ir.attachment')
attachment_obj.unlink(cr, uid, attachment_ids, context=context)
return ret
<?xml version="1.0" encoding="utf-8"?>
<openerp>
<data>
<!-- Views related to the document_attachment_type model. -->
<record id="document_attachment_type_tree_view" model="ir.ui.view">
<field name="name">document_attachment_type_tree_view</field>
<field name="model">document_attachment.type</field>
<field name="arch" type="xml">
<tree string="Document types" version="7.0">
<field name="name" />
</tree>
</field>
</record>
<record id="document_attachment_type_form_view" model="ir.ui.view">
<field name="name">document_attachment_type_form_view</field>
<field name="model">document_attachment.type</field>
<field name="arch" type="xml">
<form string="Document types" version="7.0">
<group cols="2">
<field name="name" />
</group>
</form>
</field>
</record>
<!-- Views related to the document_attachment model. -->
<record id="document_attachment_tree_view" model="ir.ui.view">
<field name="name">document_attachment_tree_view</field>
<field name="model">document_attachment</field>
<field name="arch" type="xml">
<tree string="Documents" version="7.0">
<field name="type_id" />
<!-- Inherited from ir.attachment. -->
<field name="datas" filename="datas_fname" />
<field name="datas_fname" invisible="1"
class="oe_inline oe_right" />
</tree>
</field>
</record>
<record id="document_attachment_form_view" model="ir.ui.view">
<field name="name">document_attachment_form_view</field>
<field name="model">document_attachment</field>
<!-- <field name="inherit_id" ref="base.view_attachment_form" /> -->
<field name="arch" type="xml">
<form string="Documents" version="7.0">
<group cols="2">
<field name="type_id"
domain="[('model', '=', context.get('res_model'))]" />
<!-- Inherited from ir.attachment. -->
<field name="datas" filename="datas_fname" />
<field name="datas_fname" invisible="1"
class="oe_inline oe_right" />
</group>
</form>
</field>
</record>
</data>
</openerp>
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment