| 1 |
# -*- coding: utf-8 -*- |
| 2 |
# Copyright 2014 Sébastien Alix |
| 3 |
# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl) |
| 4 |
"""This module contains classes representing the fields supported by Odoo. |
| 5 |
A field is a Python descriptor which defines getter/setter methods for |
| 6 |
its related attribute. |
| 7 |
""" |
| 8 |
import datetime |
| 9 |
import sys |
| 10 |
|
| 11 |
# from odoorpc import error |
| 12 |
from odoorpc.models import IncrementalRecords, Model |
| 13 |
|
| 14 |
|
| 15 |
def is_int(value): |
| 16 |
"""Return `True` if ``value`` is an integer.""" |
| 17 |
if isinstance(value, bool): |
| 18 |
return False |
| 19 |
try: |
| 20 |
int(value) |
| 21 |
return True |
| 22 |
except (ValueError, TypeError): |
| 23 |
return False |
| 24 |
|
| 25 |
|
| 26 |
# Python 2 |
| 27 |
if sys.version_info[0] < 3: |
| 28 |
|
| 29 |
def is_string(value): |
| 30 |
"""Return `True` if ``value`` is a string.""" |
| 31 |
# noqa: F821 |
| 32 |
return isinstance(value, basestring) # noqa: F821 |
| 33 |
|
| 34 |
|
| 35 |
# Python >= 3 |
| 36 |
else: |
| 37 |
|
| 38 |
def is_string(value): |
| 39 |
"""Return `True` if ``value`` is a string.""" |
| 40 |
return isinstance(value, str) |
| 41 |
|
| 42 |
|
| 43 |
def odoo_tuple_in(iterable): |
| 44 |
"""Return `True` if `iterable` contains an expected tuple like |
| 45 |
``(6, 0, IDS)`` (and so on). |
| 46 |
|
| 47 |
>>> odoo_tuple_in([0, 1, 2]) # Simple list |
| 48 |
False |
| 49 |
>>> odoo_tuple_in([(6, 0, [42])]) # List of tuples |
| 50 |
True |
| 51 |
>>> odoo_tuple_in([[1, 42]]) # List of lists |
| 52 |
True |
| 53 |
""" |
| 54 |
if not iterable: |
| 55 |
return False |
| 56 |
|
| 57 |
def is_odoo_tuple(elt): |
| 58 |
"""Return `True` if `elt` is a Odoo special tuple.""" |
| 59 |
try: |
| 60 |
return elt[:1][0] in [1, 2, 3, 4, 5] or elt[:2] in [ |
| 61 |
(6, 0), |
| 62 |
[6, 0], |
| 63 |
(0, 0), |
| 64 |
[0, 0], |
| 65 |
] |
| 66 |
except (TypeError, IndexError): |
| 67 |
return False |
| 68 |
|
| 69 |
return any(is_odoo_tuple(elt) for elt in iterable) |
| 70 |
|
| 71 |
|
| 72 |
def tuples2ids(tuples, ids): |
| 73 |
"""Update `ids` according to `tuples`, e.g. (3, 0, X), (4, 0, X)...""" |
| 74 |
for value in tuples: |
| 75 |
if value[0] == 6 and value[2]: |
| 76 |
ids = value[2] |
| 77 |
elif value[0] == 5: |
| 78 |
ids[:] = [] |
| 79 |
elif value[0] == 4 and value[1] and value[1] not in ids: |
| 80 |
ids.append(value[1]) |
| 81 |
elif value[0] == 3 and value[1] and value[1] in ids: |
| 82 |
ids.remove(value[1]) |
| 83 |
return ids |
| 84 |
|
| 85 |
|
| 86 |
def records2ids(iterable): |
| 87 |
"""Replace records contained in `iterable` with their corresponding IDs: |
| 88 |
|
| 89 |
>>> groups = list(odoo.env.user.groups_id) |
| 90 |
>>> records2ids(groups) |
| 91 |
[1, 2, 3, 14, 17, 18, 19, 7, 8, 9, 5, 20, 21, 22, 23] |
| 92 |
""" |
| 93 |
|
| 94 |
def record2id(elt): |
| 95 |
"""If `elt` is a record, return its ID.""" |
| 96 |
if isinstance(elt, Model): |
| 97 |
return elt.id |
| 98 |
return elt |
| 99 |
|
| 100 |
return [record2id(elt) for elt in iterable] |
| 101 |
|
| 102 |
|
| 103 |
class BaseField(object): |
| 104 |
"""Field which all other fields inherit. |
| 105 |
Manage common metadata. |
| 106 |
""" |
| 107 |
|
| 108 |
def __init__(self, name, data): |
| 109 |
self.name = name |
| 110 |
self.type = 'type' in data and data['type'] or False |
| 111 |
self.string = 'string' in data and data['string'] or False |
| 112 |
self.size = 'size' in data and data['size'] or False |
| 113 |
self.required = 'required' in data and data['required'] or False |
| 114 |
self.readonly = 'readonly' in data and data['readonly'] or False |
| 115 |
self.help = 'help' in data and data['help'] or False |
| 116 |
self.states = 'states' in data and data['states'] or False |
| 117 |
|
| 118 |
def __get__(self, instance, owner): |
| 119 |
pass |
| 120 |
|
| 121 |
def __set__(self, instance, value): |
| 122 |
"""Each time a record is modified, it is marked as dirty |
| 123 |
in the environment. |
| 124 |
""" |
| 125 |
instance.env.dirty.add(instance) |
| 126 |
if instance._odoo.config.get('auto_commit'): |
| 127 |
instance.env.commit() |
| 128 |
|
| 129 |
def __str__(self): |
| 130 |
"""Return a human readable string representation of the field.""" |
| 131 |
attrs = [ |
| 132 |
'string', |
| 133 |
'relation', |
| 134 |
'required', |
| 135 |
'readonly', |
| 136 |
'size', |
| 137 |
'domain', |
| 138 |
] |
| 139 |
attrs_rep = [] |
| 140 |
for attr in attrs: |
| 141 |
if hasattr(self, attr): |
| 142 |
value = getattr(self, attr) |
| 143 |
if value: |
| 144 |
if is_string(value): |
| 145 |
attrs_rep.append("{}='{}'".format(attr, value)) |
| 146 |
else: |
| 147 |
attrs_rep.append("{}={}".format(attr, value)) |
| 148 |
attrs_rep = ", ".join(attrs_rep) |
| 149 |
return "{}({})".format(self.type, attrs_rep) |
| 150 |
|
| 151 |
def check_required(self, value): |
| 152 |
"""Check the value if the field is required. |
| 153 |
|
| 154 |
Aim to be overridden by field classes. |
| 155 |
|
| 156 |
:return: `True` if the value is accepted, `False` otherwise. |
| 157 |
""" |
| 158 |
return bool(value) |
| 159 |
|
| 160 |
def check_value(self, value): |
| 161 |
"""Check the validity of a value for the field.""" |
| 162 |
# if self.readonly: |
| 163 |
# raise error.Error( |
| 164 |
# "'{field_name}' field is readonly".format( |
| 165 |
# field_name=self.name)) |
| 166 |
if value and self.size: |
| 167 |
if not is_string(value): |
| 168 |
raise ValueError("Value supplied has to be a string") |
| 169 |
if len(value) > self.size: |
| 170 |
raise ValueError( |
| 171 |
"Lenght of the '{}' is limited to {}".format( |
| 172 |
self.name, self.size |
| 173 |
) |
| 174 |
) |
| 175 |
if self.required and not self.check_required(value): |
| 176 |
raise ValueError("'{}' field is required".format(self.name)) |
| 177 |
return value |
| 178 |
|
| 179 |
def store(self, record, value): |
| 180 |
"""Store the value in the record.""" |
| 181 |
record._values[self.name][record.id] = value |
| 182 |
|
| 183 |
|
| 184 |
class Binary(BaseField): |
| 185 |
"""Equivalent of the `fields.Binary` class.""" |
| 186 |
|
| 187 |
def __init__(self, name, data): |
| 188 |
super(Binary, self).__init__(name, data) |
| 189 |
|
| 190 |
def __get__(self, instance, owner): |
| 191 |
value = instance._values[self.name][instance.id] |
| 192 |
if instance.id in instance._values_to_write[self.name]: |
| 193 |
value = instance._values_to_write[self.name][instance.id] |
| 194 |
return value |
| 195 |
|
| 196 |
def __set__(self, instance, value): |
| 197 |
if value is None: |
| 198 |
value = False |
| 199 |
value = self.check_value(value) |
| 200 |
instance._values_to_write[self.name][instance.id] = value |
| 201 |
super(Binary, self).__set__(instance, value) |
| 202 |
|
| 203 |
|
| 204 |
class Boolean(BaseField): |
| 205 |
"""Equivalent of the `fields.Boolean` class.""" |
| 206 |
|
| 207 |
def __init__(self, name, data): |
| 208 |
super(Boolean, self).__init__(name, data) |
| 209 |
|
| 210 |
def __get__(self, instance, owner): |
| 211 |
value = instance._values[self.name][instance.id] |
| 212 |
if instance.id in instance._values_to_write[self.name]: |
| 213 |
value = instance._values_to_write[self.name][instance.id] |
| 214 |
return value |
| 215 |
|
| 216 |
def __set__(self, instance, value): |
| 217 |
value = bool(value) |
| 218 |
value = self.check_value(value) |
| 219 |
instance._values_to_write[self.name][instance.id] = value |
| 220 |
super(Boolean, self).__set__(instance, value) |
| 221 |
|
| 222 |
|
| 223 |
class Char(BaseField): |
| 224 |
"""Equivalent of the `fields.Char` class.""" |
| 225 |
|
| 226 |
def __init__(self, name, data): |
| 227 |
super(Char, self).__init__(name, data) |
| 228 |
|
| 229 |
def __get__(self, instance, owner): |
| 230 |
value = instance._values[self.name].get(instance.id) |
| 231 |
if instance.id in instance._values_to_write[self.name]: |
| 232 |
value = instance._values_to_write[self.name][instance.id] |
| 233 |
return value |
| 234 |
|
| 235 |
def __set__(self, instance, value): |
| 236 |
if value is None: |
| 237 |
value = False |
| 238 |
value = self.check_value(value) |
| 239 |
instance._values_to_write[self.name][instance.id] = value |
| 240 |
super(Char, self).__set__(instance, value) |
| 241 |
|
| 242 |
|
| 243 |
class Date(BaseField): |
| 244 |
"""Represent the OpenObject 'fields.data'""" |
| 245 |
|
| 246 |
pattern = "%Y-%m-%d" |
| 247 |
|
| 248 |
def __init__(self, name, data): |
| 249 |
super(Date, self).__init__(name, data) |
| 250 |
|
| 251 |
def __get__(self, instance, owner): |
| 252 |
value = instance._values[self.name].get(instance.id) or False |
| 253 |
if instance.id in instance._values_to_write[self.name]: |
| 254 |
value = instance._values_to_write[self.name][instance.id] |
| 255 |
try: |
| 256 |
res = datetime.datetime.strptime(value, self.pattern).date() |
| 257 |
except (ValueError, TypeError): |
| 258 |
res = value |
| 259 |
return res |
| 260 |
|
| 261 |
def __set__(self, instance, value): |
| 262 |
value = self.check_value(value) |
| 263 |
instance._values_to_write[self.name][instance.id] = value |
| 264 |
super(Date, self).__set__(instance, value) |
| 265 |
|
| 266 |
def check_value(self, value): |
| 267 |
super(Date, self).check_value(value) |
| 268 |
if isinstance(value, datetime.date): |
| 269 |
value = value.strftime("%Y-%m-%d") |
| 270 |
elif is_string(value): |
| 271 |
datetime.datetime.strptime(value, self.pattern) |
| 272 |
elif isinstance(value, bool) or value is None: |
| 273 |
return value |
| 274 |
else: |
| 275 |
raise ValueError("Expecting a datetime.date object or string") |
| 276 |
return value |
| 277 |
|
| 278 |
|
| 279 |
class Datetime(BaseField): |
| 280 |
"""Represent the OpenObject 'fields.datetime'""" |
| 281 |
|
| 282 |
pattern = "%Y-%m-%d %H:%M:%S" |
| 283 |
|
| 284 |
def __init__(self, name, data): |
| 285 |
super(Datetime, self).__init__(name, data) |
| 286 |
|
| 287 |
def __get__(self, instance, owner): |
| 288 |
value = instance._values[self.name].get(instance.id) |
| 289 |
if instance.id in instance._values_to_write[self.name]: |
| 290 |
value = instance._values_to_write[self.name][instance.id] |
| 291 |
try: |
| 292 |
res = datetime.datetime.strptime(value, self.pattern) |
| 293 |
except (ValueError, TypeError): |
| 294 |
res = value |
| 295 |
return res |
| 296 |
|
| 297 |
def __set__(self, instance, value): |
| 298 |
value = self.check_value(value) |
| 299 |
instance._values_to_write[self.name][instance.id] = value |
| 300 |
super(Datetime, self).__set__(instance, value) |
| 301 |
|
| 302 |
def check_value(self, value): |
| 303 |
super(Datetime, self).check_value(value) |
| 304 |
if isinstance(value, datetime.datetime): |
| 305 |
value = value.strftime("%Y-%m-%d %H:%M:%S") |
| 306 |
elif is_string(value): |
| 307 |
datetime.datetime.strptime(value, self.pattern) |
| 308 |
elif isinstance(value, bool): |
| 309 |
return value |
| 310 |
else: |
| 311 |
raise ValueError("Expecting a datetime.datetime object or string") |
| 312 |
return value |
| 313 |
|
| 314 |
|
| 315 |
class Float(BaseField): |
| 316 |
"""Equivalent of the `fields.Float` class.""" |
| 317 |
|
| 318 |
def __init__(self, name, data): |
| 319 |
super(Float, self).__init__(name, data) |
| 320 |
|
| 321 |
def __get__(self, instance, owner): |
| 322 |
value = instance._values[self.name].get(instance.id) |
| 323 |
if instance.id in instance._values_to_write[self.name]: |
| 324 |
value = instance._values_to_write[self.name][instance.id] |
| 325 |
if value in [None, False]: |
| 326 |
value = 0.0 |
| 327 |
return value |
| 328 |
|
| 329 |
def __set__(self, instance, value): |
| 330 |
if value is None: |
| 331 |
value = False |
| 332 |
value = self.check_value(value) |
| 333 |
instance._values_to_write[self.name][instance.id] = value |
| 334 |
super(Float, self).__set__(instance, value) |
| 335 |
|
| 336 |
def check_required(self, value): |
| 337 |
# Accept 0 values |
| 338 |
return super(Float, self).check_required() or value == 0 |
| 339 |
|
| 340 |
|
| 341 |
class Integer(BaseField): |
| 342 |
"""Equivalent of the `fields.Integer` class.""" |
| 343 |
|
| 344 |
def __init__(self, name, data): |
| 345 |
super(Integer, self).__init__(name, data) |
| 346 |
|
| 347 |
def __get__(self, instance, owner): |
| 348 |
value = instance._values[self.name].get(instance.id) |
| 349 |
if instance.id in instance._values_to_write[self.name]: |
| 350 |
value = instance._values_to_write[self.name][instance.id] |
| 351 |
if value in [None, False]: |
| 352 |
value = 0 |
| 353 |
return value |
| 354 |
|
| 355 |
def __set__(self, instance, value): |
| 356 |
if value is None: |
| 357 |
value = False |
| 358 |
value = self.check_value(value) |
| 359 |
instance._values_to_write[self.name][instance.id] = value |
| 360 |
super(Integer, self).__set__(instance, value) |
| 361 |
|
| 362 |
def check_required(self, value): |
| 363 |
# Accept 0 values |
| 364 |
return super(Float, self).check_required() or value == 0 |
| 365 |
|
| 366 |
|
| 367 |
class Selection(BaseField): |
| 368 |
"""Represent the OpenObject 'fields.selection'""" |
| 369 |
|
| 370 |
def __init__(self, name, data): |
| 371 |
super(Selection, self).__init__(name, data) |
| 372 |
self.selection = 'selection' in data and data['selection'] or False |
| 373 |
|
| 374 |
def __get__(self, instance, owner): |
| 375 |
value = instance._values[self.name].get(instance.id, False) |
| 376 |
if instance.id in instance._values_to_write[self.name]: |
| 377 |
value = instance._values_to_write[self.name][instance.id] |
| 378 |
return value |
| 379 |
|
| 380 |
def __set__(self, instance, value): |
| 381 |
if value is None: |
| 382 |
value = False |
| 383 |
value = self.check_value(value) |
| 384 |
instance._values_to_write[self.name][instance.id] = value |
| 385 |
super(Selection, self).__set__(instance, value) |
| 386 |
|
| 387 |
def check_value(self, value): |
| 388 |
super(Selection, self).check_value(value) |
| 389 |
selection = [val[0] for val in self.selection] |
| 390 |
if value and value not in selection: |
| 391 |
raise ValueError( |
| 392 |
"The value '{}' supplied doesn't match with the possible " |
| 393 |
"values '{}' for the '{}' field".format( |
| 394 |
value, selection, self.name |
| 395 |
) |
| 396 |
) |
| 397 |
return value |
| 398 |
|
| 399 |
|
| 400 |
class Many2many(BaseField): |
| 401 |
"""Represent the OpenObject 'fields.many2many'""" |
| 402 |
|
| 403 |
def __init__(self, name, data): |
| 404 |
super(Many2many, self).__init__(name, data) |
| 405 |
self.relation = 'relation' in data and data['relation'] or False |
| 406 |
self.context = 'context' in data and data['context'] or {} |
| 407 |
self.domain = 'domain' in data and data['domain'] or False |
| 408 |
|
| 409 |
def __get__(self, instance, owner): |
| 410 |
"""Return a recordset.""" |
| 411 |
ids = None |
| 412 |
if instance._values[self.name].get(instance.id): |
| 413 |
ids = instance._values[self.name][instance.id][:] |
| 414 |
# None value => get the value on the fly |
| 415 |
if ids is None: |
| 416 |
args = [[instance.id], [self.name]] |
| 417 |
kwargs = {'context': self.context, 'load': '_classic_write'} |
| 418 |
orig_ids = instance._odoo.execute_kw( |
| 419 |
instance._name, 'read', args, kwargs |
| 420 |
)[0][self.name] |
| 421 |
instance._values[self.name][instance.id] = orig_ids |
| 422 |
ids = orig_ids and orig_ids[:] or [] |
| 423 |
# Take updated values into account |
| 424 |
if instance.id in instance._values_to_write[self.name]: |
| 425 |
values = instance._values_to_write[self.name][instance.id] |
| 426 |
# Handle ODOO tuples to update 'ids' |
| 427 |
ids = tuples2ids(values, ids or []) |
| 428 |
# Handle the field context |
| 429 |
Relation = instance.env[self.relation] |
| 430 |
env = instance.env |
| 431 |
if self.context: |
| 432 |
context = instance.env.context.copy() |
| 433 |
context.update(self.context) |
| 434 |
env = instance.env(context=context) |
| 435 |
return Relation._browse(env, ids, from_record=(instance, self)) |
| 436 |
|
| 437 |
def __set__(self, instance, value): |
| 438 |
value = self.check_value(value) |
| 439 |
if isinstance(value, IncrementalRecords): |
| 440 |
value = value.tuples |
| 441 |
else: |
| 442 |
if value and not odoo_tuple_in(value): |
| 443 |
value = [(6, 0, records2ids(value))] |
| 444 |
elif not value: |
| 445 |
value = [(5,)] |
| 446 |
instance._values_to_write[self.name][instance.id] = value |
| 447 |
super(Many2many, self).__set__(instance, value) |
| 448 |
|
| 449 |
def check_value(self, value): |
| 450 |
if value: |
| 451 |
if ( |
| 452 |
not isinstance(value, list) |
| 453 |
and not isinstance(value, Model) |
| 454 |
and not isinstance(value, IncrementalRecords) |
| 455 |
): |
| 456 |
raise ValueError( |
| 457 |
"The value supplied has to be a list, a recordset " |
| 458 |
"or 'False'" |
| 459 |
) |
| 460 |
return super(Many2many, self).check_value(value) |
| 461 |
|
| 462 |
def store(self, record, value): |
| 463 |
"""Store the value in the record.""" |
| 464 |
if record._values[self.name].get(record.id): |
| 465 |
tuples2ids(value, record._values[self.name][record.id]) |
| 466 |
else: |
| 467 |
record._values[self.name][record.id] = tuples2ids(value, []) |
| 468 |
|
| 469 |
|
| 470 |
class Many2one(BaseField): |
| 471 |
"""Represent the OpenObject 'fields.many2one'""" |
| 472 |
|
| 473 |
def __init__(self, name, data): |
| 474 |
super(Many2one, self).__init__(name, data) |
| 475 |
self.relation = 'relation' in data and data['relation'] or False |
| 476 |
self.context = 'context' in data and data['context'] or {} |
| 477 |
self.domain = 'domain' in data and data['domain'] or False |
| 478 |
|
| 479 |
def __get__(self, instance, owner): |
| 480 |
id_ = instance._values[self.name].get(instance.id) |
| 481 |
if instance.id in instance._values_to_write[self.name]: |
| 482 |
id_ = instance._values_to_write[self.name][instance.id] |
| 483 |
# None value => get the value on the fly |
| 484 |
if id_ is None: |
| 485 |
args = [[instance.id], [self.name]] |
| 486 |
kwargs = {'context': self.context, 'load': '_classic_write'} |
| 487 |
id_ = instance._odoo.execute_kw( |
| 488 |
instance._name, 'read', args, kwargs |
| 489 |
)[0][self.name] |
| 490 |
instance._values[self.name][instance.id] = id_ |
| 491 |
Relation = instance.env[self.relation] |
| 492 |
if id_: |
| 493 |
env = instance.env |
| 494 |
if self.context: |
| 495 |
context = instance.env.context.copy() |
| 496 |
context.update(self.context) |
| 497 |
env = instance.env(context=context) |
| 498 |
return Relation._browse(env, id_, from_record=(instance, self)) |
| 499 |
return Relation.browse(False) |
| 500 |
|
| 501 |
def __set__(self, instance, value): |
| 502 |
if isinstance(value, Model): |
| 503 |
o_rel = value |
| 504 |
elif is_int(value): |
| 505 |
rel_obj = instance.env[self.relation] |
| 506 |
o_rel = rel_obj.browse(value) |
| 507 |
elif value in [None, False]: |
| 508 |
o_rel = False |
| 509 |
else: |
| 510 |
raise ValueError( |
| 511 |
"Value supplied has to be an integer, " |
| 512 |
"a record object or 'None/False'." |
| 513 |
) |
| 514 |
o_rel = self.check_value(o_rel) |
| 515 |
# instance.__data__['updated_values'][self.name] = \ |
| 516 |
# o_rel and [o_rel.id, False] |
| 517 |
instance._values_to_write[self.name][instance.id] = ( |
| 518 |
o_rel and o_rel.id or False |
| 519 |
) |
| 520 |
super(Many2one, self).__set__(instance, value) |
| 521 |
|
| 522 |
def check_value(self, value): |
| 523 |
super(Many2one, self).check_value(value) |
| 524 |
if value and value._name != self.relation: |
| 525 |
raise ValueError( |
| 526 |
( |
| 527 |
"Instance of '{model}' supplied doesn't match with the " |
| 528 |
+ "relation '{relation}' of the '{field_name}' field." |
| 529 |
).format( |
| 530 |
model=value._name, |
| 531 |
relation=self.relation, |
| 532 |
field_name=self.name, |
| 533 |
) |
| 534 |
) |
| 535 |
return value |
| 536 |
|
| 537 |
|
| 538 |
class One2many(BaseField): |
| 539 |
"""Represent the OpenObject 'fields.one2many'""" |
| 540 |
|
| 541 |
def __init__(self, name, data): |
| 542 |
super(One2many, self).__init__(name, data) |
| 543 |
self.relation = 'relation' in data and data['relation'] or False |
| 544 |
self.context = 'context' in data and data['context'] or {} |
| 545 |
self.domain = 'domain' in data and data['domain'] or False |
| 546 |
|
| 547 |
def __get__(self, instance, owner): |
| 548 |
"""Return a recordset.""" |
| 549 |
ids = None |
| 550 |
if instance._values[self.name].get(instance.id): |
| 551 |
ids = instance._values[self.name][instance.id][:] |
| 552 |
# None value => get the value on the fly |
| 553 |
if ids is None: |
| 554 |
args = [[instance.id], [self.name]] |
| 555 |
kwargs = {'context': self.context, 'load': '_classic_write'} |
| 556 |
orig_ids = instance._odoo.execute_kw( |
| 557 |
instance._name, 'read', args, kwargs |
| 558 |
)[0][self.name] |
| 559 |
instance._values[self.name][instance.id] = orig_ids |
| 560 |
ids = orig_ids and orig_ids[:] or [] |
| 561 |
# Take updated values into account |
| 562 |
if instance.id in instance._values_to_write[self.name]: |
| 563 |
values = instance._values_to_write[self.name][instance.id] |
| 564 |
# Handle ODOO tuples to update 'ids' |
| 565 |
ids = tuples2ids(values, ids or []) |
| 566 |
Relation = instance.env[self.relation] |
| 567 |
env = instance.env |
| 568 |
if self.context: |
| 569 |
context = instance.env.context.copy() |
| 570 |
context.update(self.context) |
| 571 |
env = instance.env(context=context) |
| 572 |
return Relation._browse(env, ids, from_record=(instance, self)) |
| 573 |
|
| 574 |
def __set__(self, instance, value): |
| 575 |
value = self.check_value(value) |
| 576 |
if isinstance(value, IncrementalRecords): |
| 577 |
value = value.tuples |
| 578 |
else: |
| 579 |
if value and not odoo_tuple_in(value): |
| 580 |
value = [(6, 0, records2ids(value))] |
| 581 |
elif not value: |
| 582 |
value = [(5,)] |
| 583 |
instance._values_to_write[self.name][instance.id] = value |
| 584 |
super(One2many, self).__set__(instance, value) |
| 585 |
|
| 586 |
def check_value(self, value): |
| 587 |
if value: |
| 588 |
if ( |
| 589 |
not isinstance(value, list) |
| 590 |
and not isinstance(value, Model) |
| 591 |
and not isinstance(value, IncrementalRecords) |
| 592 |
): |
| 593 |
raise ValueError( |
| 594 |
"The value supplied has to be a list, a recordset " |
| 595 |
"or 'False'" |
| 596 |
) |
| 597 |
return super(One2many, self).check_value(value) |
| 598 |
|
| 599 |
def store(self, record, value): |
| 600 |
"""Store the value in the record.""" |
| 601 |
if record._values[self.name].get(record.id): |
| 602 |
tuples2ids(value, record._values[self.name][record.id]) |
| 603 |
else: |
| 604 |
record._values[self.name][record.id] = tuples2ids(value, []) |
| 605 |
|
| 606 |
|
| 607 |
class Reference(BaseField): |
| 608 |
"""Represent the OpenObject 'fields.reference'.""" |
| 609 |
|
| 610 |
def __init__(self, name, data): |
| 611 |
super(Reference, self).__init__(name, data) |
| 612 |
self.context = 'context' in data and data['context'] or {} |
| 613 |
self.domain = 'domain' in data and data['domain'] or False |
| 614 |
self.selection = 'selection' in data and data['selection'] or False |
| 615 |
|
| 616 |
def __get__(self, instance, owner): |
| 617 |
value = instance._values[self.name].get(instance.id) or False |
| 618 |
if instance.id in instance._values_to_write[self.name]: |
| 619 |
value = instance._values_to_write[self.name][instance.id] |
| 620 |
# None value => get the value on the fly |
| 621 |
if value is None: |
| 622 |
args = [[instance.id], [self.name]] |
| 623 |
kwargs = {'context': self.context, 'load': '_classic_write'} |
| 624 |
value = instance._odoo.execute_kw( |
| 625 |
instance._name, 'read', args, kwargs |
| 626 |
)[0][self.name] |
| 627 |
instance._values_to_write[self.name][instance.id] = value |
| 628 |
if value: |
| 629 |
parts = value.rpartition(',') |
| 630 |
relation, o_id = parts[0], parts[2] |
| 631 |
relation = relation.strip() |
| 632 |
o_id = int(o_id.strip()) |
| 633 |
if relation and o_id: |
| 634 |
Relation = instance.env[relation] |
| 635 |
env = instance.env |
| 636 |
if self.context: |
| 637 |
context = instance.env.context.copy() |
| 638 |
context.update(self.context) |
| 639 |
env = instance.env(context=context) |
| 640 |
return Relation._browse( |
| 641 |
env, o_id, from_record=(instance, self) |
| 642 |
) |
| 643 |
return False |
| 644 |
|
| 645 |
def __set__(self, instance, value): |
| 646 |
value = self.check_value(value) |
| 647 |
instance._values_to_write[self.name][instance.id] = value |
| 648 |
super(Reference, self).__set__(instance, value) |
| 649 |
|
| 650 |
def _check_relation(self, relation): |
| 651 |
"""Raise a `ValueError` if `relation` is not allowed among |
| 652 |
the possible values. |
| 653 |
""" |
| 654 |
selection = [val[0] for val in self.selection] |
| 655 |
if relation not in selection: |
| 656 |
raise ValueError( |
| 657 |
( |
| 658 |
"The value '{value}' supplied doesn't match with the possible" |
| 659 |
" values '{selection}' for the '{field_name}' field" |
| 660 |
).format( |
| 661 |
value=relation, selection=selection, field_name=self.name |
| 662 |
) |
| 663 |
) |
| 664 |
return relation |
| 665 |
|
| 666 |
def check_value(self, value): |
| 667 |
if isinstance(value, Model): |
| 668 |
relation = value.__class__.__osv__['name'] |
| 669 |
self._check_relation(relation) |
| 670 |
value = "{},{}".format(relation, value.id) |
| 671 |
super(Reference, self).check_value(value) |
| 672 |
elif is_string(value): |
| 673 |
super(Reference, self).check_value(value) |
| 674 |
parts = value.rpartition(',') |
| 675 |
relation, o_id = parts[0], parts[2] |
| 676 |
relation = relation.strip() |
| 677 |
o_id = o_id.strip() |
| 678 |
# o_rel = instance.__class__.__odoo__.browse(relation, o_id) |
| 679 |
if not relation or not is_int(o_id): |
| 680 |
raise ValueError( |
| 681 |
"String not well formatted, expecting " |
| 682 |
"'{relation},{id}' format" |
| 683 |
) |
| 684 |
self._check_relation(relation) |
| 685 |
else: |
| 686 |
raise ValueError( |
| 687 |
"Value supplied has to be a string or" |
| 688 |
" a browse_record object." |
| 689 |
) |
| 690 |
return value |
| 691 |
|
| 692 |
|
| 693 |
class Text(BaseField): |
| 694 |
"""Equivalent of the `fields.Text` class.""" |
| 695 |
|
| 696 |
def __init__(self, name, data): |
| 697 |
super(Text, self).__init__(name, data) |
| 698 |
|
| 699 |
def __get__(self, instance, owner): |
| 700 |
value = instance._values[self.name].get(instance.id) |
| 701 |
if instance.id in instance._values_to_write[self.name]: |
| 702 |
value = instance._values_to_write[self.name][instance.id] |
| 703 |
return value |
| 704 |
|
| 705 |
def __set__(self, instance, value): |
| 706 |
if value is None: |
| 707 |
value = False |
| 708 |
value = self.check_value(value) |
| 709 |
instance._values_to_write[self.name][instance.id] = value |
| 710 |
super(Text, self).__set__(instance, value) |
| 711 |
|
| 712 |
|
| 713 |
class Html(Text): |
| 714 |
"""Equivalent of the `fields.Html` class.""" |
| 715 |
|
| 716 |
def __init__(self, name, data): |
| 717 |
super(Html, self).__init__(name, data) |
| 718 |
|
| 719 |
|
| 720 |
class Unknown(BaseField): |
| 721 |
"""Represent an unknown field. This should not happen but this kind of |
| 722 |
field only exists to avoid a blocking situation from a RPC point of view. |
| 723 |
""" |
| 724 |
|
| 725 |
def __init__(self, name, data): |
| 726 |
super(Unknown, self).__init__(name, data) |
| 727 |
|
| 728 |
def __get__(self, instance, owner): |
| 729 |
value = instance._values[self.name][instance.id] |
| 730 |
if instance.id in instance._values_to_write[self.name]: |
| 731 |
value = instance._values_to_write[self.name][instance.id] |
| 732 |
return value |
| 733 |
|
| 734 |
def __set__(self, instance, value): |
| 735 |
value = self.check_value(value) |
| 736 |
instance._values_to_write[self.name][instance.id] = value |
| 737 |
super(Unknown, self).__set__(instance, value) |
| 738 |
|
| 739 |
|
| 740 |
TYPES_TO_FIELDS = { |
| 741 |
'binary': Binary, |
| 742 |
'boolean': Boolean, |
| 743 |
'char': Char, |
| 744 |
'date': Date, |
| 745 |
'datetime': Datetime, |
| 746 |
'float': Float, |
| 747 |
'html': Html, |
| 748 |
'integer': Integer, |
| 749 |
'many2many': Many2many, |
| 750 |
'many2one': Many2one, |
| 751 |
'one2many': One2many, |
| 752 |
'reference': Reference, |
| 753 |
'selection': Selection, |
| 754 |
'text': Text, |
| 755 |
} |
| 756 |
|
| 757 |
|
| 758 |
def generate_field(name, data): |
| 759 |
"""Generate a well-typed field according to the data dictionary supplied |
| 760 |
(obtained via the `fields_get' method of any models). |
| 761 |
""" |
| 762 |
assert 'type' in data |
| 763 |
field = TYPES_TO_FIELDS.get(data['type'], Unknown)(name, data) |
| 764 |
return field |