Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
from odoo import _, api, exceptions, fields, models
class DocumentAttachmentType(models.Model):
"""Kind of document that can be attached."""
_name = 'document_attachment.type'
name = fields.Char(
string='Description',
size=256,
required=True
)
model = fields.Char(
string='Model',
size=128,
required=True,
)
class DocumentAttachment(models.Model):
"""A document that can be attached.
Inherit[s] from ir.attachment to store documents as attachments.
"""
_name = 'document_attachment'
type_id = fields.Many2one(
comodel_name='document_attachment.type',
string='Type',
required=True,
)
file_id = fields.Many2one(
comodel_name='ir.attachment',
string='File',
ondelete='cascade',
required=True,
delegate=True,
)
@api.model
def create(self, vals):
"""- Fill the "name" from the original filename.
- Associate the new attachment with the current object.
"""
context = self._context
if 'res_model' not in context:
raise exceptions.Warning(
_("You must send the 'res_model' through the m2m context")
)
if 'res_id' not in context:
raise exceptions.Warning(
_("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(DocumentAttachment, self).create(vals)
@api.multi
def write(self, vals):
"""Fill the "name" from the original filename."""
if 'datas_fname' in vals:
vals['name'] = vals['datas_fname']
return super(DocumentAttachment, self).write(vals)
@api.multi
def unlink(self):
"""Delete the attachment associated with records being deleted."""
attachment_ids = [doc.file_id.id for doc in self]
ret = super(DocumentAttachment, self).unlink()
attachments = self.env['ir.attachment'].browse(attachment_ids)
attachments.unlink()
return ret