gooderp18绿色标准版
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

5389 rindas
231KB

  1. # -*- coding: utf-8 -*-
  2. # Part of Odoo. See LICENSE file for full copyright and licensing details.
  3. """ High-level objects for fields. """
  4. from __future__ import annotations
  5. from collections import defaultdict
  6. from datetime import date, datetime, time
  7. from operator import attrgetter
  8. from xmlrpc.client import MAXINT
  9. import ast
  10. import base64
  11. import copy
  12. import contextlib
  13. import binascii
  14. import enum
  15. import itertools
  16. import json
  17. import logging
  18. import uuid
  19. import warnings
  20. import psycopg2
  21. import pytz
  22. from markupsafe import Markup, escape as markup_escape
  23. from psycopg2.extras import Json as PsycopgJson
  24. from difflib import get_close_matches, unified_diff
  25. from hashlib import sha256
  26. from .models import check_property_field_value_name
  27. from .netsvc import ColoredFormatter, GREEN, RED, DEFAULT, COLOR_PATTERN
  28. from .tools import (
  29. float_repr, float_round, float_compare, float_is_zero, human_size,
  30. OrderedSet, sql, SQL, date_utils, unique, lazy_property,
  31. image_process, merge_sequences, is_list_of,
  32. html_normalize, html_sanitize,
  33. DEFAULT_SERVER_DATE_FORMAT as DATE_FORMAT,
  34. DEFAULT_SERVER_DATETIME_FORMAT as DATETIME_FORMAT,
  35. )
  36. from .tools.sql import pg_varchar
  37. from .tools.mimetypes import guess_mimetype
  38. from .tools.misc import unquote, has_list_types, Sentinel, SENTINEL
  39. from .tools.translate import html_translate
  40. from odoo import SUPERUSER_ID
  41. from odoo.exceptions import CacheMiss
  42. from odoo.osv import expression
  43. import typing
  44. from odoo.api import ContextType, DomainType, IdType, NewId, M, T
  45. DATE_LENGTH = len(date.today().strftime(DATE_FORMAT))
  46. DATETIME_LENGTH = len(datetime.now().strftime(DATETIME_FORMAT))
  47. # hacky-ish way to prevent access to a field through the ORM (except for sudo mode)
  48. NO_ACCESS='.'
  49. IR_MODELS = (
  50. 'ir.model', 'ir.model.data', 'ir.model.fields', 'ir.model.fields.selection',
  51. 'ir.model.relation', 'ir.model.constraint', 'ir.module.module',
  52. )
  53. COMPANY_DEPENDENT_FIELDS = (
  54. 'char', 'float', 'boolean', 'integer', 'text', 'many2one', 'date', 'datetime', 'selection', 'html'
  55. )
  56. _logger = logging.getLogger(__name__)
  57. _schema = logging.getLogger(__name__[:-7] + '.schema')
  58. NoneType = type(None)
  59. def first(records):
  60. """ Return the first record in ``records``, with the same prefetching. """
  61. return next(iter(records)) if len(records) > 1 else records
  62. def resolve_mro(model, name, predicate):
  63. """ Return the list of successively overridden values of attribute ``name``
  64. in mro order on ``model`` that satisfy ``predicate``. Model registry
  65. classes are ignored.
  66. """
  67. result = []
  68. for cls in model._model_classes__:
  69. value = cls.__dict__.get(name, SENTINEL)
  70. if value is SENTINEL:
  71. continue
  72. if not predicate(value):
  73. break
  74. result.append(value)
  75. return result
  76. def determine(needle, records, *args):
  77. """ Simple helper for calling a method given as a string or a function.
  78. :param needle: callable or name of method to call on ``records``
  79. :param BaseModel records: recordset to call ``needle`` on or with
  80. :params args: additional arguments to pass to the determinant
  81. :returns: the determined value if the determinant is a method name or callable
  82. :raise TypeError: if ``records`` is not a recordset, or ``needle`` is not
  83. a callable or valid method name
  84. """
  85. if not isinstance(records, BaseModel):
  86. raise TypeError("Determination requires a subject recordset")
  87. if isinstance(needle, str):
  88. needle = getattr(records, needle)
  89. if needle.__name__.find('__'):
  90. return needle(*args)
  91. elif callable(needle):
  92. if needle.__name__.find('__'):
  93. return needle(records, *args)
  94. raise TypeError("Determination requires a callable or method name")
  95. class MetaField(type):
  96. """ Metaclass for field classes. """
  97. by_type = {}
  98. def __init__(cls, name, bases, attrs):
  99. super(MetaField, cls).__init__(name, bases, attrs)
  100. if not hasattr(cls, 'type'):
  101. return
  102. if cls.type and cls.type not in MetaField.by_type:
  103. MetaField.by_type[cls.type] = cls
  104. # compute class attributes to avoid calling dir() on fields
  105. cls.related_attrs = []
  106. cls.description_attrs = []
  107. for attr in dir(cls):
  108. if attr.startswith('_related_'):
  109. cls.related_attrs.append((attr[9:], attr))
  110. elif attr.startswith('_description_'):
  111. cls.description_attrs.append((attr[13:], attr))
  112. _global_seq = iter(itertools.count())
  113. class Field(MetaField('DummyField', (object,), {}), typing.Generic[T]):
  114. """The field descriptor contains the field definition, and manages accesses
  115. and assignments of the corresponding field on records. The following
  116. attributes may be provided when instantiating a field:
  117. :param str string: the label of the field seen by users; if not
  118. set, the ORM takes the field name in the class (capitalized).
  119. :param str help: the tooltip of the field seen by users
  120. :param bool readonly: whether the field is readonly (default: ``False``)
  121. This only has an impact on the UI. Any field assignation in code will work
  122. (if the field is a stored field or an inversable one).
  123. :param bool required: whether the value of the field is required (default: ``False``)
  124. :param str index: whether the field is indexed in database, and the kind of index.
  125. Note: this has no effect on non-stored and virtual fields.
  126. The possible values are:
  127. * ``"btree"`` or ``True``: standard index, good for many2one
  128. * ``"btree_not_null"``: BTREE index without NULL values (useful when most
  129. values are NULL, or when NULL is never searched for)
  130. * ``"trigram"``: Generalized Inverted Index (GIN) with trigrams (good for full-text search)
  131. * ``None`` or ``False``: no index (default)
  132. :param default: the default value for the field; this is either a static
  133. value, or a function taking a recordset and returning a value; use
  134. ``default=None`` to discard default values for the field
  135. :type default: value or callable
  136. :param str groups: comma-separated list of group xml ids (string); this
  137. restricts the field access to the users of the given groups only
  138. :param bool company_dependent: whether the field value is dependent of the current company;
  139. The value is stored on the model table as jsonb dict with the company id as the key.
  140. The field's default values stored in model ir.default are used as fallbacks for
  141. unspecified values in the jsonb dict.
  142. :param bool copy: whether the field value should be copied when the record
  143. is duplicated (default: ``True`` for normal fields, ``False`` for
  144. ``one2many`` and computed fields, including property fields and
  145. related fields)
  146. :param bool store: whether the field is stored in database
  147. (default:``True``, ``False`` for computed fields)
  148. :param str aggregator: aggregate function used by :meth:`~odoo.models.Model.read_group`
  149. when grouping on this field.
  150. Supported aggregate functions are:
  151. * ``array_agg`` : values, including nulls, concatenated into an array
  152. * ``count`` : number of rows
  153. * ``count_distinct`` : number of distinct rows
  154. * ``bool_and`` : true if all values are true, otherwise false
  155. * ``bool_or`` : true if at least one value is true, otherwise false
  156. * ``max`` : maximum value of all values
  157. * ``min`` : minimum value of all values
  158. * ``avg`` : the average (arithmetic mean) of all values
  159. * ``sum`` : sum of all values
  160. :param str group_expand: function used to expand read_group results when grouping on
  161. the current field. For selection fields, ``group_expand=True`` automatically
  162. expands groups for all selection keys.
  163. .. code-block:: python
  164. @api.model
  165. def _read_group_selection_field(self, values, domain, order):
  166. return ['choice1', 'choice2', ...] # available selection choices.
  167. @api.model
  168. def _read_group_many2one_field(self, records, domain, order):
  169. return records + self.search([custom_domain])
  170. .. rubric:: Computed Fields
  171. :param str compute: name of a method that computes the field
  172. .. seealso:: :ref:`Advanced Fields/Compute fields <reference/fields/compute>`
  173. :param bool precompute: whether the field should be computed before record insertion
  174. in database. Should be used to specify manually some fields as precompute=True
  175. when the field can be computed before record insertion.
  176. (e.g. avoid statistics fields based on search/read_group), many2one
  177. linking to the previous record, ... (default: `False`)
  178. .. warning::
  179. Precomputation only happens when no explicit value and no default
  180. value is provided to create(). This means that a default value
  181. disables the precomputation, even if the field is specified as
  182. precompute=True.
  183. Precomputing a field can be counterproductive if the records of the
  184. given model are not created in batch. Consider the situation were
  185. many records are created one by one. If the field is not
  186. precomputed, it will normally be computed in batch at the flush(),
  187. and the prefetching mechanism will help making the computation
  188. efficient. On the other hand, if the field is precomputed, the
  189. computation will be made one by one, and will therefore not be able
  190. to take advantage of the prefetching mechanism.
  191. Following the remark above, precomputed fields can be interesting on
  192. the lines of a one2many, which are usually created in batch by the
  193. ORM itself, provided that they are created by writing on the record
  194. that contains them.
  195. :param bool compute_sudo: whether the field should be recomputed as superuser
  196. to bypass access rights (by default ``True`` for stored fields, ``False``
  197. for non stored fields)
  198. :param bool recursive: whether the field has recursive dependencies (the field
  199. ``X`` has a dependency like ``parent_id.X``); declaring a field recursive
  200. must be explicit to guarantee that recomputation is correct
  201. :param str inverse: name of a method that inverses the field (optional)
  202. :param str search: name of a method that implement search on the field (optional)
  203. :param str related: sequence of field names
  204. :param bool default_export_compatible: whether the field must be exported by default in an import-compatible export
  205. .. seealso:: :ref:`Advanced fields/Related fields <reference/fields/related>`
  206. """
  207. type: str # type of the field (string)
  208. relational = False # whether the field is a relational one
  209. translate = False # whether the field is translated
  210. write_sequence = 0 # field ordering for write()
  211. # Database column type (ident, spec) for non-company-dependent fields.
  212. # Company-dependent fields are stored as jsonb (see column_type).
  213. _column_type: typing.Tuple[str, str] | None = None
  214. args = None # the parameters given to __init__()
  215. _module = None # the field's module name
  216. _modules = None # modules that define this field
  217. _setup_done = True # whether the field is completely set up
  218. _sequence = None # absolute ordering of the field
  219. _base_fields = () # the fields defining self, in override order
  220. _extra_keys = () # unknown attributes set on the field
  221. _direct = False # whether self may be used directly (shared)
  222. _toplevel = False # whether self is on the model's registry class
  223. automatic = False # whether the field is automatically created ("magic" field)
  224. inherited = False # whether the field is inherited (_inherits)
  225. inherited_field = None # the corresponding inherited field
  226. name: str # name of the field
  227. model_name: str | None = None # name of the model of this field
  228. comodel_name: str | None = None # name of the model of values (if relational)
  229. store = True # whether the field is stored in database
  230. index = None # how the field is indexed in database
  231. manual = False # whether the field is a custom field
  232. copy = True # whether the field is copied over by BaseModel.copy()
  233. _depends = None # collection of field dependencies
  234. _depends_context = None # collection of context key dependencies
  235. recursive = False # whether self depends on itself
  236. compute = None # compute(recs) computes field on recs
  237. compute_sudo = False # whether field should be recomputed as superuser
  238. precompute = False # whether field has to be computed before creation
  239. inverse = None # inverse(recs) inverses field on recs
  240. search = None # search(recs, operator, value) searches on self
  241. related = None # sequence of field names, for related fields
  242. company_dependent = False # whether ``self`` is company-dependent (property field)
  243. default = None # default(recs) returns the default value
  244. string: str | None = None # field label
  245. export_string_translation = True # whether the field label translations are exported
  246. help: str | None = None # field tooltip
  247. readonly = False # whether the field is readonly
  248. required = False # whether the field is required
  249. groups: str | None = None # csv list of group xml ids
  250. change_default = False # whether the field may trigger a "user-onchange"
  251. related_field = None # corresponding related field
  252. aggregator = None # operator for aggregating values
  253. group_expand = None # name of method to expand groups in read_group()
  254. prefetch = True # the prefetch group (False means no group)
  255. default_export_compatible = False # whether the field must be exported by default in an import-compatible export
  256. exportable = True
  257. def __init__(self, string: str | Sentinel = SENTINEL, **kwargs):
  258. kwargs['string'] = string
  259. self._sequence = next(_global_seq)
  260. self.args = {key: val for key, val in kwargs.items() if val is not SENTINEL}
  261. def __str__(self):
  262. if self.name is None:
  263. return "<%s.%s>" % (__name__, type(self).__name__)
  264. return "%s.%s" % (self.model_name, self.name)
  265. def __repr__(self):
  266. if self.name is None:
  267. return f"{'<%s.%s>'!r}" % (__name__, type(self).__name__)
  268. return f"{'%s.%s'!r}" % (self.model_name, self.name)
  269. ############################################################################
  270. #
  271. # Base field setup: things that do not depend on other models/fields
  272. #
  273. # The base field setup is done by field.__set_name__(), which determines the
  274. # field's name, model name, module and its parameters.
  275. #
  276. # The dictionary field.args gives the parameters passed to the field's
  277. # constructor. Most parameters have an attribute of the same name on the
  278. # field. The parameters as attributes are assigned by the field setup.
  279. #
  280. # When several definition classes of the same model redefine a given field,
  281. # the field occurrences are "merged" into one new field instantiated at
  282. # runtime on the registry class of the model. The occurrences of the field
  283. # are given to the new field as the parameter '_base_fields'; it is a list
  284. # of fields in override order (or reverse MRO).
  285. #
  286. # In order to save memory, a field should avoid having field.args and/or
  287. # many attributes when possible. We call "direct" a field that can be set
  288. # up directly from its definition class. Direct fields are non-related
  289. # fields defined on models, and can be shared across registries. We call
  290. # "toplevel" a field that is put on the model's registry class, and is
  291. # therefore specific to the registry.
  292. #
  293. # Toplevel field are set up once, and are no longer set up from scratch
  294. # after that. Those fields can save memory by discarding field.args and
  295. # field._base_fields once set up, because those are no longer necessary.
  296. #
  297. # Non-toplevel non-direct fields are the fields on definition classes that
  298. # may not be shared. In other words, those fields are never used directly,
  299. # and are always recreated as toplevel fields. On those fields, the base
  300. # setup is useless, because only field.args is used for setting up other
  301. # fields. We therefore skip the base setup for those fields. The only
  302. # attributes of those fields are: '_sequence', 'args', 'model_name', 'name'
  303. # and '_module', which makes their __dict__'s size minimal.
  304. def __set_name__(self, owner, name):
  305. """ Perform the base setup of a field.
  306. :param owner: the owner class of the field (the model's definition or registry class)
  307. :param name: the name of the field
  308. """
  309. assert issubclass(owner, BaseModel)
  310. self.model_name = owner._name
  311. self.name = name
  312. if is_definition_class(owner):
  313. # only for fields on definition classes, not registry classes
  314. self._module = owner._module
  315. owner._field_definitions.append(self)
  316. if not self.args.get('related'):
  317. self._direct = True
  318. if self._direct or self._toplevel:
  319. self._setup_attrs(owner, name)
  320. if self._toplevel:
  321. # free memory, self.args and self._base_fields are no longer useful
  322. self.__dict__.pop('args', None)
  323. self.__dict__.pop('_base_fields', None)
  324. #
  325. # Setup field parameter attributes
  326. #
  327. def _get_attrs(self, model_class, name):
  328. """ Return the field parameter attributes as a dictionary. """
  329. # determine all inherited field attributes
  330. attrs = {}
  331. modules = []
  332. for field in self.args.get('_base_fields', ()):
  333. if not isinstance(self, type(field)):
  334. # 'self' overrides 'field' and their types are not compatible;
  335. # so we ignore all the parameters collected so far
  336. attrs.clear()
  337. modules.clear()
  338. continue
  339. attrs.update(field.args)
  340. if field._module:
  341. modules.append(field._module)
  342. attrs.update(self.args)
  343. if self._module:
  344. modules.append(self._module)
  345. attrs['args'] = self.args
  346. attrs['model_name'] = model_class._name
  347. attrs['name'] = name
  348. attrs['_module'] = modules[-1] if modules else None
  349. attrs['_modules'] = tuple(set(modules))
  350. # initialize ``self`` with ``attrs``
  351. if name == 'state':
  352. # by default, `state` fields should be reset on copy
  353. attrs['copy'] = attrs.get('copy', False)
  354. if attrs.get('compute'):
  355. # by default, computed fields are not stored, computed in superuser
  356. # mode if stored, not copied (unless stored and explicitly not
  357. # readonly), and readonly (unless inversible)
  358. attrs['store'] = store = attrs.get('store', False)
  359. attrs['compute_sudo'] = attrs.get('compute_sudo', store)
  360. if not (attrs['store'] and not attrs.get('readonly', True)):
  361. attrs['copy'] = attrs.get('copy', False)
  362. attrs['readonly'] = attrs.get('readonly', not attrs.get('inverse'))
  363. if attrs.get('related'):
  364. # by default, related fields are not stored, computed in superuser
  365. # mode, not copied and readonly
  366. attrs['store'] = store = attrs.get('store', False)
  367. attrs['compute_sudo'] = attrs.get('compute_sudo', attrs.get('related_sudo', True))
  368. attrs['copy'] = attrs.get('copy', False)
  369. attrs['readonly'] = attrs.get('readonly', True)
  370. if attrs.get('precompute'):
  371. if not attrs.get('compute') and not attrs.get('related'):
  372. warnings.warn(f"precompute attribute doesn't make any sense on non computed field {self}")
  373. attrs['precompute'] = False
  374. elif not attrs.get('store'):
  375. warnings.warn(f"precompute attribute has no impact on non stored field {self}")
  376. attrs['precompute'] = False
  377. if attrs.get('company_dependent'):
  378. if attrs.get('required'):
  379. warnings.warn(f"company_dependent field {self} cannot be required")
  380. if attrs.get('translate'):
  381. warnings.warn(f"company_dependent field {self} cannot be translated")
  382. if self.type not in COMPANY_DEPENDENT_FIELDS:
  383. warnings.warn(f"company_dependent field {self} is not one of the allowed types {COMPANY_DEPENDENT_FIELDS}")
  384. attrs['copy'] = attrs.get('copy', False)
  385. # speed up search and on delete
  386. attrs['index'] = attrs.get('index', 'btree_not_null')
  387. attrs['prefetch'] = attrs.get('prefetch', 'company_dependent')
  388. attrs['_depends_context'] = ('company',)
  389. # parameters 'depends' and 'depends_context' are stored in attributes
  390. # '_depends' and '_depends_context', respectively
  391. if 'depends' in attrs:
  392. attrs['_depends'] = tuple(attrs.pop('depends'))
  393. if 'depends_context' in attrs:
  394. attrs['_depends_context'] = tuple(attrs.pop('depends_context'))
  395. if 'group_operator' in attrs:
  396. warnings.warn("Since Odoo 18, 'group_operator' is deprecated, use 'aggregator' instead", DeprecationWarning, 2)
  397. attrs['aggregator'] = attrs.pop('group_operator')
  398. return attrs
  399. def _setup_attrs(self, model_class, name):
  400. """ Initialize the field parameter attributes. """
  401. attrs = self._get_attrs(model_class, name)
  402. # determine parameters that must be validated
  403. extra_keys = [key for key in attrs if not hasattr(self, key)]
  404. if extra_keys:
  405. attrs['_extra_keys'] = extra_keys
  406. self.__dict__.update(attrs)
  407. # prefetch only stored, column, non-manual fields
  408. if not self.store or not self.column_type or self.manual:
  409. self.prefetch = False
  410. if not self.string and not self.related:
  411. # related fields get their string from their parent field
  412. self.string = (
  413. name[:-4] if name.endswith('_ids') else
  414. name[:-3] if name.endswith('_id') else name
  415. ).replace('_', ' ').title()
  416. # self.default must be either None or a callable
  417. if self.default is not None and not callable(self.default):
  418. value = self.default
  419. self.default = lambda model: value
  420. ############################################################################
  421. #
  422. # Complete field setup: everything else
  423. #
  424. def prepare_setup(self):
  425. self._setup_done = False
  426. def setup(self, model):
  427. """ Perform the complete setup of a field. """
  428. if not self._setup_done:
  429. # validate field params
  430. for key in self._extra_keys:
  431. if not model._valid_field_parameter(self, key):
  432. _logger.warning(
  433. "Field %s: unknown parameter %r, if this is an actual"
  434. " parameter you may want to override the method"
  435. " _valid_field_parameter on the relevant model in order to"
  436. " allow it",
  437. self, key
  438. )
  439. if self.related:
  440. self.setup_related(model)
  441. else:
  442. self.setup_nonrelated(model)
  443. if not isinstance(self.required, bool):
  444. warnings.warn(f'Property {self}.required should be a boolean ({self.required}).')
  445. if not isinstance(self.readonly, bool):
  446. warnings.warn(f'Property {self}.readonly should be a boolean ({self.readonly}).')
  447. self._setup_done = True
  448. #
  449. # Setup of non-related fields
  450. #
  451. def setup_nonrelated(self, model):
  452. """ Determine the dependencies and inverse field(s) of ``self``. """
  453. pass
  454. def get_depends(self, model: BaseModel):
  455. """ Return the field's dependencies and cache dependencies. """
  456. if self._depends is not None:
  457. # the parameter 'depends' has priority over 'depends' on compute
  458. return self._depends, self._depends_context or ()
  459. if self.related:
  460. if self._depends_context is not None:
  461. depends_context = self._depends_context
  462. else:
  463. related_model = model.env[self.related_field.model_name]
  464. depends, depends_context = self.related_field.get_depends(related_model)
  465. return [self.related], depends_context
  466. if not self.compute:
  467. return (), self._depends_context or ()
  468. # determine the functions implementing self.compute
  469. if isinstance(self.compute, str):
  470. funcs = resolve_mro(model, self.compute, callable)
  471. else:
  472. funcs = [self.compute]
  473. # collect depends and depends_context
  474. depends = []
  475. depends_context = list(self._depends_context or ())
  476. for func in funcs:
  477. deps = getattr(func, '_depends', ())
  478. depends.extend(deps(model) if callable(deps) else deps)
  479. depends_context.extend(getattr(func, '_depends_context', ()))
  480. # display_name may depend on context['lang'] (`test_lp1071710`)
  481. if self.automatic and self.name == 'display_name' and model._rec_name:
  482. if model._fields[model._rec_name].base_field.translate:
  483. if 'lang' not in depends_context:
  484. depends_context.append('lang')
  485. return depends, depends_context
  486. #
  487. # Setup of related fields
  488. #
  489. def setup_related(self, model):
  490. """ Setup the attributes of a related field. """
  491. assert isinstance(self.related, str), self.related
  492. # determine the chain of fields, and make sure they are all set up
  493. model_name = self.model_name
  494. for name in self.related.split('.'):
  495. field = model.pool[model_name]._fields.get(name)
  496. if field is None:
  497. raise KeyError(
  498. f"Field {name} referenced in related field definition {self} does not exist."
  499. )
  500. if not field._setup_done:
  501. field.setup(model.env[model_name])
  502. model_name = field.comodel_name
  503. self.related_field = field
  504. # check type consistency
  505. if self.type != field.type:
  506. raise TypeError("Type of related field %s is inconsistent with %s" % (self, field))
  507. # determine dependencies, compute, inverse, and search
  508. self.compute = self._compute_related
  509. if self.inherited or not (self.readonly or field.readonly):
  510. self.inverse = self._inverse_related
  511. if field._description_searchable:
  512. # allow searching on self only if the related field is searchable
  513. self.search = self._search_related
  514. # A readonly related field without an inverse method should not have a
  515. # default value, as it does not make sense.
  516. if self.default and self.readonly and not self.inverse:
  517. _logger.warning("Redundant default on %s", self)
  518. # copy attributes from field to self (string, help, etc.)
  519. for attr, prop in self.related_attrs:
  520. # check whether 'attr' is explicitly set on self (from its field
  521. # definition), and ignore its class-level value (only a default)
  522. if attr not in self.__dict__ and prop.startswith('_related_'):
  523. setattr(self, attr, getattr(field, prop))
  524. for attr in field._extra_keys:
  525. if not hasattr(self, attr) and model._valid_field_parameter(self, attr):
  526. setattr(self, attr, getattr(field, attr))
  527. # special cases of inherited fields
  528. if self.inherited:
  529. self.inherited_field = field
  530. if field.required:
  531. self.required = True
  532. # add modules from delegate and target fields; the first one ensures
  533. # that inherited fields introduced via an abstract model (_inherits
  534. # being on the abstract model) are assigned an XML id
  535. delegate_field = model._fields[self.related.split('.')[0]]
  536. self._modules = tuple({*self._modules, *delegate_field._modules, *field._modules})
  537. if self.store and self.translate:
  538. _logger.warning("Translated stored related field (%s) will not be computed correctly in all languages", self)
  539. def traverse_related(self, record):
  540. """ Traverse the fields of the related field `self` except for the last
  541. one, and return it as a pair `(last_record, last_field)`. """
  542. for name in self.related.split('.')[:-1]:
  543. record = first(record[name])
  544. return record, self.related_field
  545. def _compute_related(self, records):
  546. """ Compute the related field ``self`` on ``records``. """
  547. #
  548. # Traverse fields one by one for all records, in order to take advantage
  549. # of prefetching for each field access. In order to clarify the impact
  550. # of the algorithm, consider traversing 'foo.bar' for records a1 and a2,
  551. # where 'foo' is already present in cache for a1, a2. Initially, both a1
  552. # and a2 are marked for prefetching. As the commented code below shows,
  553. # traversing all fields one record at a time will fetch 'bar' one record
  554. # at a time.
  555. #
  556. # b1 = a1.foo # mark b1 for prefetching
  557. # v1 = b1.bar # fetch/compute bar for b1
  558. # b2 = a2.foo # mark b2 for prefetching
  559. # v2 = b2.bar # fetch/compute bar for b2
  560. #
  561. # On the other hand, traversing all records one field at a time ensures
  562. # maximal prefetching for each field access.
  563. #
  564. # b1 = a1.foo # mark b1 for prefetching
  565. # b2 = a2.foo # mark b2 for prefetching
  566. # v1 = b1.bar # fetch/compute bar for b1, b2
  567. # v2 = b2.bar # value already in cache
  568. #
  569. # This difference has a major impact on performance, in particular in
  570. # the case where 'bar' is a computed field that takes advantage of batch
  571. # computation.
  572. #
  573. values = list(records)
  574. for name in self.related.split('.')[:-1]:
  575. try:
  576. values = [first(value[name]) for value in values]
  577. except AccessError as e:
  578. description = records.env['ir.model']._get(records._name).name
  579. env = records.env
  580. raise AccessError(env._(
  581. "%(previous_message)s\n\nImplicitly accessed through '%(document_kind)s' (%(document_model)s).",
  582. previous_message=e.args[0],
  583. document_kind=description,
  584. document_model=records._name,
  585. ))
  586. # assign final values to records
  587. for record, value in zip(records, values):
  588. record[self.name] = self._process_related(value[self.related_field.name], record.env)
  589. def _process_related(self, value, env):
  590. """No transformation by default, but allows override."""
  591. return value
  592. def _inverse_related(self, records):
  593. """ Inverse the related field ``self`` on ``records``. """
  594. # store record values, otherwise they may be lost by cache invalidation!
  595. record_value = {record: record[self.name] for record in records}
  596. for record in records:
  597. target, field = self.traverse_related(record)
  598. # update 'target' only if 'record' and 'target' are both real or
  599. # both new (see `test_base_objects.py`, `test_basic`)
  600. if target and bool(target.id) == bool(record.id):
  601. target[field.name] = record_value[record]
  602. def _search_related(self, records, operator, value):
  603. """ Determine the domain to search on field ``self``. """
  604. # This should never happen to avoid bypassing security checks
  605. # and should already be converted to (..., 'in', subquery)
  606. assert operator not in ('any', 'not any')
  607. # determine whether the related field can be null
  608. if isinstance(value, (list, tuple)):
  609. value_is_null = any(val is False or val is None for val in value)
  610. else:
  611. value_is_null = value is False or value is None
  612. can_be_null = ( # (..., '=', False) or (..., 'not in', [truthy vals])
  613. (operator not in expression.NEGATIVE_TERM_OPERATORS and value_is_null)
  614. or (operator in expression.NEGATIVE_TERM_OPERATORS and not value_is_null)
  615. )
  616. def make_domain(path, model):
  617. if '.' not in path:
  618. return [(path, operator, value)]
  619. prefix, suffix = path.split('.', 1)
  620. field = model._fields[prefix]
  621. comodel = model.env[field.comodel_name]
  622. domain = [(prefix, 'in', comodel._search(make_domain(suffix, comodel)))]
  623. if can_be_null and field.type == 'many2one' and not field.required:
  624. return expression.OR([domain, [(prefix, '=', False)]])
  625. return domain
  626. model = records.env[self.model_name].with_context(active_test=False)
  627. model = model.sudo(records.env.su or self.compute_sudo)
  628. return make_domain(self.related, model)
  629. # properties used by setup_related() to copy values from related field
  630. _related_comodel_name = property(attrgetter('comodel_name'))
  631. _related_string = property(attrgetter('string'))
  632. _related_help = property(attrgetter('help'))
  633. _related_groups = property(attrgetter('groups'))
  634. _related_aggregator = property(attrgetter('aggregator'))
  635. @lazy_property
  636. def column_type(self) -> tuple[str, str] | None:
  637. """ Return the actual column type for this field, if stored as a column. """
  638. return ('jsonb', 'jsonb') if self.company_dependent or self.translate else self._column_type
  639. @property
  640. def base_field(self):
  641. """ Return the base field of an inherited field, or ``self``. """
  642. return self.inherited_field.base_field if self.inherited_field else self
  643. #
  644. # Company-dependent fields
  645. #
  646. def get_company_dependent_fallback(self, records):
  647. assert self.company_dependent
  648. fallback = records.env['ir.default'] \
  649. .with_user(SUPERUSER_ID) \
  650. .with_company(records.env.company) \
  651. ._get_model_defaults(records._name).get(self.name)
  652. fallback = self.convert_to_cache(fallback, records, validate=False)
  653. return self.convert_to_record(fallback, records)
  654. #
  655. # Setup of field triggers
  656. #
  657. def resolve_depends(self, registry):
  658. """ Return the dependencies of `self` as a collection of field tuples. """
  659. Model0 = registry[self.model_name]
  660. for dotnames in registry.field_depends[self]:
  661. field_seq = []
  662. model_name = self.model_name
  663. check_precompute = self.precompute
  664. for index, fname in enumerate(dotnames.split('.')):
  665. Model = registry[model_name]
  666. if Model0._transient and not Model._transient:
  667. # modifying fields on regular models should not trigger
  668. # recomputations of fields on transient models
  669. break
  670. try:
  671. field = Model._fields[fname]
  672. except KeyError:
  673. raise ValueError(
  674. f"Wrong @depends on '{self.compute}' (compute method of field {self}). "
  675. f"Dependency field '{fname}' not found in model {model_name}."
  676. )
  677. if field is self and index and not self.recursive:
  678. self.recursive = True
  679. warnings.warn(f"Field {self} should be declared with recursive=True")
  680. # precomputed fields can depend on non-precomputed ones, as long
  681. # as they are reachable through at least one many2one field
  682. if check_precompute and field.store and field.compute and not field.precompute:
  683. warnings.warn(f"Field {self} cannot be precomputed as it depends on non-precomputed field {field}")
  684. self.precompute = False
  685. if field_seq and not field_seq[-1]._description_searchable:
  686. # the field before this one is not searchable, so there is
  687. # no way to know which on records to recompute self
  688. warnings.warn(
  689. f"Field {field_seq[-1]!r} in dependency of {self} should be searchable. "
  690. f"This is necessary to determine which records to recompute when {field} is modified. "
  691. f"You should either make the field searchable, or simplify the field dependency."
  692. )
  693. field_seq.append(field)
  694. # do not make self trigger itself: for instance, a one2many
  695. # field line_ids with domain [('foo', ...)] will have
  696. # 'line_ids.foo' as a dependency
  697. if not (field is self and not index):
  698. yield tuple(field_seq)
  699. if field.type == 'one2many':
  700. for inv_field in Model.pool.field_inverses[field]:
  701. yield tuple(field_seq) + (inv_field,)
  702. if check_precompute and field.type == 'many2one':
  703. check_precompute = False
  704. model_name = field.comodel_name
  705. ############################################################################
  706. #
  707. # Field description
  708. #
  709. def get_description(self, env, attributes=None):
  710. """ Return a dictionary that describes the field ``self``. """
  711. desc = {}
  712. for attr, prop in self.description_attrs:
  713. if attributes is not None and attr not in attributes:
  714. continue
  715. if not prop.startswith('_description_'):
  716. continue
  717. value = getattr(self, prop)
  718. if callable(value):
  719. value = value(env)
  720. if value is not None:
  721. desc[attr] = value
  722. return desc
  723. # properties used by get_description()
  724. _description_name = property(attrgetter('name'))
  725. _description_type = property(attrgetter('type'))
  726. _description_store = property(attrgetter('store'))
  727. _description_manual = property(attrgetter('manual'))
  728. _description_related = property(attrgetter('related'))
  729. _description_company_dependent = property(attrgetter('company_dependent'))
  730. _description_readonly = property(attrgetter('readonly'))
  731. _description_required = property(attrgetter('required'))
  732. _description_groups = property(attrgetter('groups'))
  733. _description_change_default = property(attrgetter('change_default'))
  734. _description_default_export_compatible = property(attrgetter('default_export_compatible'))
  735. _description_exportable = property(attrgetter('exportable'))
  736. def _description_depends(self, env):
  737. return env.registry.field_depends[self]
  738. @property
  739. def _description_searchable(self):
  740. return bool(self.store or self.search)
  741. def _description_sortable(self, env):
  742. if self.column_type and self.store: # shortcut
  743. return True
  744. model = env[self.model_name]
  745. query = model._as_query(ordered=False)
  746. try:
  747. model._order_field_to_sql(model._table, self.name, SQL(), SQL(), query)
  748. return True
  749. except (ValueError, AccessError):
  750. return False
  751. def _description_groupable(self, env):
  752. if self.column_type and self.store: # shortcut
  753. return True
  754. model = env[self.model_name]
  755. query = model._as_query(ordered=False)
  756. groupby = self.name if self.type not in ('date', 'datetime') else f"{self.name}:month"
  757. try:
  758. model._read_group_groupby(groupby, query)
  759. return True
  760. except (ValueError, AccessError):
  761. return False
  762. def _description_aggregator(self, env):
  763. if not self.aggregator or self.column_type and self.store: # shortcut
  764. return self.aggregator
  765. model = env[self.model_name]
  766. query = model._as_query(ordered=False)
  767. try:
  768. model._read_group_select(f"{self.name}:{self.aggregator}", query)
  769. return self.aggregator
  770. except (ValueError, AccessError):
  771. return None
  772. def _description_string(self, env):
  773. if self.string and env.lang:
  774. model_name = self.base_field.model_name
  775. field_string = env['ir.model.fields'].get_field_string(model_name)
  776. return field_string.get(self.name) or self.string
  777. return self.string
  778. def _description_help(self, env):
  779. if self.help and env.lang:
  780. model_name = self.base_field.model_name
  781. field_help = env['ir.model.fields'].get_field_help(model_name)
  782. return field_help.get(self.name) or self.help
  783. return self.help
  784. def is_editable(self):
  785. """ Return whether the field can be editable in a view. """
  786. return not self.readonly
  787. def is_accessible(self, env):
  788. """ Return whether the field is accessible from the given environment. """
  789. if not self.groups or env.is_superuser():
  790. return True
  791. if self.groups == '.':
  792. return False
  793. return env.user.has_groups(self.groups)
  794. ############################################################################
  795. #
  796. # Conversion of values
  797. #
  798. def convert_to_column(self, value, record, values=None, validate=True):
  799. """ Convert ``value`` from the ``write`` format to the SQL parameter
  800. format for SQL conditions. This is used to compare a field's value when
  801. the field actually stores multiple values (translated or company-dependent).
  802. """
  803. if value is None or value is False:
  804. return None
  805. if isinstance(value, str):
  806. return value
  807. elif isinstance(value, bytes):
  808. return value.decode()
  809. else:
  810. return str(value)
  811. def convert_to_column_insert(self, value, record, values=None, validate=True):
  812. """ Convert ``value`` from the ``write`` format to the SQL parameter
  813. format for INSERT queries. This method handles the case of fields that
  814. store multiple values (translated or company-dependent).
  815. """
  816. value = self.convert_to_column(value, record, values, validate)
  817. if not self.company_dependent:
  818. return value
  819. fallback = record.env['ir.default']._get_model_defaults(record._name).get(self.name)
  820. if value == self.convert_to_column(fallback, record):
  821. return None
  822. return PsycopgJson({record.env.company.id: value})
  823. def convert_to_column_update(self, value, record):
  824. """ Convert ``value`` from the ``to_flush`` format to the SQL parameter
  825. format for UPDATE queries. The ``to_flush`` format is the same as the
  826. cache format, except for translated fields (``{'lang_code': 'value', ...}``
  827. or ``None``) and company-dependent fields (``{company_id: value, ...}``).
  828. """
  829. if self.company_dependent:
  830. return PsycopgJson(value)
  831. return self.convert_to_column_insert(
  832. self.convert_to_write(value, record),
  833. record,
  834. )
  835. def convert_to_cache(self, value, record, validate=True):
  836. """ Convert ``value`` to the cache format; ``value`` may come from an
  837. assignment, or have the format of methods :meth:`BaseModel.read` or
  838. :meth:`BaseModel.write`. If the value represents a recordset, it should
  839. be added for prefetching on ``record``.
  840. :param value:
  841. :param record:
  842. :param bool validate: when True, field-specific validation of ``value``
  843. will be performed
  844. """
  845. return value
  846. def convert_to_record(self, value, record):
  847. """ Convert ``value`` from the cache format to the record format.
  848. If the value represents a recordset, it should share the prefetching of
  849. ``record``.
  850. """
  851. return False if value is None else value
  852. def convert_to_record_multi(self, values, records):
  853. """ Convert a list of values from the cache format to the record format.
  854. Some field classes may override this method to add optimizations for
  855. batch processing.
  856. """
  857. # spare the method lookup overhead
  858. convert = self.convert_to_record
  859. return [convert(value, record) for value, record in zip(values, records)]
  860. def convert_to_read(self, value, record, use_display_name=True):
  861. """ Convert ``value`` from the record format to the format returned by
  862. method :meth:`BaseModel.read`.
  863. :param value:
  864. :param record:
  865. :param bool use_display_name: when True, the value's display name will be
  866. computed using `display_name`, if relevant for the field
  867. """
  868. return False if value is None else value
  869. def convert_to_write(self, value, record):
  870. """ Convert ``value`` from any format to the format of method
  871. :meth:`BaseModel.write`.
  872. """
  873. cache_value = self.convert_to_cache(value, record, validate=False)
  874. record_value = self.convert_to_record(cache_value, record)
  875. return self.convert_to_read(record_value, record)
  876. def convert_to_export(self, value, record):
  877. """ Convert ``value`` from the record format to the export format. """
  878. if not value:
  879. return ''
  880. return value
  881. def convert_to_display_name(self, value, record):
  882. """ Convert ``value`` from the record format to a suitable display name. """
  883. return str(value) if value else False
  884. ############################################################################
  885. #
  886. # Update database schema
  887. #
  888. @property
  889. def column_order(self):
  890. """ Prescribed column order in table. """
  891. return 0 if self.column_type is None else sql.SQL_ORDER_BY_TYPE[self.column_type[0]]
  892. def update_db(self, model, columns):
  893. """ Update the database schema to implement this field.
  894. :param model: an instance of the field's model
  895. :param columns: a dict mapping column names to their configuration in database
  896. :return: ``True`` if the field must be recomputed on existing rows
  897. """
  898. if not self.column_type:
  899. return
  900. column = columns.get(self.name)
  901. # create/update the column, not null constraint; the index will be
  902. # managed by registry.check_indexes()
  903. self.update_db_column(model, column)
  904. self.update_db_notnull(model, column)
  905. # optimization for computing simple related fields like 'foo_id.bar'
  906. if (
  907. not column
  908. and self.related and self.related.count('.') == 1
  909. and self.related_field.store and not self.related_field.compute
  910. and not (self.related_field.type == 'binary' and self.related_field.attachment)
  911. and self.related_field.type not in ('one2many', 'many2many')
  912. ):
  913. join_field = model._fields[self.related.split('.')[0]]
  914. if (
  915. join_field.type == 'many2one'
  916. and join_field.store and not join_field.compute
  917. ):
  918. model.pool.post_init(self.update_db_related, model)
  919. # discard the "classical" computation
  920. return False
  921. return not column
  922. def update_db_column(self, model, column):
  923. """ Create/update the column corresponding to ``self``.
  924. :param model: an instance of the field's model
  925. :param column: the column's configuration (dict) if it exists, or ``None``
  926. """
  927. if not column:
  928. # the column does not exist, create it
  929. sql.create_column(model._cr, model._table, self.name, self.column_type[1], self.string)
  930. return
  931. if column['udt_name'] == self.column_type[0]:
  932. return
  933. if column['is_nullable'] == 'NO':
  934. sql.drop_not_null(model._cr, model._table, self.name)
  935. self._convert_db_column(model, column)
  936. def _convert_db_column(self, model, column):
  937. """ Convert the given database column to the type of the field. """
  938. sql.convert_column(model._cr, model._table, self.name, self.column_type[1])
  939. def update_db_notnull(self, model, column):
  940. """ Add or remove the NOT NULL constraint on ``self``.
  941. :param model: an instance of the field's model
  942. :param column: the column's configuration (dict) if it exists, or ``None``
  943. """
  944. has_notnull = column and column['is_nullable'] == 'NO'
  945. if not column or (self.required and not has_notnull):
  946. # the column is new or it becomes required; initialize its values
  947. if model._table_has_rows():
  948. model._init_column(self.name)
  949. if self.required and not has_notnull:
  950. # _init_column may delay computations in post-init phase
  951. @model.pool.post_init
  952. def add_not_null():
  953. # flush values before adding NOT NULL constraint
  954. model.flush_model([self.name])
  955. model.pool.post_constraint(apply_required, model, self.name)
  956. elif not self.required and has_notnull:
  957. sql.drop_not_null(model._cr, model._table, self.name)
  958. def update_db_related(self, model):
  959. """ Compute a stored related field directly in SQL. """
  960. comodel = model.env[self.related_field.model_name]
  961. join_field, comodel_field = self.related.split('.')
  962. model.env.cr.execute(SQL(
  963. """ UPDATE %(model_table)s AS x
  964. SET %(model_field)s = y.%(comodel_field)s
  965. FROM %(comodel_table)s AS y
  966. WHERE x.%(join_field)s = y.id """,
  967. model_table=SQL.identifier(model._table),
  968. model_field=SQL.identifier(self.name),
  969. comodel_table=SQL.identifier(comodel._table),
  970. comodel_field=SQL.identifier(comodel_field),
  971. join_field=SQL.identifier(join_field),
  972. ))
  973. ############################################################################
  974. #
  975. # Alternatively stored fields: if fields don't have a `column_type` (not
  976. # stored as regular db columns) they go through a read/create/write
  977. # protocol instead
  978. #
  979. def read(self, records):
  980. """ Read the value of ``self`` on ``records``, and store it in cache. """
  981. if not self.column_type:
  982. raise NotImplementedError("Method read() undefined on %s" % self)
  983. def create(self, record_values):
  984. """ Write the value of ``self`` on the given records, which have just
  985. been created.
  986. :param record_values: a list of pairs ``(record, value)``, where
  987. ``value`` is in the format of method :meth:`BaseModel.write`
  988. """
  989. for record, value in record_values:
  990. self.write(record, value)
  991. def write(self, records, value):
  992. """ Write the value of ``self`` on ``records``. This method must update
  993. the cache and prepare database updates.
  994. :param records:
  995. :param value: a value in any format
  996. """
  997. # discard recomputation of self on records
  998. records.env.remove_to_compute(self, records)
  999. # discard the records that are not modified
  1000. cache = records.env.cache
  1001. cache_value = self.convert_to_cache(value, records)
  1002. records = cache.get_records_different_from(records, self, cache_value)
  1003. if not records:
  1004. return
  1005. # update the cache
  1006. dirty = self.store and any(records._ids)
  1007. cache.update(records, self, itertools.repeat(cache_value), dirty=dirty)
  1008. ############################################################################
  1009. #
  1010. # Descriptor methods
  1011. #
  1012. def __get__(self, record: BaseModel, owner=None) -> T:
  1013. """ return the value of field ``self`` on ``record`` """
  1014. if record is None:
  1015. return self # the field is accessed through the owner class
  1016. if not record._ids:
  1017. # null record -> return the null value for this field
  1018. value = self.convert_to_cache(False, record, validate=False)
  1019. return self.convert_to_record(value, record)
  1020. env = record.env
  1021. # only a single record may be accessed
  1022. record.ensure_one()
  1023. if self.compute and self.store:
  1024. # process pending computations
  1025. self.recompute(record)
  1026. try:
  1027. value = env.cache.get(record, self)
  1028. return self.convert_to_record(value, record)
  1029. except KeyError:
  1030. pass
  1031. # behavior in case of cache miss:
  1032. #
  1033. # on a real record:
  1034. # stored -> fetch from database (computation done above)
  1035. # not stored and computed -> compute
  1036. # not stored and not computed -> default
  1037. #
  1038. # on a new record w/ origin:
  1039. # stored and not (computed and readonly) -> fetch from origin
  1040. # stored and computed and readonly -> compute
  1041. # not stored and computed -> compute
  1042. # not stored and not computed -> default
  1043. #
  1044. # on a new record w/o origin:
  1045. # stored and computed -> compute
  1046. # stored and not computed -> new delegate or default
  1047. # not stored and computed -> compute
  1048. # not stored and not computed -> default
  1049. #
  1050. if self.store and record.id:
  1051. # real record: fetch from database
  1052. recs = record._in_cache_without(self)
  1053. try:
  1054. recs._fetch_field(self)
  1055. except AccessError:
  1056. if len(recs) == 1:
  1057. raise
  1058. record._fetch_field(self)
  1059. if not env.cache.contains(record, self):
  1060. raise MissingError("\n".join([
  1061. env._("Record does not exist or has been deleted."),
  1062. env._("(Record: %(record)s, User: %(user)s)", record=record, user=env.uid),
  1063. ])) from None
  1064. value = env.cache.get(record, self)
  1065. elif self.store and record._origin and not (self.compute and self.readonly):
  1066. # new record with origin: fetch from origin, and assign the
  1067. # records to prefetch in cache (which is necessary for
  1068. # relational fields to "map" prefetching ids to their value)
  1069. recs = record._in_cache_without(self)
  1070. try:
  1071. for rec in recs:
  1072. if (rec_origin := rec._origin):
  1073. value = self.convert_to_cache(rec_origin[self.name], rec, validate=False)
  1074. env.cache.patch_and_set(rec, self, value)
  1075. value = env.cache.get(record, self)
  1076. except (AccessError, MissingError):
  1077. if len(recs) == 1:
  1078. raise
  1079. value = self.convert_to_cache(record._origin[self.name], record, validate=False)
  1080. value = env.cache.patch_and_set(record, self, value)
  1081. elif self.compute: #pylint: disable=using-constant-test
  1082. # non-stored field or new record without origin: compute
  1083. if env.is_protected(self, record):
  1084. value = self.convert_to_cache(False, record, validate=False)
  1085. env.cache.set(record, self, value)
  1086. else:
  1087. recs = record if self.recursive else record._in_cache_without(self)
  1088. try:
  1089. self.compute_value(recs)
  1090. except (AccessError, MissingError):
  1091. self.compute_value(record)
  1092. recs = record
  1093. missing_recs_ids = tuple(env.cache.get_missing_ids(recs, self))
  1094. if missing_recs_ids:
  1095. missing_recs = record.browse(missing_recs_ids)
  1096. if self.readonly and not self.store:
  1097. raise ValueError(f"Compute method failed to assign {missing_recs}.{self.name}")
  1098. # fallback to null value if compute gives nothing, do it for every unset record
  1099. false_value = self.convert_to_cache(False, record, validate=False)
  1100. env.cache.update(missing_recs, self, itertools.repeat(false_value))
  1101. value = env.cache.get(record, self)
  1102. elif self.type == 'many2one' and self.delegate and not record.id:
  1103. # parent record of a new record: new record, with the same
  1104. # values as record for the corresponding inherited fields
  1105. def is_inherited_field(name):
  1106. field = record._fields[name]
  1107. return field.inherited and field.related.split('.')[0] == self.name
  1108. parent = record.env[self.comodel_name].new({
  1109. name: value
  1110. for name, value in record._cache.items()
  1111. if is_inherited_field(name)
  1112. })
  1113. # in case the delegate field has inverse one2many fields, this
  1114. # updates the inverse fields as well
  1115. record._update_cache({self.name: parent}, validate=False)
  1116. value = env.cache.get(record, self)
  1117. else:
  1118. # non-stored field or stored field on new record: default value
  1119. value = self.convert_to_cache(False, record, validate=False)
  1120. value = env.cache.patch_and_set(record, self, value)
  1121. defaults = record.default_get([self.name])
  1122. if self.name in defaults:
  1123. # The null value above is necessary to convert x2many field
  1124. # values. For instance, converting [(Command.LINK, id)]
  1125. # accesses the field's current value, then adds the given
  1126. # id. Without an initial value, the conversion ends up here
  1127. # to determine the field's value, and generates an infinite
  1128. # recursion.
  1129. value = self.convert_to_cache(defaults[self.name], record)
  1130. env.cache.set(record, self, value)
  1131. return self.convert_to_record(value, record)
  1132. def mapped(self, records):
  1133. """ Return the values of ``self`` for ``records``, either as a list
  1134. (scalar fields), or as a recordset (relational fields).
  1135. This method is meant to be used internally and has very little benefit
  1136. over a simple call to `~odoo.models.BaseModel.mapped()` on a recordset.
  1137. """
  1138. if self.name == 'id':
  1139. # not stored in cache
  1140. return list(records._ids)
  1141. if self.compute and self.store:
  1142. # process pending computations
  1143. self.recompute(records)
  1144. # retrieve values in cache, and fetch missing ones
  1145. vals = records.env.cache.get_until_miss(records, self)
  1146. while len(vals) < len(records):
  1147. # It is important to construct a 'remaining' recordset with the
  1148. # _prefetch_ids of the original recordset, in order to prefetch as
  1149. # many records as possible. If not done this way, scenarios such as
  1150. # [rec.line_ids.mapped('name') for rec in recs] would generate one
  1151. # query per record in `recs`!
  1152. remaining = records.__class__(records.env, records._ids[len(vals):], records._prefetch_ids)
  1153. self.__get__(first(remaining))
  1154. vals += records.env.cache.get_until_miss(remaining, self)
  1155. return self.convert_to_record_multi(vals, records)
  1156. def __set__(self, records, value):
  1157. """ set the value of field ``self`` on ``records`` """
  1158. protected_ids = []
  1159. new_ids = []
  1160. other_ids = []
  1161. for record_id in records._ids:
  1162. if record_id in records.env._protected.get(self, ()):
  1163. protected_ids.append(record_id)
  1164. elif not record_id:
  1165. new_ids.append(record_id)
  1166. else:
  1167. other_ids.append(record_id)
  1168. if protected_ids:
  1169. # records being computed: no business logic, no recomputation
  1170. protected_records = records.__class__(records.env, tuple(protected_ids), records._prefetch_ids)
  1171. self.write(protected_records, value)
  1172. if new_ids:
  1173. # new records: no business logic
  1174. new_records = records.__class__(records.env, tuple(new_ids), records._prefetch_ids)
  1175. with records.env.protecting(records.pool.field_computed.get(self, [self]), new_records):
  1176. if self.relational:
  1177. new_records.modified([self.name], before=True)
  1178. self.write(new_records, value)
  1179. new_records.modified([self.name])
  1180. if self.inherited:
  1181. # special case: also assign parent records if they are new
  1182. parents = new_records[self.related.split('.')[0]]
  1183. parents.filtered(lambda r: not r.id)[self.name] = value
  1184. if other_ids:
  1185. # base case: full business logic
  1186. records = records.__class__(records.env, tuple(other_ids), records._prefetch_ids)
  1187. write_value = self.convert_to_write(value, records)
  1188. records.write({self.name: write_value})
  1189. ############################################################################
  1190. #
  1191. # Computation of field values
  1192. #
  1193. def recompute(self, records):
  1194. """ Process the pending computations of ``self`` on ``records``. This
  1195. should be called only if ``self`` is computed and stored.
  1196. """
  1197. to_compute_ids = records.env.transaction.tocompute.get(self)
  1198. if not to_compute_ids:
  1199. return
  1200. def apply_except_missing(func, records):
  1201. """ Apply `func` on `records`, with a fallback ignoring non-existent records. """
  1202. try:
  1203. func(records)
  1204. except MissingError:
  1205. existing = records.exists()
  1206. if existing:
  1207. func(existing)
  1208. # mark the field as computed on missing records, otherwise they
  1209. # remain to compute forever, which may lead to an infinite loop
  1210. missing = records - existing
  1211. for f in records.pool.field_computed[self]:
  1212. records.env.remove_to_compute(f, missing)
  1213. if self.recursive:
  1214. # recursive computed fields are computed record by record, in order
  1215. # to recursively handle dependencies inside records
  1216. def recursive_compute(records):
  1217. for record in records:
  1218. if record.id in to_compute_ids:
  1219. self.compute_value(record)
  1220. apply_except_missing(recursive_compute, records)
  1221. return
  1222. for record in records:
  1223. if record.id in to_compute_ids:
  1224. ids = expand_ids(record.id, to_compute_ids)
  1225. recs = record.browse(itertools.islice(ids, PREFETCH_MAX))
  1226. try:
  1227. apply_except_missing(self.compute_value, recs)
  1228. except AccessError:
  1229. self.compute_value(record)
  1230. def compute_value(self, records):
  1231. """ Invoke the compute method on ``records``; the results are in cache. """
  1232. env = records.env
  1233. if self.compute_sudo:
  1234. records = records.sudo()
  1235. fields = records.pool.field_computed[self]
  1236. # Just in case the compute method does not assign a value, we already
  1237. # mark the computation as done. This is also necessary if the compute
  1238. # method accesses the old value of the field: the field will be fetched
  1239. # with _read(), which will flush() it. If the field is still to compute,
  1240. # the latter flush() will recursively compute this field!
  1241. for field in fields:
  1242. if field.store:
  1243. env.remove_to_compute(field, records)
  1244. try:
  1245. with records.env.protecting(fields, records):
  1246. records._compute_field_value(self)
  1247. except Exception:
  1248. for field in fields:
  1249. if field.store:
  1250. env.add_to_compute(field, records)
  1251. raise
  1252. def determine_inverse(self, records):
  1253. """ Given the value of ``self`` on ``records``, inverse the computation. """
  1254. determine(self.inverse, records)
  1255. def determine_domain(self, records, operator, value):
  1256. """ Return a domain representing a condition on ``self``. """
  1257. return determine(self.search, records, operator, value)
  1258. class Boolean(Field[bool]):
  1259. """ Encapsulates a :class:`bool`. """
  1260. type = 'boolean'
  1261. _column_type = ('bool', 'bool')
  1262. def convert_to_column(self, value, record, values=None, validate=True):
  1263. return bool(value)
  1264. def convert_to_column_update(self, value, record):
  1265. if self.company_dependent:
  1266. value = {k: bool(v) for k, v in value.items()}
  1267. return super().convert_to_column_update(value, record)
  1268. def convert_to_cache(self, value, record, validate=True):
  1269. return bool(value)
  1270. def convert_to_export(self, value, record):
  1271. return bool(value)
  1272. class Integer(Field[int]):
  1273. """ Encapsulates an :class:`int`. """
  1274. type = 'integer'
  1275. _column_type = ('int4', 'int4')
  1276. aggregator = 'sum'
  1277. def _get_attrs(self, model_class, name):
  1278. res = super()._get_attrs(model_class, name)
  1279. # The default aggregator is None for sequence fields
  1280. if 'aggregator' not in res and name == 'sequence':
  1281. res['aggregator'] = None
  1282. return res
  1283. def convert_to_column(self, value, record, values=None, validate=True):
  1284. return int(value or 0)
  1285. def convert_to_column_update(self, value, record):
  1286. if self.company_dependent:
  1287. value = {k: int(v or 0) for k, v in value.items()}
  1288. return super().convert_to_column_update(value, record)
  1289. def convert_to_cache(self, value, record, validate=True):
  1290. if isinstance(value, dict):
  1291. # special case, when an integer field is used as inverse for a one2many
  1292. return value.get('id', None)
  1293. return int(value or 0)
  1294. def convert_to_record(self, value, record):
  1295. return value or 0
  1296. def convert_to_read(self, value, record, use_display_name=True):
  1297. # Integer values greater than 2^31-1 are not supported in pure XMLRPC,
  1298. # so we have to pass them as floats :-(
  1299. if value and value > MAXINT:
  1300. return float(value)
  1301. return value
  1302. def _update(self, records, value):
  1303. cache = records.env.cache
  1304. for record in records:
  1305. cache.set(record, self, value.id or 0)
  1306. def convert_to_export(self, value, record):
  1307. if value or value == 0:
  1308. return value
  1309. return ''
  1310. class Float(Field[float]):
  1311. """ Encapsulates a :class:`float`.
  1312. The precision digits are given by the (optional) ``digits`` attribute.
  1313. :param digits: a pair (total, decimal) or a string referencing a
  1314. :class:`~odoo.addons.base.models.decimal_precision.DecimalPrecision` record name.
  1315. :type digits: tuple(int,int) or str
  1316. When a float is a quantity associated with an unit of measure, it is important
  1317. to use the right tool to compare or round values with the correct precision.
  1318. The Float class provides some static methods for this purpose:
  1319. :func:`~odoo.fields.Float.round()` to round a float with the given precision.
  1320. :func:`~odoo.fields.Float.is_zero()` to check if a float equals zero at the given precision.
  1321. :func:`~odoo.fields.Float.compare()` to compare two floats at the given precision.
  1322. .. admonition:: Example
  1323. To round a quantity with the precision of the unit of measure::
  1324. fields.Float.round(self.product_uom_qty, precision_rounding=self.product_uom_id.rounding)
  1325. To check if the quantity is zero with the precision of the unit of measure::
  1326. fields.Float.is_zero(self.product_uom_qty, precision_rounding=self.product_uom_id.rounding)
  1327. To compare two quantities::
  1328. field.Float.compare(self.product_uom_qty, self.qty_done, precision_rounding=self.product_uom_id.rounding)
  1329. The compare helper uses the __cmp__ semantics for historic purposes, therefore
  1330. the proper, idiomatic way to use this helper is like so:
  1331. if result == 0, the first and second floats are equal
  1332. if result < 0, the first float is lower than the second
  1333. if result > 0, the first float is greater than the second
  1334. """
  1335. type = 'float'
  1336. _digits = None # digits argument passed to class initializer
  1337. aggregator = 'sum'
  1338. def __init__(self, string: str | Sentinel = SENTINEL, digits: str | tuple[int, int] | None | Sentinel = SENTINEL, **kwargs):
  1339. super(Float, self).__init__(string=string, _digits=digits, **kwargs)
  1340. @property
  1341. def _column_type(self):
  1342. # Explicit support for "falsy" digits (0, False) to indicate a NUMERIC
  1343. # field with no fixed precision. The values are saved in the database
  1344. # with all significant digits.
  1345. # FLOAT8 type is still the default when there is no precision because it
  1346. # is faster for most operations (sums, etc.)
  1347. return ('numeric', 'numeric') if self._digits is not None else \
  1348. ('float8', 'double precision')
  1349. def get_digits(self, env):
  1350. if isinstance(self._digits, str):
  1351. precision = env['decimal.precision'].precision_get(self._digits)
  1352. return 16, precision
  1353. else:
  1354. return self._digits
  1355. _related__digits = property(attrgetter('_digits'))
  1356. def _description_digits(self, env):
  1357. return self.get_digits(env)
  1358. def convert_to_column(self, value, record, values=None, validate=True):
  1359. value_float = value = float(value or 0.0)
  1360. if digits := self.get_digits(record.env):
  1361. precision, scale = digits
  1362. value_float = float_round(value, precision_digits=scale)
  1363. value = float_repr(value_float, precision_digits=scale)
  1364. if self.company_dependent:
  1365. return value_float
  1366. return value
  1367. def convert_to_column_update(self, value, record):
  1368. if self.company_dependent:
  1369. value = {k: float(v or 0.0) for k, v in value.items()}
  1370. return super().convert_to_column_update(value, record)
  1371. def convert_to_cache(self, value, record, validate=True):
  1372. # apply rounding here, otherwise value in cache may be wrong!
  1373. value = float(value or 0.0)
  1374. digits = self.get_digits(record.env)
  1375. return float_round(value, precision_digits=digits[1]) if digits else value
  1376. def convert_to_record(self, value, record):
  1377. return value or 0.0
  1378. def convert_to_export(self, value, record):
  1379. if value or value == 0.0:
  1380. return value
  1381. return ''
  1382. round = staticmethod(float_round)
  1383. is_zero = staticmethod(float_is_zero)
  1384. compare = staticmethod(float_compare)
  1385. class Monetary(Field[float]):
  1386. """ Encapsulates a :class:`float` expressed in a given
  1387. :class:`res_currency<odoo.addons.base.models.res_currency.Currency>`.
  1388. The decimal precision and currency symbol are taken from the ``currency_field`` attribute.
  1389. :param str currency_field: name of the :class:`Many2one` field
  1390. holding the :class:`res_currency <odoo.addons.base.models.res_currency.Currency>`
  1391. this monetary field is expressed in (default: `\'currency_id\'`)
  1392. """
  1393. type = 'monetary'
  1394. write_sequence = 10
  1395. _column_type = ('numeric', 'numeric')
  1396. currency_field = None
  1397. aggregator = 'sum'
  1398. def __init__(self, string: str | Sentinel = SENTINEL, currency_field: str | Sentinel = SENTINEL, **kwargs):
  1399. super(Monetary, self).__init__(string=string, currency_field=currency_field, **kwargs)
  1400. def _description_currency_field(self, env):
  1401. return self.get_currency_field(env[self.model_name])
  1402. def get_currency_field(self, model):
  1403. """ Return the name of the currency field. """
  1404. return self.currency_field or (
  1405. 'currency_id' if 'currency_id' in model._fields else
  1406. 'x_currency_id' if 'x_currency_id' in model._fields else
  1407. None
  1408. )
  1409. def setup_nonrelated(self, model):
  1410. super().setup_nonrelated(model)
  1411. assert self.get_currency_field(model) in model._fields, \
  1412. "Field %s with unknown currency_field %r" % (self, self.get_currency_field(model))
  1413. def setup_related(self, model):
  1414. super().setup_related(model)
  1415. if self.inherited:
  1416. self.currency_field = self.related_field.get_currency_field(model.env[self.related_field.model_name])
  1417. assert self.get_currency_field(model) in model._fields, \
  1418. "Field %s with unknown currency_field %r" % (self, self.get_currency_field(model))
  1419. def convert_to_column_insert(self, value, record, values=None, validate=True):
  1420. # retrieve currency from values or record
  1421. currency_field_name = self.get_currency_field(record)
  1422. currency_field = record._fields[currency_field_name]
  1423. if values and currency_field_name in values:
  1424. dummy = record.new({currency_field_name: values[currency_field_name]})
  1425. currency = dummy[currency_field_name]
  1426. elif values and currency_field.related and currency_field.related.split('.')[0] in values:
  1427. related_field_name = currency_field.related.split('.')[0]
  1428. dummy = record.new({related_field_name: values[related_field_name]})
  1429. currency = dummy[currency_field_name]
  1430. else:
  1431. # Note: this is wrong if 'record' is several records with different
  1432. # currencies, which is functional nonsense and should not happen
  1433. # BEWARE: do not prefetch other fields, because 'value' may be in
  1434. # cache, and would be overridden by the value read from database!
  1435. currency = record[:1].with_context(prefetch_fields=False)[currency_field_name]
  1436. currency = currency.with_env(record.env)
  1437. value = float(value or 0.0)
  1438. if currency:
  1439. return float_repr(currency.round(value), currency.decimal_places)
  1440. return value
  1441. def convert_to_cache(self, value, record, validate=True):
  1442. # cache format: float
  1443. value = float(value or 0.0)
  1444. if value and validate:
  1445. # FIXME @rco-odoo: currency may not be already initialized if it is
  1446. # a function or related field!
  1447. # BEWARE: do not prefetch other fields, because 'value' may be in
  1448. # cache, and would be overridden by the value read from database!
  1449. currency_field = self.get_currency_field(record)
  1450. currency = record.sudo().with_context(prefetch_fields=False)[currency_field]
  1451. if len(currency) > 1:
  1452. raise ValueError("Got multiple currencies while assigning values of monetary field %s" % str(self))
  1453. elif currency:
  1454. value = currency.with_env(record.env).round(value)
  1455. return value
  1456. def convert_to_record(self, value, record):
  1457. return value or 0.0
  1458. def convert_to_read(self, value, record, use_display_name=True):
  1459. return value
  1460. def convert_to_write(self, value, record):
  1461. return value
  1462. def convert_to_export(self, value, record):
  1463. if value or value == 0.0:
  1464. return value
  1465. return ''
  1466. class _String(Field[str | typing.Literal[False]]):
  1467. """ Abstract class for string fields. """
  1468. translate = False # whether the field is translated
  1469. size = None # maximum size of values (deprecated)
  1470. def __init__(self, string: str | Sentinel = SENTINEL, **kwargs):
  1471. # translate is either True, False, or a callable
  1472. if 'translate' in kwargs and not callable(kwargs['translate']):
  1473. kwargs['translate'] = bool(kwargs['translate'])
  1474. super(_String, self).__init__(string=string, **kwargs)
  1475. _related_translate = property(attrgetter('translate'))
  1476. def _description_translate(self, env):
  1477. return bool(self.translate)
  1478. def _convert_db_column(self, model, column):
  1479. # specialized implementation for converting from/to translated fields
  1480. if self.translate or column['udt_name'] == 'jsonb':
  1481. sql.convert_column_translatable(model._cr, model._table, self.name, self.column_type[1])
  1482. else:
  1483. sql.convert_column(model._cr, model._table, self.name, self.column_type[1])
  1484. def get_trans_terms(self, value):
  1485. """ Return the sequence of terms to translate found in `value`. """
  1486. if not callable(self.translate):
  1487. return [value] if value else []
  1488. terms = []
  1489. self.translate(terms.append, value)
  1490. return terms
  1491. def get_text_content(self, term):
  1492. """ Return the textual content for the given term. """
  1493. func = getattr(self.translate, 'get_text_content', lambda term: term)
  1494. return func(term)
  1495. def convert_to_column(self, value, record, values=None, validate=True):
  1496. return self.convert_to_cache(value, record, validate)
  1497. def convert_to_column_insert(self, value, record, values=None, validate=True):
  1498. if self.translate:
  1499. value = self.convert_to_column(value, record, values, validate)
  1500. if value is None:
  1501. return None
  1502. return PsycopgJson({'en_US': value, record.env.lang or 'en_US': value})
  1503. return super().convert_to_column_insert(value, record, values, validate)
  1504. def convert_to_column_update(self, value, record):
  1505. if self.translate:
  1506. return PsycopgJson(value) if value else value
  1507. return super().convert_to_column_update(value, record)
  1508. def convert_to_cache(self, value, record, validate=True):
  1509. if value is None or value is False:
  1510. return None
  1511. if isinstance(value, bytes):
  1512. s = value.decode()
  1513. else:
  1514. s = str(value)
  1515. value = s[:self.size]
  1516. if callable(self.translate):
  1517. # pylint: disable=not-callable
  1518. value = self.translate(lambda t: None, value)
  1519. return value
  1520. def convert_to_record(self, value, record):
  1521. if value is None:
  1522. return False
  1523. if callable(self.translate) and record.env.context.get('edit_translations'):
  1524. if not self.get_trans_terms(value):
  1525. return value
  1526. base_lang = record._get_base_lang()
  1527. lang = record.env.lang or 'en_US'
  1528. if lang != base_lang:
  1529. base_value = record.with_context(edit_translations=None, check_translations=True, lang=base_lang)[self.name]
  1530. base_terms_iter = iter(self.get_trans_terms(base_value))
  1531. get_base = lambda term: next(base_terms_iter)
  1532. else:
  1533. get_base = lambda term: term
  1534. delay_translation = value != record.with_context(edit_translations=None, check_translations=None, lang=lang)[self.name]
  1535. # use a wrapper to let the frontend js code identify each term and
  1536. # its metadata in the 'edit_translations' context
  1537. def translate_func(term):
  1538. source_term = get_base(term)
  1539. translation_state = 'translated' if lang == base_lang or source_term != term else 'to_translate'
  1540. translation_source_sha = sha256(source_term.encode()).hexdigest()
  1541. return (
  1542. '<span '
  1543. f'''{'class="o_delay_translation" ' if delay_translation else ''}'''
  1544. f'data-oe-model="{markup_escape(record._name)}" '
  1545. f'data-oe-id="{markup_escape(record.id)}" '
  1546. f'data-oe-field="{markup_escape(self.name)}" '
  1547. f'data-oe-translation-state="{translation_state}" '
  1548. f'data-oe-translation-source-sha="{translation_source_sha}"'
  1549. '>'
  1550. f'{term}'
  1551. '</span>'
  1552. )
  1553. # pylint: disable=not-callable
  1554. value = self.translate(translate_func, value)
  1555. return value
  1556. def convert_to_write(self, value, record):
  1557. return value
  1558. def get_translation_dictionary(self, from_lang_value, to_lang_values):
  1559. """ Build a dictionary from terms in from_lang_value to terms in to_lang_values
  1560. :param str from_lang_value: from xml/html
  1561. :param dict to_lang_values: {lang: lang_value}
  1562. :return: {from_lang_term: {lang: lang_term}}
  1563. :rtype: dict
  1564. """
  1565. from_lang_terms = self.get_trans_terms(from_lang_value)
  1566. dictionary = defaultdict(lambda: defaultdict(dict))
  1567. if not from_lang_terms:
  1568. return dictionary
  1569. dictionary.update({from_lang_term: defaultdict(dict) for from_lang_term in from_lang_terms})
  1570. for lang, to_lang_value in to_lang_values.items():
  1571. to_lang_terms = self.get_trans_terms(to_lang_value)
  1572. if len(from_lang_terms) != len(to_lang_terms):
  1573. for from_lang_term in from_lang_terms:
  1574. dictionary[from_lang_term][lang] = from_lang_term
  1575. else:
  1576. for from_lang_term, to_lang_term in zip(from_lang_terms, to_lang_terms):
  1577. dictionary[from_lang_term][lang] = to_lang_term
  1578. return dictionary
  1579. def _get_stored_translations(self, record):
  1580. """
  1581. : return: {'en_US': 'value_en_US', 'fr_FR': 'French'}
  1582. """
  1583. # assert (self.translate and self.store and record)
  1584. record.flush_recordset([self.name])
  1585. cr = record.env.cr
  1586. cr.execute(SQL(
  1587. "SELECT %s FROM %s WHERE id = %s",
  1588. SQL.identifier(self.name),
  1589. SQL.identifier(record._table),
  1590. record.id,
  1591. ))
  1592. res = cr.fetchone()
  1593. return res[0] if res else None
  1594. def get_translation_fallback_langs(self, env):
  1595. lang = (env.lang or 'en_US') if self.translate is True else env._lang
  1596. if lang == '_en_US':
  1597. return '_en_US', 'en_US'
  1598. if lang == 'en_US':
  1599. return ('en_US',)
  1600. if lang.startswith('_'):
  1601. return lang, lang[1:], '_en_US', 'en_US'
  1602. return lang, 'en_US'
  1603. def write(self, records, value):
  1604. if not self.translate or value is False or value is None:
  1605. super().write(records, value)
  1606. return
  1607. cache = records.env.cache
  1608. cache_value = self.convert_to_cache(value, records)
  1609. records = cache.get_records_different_from(records, self, cache_value)
  1610. if not records:
  1611. return
  1612. # flush dirty None values
  1613. dirty_records = records & cache.get_dirty_records(records, self)
  1614. if any(v is None for v in cache.get_values(dirty_records, self)):
  1615. dirty_records.flush_recordset([self.name])
  1616. dirty = self.store and any(records._ids)
  1617. lang = (records.env.lang or 'en_US') if self.translate is True else records.env._lang
  1618. # not dirty fields
  1619. if not dirty:
  1620. cache.update_raw(records, self, [{lang: cache_value} for _id in records._ids], dirty=False)
  1621. return
  1622. # model translation
  1623. if not callable(self.translate):
  1624. # invalidate clean fields because them may contain fallback value
  1625. clean_records = records - cache.get_dirty_records(records, self)
  1626. clean_records.invalidate_recordset([self.name])
  1627. cache.update(records, self, itertools.repeat(cache_value), dirty=True)
  1628. if lang != 'en_US' and not records.env['res.lang']._get_data(code='en_US'):
  1629. # if 'en_US' is not active, we always write en_US to make sure value_en is meaningful
  1630. cache.update(records.with_context(lang='en_US'), self, itertools.repeat(cache_value), dirty=True)
  1631. return
  1632. # model term translation
  1633. new_translations_list = []
  1634. new_terms = set(self.get_trans_terms(cache_value))
  1635. delay_translations = records.env.context.get('delay_translations')
  1636. for record in records:
  1637. # shortcut when no term needs to be translated
  1638. if not new_terms:
  1639. new_translations_list.append({'en_US': cache_value, lang: cache_value})
  1640. continue
  1641. # _get_stored_translations can be refactored and prefetches translations for multi records,
  1642. # but it is really rare to write the same non-False/None/no-term value to multi records
  1643. stored_translations = self._get_stored_translations(record)
  1644. if not stored_translations:
  1645. new_translations_list.append({'en_US': cache_value, lang: cache_value})
  1646. continue
  1647. old_translations = {
  1648. k: stored_translations.get(f'_{k}', v)
  1649. for k, v in stored_translations.items()
  1650. if not k.startswith('_')
  1651. }
  1652. from_lang_value = old_translations.pop(lang, old_translations['en_US'])
  1653. translation_dictionary = self.get_translation_dictionary(from_lang_value, old_translations)
  1654. text2terms = defaultdict(list)
  1655. for term in new_terms:
  1656. term_text = self.get_text_content(term)
  1657. if term_text:
  1658. text2terms[term_text].append(term)
  1659. is_text = self.translate.is_text if hasattr(self.translate, 'is_text') else lambda term: True
  1660. term_adapter = self.translate.term_adapter if hasattr(self.translate, 'term_adapter') else None
  1661. for old_term in list(translation_dictionary.keys()):
  1662. if old_term not in new_terms:
  1663. old_term_text = self.get_text_content(old_term)
  1664. matches = get_close_matches(old_term_text, text2terms, 1, 0.9)
  1665. if matches:
  1666. closest_term = get_close_matches(old_term, text2terms[matches[0]], 1, 0)[0]
  1667. if closest_term in translation_dictionary:
  1668. continue
  1669. old_is_text = is_text(old_term)
  1670. closest_is_text = is_text(closest_term)
  1671. if old_is_text or not closest_is_text:
  1672. if not closest_is_text and records.env.context.get("install_mode") and lang == 'en_US' and term_adapter:
  1673. adapter = term_adapter(closest_term)
  1674. if adapter(old_term) is None: # old term and closest_term have different structures
  1675. continue
  1676. translation_dictionary[closest_term] = {k: adapter(v) for k, v in translation_dictionary.pop(old_term).items()}
  1677. else:
  1678. translation_dictionary[closest_term] = translation_dictionary.pop(old_term)
  1679. # pylint: disable=not-callable
  1680. new_translations = {
  1681. l: self.translate(lambda term: translation_dictionary.get(term, {l: None})[l], cache_value)
  1682. for l in old_translations.keys()
  1683. }
  1684. if delay_translations:
  1685. new_store_translations = stored_translations
  1686. new_store_translations.update({f'_{k}': v for k, v in new_translations.items()})
  1687. new_store_translations.pop(f'_{lang}', None)
  1688. else:
  1689. new_store_translations = new_translations
  1690. new_store_translations[lang] = cache_value
  1691. if not records.env['res.lang']._get_data(code='en_US'):
  1692. new_store_translations['en_US'] = cache_value
  1693. new_store_translations.pop('_en_US', None)
  1694. new_translations_list.append(new_store_translations)
  1695. # Maybe we can use Cache.update(records.with_context(cache_update_raw=True), self, new_translations_list, dirty=True)
  1696. cache.update_raw(records, self, new_translations_list, dirty=True)
  1697. class Char(_String):
  1698. """ Basic string field, can be length-limited, usually displayed as a
  1699. single-line string in clients.
  1700. :param int size: the maximum size of values stored for that field
  1701. :param bool trim: states whether the value is trimmed or not (by default,
  1702. ``True``). Note that the trim operation is applied only by the web client.
  1703. :param translate: enable the translation of the field's values; use
  1704. ``translate=True`` to translate field values as a whole; ``translate``
  1705. may also be a callable such that ``translate(callback, value)``
  1706. translates ``value`` by using ``callback(term)`` to retrieve the
  1707. translation of terms.
  1708. :type translate: bool or callable
  1709. """
  1710. type = 'char'
  1711. trim = True # whether value is trimmed (only by web client)
  1712. def _setup_attrs(self, model_class, name):
  1713. super()._setup_attrs(model_class, name)
  1714. assert self.size is None or isinstance(self.size, int), \
  1715. "Char field %s with non-integer size %r" % (self, self.size)
  1716. @property
  1717. def _column_type(self):
  1718. return ('varchar', pg_varchar(self.size))
  1719. def update_db_column(self, model, column):
  1720. if (
  1721. column and self.column_type[0] == 'varchar' and
  1722. column['udt_name'] == 'varchar' and column['character_maximum_length'] and
  1723. (self.size is None or column['character_maximum_length'] < self.size)
  1724. ):
  1725. # the column's varchar size does not match self.size; convert it
  1726. sql.convert_column(model._cr, model._table, self.name, self.column_type[1])
  1727. super().update_db_column(model, column)
  1728. _related_size = property(attrgetter('size'))
  1729. _related_trim = property(attrgetter('trim'))
  1730. _description_size = property(attrgetter('size'))
  1731. _description_trim = property(attrgetter('trim'))
  1732. class Text(_String):
  1733. """ Very similar to :class:`Char` but used for longer contents, does not
  1734. have a size and usually displayed as a multiline text box.
  1735. :param translate: enable the translation of the field's values; use
  1736. ``translate=True`` to translate field values as a whole; ``translate``
  1737. may also be a callable such that ``translate(callback, value)``
  1738. translates ``value`` by using ``callback(term)`` to retrieve the
  1739. translation of terms.
  1740. :type translate: bool or callable
  1741. """
  1742. type = 'text'
  1743. _column_type = ('text', 'text')
  1744. class Html(_String):
  1745. """ Encapsulates an html code content.
  1746. :param bool sanitize: whether value must be sanitized (default: ``True``)
  1747. :param bool sanitize_overridable: whether the sanitation can be bypassed by
  1748. the users part of the `base.group_sanitize_override` group (default: ``False``)
  1749. :param bool sanitize_tags: whether to sanitize tags
  1750. (only a white list of attributes is accepted, default: ``True``)
  1751. :param bool sanitize_attributes: whether to sanitize attributes
  1752. (only a white list of attributes is accepted, default: ``True``)
  1753. :param bool sanitize_style: whether to sanitize style attributes (default: ``False``)
  1754. :param bool sanitize_conditional_comments: whether to kill conditional comments. (default: ``True``)
  1755. :param bool sanitize_output_method: whether to sanitize using html or xhtml (default: ``html``)
  1756. :param bool strip_style: whether to strip style attributes
  1757. (removed and therefore not sanitized, default: ``False``)
  1758. :param bool strip_classes: whether to strip classes attributes (default: ``False``)
  1759. """
  1760. type = 'html'
  1761. _column_type = ('text', 'text')
  1762. sanitize = True # whether value must be sanitized
  1763. sanitize_overridable = False # whether the sanitation can be bypassed by the users part of the `base.group_sanitize_override` group
  1764. sanitize_tags = True # whether to sanitize tags (only a white list of attributes is accepted)
  1765. sanitize_attributes = True # whether to sanitize attributes (only a white list of attributes is accepted)
  1766. sanitize_style = False # whether to sanitize style attributes
  1767. sanitize_form = True # whether to sanitize forms
  1768. sanitize_conditional_comments = True # whether to kill conditional comments. Otherwise keep them but with their content sanitized.
  1769. sanitize_output_method = 'html' # whether to sanitize using html or xhtml
  1770. strip_style = False # whether to strip style attributes (removed and therefore not sanitized)
  1771. strip_classes = False # whether to strip classes attributes
  1772. def _get_attrs(self, model_class, name):
  1773. # called by _setup_attrs(), working together with _String._setup_attrs()
  1774. attrs = super()._get_attrs(model_class, name)
  1775. # Shortcut for common sanitize options
  1776. # Outgoing and incoming emails should not be sanitized with the same options.
  1777. # e.g. conditional comments: no need to keep conditional comments for incoming emails,
  1778. # we do not need this Microsoft Outlook client feature for emails displayed Odoo's web client.
  1779. # While we need to keep them in mail templates and mass mailings, because they could be rendered in Outlook.
  1780. if attrs.get('sanitize') == 'email_outgoing':
  1781. attrs['sanitize'] = True
  1782. attrs.update({key: value for key, value in {
  1783. 'sanitize_tags': False,
  1784. 'sanitize_attributes': False,
  1785. 'sanitize_conditional_comments': False,
  1786. 'sanitize_output_method': 'xml',
  1787. }.items() if key not in attrs})
  1788. # Translated sanitized html fields must use html_translate or a callable.
  1789. # `elif` intended, because HTML fields with translate=True and sanitize=False
  1790. # where not using `html_translate` before and they must remain without `html_translate`.
  1791. # Otherwise, breaks `--test-tags .test_render_field`, for instance.
  1792. elif attrs.get('translate') is True and attrs.get('sanitize', True):
  1793. attrs['translate'] = html_translate
  1794. return attrs
  1795. _related_sanitize = property(attrgetter('sanitize'))
  1796. _related_sanitize_tags = property(attrgetter('sanitize_tags'))
  1797. _related_sanitize_attributes = property(attrgetter('sanitize_attributes'))
  1798. _related_sanitize_style = property(attrgetter('sanitize_style'))
  1799. _related_strip_style = property(attrgetter('strip_style'))
  1800. _related_strip_classes = property(attrgetter('strip_classes'))
  1801. _description_sanitize = property(attrgetter('sanitize'))
  1802. _description_sanitize_tags = property(attrgetter('sanitize_tags'))
  1803. _description_sanitize_attributes = property(attrgetter('sanitize_attributes'))
  1804. _description_sanitize_style = property(attrgetter('sanitize_style'))
  1805. _description_strip_style = property(attrgetter('strip_style'))
  1806. _description_strip_classes = property(attrgetter('strip_classes'))
  1807. def convert_to_column(self, value, record, values=None, validate=True):
  1808. value = self._convert(value, record, validate=True)
  1809. return super().convert_to_column(value, record, values, validate=False)
  1810. def convert_to_cache(self, value, record, validate=True):
  1811. return self._convert(value, record, validate)
  1812. def _convert(self, value, record, validate):
  1813. if value is None or value is False:
  1814. return None
  1815. if not validate or not self.sanitize:
  1816. return value
  1817. sanitize_vals = {
  1818. 'silent': True,
  1819. 'sanitize_tags': self.sanitize_tags,
  1820. 'sanitize_attributes': self.sanitize_attributes,
  1821. 'sanitize_style': self.sanitize_style,
  1822. 'sanitize_form': self.sanitize_form,
  1823. 'sanitize_conditional_comments': self.sanitize_conditional_comments,
  1824. 'output_method': self.sanitize_output_method,
  1825. 'strip_style': self.strip_style,
  1826. 'strip_classes': self.strip_classes
  1827. }
  1828. if self.sanitize_overridable:
  1829. if record.env.user.has_group('base.group_sanitize_override'):
  1830. return value
  1831. original_value = record[self.name]
  1832. if original_value:
  1833. # Note that sanitize also normalize
  1834. original_value_sanitized = html_sanitize(original_value, **sanitize_vals)
  1835. original_value_normalized = html_normalize(original_value)
  1836. if (
  1837. not original_value_sanitized # sanitizer could empty it
  1838. or original_value_normalized != original_value_sanitized
  1839. ):
  1840. # The field contains element(s) that would be removed if
  1841. # sanitized. It means that someone who was part of a group
  1842. # allowing to bypass the sanitation saved that field
  1843. # previously.
  1844. diff = unified_diff(
  1845. original_value_sanitized.splitlines(),
  1846. original_value_normalized.splitlines(),
  1847. )
  1848. with_colors = isinstance(logging.getLogger().handlers[0].formatter, ColoredFormatter)
  1849. diff_str = f'The field ({record._description}, {self.string}) will not be editable:\n'
  1850. for line in list(diff)[2:]:
  1851. if with_colors:
  1852. color = {'-': RED, '+': GREEN}.get(line[:1], DEFAULT)
  1853. diff_str += COLOR_PATTERN % (30 + color, 40 + DEFAULT, line.rstrip() + "\n")
  1854. else:
  1855. diff_str += line.rstrip() + '\n'
  1856. _logger.info(diff_str)
  1857. raise UserError(record.env._(
  1858. "The field value you're saving (%(model)s %(field)s) includes content that is "
  1859. "restricted for security reasons. It is possible that someone "
  1860. "with higher privileges previously modified it, and you are therefore "
  1861. "not able to modify it yourself while preserving the content.",
  1862. model=record._description, field=self.string,
  1863. ))
  1864. return html_sanitize(value, **sanitize_vals)
  1865. def convert_to_record(self, value, record):
  1866. r = super().convert_to_record(value, record)
  1867. if isinstance(r, bytes):
  1868. r = r.decode()
  1869. return r and Markup(r)
  1870. def convert_to_read(self, value, record, use_display_name=True):
  1871. r = super().convert_to_read(value, record, use_display_name)
  1872. if isinstance(r, bytes):
  1873. r = r.decode()
  1874. return r and Markup(r)
  1875. def get_trans_terms(self, value):
  1876. # ensure the translation terms are stringified, otherwise we can break the PO file
  1877. return list(map(str, super().get_trans_terms(value)))
  1878. class Date(Field[date | typing.Literal[False]]):
  1879. """ Encapsulates a python :class:`date <datetime.date>` object. """
  1880. type = 'date'
  1881. _column_type = ('date', 'date')
  1882. start_of = staticmethod(date_utils.start_of)
  1883. end_of = staticmethod(date_utils.end_of)
  1884. add = staticmethod(date_utils.add)
  1885. subtract = staticmethod(date_utils.subtract)
  1886. @staticmethod
  1887. def today(*args):
  1888. """Return the current day in the format expected by the ORM.
  1889. .. note:: This function may be used to compute default values.
  1890. """
  1891. return date.today()
  1892. @staticmethod
  1893. def context_today(record, timestamp=None):
  1894. """Return the current date as seen in the client's timezone in a format
  1895. fit for date fields.
  1896. .. note:: This method may be used to compute default values.
  1897. :param record: recordset from which the timezone will be obtained.
  1898. :param datetime timestamp: optional datetime value to use instead of
  1899. the current date and time (must be a datetime, regular dates
  1900. can't be converted between timezones).
  1901. :rtype: date
  1902. """
  1903. today = timestamp or datetime.now()
  1904. context_today = None
  1905. tz_name = record._context.get('tz') or record.env.user.tz
  1906. if tz_name:
  1907. try:
  1908. today_utc = pytz.timezone('UTC').localize(today, is_dst=False) # UTC = no DST
  1909. context_today = today_utc.astimezone(pytz.timezone(tz_name))
  1910. except Exception:
  1911. _logger.debug("failed to compute context/client-specific today date, using UTC value for `today`",
  1912. exc_info=True)
  1913. return (context_today or today).date()
  1914. @staticmethod
  1915. def to_date(value):
  1916. """Attempt to convert ``value`` to a :class:`date` object.
  1917. .. warning::
  1918. If a datetime object is given as value,
  1919. it will be converted to a date object and all
  1920. datetime-specific information will be lost (HMS, TZ, ...).
  1921. :param value: value to convert.
  1922. :type value: str or date or datetime
  1923. :return: an object representing ``value``.
  1924. :rtype: date or None
  1925. """
  1926. if not value:
  1927. return None
  1928. if isinstance(value, date):
  1929. if isinstance(value, datetime):
  1930. return value.date()
  1931. return value
  1932. value = value[:DATE_LENGTH]
  1933. return datetime.strptime(value, DATE_FORMAT).date()
  1934. # kept for backwards compatibility, but consider `from_string` as deprecated, will probably
  1935. # be removed after V12
  1936. from_string = to_date
  1937. @staticmethod
  1938. def to_string(value):
  1939. """
  1940. Convert a :class:`date` or :class:`datetime` object to a string.
  1941. :param value: value to convert.
  1942. :return: a string representing ``value`` in the server's date format, if ``value`` is of
  1943. type :class:`datetime`, the hours, minute, seconds, tzinfo will be truncated.
  1944. :rtype: str
  1945. """
  1946. return value.strftime(DATE_FORMAT) if value else False
  1947. def convert_to_column_update(self, value, record):
  1948. if self.company_dependent:
  1949. return PsycopgJson({k: self.to_string(v) or None for k, v in value.items()})
  1950. return super().convert_to_column_update(value, record)
  1951. def convert_to_cache(self, value, record, validate=True):
  1952. if not value:
  1953. return None
  1954. if isinstance(value, datetime):
  1955. # TODO: better fix data files (crm demo data)
  1956. value = value.date()
  1957. # raise TypeError("%s (field %s) must be string or date, not datetime." % (value, self))
  1958. return self.to_date(value)
  1959. def convert_to_export(self, value, record):
  1960. if not value:
  1961. return ''
  1962. return self.from_string(value)
  1963. def convert_to_display_name(self, value, record):
  1964. return Date.to_string(value)
  1965. class Datetime(Field[datetime | typing.Literal[False]]):
  1966. """ Encapsulates a python :class:`datetime <datetime.datetime>` object. """
  1967. type = 'datetime'
  1968. _column_type = ('timestamp', 'timestamp')
  1969. start_of = staticmethod(date_utils.start_of)
  1970. end_of = staticmethod(date_utils.end_of)
  1971. add = staticmethod(date_utils.add)
  1972. subtract = staticmethod(date_utils.subtract)
  1973. @staticmethod
  1974. def now(*args):
  1975. """Return the current day and time in the format expected by the ORM.
  1976. .. note:: This function may be used to compute default values.
  1977. """
  1978. # microseconds must be annihilated as they don't comply with the server datetime format
  1979. return datetime.now().replace(microsecond=0)
  1980. @staticmethod
  1981. def today(*args):
  1982. """Return the current day, at midnight (00:00:00)."""
  1983. return Datetime.now().replace(hour=0, minute=0, second=0)
  1984. @staticmethod
  1985. def context_timestamp(record, timestamp):
  1986. """Return the given timestamp converted to the client's timezone.
  1987. .. note:: This method is *not* meant for use as a default initializer,
  1988. because datetime fields are automatically converted upon
  1989. display on client side. For default values, :meth:`now`
  1990. should be used instead.
  1991. :param record: recordset from which the timezone will be obtained.
  1992. :param datetime timestamp: naive datetime value (expressed in UTC)
  1993. to be converted to the client timezone.
  1994. :return: timestamp converted to timezone-aware datetime in context timezone.
  1995. :rtype: datetime
  1996. """
  1997. assert isinstance(timestamp, datetime), 'Datetime instance expected'
  1998. tz_name = record._context.get('tz') or record.env.user.tz
  1999. utc_timestamp = pytz.utc.localize(timestamp, is_dst=False) # UTC = no DST
  2000. if tz_name:
  2001. try:
  2002. context_tz = pytz.timezone(tz_name)
  2003. return utc_timestamp.astimezone(context_tz)
  2004. except Exception:
  2005. _logger.debug("failed to compute context/client-specific timestamp, "
  2006. "using the UTC value",
  2007. exc_info=True)
  2008. return utc_timestamp
  2009. @staticmethod
  2010. def to_datetime(value):
  2011. """Convert an ORM ``value`` into a :class:`datetime` value.
  2012. :param value: value to convert.
  2013. :type value: str or date or datetime
  2014. :return: an object representing ``value``.
  2015. :rtype: datetime or None
  2016. """
  2017. if not value:
  2018. return None
  2019. if isinstance(value, date):
  2020. if isinstance(value, datetime):
  2021. if value.tzinfo:
  2022. raise ValueError("Datetime field expects a naive datetime: %s" % value)
  2023. return value
  2024. return datetime.combine(value, time.min)
  2025. # TODO: fix data files
  2026. return datetime.strptime(value, DATETIME_FORMAT[:len(value)-2])
  2027. # kept for backwards compatibility, but consider `from_string` as deprecated, will probably
  2028. # be removed after V12
  2029. from_string = to_datetime
  2030. @staticmethod
  2031. def to_string(value):
  2032. """Convert a :class:`datetime` or :class:`date` object to a string.
  2033. :param value: value to convert.
  2034. :type value: datetime or date
  2035. :return: a string representing ``value`` in the server's datetime format,
  2036. if ``value`` is of type :class:`date`,
  2037. the time portion will be midnight (00:00:00).
  2038. :rtype: str
  2039. """
  2040. return value.strftime(DATETIME_FORMAT) if value else False
  2041. def convert_to_column_update(self, value, record):
  2042. if self.company_dependent:
  2043. return PsycopgJson({k: self.to_string(v) or None for k, v in value.items()})
  2044. return super().convert_to_column_update(value, record)
  2045. def convert_to_cache(self, value, record, validate=True):
  2046. return self.to_datetime(value)
  2047. def convert_to_export(self, value, record):
  2048. if not value:
  2049. return ''
  2050. value = self.convert_to_display_name(value, record)
  2051. return self.from_string(value)
  2052. def convert_to_display_name(self, value, record):
  2053. if not value:
  2054. return False
  2055. return Datetime.to_string(Datetime.context_timestamp(record, value))
  2056. # http://initd.org/psycopg/docs/usage.html#binary-adaptation
  2057. # Received data is returned as buffer (in Python 2) or memoryview (in Python 3).
  2058. _BINARY = memoryview
  2059. class Binary(Field):
  2060. """Encapsulates a binary content (e.g. a file).
  2061. :param bool attachment: whether the field should be stored as `ir_attachment`
  2062. or in a column of the model's table (default: ``True``).
  2063. """
  2064. type = 'binary'
  2065. prefetch = False # not prefetched by default
  2066. _depends_context = ('bin_size',) # depends on context (content or size)
  2067. attachment = True # whether value is stored in attachment
  2068. @lazy_property
  2069. def column_type(self):
  2070. return None if self.attachment else ('bytea', 'bytea')
  2071. def _get_attrs(self, model_class, name):
  2072. attrs = super()._get_attrs(model_class, name)
  2073. if not attrs.get('store', True):
  2074. attrs['attachment'] = False
  2075. return attrs
  2076. _description_attachment = property(attrgetter('attachment'))
  2077. def convert_to_column(self, value, record, values=None, validate=True):
  2078. # Binary values may be byte strings (python 2.6 byte array), but
  2079. # the legacy OpenERP convention is to transfer and store binaries
  2080. # as base64-encoded strings. The base64 string may be provided as a
  2081. # unicode in some circumstances, hence the str() cast here.
  2082. # This str() coercion will only work for pure ASCII unicode strings,
  2083. # on purpose - non base64 data must be passed as a 8bit byte strings.
  2084. if not value:
  2085. return None
  2086. # Detect if the binary content is an SVG for restricting its upload
  2087. # only to system users.
  2088. magic_bytes = {
  2089. b'P', # first 6 bits of '<' (0x3C) b64 encoded
  2090. b'<', # plaintext XML tag opening
  2091. }
  2092. if isinstance(value, str):
  2093. value = value.encode()
  2094. if value[:1] in magic_bytes:
  2095. try:
  2096. decoded_value = base64.b64decode(value.translate(None, delete=b'\r\n'), validate=True)
  2097. except binascii.Error:
  2098. decoded_value = value
  2099. # Full mimetype detection
  2100. if (guess_mimetype(decoded_value).startswith('image/svg') and
  2101. not record.env.is_system()):
  2102. raise UserError(record.env._("Only admins can upload SVG files."))
  2103. if isinstance(value, bytes):
  2104. return psycopg2.Binary(value)
  2105. try:
  2106. return psycopg2.Binary(str(value).encode('ascii'))
  2107. except UnicodeEncodeError:
  2108. raise UserError(record.env._("ASCII characters are required for %(value)s in %(field)s", value=value, field=self.name))
  2109. def convert_to_cache(self, value, record, validate=True):
  2110. if isinstance(value, _BINARY):
  2111. return bytes(value)
  2112. if isinstance(value, str):
  2113. # the cache must contain bytes or memoryview, but sometimes a string
  2114. # is given when assigning a binary field (test `TestFileSeparator`)
  2115. return value.encode()
  2116. if isinstance(value, int) and \
  2117. (record._context.get('bin_size') or
  2118. record._context.get('bin_size_' + self.name)):
  2119. # If the client requests only the size of the field, we return that
  2120. # instead of the content. Presumably a separate request will be done
  2121. # to read the actual content, if necessary.
  2122. value = human_size(value)
  2123. # human_size can return False (-> None) or a string (-> encoded)
  2124. return value.encode() if value else None
  2125. return None if value is False else value
  2126. def convert_to_record(self, value, record):
  2127. if isinstance(value, _BINARY):
  2128. return bytes(value)
  2129. return False if value is None else value
  2130. def compute_value(self, records):
  2131. bin_size_name = 'bin_size_' + self.name
  2132. if records.env.context.get('bin_size') or records.env.context.get(bin_size_name):
  2133. # always compute without bin_size
  2134. records_no_bin_size = records.with_context(**{'bin_size': False, bin_size_name: False})
  2135. super().compute_value(records_no_bin_size)
  2136. # manually update the bin_size cache
  2137. cache = records.env.cache
  2138. for record_no_bin_size, record in zip(records_no_bin_size, records):
  2139. try:
  2140. value = cache.get(record_no_bin_size, self)
  2141. # don't decode non-attachments to be consistent with pg_size_pretty
  2142. if not (self.store and self.column_type):
  2143. with contextlib.suppress(TypeError, binascii.Error):
  2144. value = base64.b64decode(value)
  2145. try:
  2146. if isinstance(value, (bytes, _BINARY)):
  2147. value = human_size(len(value))
  2148. except (TypeError):
  2149. pass
  2150. cache_value = self.convert_to_cache(value, record)
  2151. # the dirty flag is independent from this assignment
  2152. cache.set(record, self, cache_value, check_dirty=False)
  2153. except CacheMiss:
  2154. pass
  2155. else:
  2156. super().compute_value(records)
  2157. def read(self, records):
  2158. # values are stored in attachments, retrieve them
  2159. assert self.attachment
  2160. domain = [
  2161. ('res_model', '=', records._name),
  2162. ('res_field', '=', self.name),
  2163. ('res_id', 'in', records.ids),
  2164. ]
  2165. # Note: the 'bin_size' flag is handled by the field 'datas' itself
  2166. data = {
  2167. att.res_id: att.datas
  2168. for att in records.env['ir.attachment'].sudo().search(domain)
  2169. }
  2170. records.env.cache.insert_missing(records, self, map(data.get, records._ids))
  2171. def create(self, record_values):
  2172. assert self.attachment
  2173. if not record_values:
  2174. return
  2175. # create the attachments that store the values
  2176. env = record_values[0][0].env
  2177. env['ir.attachment'].sudo().create([
  2178. {
  2179. 'name': self.name,
  2180. 'res_model': self.model_name,
  2181. 'res_field': self.name,
  2182. 'res_id': record.id,
  2183. 'type': 'binary',
  2184. 'datas': value,
  2185. }
  2186. for record, value in record_values
  2187. if value
  2188. ])
  2189. def write(self, records, value):
  2190. records = records.with_context(bin_size=False)
  2191. if not self.attachment:
  2192. super().write(records, value)
  2193. return
  2194. # discard recomputation of self on records
  2195. records.env.remove_to_compute(self, records)
  2196. # update the cache, and discard the records that are not modified
  2197. cache = records.env.cache
  2198. cache_value = self.convert_to_cache(value, records)
  2199. records = cache.get_records_different_from(records, self, cache_value)
  2200. if not records:
  2201. return
  2202. if self.store:
  2203. # determine records that are known to be not null
  2204. not_null = cache.get_records_different_from(records, self, None)
  2205. cache.update(records, self, itertools.repeat(cache_value))
  2206. # retrieve the attachments that store the values, and adapt them
  2207. if self.store and any(records._ids):
  2208. real_records = records.filtered('id')
  2209. atts = records.env['ir.attachment'].sudo()
  2210. if not_null:
  2211. atts = atts.search([
  2212. ('res_model', '=', self.model_name),
  2213. ('res_field', '=', self.name),
  2214. ('res_id', 'in', real_records.ids),
  2215. ])
  2216. if value:
  2217. # update the existing attachments
  2218. atts.write({'datas': value})
  2219. atts_records = records.browse(atts.mapped('res_id'))
  2220. # create the missing attachments
  2221. missing = (real_records - atts_records)
  2222. if missing:
  2223. atts.create([{
  2224. 'name': self.name,
  2225. 'res_model': record._name,
  2226. 'res_field': self.name,
  2227. 'res_id': record.id,
  2228. 'type': 'binary',
  2229. 'datas': value,
  2230. }
  2231. for record in missing
  2232. ])
  2233. else:
  2234. atts.unlink()
  2235. class Image(Binary):
  2236. """Encapsulates an image, extending :class:`Binary`.
  2237. If image size is greater than the ``max_width``/``max_height`` limit of pixels, the image will be
  2238. resized to the limit by keeping aspect ratio.
  2239. :param int max_width: the maximum width of the image (default: ``0``, no limit)
  2240. :param int max_height: the maximum height of the image (default: ``0``, no limit)
  2241. :param bool verify_resolution: whether the image resolution should be verified
  2242. to ensure it doesn't go over the maximum image resolution (default: ``True``).
  2243. See :class:`odoo.tools.image.ImageProcess` for maximum image resolution (default: ``50e6``).
  2244. .. note::
  2245. If no ``max_width``/``max_height`` is specified (or is set to 0) and ``verify_resolution`` is False,
  2246. the field content won't be verified at all and a :class:`Binary` field should be used.
  2247. """
  2248. max_width = 0
  2249. max_height = 0
  2250. verify_resolution = True
  2251. def setup(self, model):
  2252. super().setup(model)
  2253. if not model._abstract and not model._log_access:
  2254. warnings.warn(f"Image field {self} requires the model to have _log_access = True")
  2255. def create(self, record_values):
  2256. new_record_values = []
  2257. for record, value in record_values:
  2258. new_value = self._image_process(value, record.env)
  2259. new_record_values.append((record, new_value))
  2260. # when setting related image field, keep the unprocessed image in
  2261. # cache to let the inverse method use the original image; the image
  2262. # will be resized once the inverse has been applied
  2263. cache_value = self.convert_to_cache(value if self.related else new_value, record)
  2264. record.env.cache.update(record, self, itertools.repeat(cache_value))
  2265. super(Image, self).create(new_record_values)
  2266. def write(self, records, value):
  2267. try:
  2268. new_value = self._image_process(value, records.env)
  2269. except UserError:
  2270. if not any(records._ids):
  2271. # Some crap is assigned to a new record. This can happen in an
  2272. # onchange, where the client sends the "bin size" value of the
  2273. # field instead of its full value (this saves bandwidth). In
  2274. # this case, we simply don't assign the field: its value will be
  2275. # taken from the records' origin.
  2276. return
  2277. raise
  2278. super(Image, self).write(records, new_value)
  2279. cache_value = self.convert_to_cache(value if self.related else new_value, records)
  2280. dirty = self.column_type and self.store and any(records._ids)
  2281. records.env.cache.update(records, self, itertools.repeat(cache_value), dirty=dirty)
  2282. def _inverse_related(self, records):
  2283. super()._inverse_related(records)
  2284. if not (self.max_width and self.max_height):
  2285. return
  2286. # the inverse has been applied with the original image; now we fix the
  2287. # cache with the resized value
  2288. for record in records:
  2289. value = self._process_related(record[self.name], record.env)
  2290. record.env.cache.set(record, self, value, dirty=(self.store and self.column_type))
  2291. def _image_process(self, value, env):
  2292. if self.readonly and not self.max_width and not self.max_height:
  2293. # no need to process images for computed fields, or related fields
  2294. return value
  2295. try:
  2296. img = base64.b64decode(value or '') or False
  2297. except:
  2298. raise UserError(env._("Image is not encoded in base64."))
  2299. if img and guess_mimetype(img, '') == 'image/webp':
  2300. if not self.max_width and not self.max_height:
  2301. return value
  2302. # Fetch resized version.
  2303. Attachment = env['ir.attachment']
  2304. checksum = Attachment._compute_checksum(img)
  2305. origins = Attachment.search([
  2306. ['id', '!=', False], # No implicit condition on res_field.
  2307. ['checksum', '=', checksum],
  2308. ])
  2309. if origins:
  2310. origin_ids = [attachment.id for attachment in origins]
  2311. resized_domain = [
  2312. ['id', '!=', False], # No implicit condition on res_field.
  2313. ['res_model', '=', 'ir.attachment'],
  2314. ['res_id', 'in', origin_ids],
  2315. ['description', '=', 'resize: %s' % max(self.max_width, self.max_height)],
  2316. ]
  2317. resized = Attachment.sudo().search(resized_domain, limit=1)
  2318. if resized:
  2319. # Fallback on non-resized image (value).
  2320. return resized.datas or value
  2321. return value
  2322. return base64.b64encode(image_process(img,
  2323. size=(self.max_width, self.max_height),
  2324. verify_resolution=self.verify_resolution,
  2325. ) or b'') or False
  2326. def _process_related(self, value, env):
  2327. """Override to resize the related value before saving it on self."""
  2328. try:
  2329. return self._image_process(super()._process_related(value, env), env)
  2330. except UserError:
  2331. # Avoid the following `write` to fail if the related image was saved
  2332. # invalid, which can happen for pre-existing databases.
  2333. return False
  2334. class Selection(Field[str | typing.Literal[False]]):
  2335. """ Encapsulates an exclusive choice between different values.
  2336. :param selection: specifies the possible values for this field.
  2337. It is given as either a list of pairs ``(value, label)``, or a model
  2338. method, or a method name.
  2339. :type selection: list(tuple(str,str)) or callable or str
  2340. :param selection_add: provides an extension of the selection in the case
  2341. of an overridden field. It is a list of pairs ``(value, label)`` or
  2342. singletons ``(value,)``, where singleton values must appear in the
  2343. overridden selection. The new values are inserted in an order that is
  2344. consistent with the overridden selection and this list::
  2345. selection = [('a', 'A'), ('b', 'B')]
  2346. selection_add = [('c', 'C'), ('b',)]
  2347. > result = [('a', 'A'), ('c', 'C'), ('b', 'B')]
  2348. :type selection_add: list(tuple(str,str))
  2349. :param ondelete: provides a fallback mechanism for any overridden
  2350. field with a selection_add. It is a dict that maps every option
  2351. from the selection_add to a fallback action.
  2352. This fallback action will be applied to all records whose
  2353. selection_add option maps to it.
  2354. The actions can be any of the following:
  2355. - 'set null' -- the default, all records with this option
  2356. will have their selection value set to False.
  2357. - 'cascade' -- all records with this option will be
  2358. deleted along with the option itself.
  2359. - 'set default' -- all records with this option will be
  2360. set to the default of the field definition
  2361. - 'set VALUE' -- all records with this option will be
  2362. set to the given value
  2363. - <callable> -- a callable whose first and only argument will be
  2364. the set of records containing the specified Selection option,
  2365. for custom processing
  2366. The attribute ``selection`` is mandatory except in the case of
  2367. ``related`` or extended fields.
  2368. """
  2369. type = 'selection'
  2370. _column_type = ('varchar', pg_varchar())
  2371. selection = None # [(value, string), ...], function or method name
  2372. validate = True # whether validating upon write
  2373. ondelete = None # {value: policy} (what to do when value is deleted)
  2374. def __init__(self, selection=SENTINEL, string: str | Sentinel = SENTINEL, **kwargs):
  2375. super(Selection, self).__init__(selection=selection, string=string, **kwargs)
  2376. self._selection = dict(selection) if isinstance(selection, list) else None
  2377. def setup_nonrelated(self, model):
  2378. super().setup_nonrelated(model)
  2379. assert self.selection is not None, "Field %s without selection" % self
  2380. def setup_related(self, model):
  2381. super().setup_related(model)
  2382. # selection must be computed on related field
  2383. field = self.related_field
  2384. self.selection = lambda model: field._description_selection(model.env)
  2385. self._selection = None
  2386. def _get_attrs(self, model_class, name):
  2387. attrs = super()._get_attrs(model_class, name)
  2388. # arguments 'selection' and 'selection_add' are processed below
  2389. attrs.pop('selection_add', None)
  2390. # Selection fields have an optional default implementation of a group_expand function
  2391. if attrs.get('group_expand') is True:
  2392. attrs['group_expand'] = self._default_group_expand
  2393. return attrs
  2394. def _setup_attrs(self, model_class, name):
  2395. super()._setup_attrs(model_class, name)
  2396. if not self._base_fields:
  2397. return
  2398. # determine selection (applying 'selection_add' extensions) as a dict
  2399. values = None
  2400. for field in self._base_fields:
  2401. # We cannot use field.selection or field.selection_add here
  2402. # because those attributes are overridden by ``_setup_attrs``.
  2403. if 'selection' in field.args:
  2404. if self.related:
  2405. _logger.warning("%s: selection attribute will be ignored as the field is related", self)
  2406. selection = field.args['selection']
  2407. if isinstance(selection, list):
  2408. if values is not None and list(values) != [kv[0] for kv in selection]:
  2409. _logger.warning("%s: selection=%r overrides existing selection; use selection_add instead", self, selection)
  2410. values = dict(selection)
  2411. self.ondelete = {}
  2412. else:
  2413. values = None
  2414. self.selection = selection
  2415. self.ondelete = None
  2416. if 'selection_add' in field.args:
  2417. if self.related:
  2418. _logger.warning("%s: selection_add attribute will be ignored as the field is related", self)
  2419. selection_add = field.args['selection_add']
  2420. assert isinstance(selection_add, list), \
  2421. "%s: selection_add=%r must be a list" % (self, selection_add)
  2422. assert values is not None, \
  2423. "%s: selection_add=%r on non-list selection %r" % (self, selection_add, self.selection)
  2424. values_add = {kv[0]: (kv[1] if len(kv) > 1 else None) for kv in selection_add}
  2425. ondelete = field.args.get('ondelete') or {}
  2426. new_values = [key for key in values_add if key not in values]
  2427. for key in new_values:
  2428. ondelete.setdefault(key, 'set null')
  2429. if self.required and new_values and 'set null' in ondelete.values():
  2430. raise ValueError(
  2431. "%r: required selection fields must define an ondelete policy that "
  2432. "implements the proper cleanup of the corresponding records upon "
  2433. "module uninstallation. Please use one or more of the following "
  2434. "policies: 'set default' (if the field has a default defined), 'cascade', "
  2435. "or a single-argument callable where the argument is the recordset "
  2436. "containing the specified option." % self
  2437. )
  2438. # check ondelete values
  2439. for key, val in ondelete.items():
  2440. if callable(val) or val in ('set null', 'cascade'):
  2441. continue
  2442. if val == 'set default':
  2443. assert self.default is not None, (
  2444. "%r: ondelete policy of type 'set default' is invalid for this field "
  2445. "as it does not define a default! Either define one in the base "
  2446. "field, or change the chosen ondelete policy" % self
  2447. )
  2448. elif val.startswith('set '):
  2449. assert val[4:] in values, (
  2450. "%s: ondelete policy of type 'set %%' must be either 'set null', "
  2451. "'set default', or 'set value' where value is a valid selection value."
  2452. ) % self
  2453. else:
  2454. raise ValueError(
  2455. "%r: ondelete policy %r for selection value %r is not a valid ondelete"
  2456. " policy, please choose one of 'set null', 'set default', "
  2457. "'set [value]', 'cascade' or a callable" % (self, val, key)
  2458. )
  2459. values = {
  2460. key: values_add.get(key) or values[key]
  2461. for key in merge_sequences(values, values_add)
  2462. }
  2463. self.ondelete.update(ondelete)
  2464. if values is not None:
  2465. self.selection = list(values.items())
  2466. assert all(isinstance(key, str) for key in values), \
  2467. "Field %s with non-str value in selection" % self
  2468. self._selection = values
  2469. def _selection_modules(self, model):
  2470. """ Return a mapping from selection values to modules defining each value. """
  2471. if not isinstance(self.selection, list):
  2472. return {}
  2473. value_modules = defaultdict(set)
  2474. for field in reversed(resolve_mro(model, self.name, type(self).__instancecheck__)):
  2475. module = field._module
  2476. if not module:
  2477. continue
  2478. if 'selection' in field.args:
  2479. value_modules.clear()
  2480. if isinstance(field.args['selection'], list):
  2481. for value, label in field.args['selection']:
  2482. value_modules[value].add(module)
  2483. if 'selection_add' in field.args:
  2484. for value_label in field.args['selection_add']:
  2485. if len(value_label) > 1:
  2486. value_modules[value_label[0]].add(module)
  2487. return value_modules
  2488. def _description_selection(self, env):
  2489. """ return the selection list (pairs (value, label)); labels are
  2490. translated according to context language
  2491. """
  2492. selection = self.selection
  2493. if isinstance(selection, str) or callable(selection):
  2494. return determine(selection, env[self.model_name])
  2495. # translate selection labels
  2496. if env.lang:
  2497. return env['ir.model.fields'].get_field_selection(self.model_name, self.name)
  2498. else:
  2499. return selection
  2500. def _default_group_expand(self, records, groups, domain):
  2501. # return a group per selection option, in definition order
  2502. return self.get_values(records.env)
  2503. def get_values(self, env):
  2504. """Return a list of the possible values."""
  2505. selection = self.selection
  2506. if isinstance(selection, str) or callable(selection):
  2507. selection = determine(selection, env[self.model_name].with_context(lang=None))
  2508. return [value for value, _ in selection]
  2509. def convert_to_column(self, value, record, values=None, validate=True):
  2510. if validate and self.validate:
  2511. value = self.convert_to_cache(value, record)
  2512. return super().convert_to_column(value, record, values, validate)
  2513. def convert_to_cache(self, value, record, validate=True):
  2514. if not validate or self._selection is None:
  2515. return value or None
  2516. if value in self._selection:
  2517. return value
  2518. if not value:
  2519. return None
  2520. raise ValueError("Wrong value for %s: %r" % (self, value))
  2521. def convert_to_export(self, value, record):
  2522. if not isinstance(self.selection, list):
  2523. # FIXME: this reproduces an existing buggy behavior!
  2524. return value if value else ''
  2525. for item in self._description_selection(record.env):
  2526. if item[0] == value:
  2527. return item[1]
  2528. return ''
  2529. class Reference(Selection):
  2530. """ Pseudo-relational field (no FK in database).
  2531. The field value is stored as a :class:`string <str>` following the pattern
  2532. ``"res_model,res_id"`` in database.
  2533. """
  2534. type = 'reference'
  2535. _column_type = ('varchar', pg_varchar())
  2536. def convert_to_column(self, value, record, values=None, validate=True):
  2537. return Field.convert_to_column(self, value, record, values, validate)
  2538. def convert_to_cache(self, value, record, validate=True):
  2539. # cache format: str ("model,id") or None
  2540. if isinstance(value, BaseModel):
  2541. if not validate or (value._name in self.get_values(record.env) and len(value) <= 1):
  2542. return "%s,%s" % (value._name, value.id) if value else None
  2543. elif isinstance(value, str):
  2544. res_model, res_id = value.split(',')
  2545. if not validate or res_model in self.get_values(record.env):
  2546. if record.env[res_model].browse(int(res_id)).exists():
  2547. return value
  2548. else:
  2549. return None
  2550. elif not value:
  2551. return None
  2552. raise ValueError("Wrong value for %s: %r" % (self, value))
  2553. def convert_to_record(self, value, record):
  2554. if value:
  2555. res_model, res_id = value.split(',')
  2556. return record.env[res_model].browse(int(res_id))
  2557. return None
  2558. def convert_to_read(self, value, record, use_display_name=True):
  2559. return "%s,%s" % (value._name, value.id) if value else False
  2560. def convert_to_export(self, value, record):
  2561. return value.display_name if value else ''
  2562. def convert_to_display_name(self, value, record):
  2563. return value.display_name if value else False
  2564. class _Relational(Field[M], typing.Generic[M]):
  2565. """ Abstract class for relational fields. """
  2566. relational = True
  2567. domain: DomainType = [] # domain for searching values
  2568. context: ContextType = {} # context for searching values
  2569. check_company = False
  2570. def __get__(self, records, owner=None):
  2571. # base case: do the regular access
  2572. if records is None or len(records._ids) <= 1:
  2573. return super().__get__(records, owner)
  2574. # multirecord case: use mapped
  2575. return self.mapped(records)
  2576. def setup_nonrelated(self, model):
  2577. super().setup_nonrelated(model)
  2578. if self.comodel_name not in model.pool:
  2579. _logger.warning("Field %s with unknown comodel_name %r", self, self.comodel_name)
  2580. self.comodel_name = '_unknown'
  2581. def get_domain_list(self, model):
  2582. """ Return a list domain from the domain parameter. """
  2583. domain = self.domain
  2584. if callable(domain):
  2585. domain = domain(model)
  2586. return domain if isinstance(domain, list) else []
  2587. @property
  2588. def _related_domain(self):
  2589. def validated(domain):
  2590. if isinstance(domain, str) and not self.inherited:
  2591. # string domains are expressions that are not valid for self's model
  2592. return None
  2593. return domain
  2594. if callable(self.domain):
  2595. # will be called with another model than self's
  2596. return lambda recs: validated(self.domain(recs.env[self.model_name])) # pylint: disable=not-callable
  2597. else:
  2598. return validated(self.domain)
  2599. _related_context = property(attrgetter('context'))
  2600. _description_relation = property(attrgetter('comodel_name'))
  2601. _description_context = property(attrgetter('context'))
  2602. def _description_domain(self, env):
  2603. domain = self.domain(env[self.model_name]) if callable(self.domain) else self.domain # pylint: disable=not-callable
  2604. if self.check_company:
  2605. field_to_check = None
  2606. if self.company_dependent:
  2607. cids = '[allowed_company_ids[0]]'
  2608. elif self.model_name == 'res.company':
  2609. # when using check_company=True on a field on 'res.company', the
  2610. # company_id comes from the id of the current record
  2611. cids = '[id]'
  2612. elif 'company_id' in env[self.model_name]:
  2613. cids = '[company_id]'
  2614. field_to_check = 'company_id'
  2615. elif 'company_ids' in env[self.model_name]:
  2616. cids = 'company_ids'
  2617. field_to_check = 'company_ids'
  2618. else:
  2619. _logger.warning(env._(
  2620. "Couldn't generate a company-dependent domain for field %s. "
  2621. "The model doesn't have a 'company_id' or 'company_ids' field, and isn't company-dependent either.",
  2622. f'{self.model_name}.{self.name}'
  2623. ))
  2624. return domain
  2625. company_domain = env[self.comodel_name]._check_company_domain(companies=unquote(cids))
  2626. if not field_to_check:
  2627. return f"{company_domain} + {domain or []}"
  2628. else:
  2629. no_company_domain = env[self.comodel_name]._check_company_domain(companies='')
  2630. return f"({field_to_check} and {company_domain} or {no_company_domain}) + ({domain or []})"
  2631. return domain
  2632. def _description_allow_hierachy_operators(self, env):
  2633. """ Return if the child_of/parent_of makes sense on this field """
  2634. comodel = env[self.comodel_name]
  2635. return comodel._parent_name in comodel._fields
  2636. class Many2one(_Relational[M]):
  2637. """ The value of such a field is a recordset of size 0 (no
  2638. record) or 1 (a single record).
  2639. :param str comodel_name: name of the target model
  2640. ``Mandatory`` except for related or extended fields.
  2641. :param domain: an optional domain to set on candidate values on the
  2642. client side (domain or a python expression that will be evaluated
  2643. to provide domain)
  2644. :param dict context: an optional context to use on the client side when
  2645. handling that field
  2646. :param str ondelete: what to do when the referred record is deleted;
  2647. possible values are: ``'set null'``, ``'restrict'``, ``'cascade'``
  2648. :param bool auto_join: whether JOINs are generated upon search through that
  2649. field (default: ``False``)
  2650. :param bool delegate: set it to ``True`` to make fields of the target model
  2651. accessible from the current model (corresponds to ``_inherits``)
  2652. :param bool check_company: Mark the field to be verified in
  2653. :meth:`~odoo.models.Model._check_company`. Has a different behaviour
  2654. depending on whether the field is company_dependent or not.
  2655. Constrains non-company-dependent fields to target records whose
  2656. company_id(s) are compatible with the record's company_id(s).
  2657. Constrains company_dependent fields to target records whose
  2658. company_id(s) are compatible with the currently active company.
  2659. """
  2660. type = 'many2one'
  2661. _column_type = ('int4', 'int4')
  2662. ondelete = None # what to do when value is deleted
  2663. auto_join = False # whether joins are generated upon search
  2664. delegate = False # whether self implements delegation
  2665. def __init__(self, comodel_name: str | Sentinel = SENTINEL, string: str | Sentinel = SENTINEL, **kwargs):
  2666. super(Many2one, self).__init__(comodel_name=comodel_name, string=string, **kwargs)
  2667. def _setup_attrs(self, model_class, name):
  2668. super()._setup_attrs(model_class, name)
  2669. # determine self.delegate
  2670. if not self.delegate and name in model_class._inherits.values():
  2671. self.delegate = True
  2672. # self.delegate implies self.auto_join
  2673. if self.delegate:
  2674. self.auto_join = True
  2675. def setup_nonrelated(self, model):
  2676. super().setup_nonrelated(model)
  2677. # 3 cases:
  2678. # 1) The ondelete attribute is not defined, we assign it a sensible default
  2679. # 2) The ondelete attribute is defined and its definition makes sense
  2680. # 3) The ondelete attribute is explicitly defined as 'set null' for a required m2o,
  2681. # this is considered a programming error.
  2682. if not self.ondelete:
  2683. comodel = model.env[self.comodel_name]
  2684. if model.is_transient() and not comodel.is_transient():
  2685. # Many2one relations from TransientModel Model are annoying because
  2686. # they can block deletion due to foreign keys. So unless stated
  2687. # otherwise, we default them to ondelete='cascade'.
  2688. self.ondelete = 'cascade' if self.required else 'set null'
  2689. else:
  2690. self.ondelete = 'restrict' if self.required else 'set null'
  2691. if self.ondelete == 'set null' and self.required:
  2692. raise ValueError(
  2693. "The m2o field %s of model %s is required but declares its ondelete policy "
  2694. "as being 'set null'. Only 'restrict' and 'cascade' make sense."
  2695. % (self.name, model._name)
  2696. )
  2697. if self.ondelete == 'restrict' and self.comodel_name in IR_MODELS:
  2698. raise ValueError(
  2699. f"Field {self.name} of model {model._name} is defined as ondelete='restrict' "
  2700. f"while having {self.comodel_name} as comodel, the 'restrict' mode is not "
  2701. f"supported for this type of field as comodel."
  2702. )
  2703. def update_db(self, model, columns):
  2704. comodel = model.env[self.comodel_name]
  2705. if not model.is_transient() and comodel.is_transient():
  2706. raise ValueError('Many2one %s from Model to TransientModel is forbidden' % self)
  2707. return super(Many2one, self).update_db(model, columns)
  2708. def update_db_column(self, model, column):
  2709. super(Many2one, self).update_db_column(model, column)
  2710. model.pool.post_init(self.update_db_foreign_key, model, column)
  2711. def update_db_foreign_key(self, model, column):
  2712. if self.company_dependent:
  2713. return
  2714. comodel = model.env[self.comodel_name]
  2715. # foreign keys do not work on views, and users can define custom models on sql views.
  2716. if not model._is_an_ordinary_table() or not comodel._is_an_ordinary_table():
  2717. return
  2718. # ir_actions is inherited, so foreign key doesn't work on it
  2719. if not comodel._auto or comodel._table == 'ir_actions':
  2720. return
  2721. # create/update the foreign key, and reflect it in 'ir.model.constraint'
  2722. model.pool.add_foreign_key(
  2723. model._table, self.name, comodel._table, 'id', self.ondelete or 'set null',
  2724. model, self._module
  2725. )
  2726. def _update(self, records, value):
  2727. """ Update the cached value of ``self`` for ``records`` with ``value``. """
  2728. cache = records.env.cache
  2729. for record in records:
  2730. cache.set(record, self, self.convert_to_cache(value, record, validate=False))
  2731. def convert_to_column(self, value, record, values=None, validate=True):
  2732. return value or None
  2733. def convert_to_cache(self, value, record, validate=True):
  2734. # cache format: id or None
  2735. if type(value) is int or type(value) is NewId:
  2736. id_ = value
  2737. elif isinstance(value, BaseModel):
  2738. if validate and (value._name != self.comodel_name or len(value) > 1):
  2739. raise ValueError("Wrong value for %s: %r" % (self, value))
  2740. id_ = value._ids[0] if value._ids else None
  2741. elif isinstance(value, tuple):
  2742. # value is either a pair (id, name), or a tuple of ids
  2743. id_ = value[0] if value else None
  2744. elif isinstance(value, dict):
  2745. # return a new record (with the given field 'id' as origin)
  2746. comodel = record.env[self.comodel_name]
  2747. origin = comodel.browse(value.get('id'))
  2748. id_ = comodel.new(value, origin=origin).id
  2749. else:
  2750. id_ = None
  2751. if self.delegate and record and not any(record._ids):
  2752. # if all records are new, then so is the parent
  2753. id_ = id_ and NewId(id_)
  2754. return id_
  2755. def convert_to_record(self, value, record):
  2756. # use registry to avoid creating a recordset for the model
  2757. ids = () if value is None else (value,)
  2758. prefetch_ids = PrefetchMany2one(record, self)
  2759. return record.pool[self.comodel_name](record.env, ids, prefetch_ids)
  2760. def convert_to_record_multi(self, values, records):
  2761. # return the ids as a recordset without duplicates
  2762. prefetch_ids = PrefetchMany2one(records, self)
  2763. ids = tuple(unique(id_ for id_ in values if id_ is not None))
  2764. return records.pool[self.comodel_name](records.env, ids, prefetch_ids)
  2765. def convert_to_read(self, value, record, use_display_name=True):
  2766. if use_display_name and value:
  2767. # evaluate display_name as superuser, because the visibility of a
  2768. # many2one field value (id and name) depends on the current record's
  2769. # access rights, and not the value's access rights.
  2770. try:
  2771. # performance: value.sudo() prefetches the same records as value
  2772. return (value.id, value.sudo().display_name)
  2773. except MissingError:
  2774. # Should not happen, unless the foreign key is missing.
  2775. return False
  2776. else:
  2777. return value.id
  2778. def convert_to_write(self, value, record):
  2779. if type(value) is int or type(value) is NewId:
  2780. return value
  2781. if not value:
  2782. return False
  2783. if isinstance(value, BaseModel) and value._name == self.comodel_name:
  2784. return value.id
  2785. if isinstance(value, tuple):
  2786. # value is either a pair (id, name), or a tuple of ids
  2787. return value[0] if value else False
  2788. if isinstance(value, dict):
  2789. return record.env[self.comodel_name].new(value).id
  2790. raise ValueError("Wrong value for %s: %r" % (self, value))
  2791. def convert_to_export(self, value, record):
  2792. return value.display_name if value else ''
  2793. def convert_to_display_name(self, value, record):
  2794. return value.display_name
  2795. def write(self, records, value):
  2796. # discard recomputation of self on records
  2797. records.env.remove_to_compute(self, records)
  2798. # discard the records that are not modified
  2799. cache = records.env.cache
  2800. cache_value = self.convert_to_cache(value, records)
  2801. records = cache.get_records_different_from(records, self, cache_value)
  2802. if not records:
  2803. return
  2804. # remove records from the cache of one2many fields of old corecords
  2805. self._remove_inverses(records, cache_value)
  2806. # update the cache of self
  2807. dirty = self.store and any(records._ids)
  2808. cache.update(records, self, itertools.repeat(cache_value), dirty=dirty)
  2809. # update the cache of one2many fields of new corecord
  2810. self._update_inverses(records, cache_value)
  2811. def _remove_inverses(self, records, value):
  2812. """ Remove `records` from the cached values of the inverse fields of `self`. """
  2813. cache = records.env.cache
  2814. record_ids = set(records._ids)
  2815. # align(id) returns a NewId if records are new, a real id otherwise
  2816. align = (lambda id_: id_) if all(record_ids) else (lambda id_: id_ and NewId(id_))
  2817. for invf in records.pool.field_inverses[self]:
  2818. corecords = records.env[self.comodel_name].browse(
  2819. align(id_) for id_ in cache.get_values(records, self)
  2820. )
  2821. for corecord in corecords:
  2822. ids0 = cache.get(corecord, invf, None)
  2823. if ids0 is not None:
  2824. ids1 = tuple(id_ for id_ in ids0 if id_ not in record_ids)
  2825. cache.set(corecord, invf, ids1)
  2826. def _update_inverses(self, records, value):
  2827. """ Add `records` to the cached values of the inverse fields of `self`. """
  2828. if value is None:
  2829. return
  2830. cache = records.env.cache
  2831. corecord = self.convert_to_record(value, records)
  2832. for invf in records.pool.field_inverses[self]:
  2833. valid_records = records.filtered_domain(invf.get_domain_list(corecord))
  2834. if not valid_records:
  2835. continue
  2836. ids0 = cache.get(corecord, invf, None)
  2837. # if the value for the corecord is not in cache, but this is a new
  2838. # record, assign it anyway, as you won't be able to fetch it from
  2839. # database (see `test_sale_order`)
  2840. if ids0 is not None or not corecord.id:
  2841. ids1 = tuple(unique((ids0 or ()) + valid_records._ids))
  2842. cache.set(corecord, invf, ids1)
  2843. class Many2oneReference(Integer):
  2844. """ Pseudo-relational field (no FK in database).
  2845. The field value is stored as an :class:`integer <int>` id in database.
  2846. Contrary to :class:`Reference` fields, the model has to be specified
  2847. in a :class:`Char` field, whose name has to be specified in the
  2848. `model_field` attribute for the current :class:`Many2oneReference` field.
  2849. :param str model_field: name of the :class:`Char` where the model name is stored.
  2850. """
  2851. type = 'many2one_reference'
  2852. model_field = None
  2853. aggregator = None
  2854. _related_model_field = property(attrgetter('model_field'))
  2855. _description_model_field = property(attrgetter('model_field'))
  2856. def convert_to_cache(self, value, record, validate=True):
  2857. # cache format: id or None
  2858. if isinstance(value, BaseModel):
  2859. value = value._ids[0] if value._ids else None
  2860. return super().convert_to_cache(value, record, validate)
  2861. def _update_inverses(self, records, value):
  2862. """ Add `records` to the cached values of the inverse fields of `self`. """
  2863. if not value:
  2864. return
  2865. cache = records.env.cache
  2866. model_ids = self._record_ids_per_res_model(records)
  2867. for invf in records.pool.field_inverses[self]:
  2868. records = records.browse(model_ids[invf.model_name])
  2869. if not records:
  2870. continue
  2871. corecord = records.env[invf.model_name].browse(value)
  2872. records = records.filtered_domain(invf.get_domain_list(corecord))
  2873. if not records:
  2874. continue
  2875. ids0 = cache.get(corecord, invf, None)
  2876. # if the value for the corecord is not in cache, but this is a new
  2877. # record, assign it anyway, as you won't be able to fetch it from
  2878. # database (see `test_sale_order`)
  2879. if ids0 is not None or not corecord.id:
  2880. ids1 = tuple(unique((ids0 or ()) + records._ids))
  2881. cache.set(corecord, invf, ids1)
  2882. def _record_ids_per_res_model(self, records):
  2883. model_ids = defaultdict(set)
  2884. for record in records:
  2885. model = record[self.model_field]
  2886. if not model and record._fields[self.model_field].compute:
  2887. # fallback when the model field is computed :-/
  2888. record._fields[self.model_field].compute_value(record)
  2889. model = record[self.model_field]
  2890. if not model:
  2891. continue
  2892. model_ids[model].add(record.id)
  2893. return model_ids
  2894. class Json(Field):
  2895. """ JSON Field that contain unstructured information in jsonb PostgreSQL column.
  2896. This field is still in beta
  2897. Some features have not been implemented and won't be implemented in stable versions, including:
  2898. * searching
  2899. * indexing
  2900. * mutating the values.
  2901. """
  2902. type = 'json'
  2903. _column_type = ('jsonb', 'jsonb')
  2904. def convert_to_record(self, value, record):
  2905. """ Return a copy of the value """
  2906. return False if value is None else copy.deepcopy(value)
  2907. def convert_to_cache(self, value, record, validate=True):
  2908. if not value:
  2909. return None
  2910. return json.loads(json.dumps(value))
  2911. def convert_to_column(self, value, record, values=None, validate=True):
  2912. if not value:
  2913. return None
  2914. return PsycopgJson(value)
  2915. def convert_to_export(self, value, record):
  2916. if not value:
  2917. return ''
  2918. return json.dumps(value)
  2919. class Properties(Field):
  2920. """ Field that contains a list of properties (aka "sub-field") based on
  2921. a definition defined on a container. Properties are pseudo-fields, acting
  2922. like Odoo fields but without being independently stored in database.
  2923. This field allows a light customization based on a container record. Used
  2924. for relationships such as <project.project> / <project.task>,... New
  2925. properties can be created on the fly without changing the structure of the
  2926. database.
  2927. The "definition_record" define the field used to find the container of the
  2928. current record. The container must have a :class:`~odoo.fields.PropertiesDefinition`
  2929. field "definition_record_field" that contains the properties definition
  2930. (type of each property, default value)...
  2931. Only the value of each property is stored on the child. When we read the
  2932. properties field, we read the definition on the container and merge it with
  2933. the value of the child. That way the web client has access to the full
  2934. field definition (property type, ...).
  2935. """
  2936. type = 'properties'
  2937. _column_type = ('jsonb', 'jsonb')
  2938. copy = False
  2939. prefetch = False
  2940. write_sequence = 10 # because it must be written after the definition field
  2941. # the field is computed editable by design (see the compute method below)
  2942. store = True
  2943. readonly = False
  2944. precompute = True
  2945. definition = None
  2946. definition_record = None # field on the current model that point to the definition record
  2947. definition_record_field = None # field on the definition record which defined the Properties field definition
  2948. _description_definition_record = property(attrgetter('definition_record'))
  2949. _description_definition_record_field = property(attrgetter('definition_record_field'))
  2950. ALLOWED_TYPES = (
  2951. # standard types
  2952. 'boolean', 'integer', 'float', 'char', 'date', 'datetime',
  2953. # relational like types
  2954. 'many2one', 'many2many', 'selection', 'tags',
  2955. # UI types
  2956. 'separator',
  2957. )
  2958. def _setup_attrs(self, model_class, name):
  2959. super()._setup_attrs(model_class, name)
  2960. self._setup_definition_attrs()
  2961. def _setup_definition_attrs(self):
  2962. if self.definition:
  2963. # determine definition_record and definition_record_field
  2964. assert self.definition.count(".") == 1
  2965. self.definition_record, self.definition_record_field = self.definition.rsplit('.', 1)
  2966. # make the field computed, and set its dependencies
  2967. self._depends = (self.definition_record, )
  2968. self.compute = self._compute
  2969. def setup_related(self, model):
  2970. super().setup_related(model)
  2971. if self.inherited_field and not self.definition:
  2972. self.definition = self.inherited_field.definition
  2973. self._setup_definition_attrs()
  2974. # Database/cache format: a value is either None, or a dict mapping property
  2975. # names to their corresponding value, like
  2976. #
  2977. # {
  2978. # '3adf37f3258cfe40': 'red',
  2979. # 'aa34746a6851ee4e': 1337,
  2980. # }
  2981. #
  2982. def convert_to_column(self, value, record, values=None, validate=True):
  2983. if not value:
  2984. return None
  2985. value = self.convert_to_cache(value, record, validate=validate)
  2986. return json.dumps(value)
  2987. def convert_to_cache(self, value, record, validate=True):
  2988. # any format -> cache format {name: value} or None
  2989. if not value:
  2990. return None
  2991. if isinstance(value, dict):
  2992. # avoid accidental side effects from shared mutable data
  2993. return copy.deepcopy(value)
  2994. if isinstance(value, str):
  2995. value = json.loads(value)
  2996. if not isinstance(value, dict):
  2997. raise ValueError(f"Wrong property value {value!r}")
  2998. return value
  2999. if isinstance(value, list):
  3000. # Convert the list with all definitions into a simple dict
  3001. # {name: value} to store the strict minimum on the child
  3002. self._remove_display_name(value)
  3003. return self._list_to_dict(value)
  3004. raise ValueError(f"Wrong property type {type(value)!r}")
  3005. # Record format: the value is either False, or a dict mapping property
  3006. # names to their corresponding value, like
  3007. #
  3008. # {
  3009. # '3adf37f3258cfe40': 'red',
  3010. # 'aa34746a6851ee4e': 1337,
  3011. # }
  3012. #
  3013. def convert_to_record(self, value, record):
  3014. return False if value is None else copy.deepcopy(value)
  3015. # Read format: the value is a list, where each element is a dict containing
  3016. # the definition of a property, together with the property's corresponding
  3017. # value, where relational field values have a display name.
  3018. #
  3019. # [{
  3020. # 'name': '3adf37f3258cfe40',
  3021. # 'string': 'Color Code',
  3022. # 'type': 'char',
  3023. # 'default': 'blue',
  3024. # 'value': 'red',
  3025. # }, {
  3026. # 'name': 'aa34746a6851ee4e',
  3027. # 'string': 'Partner',
  3028. # 'type': 'many2one',
  3029. # 'comodel': 'test_new_api.partner',
  3030. # 'value': [1337, 'Bob'],
  3031. # }]
  3032. #
  3033. def convert_to_read(self, value, record, use_display_name=True):
  3034. return self.convert_to_read_multi([value], record)[0]
  3035. def convert_to_read_multi(self, values, records):
  3036. if not records:
  3037. return values
  3038. assert len(values) == len(records)
  3039. # each value is either None or a dict
  3040. result = []
  3041. for record, value in zip(records, values):
  3042. definition = self._get_properties_definition(record)
  3043. if not value or not definition:
  3044. result.append(definition or [])
  3045. else:
  3046. assert isinstance(value, dict), f"Wrong type {value!r}"
  3047. result.append(self._dict_to_list(value, definition))
  3048. res_ids_per_model = self._get_res_ids_per_model(records, result)
  3049. # value is in record format
  3050. for value in result:
  3051. self._parse_json_types(value, records.env, res_ids_per_model)
  3052. for value in result:
  3053. self._add_display_name(value, records.env)
  3054. return result
  3055. def convert_to_write(self, value, record):
  3056. """If we write a list on the child, update the definition record."""
  3057. if isinstance(value, list):
  3058. # will update the definition record
  3059. self._remove_display_name(value)
  3060. return value
  3061. return super().convert_to_write(value, record)
  3062. def _get_res_ids_per_model(self, records, values_list):
  3063. """Read everything needed in batch for the given records.
  3064. To retrieve relational properties names, or to check their existence,
  3065. we need to do some SQL queries. To reduce the number of queries when we read
  3066. in batch, we prefetch everything needed before calling
  3067. convert_to_record / convert_to_read.
  3068. Return a dict {model: record_ids} that contains
  3069. the existing ids for each needed models.
  3070. """
  3071. # ids per model we need to fetch in batch to put in cache
  3072. ids_per_model = defaultdict(OrderedSet)
  3073. for record, record_values in zip(records, values_list):
  3074. for property_definition in record_values:
  3075. comodel = property_definition.get('comodel')
  3076. type_ = property_definition.get('type')
  3077. property_value = property_definition.get('value') or []
  3078. default = property_definition.get('default') or []
  3079. if type_ not in ('many2one', 'many2many') or comodel not in records.env:
  3080. continue
  3081. if type_ == 'many2one':
  3082. default = [default] if default else []
  3083. property_value = [property_value] if property_value else []
  3084. ids_per_model[comodel].update(default)
  3085. ids_per_model[comodel].update(property_value)
  3086. # check existence and pre-fetch in batch
  3087. res_ids_per_model = {}
  3088. for model, ids in ids_per_model.items():
  3089. recs = records.env[model].browse(ids).exists()
  3090. res_ids_per_model[model] = set(recs.ids)
  3091. for record in recs:
  3092. # read a field to pre-fetch the recordset
  3093. with contextlib.suppress(AccessError):
  3094. record.display_name
  3095. return res_ids_per_model
  3096. def write(self, records, value):
  3097. """Check if the properties definition has been changed.
  3098. To avoid extra SQL queries used to detect definition change, we add a
  3099. flag in the properties list. Parent update is done only when this flag
  3100. is present, delegating the check to the caller (generally web client).
  3101. For deletion, we need to keep the removed property definition in the
  3102. list to be able to put the delete flag in it. Otherwise we have no way
  3103. to know that a property has been removed.
  3104. """
  3105. if isinstance(value, str):
  3106. value = json.loads(value)
  3107. if isinstance(value, dict):
  3108. # don't need to write on the container definition
  3109. return super().write(records, value)
  3110. definition_changed = any(
  3111. definition.get('definition_changed')
  3112. or definition.get('definition_deleted')
  3113. for definition in (value or [])
  3114. )
  3115. if definition_changed:
  3116. value = [
  3117. definition for definition in value
  3118. if not definition.get('definition_deleted')
  3119. ]
  3120. for definition in value:
  3121. definition.pop('definition_changed', None)
  3122. # update the properties definition on the container
  3123. container = records[self.definition_record]
  3124. if container:
  3125. properties_definition = copy.deepcopy(value)
  3126. for property_definition in properties_definition:
  3127. property_definition.pop('value', None)
  3128. container[self.definition_record_field] = properties_definition
  3129. _logger.info('Properties field: User #%i changed definition of %r', records.env.user.id, container)
  3130. return super().write(records, value)
  3131. def _compute(self, records):
  3132. """Add the default properties value when the container is changed."""
  3133. for record in records:
  3134. record[self.name] = self._add_default_values(
  3135. record.env,
  3136. {self.name: record[self.name], self.definition_record: record[self.definition_record]},
  3137. )
  3138. def _add_default_values(self, env, values):
  3139. """Read the properties definition to add default values.
  3140. Default values are defined on the container in the 'default' key of
  3141. the definition.
  3142. :param env: environment
  3143. :param values: All values that will be written on the record
  3144. :return: Return the default values in the "dict" format
  3145. """
  3146. properties_values = values.get(self.name) or {}
  3147. if not values.get(self.definition_record):
  3148. # container is not given in the value, can not find properties definition
  3149. return {}
  3150. container_id = values[self.definition_record]
  3151. if not isinstance(container_id, (int, BaseModel)):
  3152. raise ValueError(f"Wrong container value {container_id!r}")
  3153. if isinstance(container_id, int):
  3154. # retrieve the container record
  3155. current_model = env[self.model_name]
  3156. definition_record_field = current_model._fields[self.definition_record]
  3157. container_model_name = definition_record_field.comodel_name
  3158. container_id = env[container_model_name].sudo().browse(container_id)
  3159. properties_definition = container_id[self.definition_record_field]
  3160. if not (properties_definition or (
  3161. isinstance(properties_values, list)
  3162. and any(d.get('definition_changed') for d in properties_values)
  3163. )):
  3164. # If a parent is set without properties, we might want to change its definition
  3165. # when we create the new record. But if we just set the value without changing
  3166. # the definition, in that case we can just ignored the passed values
  3167. return {}
  3168. assert isinstance(properties_values, (list, dict))
  3169. if isinstance(properties_values, list):
  3170. self._remove_display_name(properties_values)
  3171. properties_list_values = properties_values
  3172. else:
  3173. properties_list_values = self._dict_to_list(properties_values, properties_definition)
  3174. for properties_value in properties_list_values:
  3175. if properties_value.get('value') is None:
  3176. property_name = properties_value.get('name')
  3177. context_key = f"default_{self.name}.{property_name}"
  3178. if property_name and context_key in env.context:
  3179. default = env.context[context_key]
  3180. else:
  3181. default = properties_value.get('default') or False
  3182. properties_value['value'] = default
  3183. return properties_list_values
  3184. def _get_properties_definition(self, record):
  3185. """Return the properties definition of the given record."""
  3186. container = record[self.definition_record]
  3187. if container:
  3188. return container.sudo()[self.definition_record_field]
  3189. @classmethod
  3190. def _add_display_name(cls, values_list, env, value_keys=('value', 'default')):
  3191. """Add the "display_name" for each many2one / many2many properties.
  3192. Modify in place "values_list".
  3193. :param values_list: List of properties definition and values
  3194. :param env: environment
  3195. """
  3196. for property_definition in values_list:
  3197. property_type = property_definition.get('type')
  3198. property_model = property_definition.get('comodel')
  3199. if not property_model:
  3200. continue
  3201. for value_key in value_keys:
  3202. property_value = property_definition.get(value_key)
  3203. if property_type == 'many2one' and property_value and isinstance(property_value, int):
  3204. try:
  3205. display_name = env[property_model].browse(property_value).display_name
  3206. property_definition[value_key] = (property_value, display_name)
  3207. except AccessError:
  3208. # protect from access error message, show an empty name
  3209. property_definition[value_key] = (property_value, None)
  3210. except MissingError:
  3211. property_definition[value_key] = False
  3212. elif property_type == 'many2many' and property_value and is_list_of(property_value, int):
  3213. property_definition[value_key] = []
  3214. records = env[property_model].browse(property_value)
  3215. for record in records:
  3216. try:
  3217. property_definition[value_key].append((record.id, record.display_name))
  3218. except AccessError:
  3219. property_definition[value_key].append((record.id, None))
  3220. except MissingError:
  3221. continue
  3222. @classmethod
  3223. def _remove_display_name(cls, values_list, value_key='value'):
  3224. """Remove the display name received by the web client for the relational properties.
  3225. Modify in place "values_list".
  3226. - many2one: (35, 'Bob') -> 35
  3227. - many2many: [(35, 'Bob'), (36, 'Alice')] -> [35, 36]
  3228. :param values_list: List of properties definition with properties value
  3229. :param value_key: In which dict key we need to remove the display name
  3230. """
  3231. for property_definition in values_list:
  3232. if not isinstance(property_definition, dict) or not property_definition.get('name'):
  3233. continue
  3234. property_value = property_definition.get(value_key)
  3235. if not property_value:
  3236. continue
  3237. property_type = property_definition.get('type')
  3238. if property_type == 'many2one' and has_list_types(property_value, [int, (str, NoneType)]):
  3239. property_definition[value_key] = property_value[0]
  3240. elif property_type == 'many2many':
  3241. if is_list_of(property_value, (list, tuple)):
  3242. # [(35, 'Admin'), (36, 'Demo')] -> [35, 36]
  3243. property_definition[value_key] = [
  3244. many2many_value[0]
  3245. for many2many_value in property_value
  3246. ]
  3247. @classmethod
  3248. def _add_missing_names(cls, values_list):
  3249. """Generate new properties name if needed.
  3250. Modify in place "values_list".
  3251. :param values_list: List of properties definition with properties value
  3252. """
  3253. for definition in values_list:
  3254. if definition.get('definition_changed') and not definition.get('name'):
  3255. # keep only the first 64 bits
  3256. definition['name'] = str(uuid.uuid4()).replace('-', '')[:16]
  3257. @classmethod
  3258. def _parse_json_types(cls, values_list, env, res_ids_per_model):
  3259. """Parse the value stored in the JSON.
  3260. Check for records existence, if we removed a selection option, ...
  3261. Modify in place "values_list".
  3262. :param values_list: List of properties definition and values
  3263. :param env: environment
  3264. """
  3265. for property_definition in values_list:
  3266. property_value = property_definition.get('value')
  3267. property_type = property_definition.get('type')
  3268. res_model = property_definition.get('comodel')
  3269. if property_type not in cls.ALLOWED_TYPES:
  3270. raise ValueError(f'Wrong property type {property_type!r}')
  3271. if property_type == 'boolean':
  3272. # E.G. convert zero to False
  3273. property_value = bool(property_value)
  3274. elif property_type == 'char' and not isinstance(property_value, str):
  3275. property_value = False
  3276. elif property_value and property_type == 'selection':
  3277. # check if the selection option still exists
  3278. options = property_definition.get('selection') or []
  3279. options = {option[0] for option in options if option or ()} # always length 2
  3280. if property_value not in options:
  3281. # maybe the option has been removed on the container
  3282. property_value = False
  3283. elif property_value and property_type == 'tags':
  3284. # remove all tags that are not defined on the container
  3285. all_tags = {tag[0] for tag in property_definition.get('tags') or ()}
  3286. property_value = [tag for tag in property_value if tag in all_tags]
  3287. elif property_type == 'many2one' and property_value and res_model in env:
  3288. if not isinstance(property_value, int):
  3289. raise ValueError(f'Wrong many2one value: {property_value!r}.')
  3290. if property_value not in res_ids_per_model[res_model]:
  3291. property_value = False
  3292. elif property_type == 'many2many' and property_value and res_model in env:
  3293. if not is_list_of(property_value, int):
  3294. raise ValueError(f'Wrong many2many value: {property_value!r}.')
  3295. if len(property_value) != len(set(property_value)):
  3296. # remove duplicated value and preserve order
  3297. property_value = list(dict.fromkeys(property_value))
  3298. property_value = [
  3299. id_ for id_ in property_value
  3300. if id_ in res_ids_per_model[res_model]
  3301. ]
  3302. property_definition['value'] = property_value
  3303. @classmethod
  3304. def _list_to_dict(cls, values_list):
  3305. """Convert a list of properties with definition into a dict {name: value}.
  3306. To not repeat data in database, we only store the value of each property on
  3307. the child. The properties definition is stored on the container.
  3308. E.G.
  3309. Input list:
  3310. [{
  3311. 'name': '3adf37f3258cfe40',
  3312. 'string': 'Color Code',
  3313. 'type': 'char',
  3314. 'default': 'blue',
  3315. 'value': 'red',
  3316. }, {
  3317. 'name': 'aa34746a6851ee4e',
  3318. 'string': 'Partner',
  3319. 'type': 'many2one',
  3320. 'comodel': 'test_new_api.partner',
  3321. 'value': [1337, 'Bob'],
  3322. }]
  3323. Output dict:
  3324. {
  3325. '3adf37f3258cfe40': 'red',
  3326. 'aa34746a6851ee4e': 1337,
  3327. }
  3328. :param values_list: List of properties definition and value
  3329. :return: Generate a dict {name: value} from this definitions / values list
  3330. """
  3331. if not is_list_of(values_list, dict):
  3332. raise ValueError(f'Wrong properties value {values_list!r}')
  3333. cls._add_missing_names(values_list)
  3334. dict_value = {}
  3335. for property_definition in values_list:
  3336. property_value = property_definition.get('value')
  3337. property_type = property_definition.get('type')
  3338. property_model = property_definition.get('comodel')
  3339. if property_type == 'separator':
  3340. # "separator" is used as a visual separator in the form view UI
  3341. # it does not have a value and does not need to be stored on children
  3342. continue
  3343. if property_type not in ('integer', 'float') or property_value != 0:
  3344. property_value = property_value or False
  3345. if property_type in ('many2one', 'many2many') and property_model and property_value:
  3346. # check that value are correct before storing them in database
  3347. if property_type == 'many2many' and property_value and not is_list_of(property_value, int):
  3348. raise ValueError(f"Wrong many2many value {property_value!r}")
  3349. if property_type == 'many2one' and not isinstance(property_value, int):
  3350. raise ValueError(f"Wrong many2one value {property_value!r}")
  3351. dict_value[property_definition['name']] = property_value
  3352. return dict_value
  3353. @classmethod
  3354. def _dict_to_list(cls, values_dict, properties_definition):
  3355. """Convert a dict of {property: value} into a list of property definition with values.
  3356. :param values_dict: JSON value coming from the child table
  3357. :param properties_definition: Properties definition coming from the container table
  3358. :return: Merge both value into a list of properties with value
  3359. Ignore every values in the child that is not defined on the container.
  3360. """
  3361. if not is_list_of(properties_definition, dict):
  3362. raise ValueError(f'Wrong properties value {properties_definition!r}')
  3363. values_list = copy.deepcopy(properties_definition)
  3364. for property_definition in values_list:
  3365. property_definition['value'] = values_dict.get(property_definition['name'])
  3366. return values_list
  3367. class PropertiesDefinition(Field):
  3368. """ Field used to define the properties definition (see :class:`~odoo.fields.Properties`
  3369. field). This field is used on the container record to define the structure
  3370. of expected properties on subrecords. It is used to check the properties
  3371. definition. """
  3372. type = 'properties_definition'
  3373. _column_type = ('jsonb', 'jsonb')
  3374. copy = True # containers may act like templates, keep definitions to ease usage
  3375. readonly = False
  3376. prefetch = True
  3377. REQUIRED_KEYS = ('name', 'type')
  3378. ALLOWED_KEYS = (
  3379. 'name', 'string', 'type', 'comodel', 'default',
  3380. 'selection', 'tags', 'domain', 'view_in_cards',
  3381. )
  3382. # those keys will be removed if the types does not match
  3383. PROPERTY_PARAMETERS_MAP = {
  3384. 'comodel': {'many2one', 'many2many'},
  3385. 'domain': {'many2one', 'many2many'},
  3386. 'selection': {'selection'},
  3387. 'tags': {'tags'},
  3388. }
  3389. def convert_to_column(self, value, record, values=None, validate=True):
  3390. """Convert the value before inserting it in database.
  3391. This method accepts a list properties definition.
  3392. The relational properties (many2one / many2many) default value
  3393. might contain the display_name of those records (and will be removed).
  3394. [{
  3395. 'name': '3adf37f3258cfe40',
  3396. 'string': 'Color Code',
  3397. 'type': 'char',
  3398. 'default': 'blue',
  3399. }, {
  3400. 'name': 'aa34746a6851ee4e',
  3401. 'string': 'Partner',
  3402. 'type': 'many2one',
  3403. 'comodel': 'test_new_api.partner',
  3404. 'default': [1337, 'Bob'],
  3405. }]
  3406. """
  3407. if not value:
  3408. return None
  3409. if isinstance(value, str):
  3410. value = json.loads(value)
  3411. if not isinstance(value, list):
  3412. raise ValueError(f'Wrong properties definition type {type(value)!r}')
  3413. Properties._remove_display_name(value, value_key='default')
  3414. self._validate_properties_definition(value, record.env)
  3415. return json.dumps(value)
  3416. def convert_to_cache(self, value, record, validate=True):
  3417. # any format -> cache format (list of dicts or None)
  3418. if not value:
  3419. return None
  3420. if isinstance(value, list):
  3421. # avoid accidental side effects from shared mutable data, and make
  3422. # the value strict with respect to JSON (tuple -> list, etc)
  3423. value = json.dumps(value)
  3424. if isinstance(value, str):
  3425. value = json.loads(value)
  3426. if not isinstance(value, list):
  3427. raise ValueError(f'Wrong properties definition type {type(value)!r}')
  3428. Properties._remove_display_name(value, value_key='default')
  3429. self._validate_properties_definition(value, record.env)
  3430. return value
  3431. def convert_to_record(self, value, record):
  3432. # cache format -> record format (list of dicts)
  3433. if not value:
  3434. return []
  3435. # return a copy of the definition in cache where all property
  3436. # definitions have been cleaned up
  3437. result = []
  3438. for property_definition in value:
  3439. if not all(property_definition.get(key) for key in self.REQUIRED_KEYS):
  3440. # some required keys are missing, ignore this property definition
  3441. continue
  3442. # don't modify the value in cache
  3443. property_definition = dict(property_definition)
  3444. # check if the model still exists in the environment, the module of the
  3445. # model might have been uninstalled so the model might not exist anymore
  3446. property_model = property_definition.get('comodel')
  3447. if property_model and property_model not in record.env:
  3448. property_definition['comodel'] = property_model = False
  3449. if not property_model and 'domain' in property_definition:
  3450. del property_definition['domain']
  3451. if property_definition.get('type') in ('selection', 'tags'):
  3452. # always set at least an empty array if there's no option
  3453. key = property_definition['type']
  3454. property_definition[key] = property_definition.get(key) or []
  3455. property_domain = property_definition.get('domain')
  3456. if property_domain:
  3457. # some fields in the domain might have been removed
  3458. # (e.g. if the module has been uninstalled)
  3459. # check if the domain is still valid
  3460. try:
  3461. expression.expression(
  3462. ast.literal_eval(property_domain),
  3463. record.env[property_model],
  3464. )
  3465. except ValueError:
  3466. del property_definition['domain']
  3467. result.append(property_definition)
  3468. for property_parameter, allowed_types in self.PROPERTY_PARAMETERS_MAP.items():
  3469. if property_definition.get('type') not in allowed_types:
  3470. property_definition.pop(property_parameter, None)
  3471. return result
  3472. def convert_to_read(self, value, record, use_display_name=True):
  3473. # record format -> read format (list of dicts with display names)
  3474. if not value:
  3475. return value
  3476. if use_display_name:
  3477. Properties._add_display_name(value, record.env, value_keys=('default',))
  3478. return value
  3479. @classmethod
  3480. def _validate_properties_definition(cls, properties_definition, env):
  3481. """Raise an error if the property definition is not valid."""
  3482. properties_names = set()
  3483. for property_definition in properties_definition:
  3484. property_definition_keys = set(property_definition.keys())
  3485. invalid_keys = property_definition_keys - set(cls.ALLOWED_KEYS)
  3486. if invalid_keys:
  3487. raise ValueError(
  3488. 'Some key are not allowed for a properties definition [%s].' %
  3489. ', '.join(invalid_keys),
  3490. )
  3491. check_property_field_value_name(property_definition['name'])
  3492. required_keys = set(cls.REQUIRED_KEYS) - property_definition_keys
  3493. if required_keys:
  3494. raise ValueError(
  3495. 'Some key are missing for a properties definition [%s].' %
  3496. ', '.join(required_keys),
  3497. )
  3498. property_name = property_definition.get('name')
  3499. if not property_name or property_name in properties_names:
  3500. raise ValueError(f'The property name {property_name!r} is not set or duplicated.')
  3501. properties_names.add(property_name)
  3502. property_type = property_definition.get('type')
  3503. if property_type and property_type not in Properties.ALLOWED_TYPES:
  3504. raise ValueError(f'Wrong property type {property_type!r}.')
  3505. model = property_definition.get('comodel')
  3506. if model and (model not in env or env[model].is_transient() or env[model]._abstract):
  3507. raise ValueError(f'Invalid model name {model!r}')
  3508. property_selection = property_definition.get('selection')
  3509. if property_selection:
  3510. if (not is_list_of(property_selection, (list, tuple))
  3511. or not all(len(selection) == 2 for selection in property_selection)):
  3512. raise ValueError(f'Wrong options {property_selection!r}.')
  3513. all_options = [option[0] for option in property_selection]
  3514. if len(all_options) != len(set(all_options)):
  3515. duplicated = set(filter(lambda x: all_options.count(x) > 1, all_options))
  3516. raise ValueError(f'Some options are duplicated: {", ".join(duplicated)}.')
  3517. property_tags = property_definition.get('tags')
  3518. if property_tags:
  3519. if (not is_list_of(property_tags, (list, tuple))
  3520. or not all(len(tag) == 3 and isinstance(tag[2], int) for tag in property_tags)):
  3521. raise ValueError(f'Wrong tags definition {property_tags!r}.')
  3522. all_tags = [tag[0] for tag in property_tags]
  3523. if len(all_tags) != len(set(all_tags)):
  3524. duplicated = set(filter(lambda x: all_tags.count(x) > 1, all_tags))
  3525. raise ValueError(f'Some tags are duplicated: {", ".join(duplicated)}.')
  3526. class Command(enum.IntEnum):
  3527. """
  3528. :class:`~odoo.fields.One2many` and :class:`~odoo.fields.Many2many` fields
  3529. expect a special command to manipulate the relation they implement.
  3530. Internally, each command is a 3-elements tuple where the first element is a
  3531. mandatory integer that identifies the command, the second element is either
  3532. the related record id to apply the command on (commands update, delete,
  3533. unlink and link) either 0 (commands create, clear and set), the third
  3534. element is either the ``values`` to write on the record (commands create
  3535. and update) either the new ``ids`` list of related records (command set),
  3536. either 0 (commands delete, unlink, link, and clear).
  3537. Via Python, we encourage developers craft new commands via the various
  3538. functions of this namespace. We also encourage developers to use the
  3539. command identifier constant names when comparing the 1st element of
  3540. existing commands.
  3541. Via RPC, it is impossible nor to use the functions nor the command constant
  3542. names. It is required to instead write the literal 3-elements tuple where
  3543. the first element is the integer identifier of the command.
  3544. """
  3545. CREATE = 0
  3546. UPDATE = 1
  3547. DELETE = 2
  3548. UNLINK = 3
  3549. LINK = 4
  3550. CLEAR = 5
  3551. SET = 6
  3552. @classmethod
  3553. def create(cls, values: dict):
  3554. """
  3555. Create new records in the comodel using ``values``, link the created
  3556. records to ``self``.
  3557. In case of a :class:`~odoo.fields.Many2many` relation, one unique
  3558. new record is created in the comodel such that all records in `self`
  3559. are linked to the new record.
  3560. In case of a :class:`~odoo.fields.One2many` relation, one new record
  3561. is created in the comodel for every record in ``self`` such that every
  3562. record in ``self`` is linked to exactly one of the new records.
  3563. Return the command triple :samp:`(CREATE, 0, {values})`
  3564. """
  3565. return (cls.CREATE, 0, values)
  3566. @classmethod
  3567. def update(cls, id: int, values: dict):
  3568. """
  3569. Write ``values`` on the related record.
  3570. Return the command triple :samp:`(UPDATE, {id}, {values})`
  3571. """
  3572. return (cls.UPDATE, id, values)
  3573. @classmethod
  3574. def delete(cls, id: int):
  3575. """
  3576. Remove the related record from the database and remove its relation
  3577. with ``self``.
  3578. In case of a :class:`~odoo.fields.Many2many` relation, removing the
  3579. record from the database may be prevented if it is still linked to
  3580. other records.
  3581. Return the command triple :samp:`(DELETE, {id}, 0)`
  3582. """
  3583. return (cls.DELETE, id, 0)
  3584. @classmethod
  3585. def unlink(cls, id: int):
  3586. """
  3587. Remove the relation between ``self`` and the related record.
  3588. In case of a :class:`~odoo.fields.One2many` relation, the given record
  3589. is deleted from the database if the inverse field is set as
  3590. ``ondelete='cascade'``. Otherwise, the value of the inverse field is
  3591. set to False and the record is kept.
  3592. Return the command triple :samp:`(UNLINK, {id}, 0)`
  3593. """
  3594. return (cls.UNLINK, id, 0)
  3595. @classmethod
  3596. def link(cls, id: int):
  3597. """
  3598. Add a relation between ``self`` and the related record.
  3599. Return the command triple :samp:`(LINK, {id}, 0)`
  3600. """
  3601. return (cls.LINK, id, 0)
  3602. @classmethod
  3603. def clear(cls):
  3604. """
  3605. Remove all records from the relation with ``self``. It behaves like
  3606. executing the `unlink` command on every record.
  3607. Return the command triple :samp:`(CLEAR, 0, 0)`
  3608. """
  3609. return (cls.CLEAR, 0, 0)
  3610. @classmethod
  3611. def set(cls, ids: list):
  3612. """
  3613. Replace the current relations of ``self`` by the given ones. It behaves
  3614. like executing the ``unlink`` command on every removed relation then
  3615. executing the ``link`` command on every new relation.
  3616. Return the command triple :samp:`(SET, 0, {ids})`
  3617. """
  3618. return (cls.SET, 0, ids)
  3619. class _RelationalMulti(_Relational[M], typing.Generic[M]):
  3620. r"Abstract class for relational fields \*2many."
  3621. write_sequence = 20
  3622. # Important: the cache contains the ids of all the records in the relation,
  3623. # including inactive records. Inactive records are filtered out by
  3624. # convert_to_record(), depending on the context.
  3625. def _update(self, records, value):
  3626. """ Update the cached value of ``self`` for ``records`` with ``value``. """
  3627. records.env.cache.patch(records, self, value.id)
  3628. records.modified([self.name])
  3629. def convert_to_cache(self, value, record, validate=True):
  3630. # cache format: tuple(ids)
  3631. if isinstance(value, BaseModel):
  3632. if validate and value._name != self.comodel_name:
  3633. raise ValueError("Wrong value for %s: %s" % (self, value))
  3634. ids = value._ids
  3635. if record and not record.id:
  3636. # x2many field value of new record is new records
  3637. ids = tuple(it and NewId(it) for it in ids)
  3638. return ids
  3639. elif isinstance(value, (list, tuple)):
  3640. # value is a list/tuple of commands, dicts or record ids
  3641. comodel = record.env[self.comodel_name]
  3642. # if record is new, the field's value is new records
  3643. if record and not record.id:
  3644. browse = lambda it: comodel.browse((it and NewId(it),))
  3645. else:
  3646. browse = comodel.browse
  3647. # determine the value ids: in case of a real record or a new record
  3648. # with origin, take its current value
  3649. ids = OrderedSet(record[self.name]._ids if record._origin else ())
  3650. # modify ids with the commands
  3651. for command in value:
  3652. if isinstance(command, (tuple, list)):
  3653. if command[0] == Command.CREATE:
  3654. ids.add(comodel.new(command[2], ref=command[1]).id)
  3655. elif command[0] == Command.UPDATE:
  3656. line = browse(command[1])
  3657. if validate:
  3658. line.update(command[2])
  3659. else:
  3660. line._update_cache(command[2], validate=False)
  3661. ids.add(line.id)
  3662. elif command[0] in (Command.DELETE, Command.UNLINK):
  3663. ids.discard(browse(command[1]).id)
  3664. elif command[0] == Command.LINK:
  3665. ids.add(browse(command[1]).id)
  3666. elif command[0] == Command.CLEAR:
  3667. ids.clear()
  3668. elif command[0] == Command.SET:
  3669. ids = OrderedSet(browse(it).id for it in command[2])
  3670. elif isinstance(command, dict):
  3671. ids.add(comodel.new(command).id)
  3672. else:
  3673. ids.add(browse(command).id)
  3674. # return result as a tuple
  3675. return tuple(ids)
  3676. elif not value:
  3677. return ()
  3678. raise ValueError("Wrong value for %s: %s" % (self, value))
  3679. def convert_to_record(self, value, record):
  3680. # use registry to avoid creating a recordset for the model
  3681. prefetch_ids = PrefetchX2many(record, self)
  3682. Comodel = record.pool[self.comodel_name]
  3683. corecords = Comodel(record.env, value, prefetch_ids)
  3684. if (
  3685. Comodel._active_name
  3686. and self.context.get('active_test', record.env.context.get('active_test', True))
  3687. ):
  3688. corecords = corecords.filtered(Comodel._active_name).with_prefetch(prefetch_ids)
  3689. return corecords
  3690. def convert_to_record_multi(self, values, records):
  3691. # return the list of ids as a recordset without duplicates
  3692. prefetch_ids = PrefetchX2many(records, self)
  3693. Comodel = records.pool[self.comodel_name]
  3694. ids = tuple(unique(id_ for ids in values for id_ in ids))
  3695. corecords = Comodel(records.env, ids, prefetch_ids)
  3696. if (
  3697. Comodel._active_name
  3698. and self.context.get('active_test', records.env.context.get('active_test', True))
  3699. ):
  3700. corecords = corecords.filtered(Comodel._active_name).with_prefetch(prefetch_ids)
  3701. return corecords
  3702. def convert_to_read(self, value, record, use_display_name=True):
  3703. return value.ids
  3704. def convert_to_write(self, value, record):
  3705. if isinstance(value, tuple):
  3706. # a tuple of ids, this is the cache format
  3707. value = record.env[self.comodel_name].browse(value)
  3708. if isinstance(value, BaseModel) and value._name == self.comodel_name:
  3709. def get_origin(val):
  3710. return val._origin if isinstance(val, BaseModel) else val
  3711. # make result with new and existing records
  3712. inv_names = {field.name for field in record.pool.field_inverses[self]}
  3713. result = [Command.set([])]
  3714. for record in value:
  3715. origin = record._origin
  3716. if not origin:
  3717. values = record._convert_to_write({
  3718. name: record[name]
  3719. for name in record._cache
  3720. if name not in inv_names
  3721. })
  3722. result.append(Command.create(values))
  3723. else:
  3724. result[0][2].append(origin.id)
  3725. if record != origin:
  3726. values = record._convert_to_write({
  3727. name: record[name]
  3728. for name in record._cache
  3729. if name not in inv_names and get_origin(record[name]) != origin[name]
  3730. })
  3731. if values:
  3732. result.append(Command.update(origin.id, values))
  3733. return result
  3734. if value is False or value is None:
  3735. return [Command.clear()]
  3736. if isinstance(value, list):
  3737. return value
  3738. raise ValueError("Wrong value for %s: %s" % (self, value))
  3739. def convert_to_export(self, value, record):
  3740. return ','.join(value.mapped('display_name')) if value else ''
  3741. def convert_to_display_name(self, value, record):
  3742. raise NotImplementedError()
  3743. def get_depends(self, model):
  3744. depends, depends_context = super().get_depends(model)
  3745. if not self.compute and isinstance(self.domain, list):
  3746. depends = unique(itertools.chain(depends, (
  3747. self.name + '.' + arg[0]
  3748. for arg in self.domain
  3749. if isinstance(arg, (tuple, list)) and isinstance(arg[0], str)
  3750. )))
  3751. return depends, depends_context
  3752. def create(self, record_values):
  3753. """ Write the value of ``self`` on the given records, which have just
  3754. been created.
  3755. :param record_values: a list of pairs ``(record, value)``, where
  3756. ``value`` is in the format of method :meth:`BaseModel.write`
  3757. """
  3758. self.write_batch(record_values, True)
  3759. def write(self, records, value):
  3760. # discard recomputation of self on records
  3761. records.env.remove_to_compute(self, records)
  3762. self.write_batch([(records, value)])
  3763. def write_batch(self, records_commands_list, create=False):
  3764. if not records_commands_list:
  3765. return
  3766. for idx, (recs, value) in enumerate(records_commands_list):
  3767. if isinstance(value, tuple):
  3768. value = [Command.set(value)]
  3769. elif isinstance(value, BaseModel) and value._name == self.comodel_name:
  3770. value = [Command.set(value._ids)]
  3771. elif value is False or value is None:
  3772. value = [Command.clear()]
  3773. elif isinstance(value, list) and value and not isinstance(value[0], (tuple, list)):
  3774. value = [Command.set(tuple(value))]
  3775. if not isinstance(value, list):
  3776. raise ValueError("Wrong value for %s: %s" % (self, value))
  3777. records_commands_list[idx] = (recs, value)
  3778. record_ids = {rid for recs, cs in records_commands_list for rid in recs._ids}
  3779. if all(record_ids):
  3780. self.write_real(records_commands_list, create)
  3781. else:
  3782. assert not any(record_ids), f"{records_commands_list} contains a mix of real and new records. It is not supported."
  3783. self.write_new(records_commands_list)
  3784. def _check_sudo_commands(self, comodel):
  3785. # if the model doesn't accept sudo commands
  3786. if not comodel._allow_sudo_commands:
  3787. # Then, disable sudo and reset the transaction origin user
  3788. return comodel.sudo(False).with_user(comodel.env.uid_origin)
  3789. return comodel
  3790. class One2many(_RelationalMulti[M]):
  3791. """One2many field; the value of such a field is the recordset of all the
  3792. records in ``comodel_name`` such that the field ``inverse_name`` is equal to
  3793. the current record.
  3794. :param str comodel_name: name of the target model
  3795. :param str inverse_name: name of the inverse ``Many2one`` field in
  3796. ``comodel_name``
  3797. :param domain: an optional domain to set on candidate values on the
  3798. client side (domain or a python expression that will be evaluated
  3799. to provide domain)
  3800. :param dict context: an optional context to use on the client side when
  3801. handling that field
  3802. :param bool auto_join: whether JOINs are generated upon search through that
  3803. field (default: ``False``)
  3804. The attributes ``comodel_name`` and ``inverse_name`` are mandatory except in
  3805. the case of related fields or field extensions.
  3806. """
  3807. type = 'one2many'
  3808. inverse_name = None # name of the inverse field
  3809. auto_join = False # whether joins are generated upon search
  3810. copy = False # o2m are not copied by default
  3811. def __init__(self, comodel_name: str | Sentinel = SENTINEL, inverse_name: str | Sentinel = SENTINEL,
  3812. string: str | Sentinel = SENTINEL, **kwargs):
  3813. super(One2many, self).__init__(
  3814. comodel_name=comodel_name,
  3815. inverse_name=inverse_name,
  3816. string=string,
  3817. **kwargs
  3818. )
  3819. def setup_nonrelated(self, model):
  3820. super(One2many, self).setup_nonrelated(model)
  3821. if self.inverse_name:
  3822. # link self to its inverse field and vice-versa
  3823. comodel = model.env[self.comodel_name]
  3824. invf = comodel._fields[self.inverse_name]
  3825. if isinstance(invf, (Many2one, Many2oneReference)):
  3826. # setting one2many fields only invalidates many2one inverses;
  3827. # integer inverses (res_model/res_id pairs) are not supported
  3828. model.pool.field_inverses.add(self, invf)
  3829. comodel.pool.field_inverses.add(invf, self)
  3830. _description_relation_field = property(attrgetter('inverse_name'))
  3831. def update_db(self, model, columns):
  3832. if self.comodel_name in model.env:
  3833. comodel = model.env[self.comodel_name]
  3834. if self.inverse_name not in comodel._fields:
  3835. raise UserError(model.env._(
  3836. 'No inverse field "%(inverse_field)s" found for "%(comodel)s"',
  3837. inverse_field=self.inverse_name,
  3838. comodel=self.comodel_name
  3839. ))
  3840. def get_domain_list(self, records):
  3841. domain = super().get_domain_list(records)
  3842. if self.comodel_name and self.inverse_name:
  3843. comodel = records.env.registry[self.comodel_name]
  3844. inverse_field = comodel._fields[self.inverse_name]
  3845. if inverse_field.type == 'many2one_reference':
  3846. domain = domain + [(inverse_field.model_field, '=', records._name)]
  3847. return domain
  3848. def __get__(self, records, owner=None):
  3849. if records is not None and self.inverse_name is not None:
  3850. # force the computation of the inverse field to ensure that the
  3851. # cache value of self is consistent
  3852. inverse_field = records.pool[self.comodel_name]._fields[self.inverse_name]
  3853. if inverse_field.compute:
  3854. records.env[self.comodel_name]._recompute_model([self.inverse_name])
  3855. return super().__get__(records, owner)
  3856. def read(self, records):
  3857. # retrieve the lines in the comodel
  3858. context = {'active_test': False}
  3859. context.update(self.context)
  3860. comodel = records.env[self.comodel_name].with_context(**context)
  3861. inverse = self.inverse_name
  3862. inverse_field = comodel._fields[inverse]
  3863. # optimization: fetch the inverse and active fields with search()
  3864. domain = self.get_domain_list(records) + [(inverse, 'in', records.ids)]
  3865. field_names = [inverse]
  3866. if comodel._active_name:
  3867. field_names.append(comodel._active_name)
  3868. lines = comodel.search_fetch(domain, field_names)
  3869. # group lines by inverse field (without prefetching other fields)
  3870. get_id = (lambda rec: rec.id) if inverse_field.type == 'many2one' else int
  3871. group = defaultdict(list)
  3872. for line in lines:
  3873. # line[inverse] may be a record or an integer
  3874. group[get_id(line[inverse])].append(line.id)
  3875. # store result in cache
  3876. values = [tuple(group[id_]) for id_ in records._ids]
  3877. records.env.cache.insert_missing(records, self, values)
  3878. def write_real(self, records_commands_list, create=False):
  3879. """ Update real records. """
  3880. # records_commands_list = [(records, commands), ...]
  3881. if not records_commands_list:
  3882. return
  3883. model = records_commands_list[0][0].browse()
  3884. comodel = model.env[self.comodel_name].with_context(**self.context)
  3885. comodel = self._check_sudo_commands(comodel)
  3886. if self.store:
  3887. inverse = self.inverse_name
  3888. to_create = [] # line vals to create
  3889. to_delete = [] # line ids to delete
  3890. to_link = defaultdict(OrderedSet) # {record: line_ids}
  3891. allow_full_delete = not create
  3892. def unlink(lines):
  3893. if getattr(comodel._fields[inverse], 'ondelete', False) == 'cascade':
  3894. to_delete.extend(lines._ids)
  3895. else:
  3896. lines[inverse] = False
  3897. def flush():
  3898. if to_link:
  3899. before = {record: record[self.name] for record in to_link}
  3900. if to_delete:
  3901. # unlink() will remove the lines from the cache
  3902. comodel.browse(to_delete).unlink()
  3903. to_delete.clear()
  3904. if to_create:
  3905. # create() will add the new lines to the cache of records
  3906. comodel.create(to_create)
  3907. to_create.clear()
  3908. if to_link:
  3909. for record, line_ids in to_link.items():
  3910. lines = comodel.browse(line_ids) - before[record]
  3911. # linking missing lines should fail
  3912. lines.mapped(inverse)
  3913. lines[inverse] = record
  3914. to_link.clear()
  3915. for recs, commands in records_commands_list:
  3916. for command in (commands or ()):
  3917. if command[0] == Command.CREATE:
  3918. for record in recs:
  3919. to_create.append(dict(command[2], **{inverse: record.id}))
  3920. allow_full_delete = False
  3921. elif command[0] == Command.UPDATE:
  3922. prefetch_ids = recs[self.name]._prefetch_ids
  3923. comodel.browse(command[1]).with_prefetch(prefetch_ids).write(command[2])
  3924. elif command[0] == Command.DELETE:
  3925. to_delete.append(command[1])
  3926. elif command[0] == Command.UNLINK:
  3927. unlink(comodel.browse(command[1]))
  3928. elif command[0] == Command.LINK:
  3929. to_link[recs[-1]].add(command[1])
  3930. allow_full_delete = False
  3931. elif command[0] in (Command.CLEAR, Command.SET):
  3932. line_ids = command[2] if command[0] == Command.SET else []
  3933. if not allow_full_delete:
  3934. # do not try to delete anything in creation mode if nothing has been created before
  3935. if line_ids:
  3936. # equivalent to Command.LINK
  3937. if line_ids.__class__ is int:
  3938. line_ids = [line_ids]
  3939. to_link[recs[-1]].update(line_ids)
  3940. allow_full_delete = False
  3941. continue
  3942. flush()
  3943. # assign the given lines to the last record only
  3944. lines = comodel.browse(line_ids)
  3945. domain = self.get_domain_list(model) + \
  3946. [(inverse, 'in', recs.ids), ('id', 'not in', lines.ids)]
  3947. unlink(comodel.search(domain))
  3948. lines[inverse] = recs[-1]
  3949. flush()
  3950. else:
  3951. ids = OrderedSet(rid for recs, cs in records_commands_list for rid in recs._ids)
  3952. records = records_commands_list[0][0].browse(ids)
  3953. cache = records.env.cache
  3954. def link(record, lines):
  3955. ids = record[self.name]._ids
  3956. cache.set(record, self, tuple(unique(ids + lines._ids)))
  3957. def unlink(lines):
  3958. for record in records:
  3959. cache.set(record, self, (record[self.name] - lines)._ids)
  3960. for recs, commands in records_commands_list:
  3961. for command in (commands or ()):
  3962. if command[0] == Command.CREATE:
  3963. for record in recs:
  3964. link(record, comodel.new(command[2], ref=command[1]))
  3965. elif command[0] == Command.UPDATE:
  3966. comodel.browse(command[1]).write(command[2])
  3967. elif command[0] == Command.DELETE:
  3968. unlink(comodel.browse(command[1]))
  3969. elif command[0] == Command.UNLINK:
  3970. unlink(comodel.browse(command[1]))
  3971. elif command[0] == Command.LINK:
  3972. link(recs[-1], comodel.browse(command[1]))
  3973. elif command[0] in (Command.CLEAR, Command.SET):
  3974. # assign the given lines to the last record only
  3975. cache.update(recs, self, itertools.repeat(()))
  3976. lines = comodel.browse(command[2] if command[0] == Command.SET else [])
  3977. cache.set(recs[-1], self, lines._ids)
  3978. def write_new(self, records_commands_list):
  3979. if not records_commands_list:
  3980. return
  3981. model = records_commands_list[0][0].browse()
  3982. cache = model.env.cache
  3983. comodel = model.env[self.comodel_name].with_context(**self.context)
  3984. comodel = self._check_sudo_commands(comodel)
  3985. ids = {record.id for records, _ in records_commands_list for record in records}
  3986. records = model.browse(ids)
  3987. def browse(ids):
  3988. return comodel.browse([id_ and NewId(id_) for id_ in ids])
  3989. # make sure self is in cache
  3990. records[self.name]
  3991. if self.store:
  3992. inverse = self.inverse_name
  3993. # make sure self's inverse is in cache
  3994. inverse_field = comodel._fields[inverse]
  3995. for record in records:
  3996. cache.update(record[self.name], inverse_field, itertools.repeat(record.id))
  3997. for recs, commands in records_commands_list:
  3998. for command in commands:
  3999. if command[0] == Command.CREATE:
  4000. for record in recs:
  4001. line = comodel.new(command[2], ref=command[1])
  4002. line[inverse] = record
  4003. elif command[0] == Command.UPDATE:
  4004. browse([command[1]]).update(command[2])
  4005. elif command[0] == Command.DELETE:
  4006. browse([command[1]])[inverse] = False
  4007. elif command[0] == Command.UNLINK:
  4008. browse([command[1]])[inverse] = False
  4009. elif command[0] == Command.LINK:
  4010. browse([command[1]])[inverse] = recs[-1]
  4011. elif command[0] == Command.CLEAR:
  4012. cache.update(recs, self, itertools.repeat(()))
  4013. elif command[0] == Command.SET:
  4014. # assign the given lines to the last record only
  4015. cache.update(recs, self, itertools.repeat(()))
  4016. last, lines = recs[-1], browse(command[2])
  4017. cache.set(last, self, lines._ids)
  4018. cache.update(lines, inverse_field, itertools.repeat(last.id))
  4019. else:
  4020. def link(record, lines):
  4021. ids = record[self.name]._ids
  4022. cache.set(record, self, tuple(unique(ids + lines._ids)))
  4023. def unlink(lines):
  4024. for record in records:
  4025. cache.set(record, self, (record[self.name] - lines)._ids)
  4026. for recs, commands in records_commands_list:
  4027. for command in commands:
  4028. if command[0] == Command.CREATE:
  4029. for record in recs:
  4030. link(record, comodel.new(command[2], ref=command[1]))
  4031. elif command[0] == Command.UPDATE:
  4032. browse([command[1]]).update(command[2])
  4033. elif command[0] == Command.DELETE:
  4034. unlink(browse([command[1]]))
  4035. elif command[0] == Command.UNLINK:
  4036. unlink(browse([command[1]]))
  4037. elif command[0] == Command.LINK:
  4038. link(recs[-1], browse([command[1]]))
  4039. elif command[0] in (Command.CLEAR, Command.SET):
  4040. # assign the given lines to the last record only
  4041. cache.update(recs, self, itertools.repeat(()))
  4042. lines = browse(command[2] if command[0] == Command.SET else [])
  4043. cache.set(recs[-1], self, lines._ids)
  4044. class Many2many(_RelationalMulti[M]):
  4045. """ Many2many field; the value of such a field is the recordset.
  4046. :param comodel_name: name of the target model (string)
  4047. mandatory except in the case of related or extended fields
  4048. :param str relation: optional name of the table that stores the relation in
  4049. the database
  4050. :param str column1: optional name of the column referring to "these" records
  4051. in the table ``relation``
  4052. :param str column2: optional name of the column referring to "those" records
  4053. in the table ``relation``
  4054. The attributes ``relation``, ``column1`` and ``column2`` are optional.
  4055. If not given, names are automatically generated from model names,
  4056. provided ``model_name`` and ``comodel_name`` are different!
  4057. Note that having several fields with implicit relation parameters on a
  4058. given model with the same comodel is not accepted by the ORM, since
  4059. those field would use the same table. The ORM prevents two many2many
  4060. fields to use the same relation parameters, except if
  4061. - both fields use the same model, comodel, and relation parameters are
  4062. explicit; or
  4063. - at least one field belongs to a model with ``_auto = False``.
  4064. :param domain: an optional domain to set on candidate values on the
  4065. client side (domain or a python expression that will be evaluated
  4066. to provide domain)
  4067. :param dict context: an optional context to use on the client side when
  4068. handling that field
  4069. :param bool check_company: Mark the field to be verified in
  4070. :meth:`~odoo.models.Model._check_company`. Add a default company
  4071. domain depending on the field attributes.
  4072. """
  4073. type = 'many2many'
  4074. _explicit = True # whether schema is explicitly given
  4075. relation = None # name of table
  4076. column1 = None # column of table referring to model
  4077. column2 = None # column of table referring to comodel
  4078. auto_join = False # whether joins are generated upon search
  4079. ondelete = 'cascade' # optional ondelete for the column2 fkey
  4080. def __init__(self, comodel_name: str | Sentinel = SENTINEL, relation: str | Sentinel = SENTINEL,
  4081. column1: str | Sentinel = SENTINEL, column2: str | Sentinel = SENTINEL,
  4082. string: str | Sentinel = SENTINEL, **kwargs):
  4083. super(Many2many, self).__init__(
  4084. comodel_name=comodel_name,
  4085. relation=relation,
  4086. column1=column1,
  4087. column2=column2,
  4088. string=string,
  4089. **kwargs
  4090. )
  4091. def setup_nonrelated(self, model):
  4092. super().setup_nonrelated(model)
  4093. # 2 cases:
  4094. # 1) The ondelete attribute is defined and its definition makes sense
  4095. # 2) The ondelete attribute is explicitly defined as 'set null' for a m2m,
  4096. # this is considered a programming error.
  4097. if self.ondelete not in ('cascade', 'restrict'):
  4098. raise ValueError(
  4099. "The m2m field %s of model %s declares its ondelete policy "
  4100. "as being %r. Only 'restrict' and 'cascade' make sense."
  4101. % (self.name, model._name, self.ondelete)
  4102. )
  4103. if self.store:
  4104. if not (self.relation and self.column1 and self.column2):
  4105. if not self.relation:
  4106. self._explicit = False
  4107. # table name is based on the stable alphabetical order of tables
  4108. comodel = model.env[self.comodel_name]
  4109. if not self.relation:
  4110. tables = sorted([model._table, comodel._table])
  4111. assert tables[0] != tables[1], \
  4112. "%s: Implicit/canonical naming of many2many relationship " \
  4113. "table is not possible when source and destination models " \
  4114. "are the same" % self
  4115. self.relation = '%s_%s_rel' % tuple(tables)
  4116. if not self.column1:
  4117. self.column1 = '%s_id' % model._table
  4118. if not self.column2:
  4119. self.column2 = '%s_id' % comodel._table
  4120. # check validity of table name
  4121. check_pg_name(self.relation)
  4122. else:
  4123. self.relation = self.column1 = self.column2 = None
  4124. if self.relation:
  4125. m2m = model.pool._m2m
  4126. # check whether other fields use the same schema
  4127. fields = m2m[(self.relation, self.column1, self.column2)]
  4128. for field in fields:
  4129. if ( # same model: relation parameters must be explicit
  4130. self.model_name == field.model_name and
  4131. self.comodel_name == field.comodel_name and
  4132. self._explicit and field._explicit
  4133. ) or ( # different models: one model must be _auto=False
  4134. self.model_name != field.model_name and
  4135. not (model._auto and model.env[field.model_name]._auto)
  4136. ):
  4137. continue
  4138. msg = "Many2many fields %s and %s use the same table and columns"
  4139. raise TypeError(msg % (self, field))
  4140. fields.append(self)
  4141. # retrieve inverse fields, and link them in field_inverses
  4142. for field in m2m[(self.relation, self.column2, self.column1)]:
  4143. model.pool.field_inverses.add(self, field)
  4144. model.pool.field_inverses.add(field, self)
  4145. def update_db(self, model, columns):
  4146. cr = model._cr
  4147. # Do not reflect relations for custom fields, as they do not belong to a
  4148. # module. They are automatically removed when dropping the corresponding
  4149. # 'ir.model.field'.
  4150. if not self.manual:
  4151. model.pool.post_init(model.env['ir.model.relation']._reflect_relation,
  4152. model, self.relation, self._module)
  4153. comodel = model.env[self.comodel_name]
  4154. if not sql.table_exists(cr, self.relation):
  4155. cr.execute(SQL(
  4156. """ CREATE TABLE %(rel)s (%(id1)s INTEGER NOT NULL,
  4157. %(id2)s INTEGER NOT NULL,
  4158. PRIMARY KEY(%(id1)s, %(id2)s));
  4159. COMMENT ON TABLE %(rel)s IS %(comment)s;
  4160. CREATE INDEX ON %(rel)s (%(id2)s, %(id1)s); """,
  4161. rel=SQL.identifier(self.relation),
  4162. id1=SQL.identifier(self.column1),
  4163. id2=SQL.identifier(self.column2),
  4164. comment=f"RELATION BETWEEN {model._table} AND {comodel._table}",
  4165. ))
  4166. _schema.debug("Create table %r: m2m relation between %r and %r", self.relation, model._table, comodel._table)
  4167. model.pool.post_init(self.update_db_foreign_keys, model)
  4168. return True
  4169. model.pool.post_init(self.update_db_foreign_keys, model)
  4170. def update_db_foreign_keys(self, model):
  4171. """ Add the foreign keys corresponding to the field's relation table. """
  4172. comodel = model.env[self.comodel_name]
  4173. if model._is_an_ordinary_table():
  4174. model.pool.add_foreign_key(
  4175. self.relation, self.column1, model._table, 'id', 'cascade',
  4176. model, self._module, force=False,
  4177. )
  4178. if comodel._is_an_ordinary_table():
  4179. model.pool.add_foreign_key(
  4180. self.relation, self.column2, comodel._table, 'id', self.ondelete,
  4181. model, self._module,
  4182. )
  4183. def read(self, records):
  4184. context = {'active_test': False}
  4185. context.update(self.context)
  4186. comodel = records.env[self.comodel_name].with_context(**context)
  4187. # make the query for the lines
  4188. domain = self.get_domain_list(records)
  4189. query = comodel._where_calc(domain)
  4190. comodel._apply_ir_rules(query, 'read')
  4191. query.order = comodel._order_to_sql(comodel._order, query)
  4192. # join with many2many relation table
  4193. sql_id1 = SQL.identifier(self.relation, self.column1)
  4194. sql_id2 = SQL.identifier(self.relation, self.column2)
  4195. query.add_join('JOIN', self.relation, None, SQL(
  4196. "%s = %s", sql_id2, SQL.identifier(comodel._table, 'id'),
  4197. ))
  4198. query.add_where(SQL("%s IN %s", sql_id1, tuple(records.ids)))
  4199. # retrieve pairs (record, line) and group by record
  4200. group = defaultdict(list)
  4201. for id1, id2 in records.env.execute_query(query.select(sql_id1, sql_id2)):
  4202. group[id1].append(id2)
  4203. # store result in cache
  4204. values = [tuple(group[id_]) for id_ in records._ids]
  4205. records.env.cache.insert_missing(records, self, values)
  4206. def write_real(self, records_commands_list, create=False):
  4207. # records_commands_list = [(records, commands), ...]
  4208. if not records_commands_list:
  4209. return
  4210. model = records_commands_list[0][0].browse()
  4211. comodel = model.env[self.comodel_name].with_context(**self.context)
  4212. comodel = self._check_sudo_commands(comodel)
  4213. cr = model.env.cr
  4214. # determine old and new relation {x: ys}
  4215. set = OrderedSet
  4216. ids = set(rid for recs, cs in records_commands_list for rid in recs.ids)
  4217. records = model.browse(ids)
  4218. if self.store:
  4219. # Using `record[self.name]` generates 2 SQL queries when the value
  4220. # is not in cache: one that actually checks access rules for
  4221. # records, and the other one fetching the actual data. We use
  4222. # `self.read` instead to shortcut the first query.
  4223. missing_ids = list(records.env.cache.get_missing_ids(records, self))
  4224. if missing_ids:
  4225. self.read(records.browse(missing_ids))
  4226. # determine new relation {x: ys}
  4227. old_relation = {record.id: set(record[self.name]._ids) for record in records}
  4228. new_relation = {x: set(ys) for x, ys in old_relation.items()}
  4229. # operations on new relation
  4230. def relation_add(xs, y):
  4231. for x in xs:
  4232. new_relation[x].add(y)
  4233. def relation_remove(xs, y):
  4234. for x in xs:
  4235. new_relation[x].discard(y)
  4236. def relation_set(xs, ys):
  4237. for x in xs:
  4238. new_relation[x] = set(ys)
  4239. def relation_delete(ys):
  4240. # the pairs (x, y) have been cascade-deleted from relation
  4241. for ys1 in old_relation.values():
  4242. ys1 -= ys
  4243. for ys1 in new_relation.values():
  4244. ys1 -= ys
  4245. for recs, commands in records_commands_list:
  4246. to_create = [] # line vals to create
  4247. to_delete = [] # line ids to delete
  4248. for command in (commands or ()):
  4249. if not isinstance(command, (list, tuple)) or not command:
  4250. continue
  4251. if command[0] == Command.CREATE:
  4252. to_create.append((recs._ids, command[2]))
  4253. elif command[0] == Command.UPDATE:
  4254. prefetch_ids = recs[self.name]._prefetch_ids
  4255. comodel.browse(command[1]).with_prefetch(prefetch_ids).write(command[2])
  4256. elif command[0] == Command.DELETE:
  4257. to_delete.append(command[1])
  4258. elif command[0] == Command.UNLINK:
  4259. relation_remove(recs._ids, command[1])
  4260. elif command[0] == Command.LINK:
  4261. relation_add(recs._ids, command[1])
  4262. elif command[0] in (Command.CLEAR, Command.SET):
  4263. # new lines must no longer be linked to records
  4264. to_create = [(set(ids) - set(recs._ids), vals) for (ids, vals) in to_create]
  4265. relation_set(recs._ids, command[2] if command[0] == Command.SET else ())
  4266. if to_create:
  4267. # create lines in batch, and link them
  4268. lines = comodel.create([vals for ids, vals in to_create])
  4269. for line, (ids, vals) in zip(lines, to_create):
  4270. relation_add(ids, line.id)
  4271. if to_delete:
  4272. # delete lines in batch
  4273. comodel.browse(to_delete).unlink()
  4274. relation_delete(to_delete)
  4275. # update the cache of self
  4276. cache = records.env.cache
  4277. for record in records:
  4278. cache.set(record, self, tuple(new_relation[record.id]))
  4279. # determine the corecords for which the relation has changed
  4280. modified_corecord_ids = set()
  4281. # process pairs to add (beware of duplicates)
  4282. pairs = [(x, y) for x, ys in new_relation.items() for y in ys - old_relation[x]]
  4283. if pairs:
  4284. if self.store:
  4285. cr.execute(SQL(
  4286. "INSERT INTO %s (%s, %s) VALUES %s ON CONFLICT DO NOTHING",
  4287. SQL.identifier(self.relation),
  4288. SQL.identifier(self.column1),
  4289. SQL.identifier(self.column2),
  4290. SQL(", ").join(pairs),
  4291. ))
  4292. # update the cache of inverse fields
  4293. y_to_xs = defaultdict(set)
  4294. for x, y in pairs:
  4295. y_to_xs[y].add(x)
  4296. modified_corecord_ids.add(y)
  4297. for invf in records.pool.field_inverses[self]:
  4298. domain = invf.get_domain_list(comodel)
  4299. valid_ids = set(records.filtered_domain(domain)._ids)
  4300. if not valid_ids:
  4301. continue
  4302. for y, xs in y_to_xs.items():
  4303. corecord = comodel.browse(y)
  4304. try:
  4305. ids0 = cache.get(corecord, invf)
  4306. ids1 = tuple(set(ids0) | (xs & valid_ids))
  4307. cache.set(corecord, invf, ids1)
  4308. except KeyError:
  4309. pass
  4310. # process pairs to remove
  4311. pairs = [(x, y) for x, ys in old_relation.items() for y in ys - new_relation[x]]
  4312. if pairs:
  4313. y_to_xs = defaultdict(set)
  4314. for x, y in pairs:
  4315. y_to_xs[y].add(x)
  4316. modified_corecord_ids.add(y)
  4317. if self.store:
  4318. # express pairs as the union of cartesian products:
  4319. # pairs = [(1, 11), (1, 12), (1, 13), (2, 11), (2, 12), (2, 14)]
  4320. # -> y_to_xs = {11: {1, 2}, 12: {1, 2}, 13: {1}, 14: {2}}
  4321. # -> xs_to_ys = {{1, 2}: {11, 12}, {2}: {14}, {1}: {13}}
  4322. xs_to_ys = defaultdict(set)
  4323. for y, xs in y_to_xs.items():
  4324. xs_to_ys[frozenset(xs)].add(y)
  4325. # delete the rows where (id1 IN xs AND id2 IN ys) OR ...
  4326. cr.execute(SQL(
  4327. "DELETE FROM %s WHERE %s",
  4328. SQL.identifier(self.relation),
  4329. SQL(" OR ").join(
  4330. SQL("%s IN %s AND %s IN %s",
  4331. SQL.identifier(self.column1), tuple(xs),
  4332. SQL.identifier(self.column2), tuple(ys))
  4333. for xs, ys in xs_to_ys.items()
  4334. ),
  4335. ))
  4336. # update the cache of inverse fields
  4337. for invf in records.pool.field_inverses[self]:
  4338. for y, xs in y_to_xs.items():
  4339. corecord = comodel.browse(y)
  4340. try:
  4341. ids0 = cache.get(corecord, invf)
  4342. ids1 = tuple(id_ for id_ in ids0 if id_ not in xs)
  4343. cache.set(corecord, invf, ids1)
  4344. except KeyError:
  4345. pass
  4346. if modified_corecord_ids:
  4347. # trigger the recomputation of fields that depend on the inverse
  4348. # fields of self on the modified corecords
  4349. corecords = comodel.browse(modified_corecord_ids)
  4350. corecords.modified([
  4351. invf.name
  4352. for invf in model.pool.field_inverses[self]
  4353. if invf.model_name == self.comodel_name
  4354. ])
  4355. def write_new(self, records_commands_list):
  4356. """ Update self on new records. """
  4357. if not records_commands_list:
  4358. return
  4359. model = records_commands_list[0][0].browse()
  4360. comodel = model.env[self.comodel_name].with_context(**self.context)
  4361. comodel = self._check_sudo_commands(comodel)
  4362. new = lambda id_: id_ and NewId(id_)
  4363. # determine old and new relation {x: ys}
  4364. set = OrderedSet
  4365. old_relation = {record.id: set(record[self.name]._ids) for records, _ in records_commands_list for record in records}
  4366. new_relation = {x: set(ys) for x, ys in old_relation.items()}
  4367. for recs, commands in records_commands_list:
  4368. for command in commands:
  4369. if not isinstance(command, (list, tuple)) or not command:
  4370. continue
  4371. if command[0] == Command.CREATE:
  4372. line_id = comodel.new(command[2], ref=command[1]).id
  4373. for line_ids in new_relation.values():
  4374. line_ids.add(line_id)
  4375. elif command[0] == Command.UPDATE:
  4376. line_id = new(command[1])
  4377. comodel.browse([line_id]).update(command[2])
  4378. elif command[0] == Command.DELETE:
  4379. line_id = new(command[1])
  4380. for line_ids in new_relation.values():
  4381. line_ids.discard(line_id)
  4382. elif command[0] == Command.UNLINK:
  4383. line_id = new(command[1])
  4384. for line_ids in new_relation.values():
  4385. line_ids.discard(line_id)
  4386. elif command[0] == Command.LINK:
  4387. line_id = new(command[1])
  4388. for line_ids in new_relation.values():
  4389. line_ids.add(line_id)
  4390. elif command[0] in (Command.CLEAR, Command.SET):
  4391. # new lines must no longer be linked to records
  4392. line_ids = command[2] if command[0] == Command.SET else ()
  4393. line_ids = set(new(line_id) for line_id in line_ids)
  4394. for id_ in recs._ids:
  4395. new_relation[id_] = set(line_ids)
  4396. if new_relation == old_relation:
  4397. return
  4398. records = model.browse(old_relation)
  4399. # update the cache of self
  4400. cache = records.env.cache
  4401. for record in records:
  4402. cache.set(record, self, tuple(new_relation[record.id]))
  4403. # determine the corecords for which the relation has changed
  4404. modified_corecord_ids = set()
  4405. # process pairs to add (beware of duplicates)
  4406. pairs = [(x, y) for x, ys in new_relation.items() for y in ys - old_relation[x]]
  4407. if pairs:
  4408. # update the cache of inverse fields
  4409. y_to_xs = defaultdict(set)
  4410. for x, y in pairs:
  4411. y_to_xs[y].add(x)
  4412. modified_corecord_ids.add(y)
  4413. for invf in records.pool.field_inverses[self]:
  4414. domain = invf.get_domain_list(comodel)
  4415. valid_ids = set(records.filtered_domain(domain)._ids)
  4416. if not valid_ids:
  4417. continue
  4418. for y, xs in y_to_xs.items():
  4419. corecord = comodel.browse([y])
  4420. try:
  4421. ids0 = cache.get(corecord, invf)
  4422. ids1 = tuple(set(ids0) | (xs & valid_ids))
  4423. cache.set(corecord, invf, ids1)
  4424. except KeyError:
  4425. pass
  4426. # process pairs to remove
  4427. pairs = [(x, y) for x, ys in old_relation.items() for y in ys - new_relation[x]]
  4428. if pairs:
  4429. # update the cache of inverse fields
  4430. y_to_xs = defaultdict(set)
  4431. for x, y in pairs:
  4432. y_to_xs[y].add(x)
  4433. modified_corecord_ids.add(y)
  4434. for invf in records.pool.field_inverses[self]:
  4435. for y, xs in y_to_xs.items():
  4436. corecord = comodel.browse([y])
  4437. try:
  4438. ids0 = cache.get(corecord, invf)
  4439. ids1 = tuple(id_ for id_ in ids0 if id_ not in xs)
  4440. cache.set(corecord, invf, ids1)
  4441. except KeyError:
  4442. pass
  4443. if modified_corecord_ids:
  4444. # trigger the recomputation of fields that depend on the inverse
  4445. # fields of self on the modified corecords
  4446. corecords = comodel.browse(modified_corecord_ids)
  4447. corecords.modified([
  4448. invf.name
  4449. for invf in model.pool.field_inverses[self]
  4450. if invf.model_name == self.comodel_name
  4451. ])
  4452. class Id(Field[IdType | typing.Literal[False]]):
  4453. """ Special case for field 'id'. """
  4454. type = 'integer'
  4455. column_type = ('int4', 'int4')
  4456. string = 'ID'
  4457. store = True
  4458. readonly = True
  4459. prefetch = False
  4460. def update_db(self, model, columns):
  4461. pass # this column is created with the table
  4462. def __get__(self, record, owner=None):
  4463. if record is None:
  4464. return self # the field is accessed through the class owner
  4465. # the code below is written to make record.id as quick as possible
  4466. ids = record._ids
  4467. size = len(ids)
  4468. if size == 0:
  4469. return False
  4470. elif size == 1:
  4471. return ids[0]
  4472. raise ValueError("Expected singleton: %s" % record)
  4473. def __set__(self, record, value):
  4474. raise TypeError("field 'id' cannot be assigned")
  4475. class PrefetchMany2one:
  4476. """ Iterable for the values of a many2one field on the prefetch set of a given record. """
  4477. __slots__ = 'record', 'field'
  4478. def __init__(self, record, field):
  4479. self.record = record
  4480. self.field = field
  4481. def __iter__(self):
  4482. records = self.record.browse(self.record._prefetch_ids)
  4483. ids = self.record.env.cache.get_values(records, self.field)
  4484. return unique(id_ for id_ in ids if id_ is not None)
  4485. def __reversed__(self):
  4486. records = self.record.browse(reversed(self.record._prefetch_ids))
  4487. ids = self.record.env.cache.get_values(records, self.field)
  4488. return unique(id_ for id_ in ids if id_ is not None)
  4489. class PrefetchX2many:
  4490. """ Iterable for the values of an x2many field on the prefetch set of a given record. """
  4491. __slots__ = 'record', 'field'
  4492. def __init__(self, record, field):
  4493. self.record = record
  4494. self.field = field
  4495. def __iter__(self):
  4496. records = self.record.browse(self.record._prefetch_ids)
  4497. ids_list = self.record.env.cache.get_values(records, self.field)
  4498. return unique(id_ for ids in ids_list for id_ in ids)
  4499. def __reversed__(self):
  4500. records = self.record.browse(reversed(self.record._prefetch_ids))
  4501. ids_list = self.record.env.cache.get_values(records, self.field)
  4502. return unique(id_ for ids in ids_list for id_ in ids)
  4503. def apply_required(model, field_name):
  4504. """ Set a NOT NULL constraint on the given field, if necessary. """
  4505. # At the time this function is called, the model's _fields may have been reset, although
  4506. # the model's class is still the same. Retrieve the field to see whether the NOT NULL
  4507. # constraint still applies
  4508. field = model._fields[field_name]
  4509. if field.store and field.required:
  4510. sql.set_not_null(model.env.cr, model._table, field_name)
  4511. # imported here to avoid dependency cycle issues
  4512. # pylint: disable=wrong-import-position
  4513. from .exceptions import AccessError, MissingError, UserError
  4514. from .models import (
  4515. check_pg_name, expand_ids, is_definition_class,
  4516. BaseModel, PREFETCH_MAX,
  4517. )
上海开阖软件有限公司 沪ICP备12045867号-1