Commit Graph
287 Commits
Author SHA1 Message Date
Nans Lefebvre 4dcae232f3 [FIX] fields, test_translate: multilanguage without en_US
Before, en_US was almost always the default in case of 'no language'.
Now, that should be superseded by lang=None;
yet still, en_US plays the role of source language.

In a multilanguage database where en_US was not installed,
writing a translation with lang=None would crash.
This could prevent module installation.

A test is added to cover that case.

In the case where we write a record with lang=None, the source is the
same as the record; in that case the cache contains the value for the
field.name under the context keys en_US and None, so we need to
invalidate the cache to avoid getting back the old value.

co-authored with mart-e

opw 2088487
opw 2083710

closes odoo/odoo#39130

X-original-commit: 1a5a999c2dd69c9a6a2dacb59e8bf45b09c6bc13
Signed-off-by: Nans Lefebvre (len) <len@odoo.com>
2019-10-21 14:31:10 +00:00
Raphael Collet c8dca514e6 [FIX] fields: compute_sudo defaults to True for stored fields only
The default `compute_sudo=True` makes sense for recomputing stored
fields that are indirectly related to a business operation.  This
ensures that the recomputation of the field does not break an operation
that is not aware of the fields to recompute.

However, computing non-stored fields in superuser mode is usually not
necessary.  It even leads to unexpected values: counting a partner's
sales orders does not give the same result in superuser mode as in
normal mode.  That is why non-stored fields are not computed in
superuser mode by default.

[FIX] account, delivery, event, hr_recruitment, point_of_sale, stock:
adapt the model definition to make all fields with the same compute
method have the same value for `compute_sudo`.

[FIX] sale: split the computation of `invoice_ids`, `invoice_count`
(non-stored) and `invoice_status` (stored), as no code is actually
shared.

closes odoo/odoo#39195

X-original-commit: 843fd38a97f02b49dc09d7f55919072d272fd80e
Signed-off-by: Olivier Dony (odo) <odo@openerp.com>
2019-10-22 14:55:51 +00:00
Nans Lefebvre 5efa30c302 [FIX] fields, test_access_rights: get currency field in sudo
This commits fixes user duplication.

Suppose there is a monetary field, with a currency_field that is a related.
When going through model's _create, we do:
`if field.type in ('one2many', 'many2many'):
    self.env.cache.set(record, field, ())`
knowing that thse values are false, with the intent to clean them later:
`for record, field in cachetoclear`
However, when setting the scalar values, we go through:
`accessing cache_value = field.convert_to_cache(value, record)`
In the case of the monetary field, this depends on another field value
(the currency_field). If it is a related, we can access its value.
However, at this point, if we check any access rights, we might use the value
of a relational in cache for which the value is incorrectly set to False.

In the case of the user, this is what happens: it inherits its currency_id
from partner, as well as its debit_limit which depends on it.
When the access if checked, company_ids is set to False in cache.
So when going through the rule 'user rule', which checks that the company_ids
intersects with the env.companies.ids, the result is always False.

In some way this is essentially hiding the problem, but the true fix is
probably not feasible in stable.

opw 2086661

closes odoo/odoo#39017

X-original-commit: aa05a9359b42317261c8e7ffcadbeb9fe74dcd58
Signed-off-by: Nans Lefebvre (len) <len@odoo.com>
2019-10-18 09:40:02 +00:00
Raphael Collet 6973dfbaef [FIX] fields: 'bin_size' must have no effect on arbitrary binary field values
This fixes a regression introduced by 291a0e99d137142bd24addd1054c1357ddd7125a.

closes odoo/odoo#39178

X-original-commit: 852ee32e3419c210a3844b0f943af8dd2bbdae88
Signed-off-by: Raphael Collet (rco) <rco@openerp.com>
2019-10-22 12:25:52 +00:00
Sébastien Theys 20ac5338ac [FIX] fields: verify image resolution on create/write
Before this commit, if `max_width` and `max_height` are 0, the image would not
be opened by Pillow at all (to gain a bit of CPU) however this was actually a
bad idea in this case because then we don't ensure the given value is actually a
valid image when we save it, only later at display we notice it and it crashes.

This optimization only made sense when `image_process` is called many times,
from the route displaying images every time a visitor is requesting an image,
for example. It doesn't really have an impact for one time operations such as
creating/writing.

So when size parameters are 0, which is the default, another parameter must be
passed to `image_process` to ensure image validity, and `verify_resolution` is
actually built for this, as it does the minimal amount of processing:
- loading the image
- making sure it is valid
- and ensuring the resolution is not completely crazy (`IMAGE_MAX_RESOLUTION`).
It doesn't alter the original image if no other operation was requested.

Pr: #38292
X-original-commit: e16c36bb4bfa3ae1739712c69c72849665a057dd
2019-10-21 09:08:10 +00:00
Sébastien Theys 38133ce0d1 [FIX] fields: ensure image values are stored in cache as binary
Binary fields are supposed to be saved as binary in cache, and not as string
which is also supported as an input value.

Pr: #38292
X-original-commit: 64e0d106adccd8132cbfeb6b60da74b3feaf05e1
2019-10-21 09:08:10 +00:00
Raphael Collet 883fd9e045 [FIX] fields: ensure Binary are never recomputed with "bin_size"
The compute methods are always expecting the real value and not the bin_size.

The solution is to always compute with `bin_size=False`, and then manually
compute the `bin_size` and set it on the cache of `bin_size=True`.

PR: #38292

Co-authored-by Sébastien Theys <seb@odoo.com>

X-original-commit: 7744886d6141ca7971d91807d0444c707e10fdf8
2019-10-21 09:08:10 +00:00
Denis Ledoux 0a2fcfbce9 [FIX] fields: do not attempt to set foreign keys on views
Users sometimes define custom models on SQL views
e.g. @nseinlet
In such a case, Odoo should not attempt to create foreign keys
as it just cannot work on views.

This could prevent the migration of a database
with such a custom model using a view
when it attempted to fix the missing foreign keys
when updating the modules.

closes odoo/odoo#38988

X-original-commit: dfaea03de57394a9a188f499da664c57dd9adc29
Signed-off-by: Denis Ledoux (dle) <dle@odoo.com>
2019-10-17 19:20:39 +00:00
Christophe Simonis d74b451805 [MERGE] forward port branch 13.0 up to f4105eb9c7 2019-10-09 02:08:17 +02:00
Raphael Collet 818168198b [FIX] fields: enable inverse on inherited readonly fields 2019-10-07 13:43:20 +00:00
Graeme Gellatly ec162dd0cb [IMP] fields: add a primary key for m2m tables
Remove individual m2m indexes and replace with a single mirrored
composite

This will help for logical replication strategies (postgresql BDR)

closes odoo/odoo#37963

Signed-off-by: Martin Trigaux (mat) <mat@odoo.com>
2019-10-04 09:44:13 +00:00
Florent de LabarreandAdrian Torres b1d5ddd18b [FIX] doc: add more information about company-dependent fields
closes odoo/odoo#37986

X-original-commit: 1b8acd57f019d4e34eac72d34a8c6ff62731d836
Signed-off-by: Adrian Torres (adt) <adt@odoo.com>
Co-authored-by: Adrian Torres <adt@odoo.com>
2019-10-04 13:40:05 +00:00
Raphael Collet 4b1cb41cf7 [FIX] models, fields: cache consistency for one2many fields
Issue: after many2one updates, the cache of the corresponding one2many
fields was inconsistent when the latter depends on `active_test`.  One
of the values in cache was updated, while the other was left intact.

We simplify the cache by not making the field depend on context: the
cache value contains all the records in the relation (corresponding to
`active_test=False`).  The value of the field is automatically filtered
by the `active` field when the value is accessed.

This makes it easier to maintain the cache value, guarantees its
consistency, and avoids queries to read the one2many field with
`active_test=False`, after having set it with `active_test=True`.
2019-09-30 15:42:36 +00:00
Raphael Collet f047425b82 [IMP] models, fields: initialize cache to None for related fields
This makes sense because in `create` the "previous" value of a field is
obviously non-existent.

This avoids having to fetch pre-existing attachments when they are known to be
inexistent, which optimizes stored related binary that are computed post-insert.
2019-09-23 20:55:39 +00:00
Sébastien TheysandAdrian Torres 9112972e15 [IMP] fields: write only once per Image field
Before this commit, flushing a model that uses the `image.mixin` would generate
twice as many queries as it should.

This is because `_compute_related` is not meant to be overridden. In the
override of `Image` we call `super()` then post process the records by
reassigning each record's relevant image field with `_image_process`.

The `super()` will assign the field of the records once which
will in turn trigger a `write` (and thus, will generate queries) then
after the `super()` call we reassign them which will re-trigger the same
`write` and the same queries.

With this commit, instead of overriding `_compute_related` we extract the
processing to a method that can be overridden, so that the `write` is called
simply once.

The result, obviously, is that queries related to the `write` are cut in half.

Related PR: #36683 & #36288

Co-authored-by: Adrian Torres <adt@odoo.com>
Co-authored-by: Sébastien Theys <seb@odoo.com>
2019-09-23 20:55:39 +00:00
Raphael Collet 50c7941fa6 [FIX] fields: set model_field on related many2one_reference fields
This fixes an issue when a Model A with a `many2one_reference` is inherited in
Model B with `_inherits`, and another Model C defines a `one2many` having as
inverse the inherited `many2one_reference` in Model B.

Without this commit, the value of `model_field` is `False` by default for the
field in B, which would lead to incorrect queries when building
`get_domain_list` for the `one2many`.
2019-09-23 20:55:39 +00:00
Xavier ALT c5fa4771fc [FIX] fields: default to ondelete='cascade' for required m2o fields on transient
TransientModel (wizards) can be annoying because if they have required
many2one fields, those fields will default to `ondelete='restrict'`,
preventing user to delete the comodel records without deleting the
transient model records first.

To improve such case, we default to `ondelete='cascade'` for required
many2one field on a TransientModel (unless specified otherwise)

closes odoo/odoo#36738

Signed-off-by: Raphael Collet (rco) <rco@openerp.com>
2019-09-20 12:23:09 +00:00
Martin Trigaux 543a5523a3 [FIMP] base: remove all translations when writing False
This "bugprovement" resolve the old issue of writing on a translatable
field a falsy value.

Before this commit, only the translation value was removed. When no
translation is present (or when the value is not set), the value is
retrieved directly from the source field.

Before 18d9c2cab2, it was still possible to go around this issue by
removing manually all ir.translation entries and writing with a user
in en_US.
This is no longer possible as writing on a translatable field always
uses translations when in multi-language environment, without
exception of en_US.

When writing a falsy value on a field, the expected behaviour is to
reset the value and see that falsy value after saving, not the old
value stored on the source model.

Remove all translations and force to update the column.

Removing one individual translation is still possible in the
translation popup.

Task id: 2062415

closes odoo/odoo#37077

Signed-off-by: Martin Trigaux (mat) <mat@odoo.com>
2019-09-18 14:56:41 +00:00
Raphael Collet 492e4cc5a1 [REF] fields: setup and use of depends_context
closes odoo/odoo#36795

Signed-off-by: Raphael Collet (rco) <rco@openerp.com>
2019-09-12 14:24:58 +00:00
Raphael Collet 44ea2adda7 [FIX] fields: conversion of Many2oneReference value
The value of that field can be given as a dict containing the fields of
the corresponding record.

Co-authored with Michael Mattiello <mcm@odoo.com>

closes odoo/odoo#36779

Signed-off-by: Raphael Collet (rco) <rco@openerp.com>
2019-09-12 12:23:55 +00:00
Denis Ledoux 5cac9c54ff [IMP] fields: avoid __set__ multiple loops on record, and new browse records
This patch avoids to create new browse records for nothing,
(as creating new browse (__new__) is costly),
and to avoid to loop multiple times on the records
(with multiple different filtered, etc.)

This brings performance gain to __set__,
which is used when setting the value of a compute field

e.g. This improves the performances of
`env['product.product'].search_read([])`

closes odoo/odoo#36006

Signed-off-by: Raphael Collet (rco) <rco@openerp.com>
2019-09-11 07:55:17 +00:00
Denis Ledoux 3d1c924cff [FIX] fields.py: improve computation time of records having a different value in cache
Instead of using a filtered with `cache.get`,
use a dedicated method in the Cache class to get
the records having a different value in cache then asked.

This is mainly to avoid the creation of intermediate
`browse` of 1 record, when doing `for rec in self`
in `filtered`.

Creating browses is costly, and avoiding it leads
to performance gains.

The dedicated method `get_records_different_from`
loops on the record ids, instead of on browse records
2019-09-11 07:55:17 +00:00
Raphael Collet 1315a1a82c [FIX] fields: clash with automatic fields and inheritance
Consider a model M that defines `display_name` as a computed stored
field, then an extension of M that introduces a mixin model A before
that definition in the MRO of M's class.  The model A is expected to
have an automatic, non stored field `display_name`, while M must have
its non-automatic, stored field `display_name`.

closes odoo/odoo#36427

Signed-off-by: Raphael Collet (rco) <rco@openerp.com>
2019-09-10 13:29:15 +00:00
Raphael Collet 36346afd96 [FIX] models: callable in api.depends() sees valid _rec_name
Determine `model._rec_name` before the dependencies of the fields.  The
compute method of the automatic field `display_name` uses a callable
depends that retrieves `model._rec_name`.

closes odoo/odoo#36484

Signed-off-by: Raphael Collet (rco) <rco@openerp.com>
2019-09-09 13:42:45 +00:00
Raphael Collet 263849c336 [FIX] models: optimize modified() in the context of record creation
This prevents a bunch of queries that are useless when creating a
record.  Indeed, right after a record has been created, no other record
has a many2one reference to it.  In other words, inversing a many2one
field from the record just created always gives an empty recordset.
Those useless inversions generate about a dozen queries when creating a
`res.partner`, for instance.

This saves queries, but not much time.

closes odoo/odoo#36566

Signed-off-by: Raphael Collet (rco) <rco@openerp.com>
2019-09-09 13:18:53 +00:00
Raphael Collet cfb08e87b0 [FIX] models: use transitive triggers instead of recursion
On average, this reduces the total time spent in method `modified` by
half (times measured on invoice creation and post).
2019-09-09 13:18:48 +00:00
Raphael Collet 55e1b44492 [FIX] fields: enable default on company-dependent field
The ORM overrides the default defined programmatically on the field, and
set a default function that retrieves the value in `ir.property`.

closes odoo/odoo#36545

Signed-off-by: Raphael Collet (rco) <rco@openerp.com>
2019-09-09 09:35:19 +00:00
Simon Lejeune 504b5fe5a9 [ADD] fields, models: company consistency check
Assigning `check_company=True` to a field will
- set a default domain filtering the companies
- allow to call `_check_company` on the records to ensure the domain is
  respected

Setting `_check_company_auto = True` on a model will ensure
`_check_company` is called at create and write, enforcing the multi
company domain.

Joint work with Raphael Collet <rco@odoo.com>

task-1985992
2019-09-03 15:03:45 +00:00
Raphael Collet c3ebc0b987 [FIX] fields: do not update translations for no record
When a translated field is set on new records, one should not update
translations in 'ir.translation'.

closes odoo/odoo#36138

Signed-off-by: Raphael Collet (rco) <rco@openerp.com>
2019-08-27 14:25:44 +00:00
Raphael Collet c7f5c4afd2 [FIX] sql_db: add flush() in savepoint()
closes odoo/odoo#36060

Signed-off-by: Raphael Collet (rco) <rco@openerp.com>
2019-08-26 13:39:16 +00:00
Fabien Pinckaers 74b055f75c [IMP] speed improvement, no double None in _fields_triggers 2019-08-26 10:20:09 +00:00
Toufik Ben Jaa 22a71d694f [FIX] odoo: do not try to set source term on new records
closes odoo/odoo#36026

Signed-off-by: Denis Ledoux <beledouxdenis@users.noreply.github.com>
2019-08-23 13:16:40 +00:00
Raphael Collet e388efd059 [FIX] api: share protected among environments
If a field is protected against recomputation, it must be protected in
*all* environments.
2019-08-23 10:00:12 +00:00
Raphael Collet fcb77f0f53 [FIX] fields: use column format in towrite 2019-08-22 13:40:40 +00:00
Raphael Collet 8918bbbb5e [IMP] fields: do not trigger recomputations on transient models from regular models
This is a performance optimization: modifying a regular record will not
trigger some recomputation on a transient record.  Most transient
records are simply waiting to be garbage-collected, so there is no need
to keep their fields up-to-date.

closes odoo/odoo#35909

Signed-off-by: Raphael Collet (rco) <rco@openerp.com>
2019-08-22 10:05:52 +00:00
Raphael Collet 1d2bccd0db [IMP] fields: stricter rules on selection fields overridings
One cannot override the attribute `selection`, and list selections can only be
extended with `selection_add`.

Adapt the bad extensions to be consistent with the new warnings

closes odoo/odoo#35663

Signed-off-by: Martin Trigaux (mat) <mat@odoo.com>
2019-08-21 14:36:19 +00:00
Raphael Collet 4cd093a001 [IMP] fields: extend selection with a given order
When extended with the selection_add parameter, a singleton (value,)
where `value` appears in the overriden selection, can be used.
The new values are inserted in an order that is consistent with the
overridden selection and this list.

selection = [('a', 'A'), ('b', 'B')]
selection_add = [('c', 'C'), ('b',)]
> result = [('a', 'A'), ('c', 'C'), ('b', 'B')]
2019-08-21 13:47:17 +00:00
Sébastien Theys 064ebe3b1a [IMP] fields, test_new_api: add ondelete support for m2m
Each model of the relation can define `ondelete` for its `column2` foreign key.

This way the behavior is similar to m2o where the target of the `ondelete` is
the `comodel` foreign key.

Some models were already defining `ondelete` 'restrict' on m2m fields and after
review it appears the behavior introduced in this commit is the expected
behavior for them.

The 'restrict' from product.product has to be removed however because the rest
of the code needs adaptation now that it is properly restricted. These changes
will be done on a separate PR.

closes odoo/odoo#35654

Signed-off-by: Xavier Morel (xmo) <xmo@odoo.com>
2019-08-20 14:09:01 +00:00
Raphael Collet 9920f20e4c [IMP] models: ORM speedup
This branch is the combination of several optimizations in the ORM:

* store field values once in the cache: the cache reflects more
faithfully the database, only fields that explicitly depend on the
context have an extra indirection in the cache;

* delay recomputations by default: use method `recompute` to explicitly
flush out pending recomputations;

* delay updates in method `write`: updates are stored in a data
structure that can be flushed efficiently to the database with method
`flush` (which also flush out recomputations);

* make method `modified` take advantage of inverse fields to inverse
dependencies;

* filter records by evaluating a domain on records in Python;

* a computed field with `readonly=False` behaves like a normal field
with an onchange method;

* computed fields are computed in superuser mode by default.

Work done by Toufik Ben Jaa, Raphael Collet, Denis Ledoux and Fabien
Pinckaers.

closes odoo/odoo#35659

Signed-off-by: Denis Ledoux <beledouxdenis@users.noreply.github.com>
2019-08-20 12:43:59 +00:00
Martin TrigauxandRaphaël Collet 7593b887df [REF] fields: use ir.model.fields.selection
The selection values of a selection field are now stored in database in the
model ir.model.fields.selection

This will allow to have a modular approche on selections and each selection
is now linked to the module that declared it.
Previously to this change, the selections were linked to the field, meaning
uninstalling a module had no impact on the selections stored on database.

With this change, the selections will now be translated in the correct module
(having an external id) and the records having a used selection will now be
reset to null.

closes odoo/odoo#30228

Signed-off-by: Martin Trigaux (mat) <mat@odoo.com>


Co-authored-by: Raphaël Collet <rco@odoo.com>
2019-08-19 11:44:34 +00:00
Christophe Simonis e8a3f1a04e [IMP] core: remove support of oldname attribute
This attribute is misleading as it is insufficient to correctly upgrade
the database. It only renames the column in the database, but other
operations are needed, like updating the corresponding `ir.model.fields`
record (and its xmlid). The default values and the translations are also
lost during the upgrade.

Moreover, this feature was misused. It was:
- left on fields during multiple versions.
- used on reports (SQL views). This would be ok if the feature was
complete, but, as is, it was useless.
- kept unchanged after a second renaming of the field (which can happen
versions later the first rename).
- used, even when the meaning of the field changed. i.e. the field
`archived` has been renamed to the classic `active`, but the value
in the database should be switched.

closes odoo/odoo#35439

Signed-off-by: Christophe Simonis <chs@odoo.com>
2019-08-05 11:49:07 +00:00
Florent de LabarreandOlivier Dony c8fca2d052 [IMP] base: add more information for incorrect dependencies
If a dependency was wrong in the triggers, it was hard to debug

Co-Authored-By: Olivier Dony <odo@odoo.com>

closes odoo/odoo#31958

Signed-off-by: Martin Trigaux (mat) <mat@odoo.com>
2019-08-05 13:57:34 +00:00
Sébastien Theys 8206b5b4cc [IMP] fields: add image field
PR: #34925
2019-08-02 16:44:47 +00:00
Martin Trigaux 66dea8bb7b [REF] web: remove raw_mode flag on export
The export is now always in raw_mode
Adapt the tests

Fixes odoo/odoo#18798

closes odoo/odoo#26724

Signed-off-by: Martin Trigaux (mat) <mat@odoo.com>
2019-08-02 08:53:24 +00:00
Adrian Torres de91028e1d [REM] api: remove references to env.dirty
closes odoo/odoo#34556

Signed-off-by: Raphael Collet (rco) <rco@openerp.com>
2019-07-09 07:36:15 +00:00
Raphael Collet 368e9530f4 [REF] *: record.env.user._is_XXX() -> record.env.is_XXX()
Superuser mode implies `record.env.is_XXX()`.
2019-07-04 11:32:22 +00:00
Raphael Collet 1e6c3bec2c [ADD] api: flag su on environments
The flag defines a "superuser mode" on environments, which allows to
bypass access rights without changing the current user id.
2019-07-04 09:24:23 +00:00
Martin Trigaux 573ccf098f [FIX] fields: implement _description_digits
Otherwise, fields_get returns the string, not the digits
Add test
2019-07-03 11:16:24 +00:00
Martin Trigaux e2bd3e328a [IMP] odoo: remove LazyCursor
Was specifically created for decimal.precision at f0646cb51b
Should no longer be needed
2019-07-03 11:16:24 +00:00
Mitali Patel 0c5121a979 [IMP] decimal_precision: integrate into base
The decimal precision feature makes sense to be an ORM feature, no need to be
in a specific module

Previous syntax was
    from odoo.addons import decimal_precision as dp
    fields.Float(digits=dp.get_precision('Foo'))

and now is:
    fields.Float(digits='Foo')

Remove the possibility to have a callable method on the digits attribute (it
was only used for precision anyway) and directly retrieve the digits on the
decimal.precision model

Rename the method digits to get_digits to avoid confusion between the field
attribute when declaring a field and the method to retrieve the precision

Task id: 48198
2019-07-03 11:15:48 +00:00