-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
red_october_char.py
81 lines (67 loc) · 2.63 KB
/
red_october_char.py
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
# -*- coding: utf-8 -*-
# Copyright 2017 LasLabs Inc.
# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl.html).
import logging
import psycopg2
from odoo import fields
from odoo.tools import human_size
_logger = logging.getLogger(__name__)
class RedOctoberChar(fields.Field):
type = 'red_october'
column_type = None
def convert_to_column(self, value, record):
return
def convert_to_cache(self, value, record, validate=True):
if isinstance(value, buffer):
return str(value)
return value
def read(self, records):
# values are stored in attachments, retrieve them
attachments = self._get_attachments(records)
if self._context.get('bin_size'):
attachments = attachments.with_context(bin_size=False)
data = {att.res_id: att.datas for att in attachments}
_logger.debug(data)
for record in records:
record._cache[self.name] = data.get(record.id, self.null())
def write(self, records, value):
# retrieve the attachments that stores the value, and adapt them
attachments = self._get_attachments(records)
_logger.debug('Writing %s with %s', attachments, value)
with records.env.norecompute():
if value:
# update the existing attachments
attachments.write({
'datas': value,
})
missing_attachments = (
records - records.browse(attachments.mapped('res_id'))
)
for record in missing_attachments:
self._create_attachment(record, value)
else:
attachments.unlink()
def _create_attachment(self, record, value):
user = record.env['red.october.user'].get_current_user()
vault = record.env['red.october.vault'].get_current_vault()
_logger.debug('%s, %s', user, user.ids)
file = record.env['red.october.file'].create({
'name': '%s(%d).%s' % (record._name, record.id, self.name),
'res_model': record._name,
'res_field': self.name,
'res_id': record.id,
'datas': value,
})
record.env['red.october.file.owner'].create({
'vault_id': vault.id,
'file_id': file.id,
'user_id': user.id,
})
return file
def _get_attachments(self, records):
domain = [
('res_model', '=', records._name),
('res_field', '=', self.name),
('res_id', 'in', records.ids),
]
return records.env['red.october.file'].sudo().search(domain)