100 Commits
Author SHA1 Message Date
Adrian Torres ed6595ed15 [FIX] base: prevent messing up existing registry 2021-04-08 08:20:03 +02:00
Adrian Torres bbee0b3081 [FIX] base: allow setting group_expand for ir.model.fields
PR #75856 introduced a default implementation of group_expand for
Selection fields that set the value of group_expand to True.

However the PR lacked the necessary bits that allow the same behavior
for fields created on the fly instead of through code.

This commit allows the propagation of the group_expand attribute for
fields of type Selection, this commit also exposes the checkbox in the
UI to enable/disable the setting.

closes odoo/odoo#76775

X-original-commit: 1cacc3e53cf722a31bedff7a76f8e5cc6ed45ce0
Signed-off-by: Raphael Collet (rco) <rco@openerp.com>
2021-09-18 17:00:52 +00:00
Adrian Torres c393a9882e [FIX] core: load all bases for ir.model before adding manual models
Commit cd12293386 introduced an
optimization in the way that models and their inheritances are loaded,
with this commit we defer the setting of each model class' bases
from the _build_model method to the _prepare_setup method.

This has the advantage of setting any given model class' `__bases__`
attribute only once, but it also means that when _add_manual_models is
called, the __bases__ for ir.model are not yet set (only the default
implementation exists), therefore any module overrides to ir.model do
not take effect when creating the custom models.

This meant that if one creates a custom model with chatter support (i.e.
custom ir.model behaviour implemented in mail) and one restarted the
server, the registry would not properly setup ir.model before creating
the custom model (yielding warnings about tracking and such not being
valid fields) and when creating a record of the custom model, the
registry would crash.

With this commit, ir.model's _prepare_setup is explicitly called before
_add_manual_models to ensure that all overrides to ir.model are taken
into account.

closes odoo/odoo#76325

X-original-commit: f3afb23cdf21f395855771a037c721e977dc93c8
Signed-off-by: Raphael Collet (rco) <rco@openerp.com>
Signed-off-by: Adrian Torres (adt) <adt@odoo.com>
2021-09-10 12:34:06 +00:00
Adrian Torres 9f11a84d71 [IMP] Order groups by selection declaration order in read_group
Before this commit, calling read_group on a model and grouping by a
selection field would return the groups in alphabetical order, meaning
that if your selection field's options were declared as:

('z', 'Z'), ('a', 'A')

read_group would return first group 'a' and then group 'z', instead of
groups 'z' and then 'a' which some might expect.

With this commit, it is now possible to set a fields.Selection
group_expand attribute to `True` when declaring it, which will use a
default group_expand implementation specific to Selection fields, this
means that when grouping by a Selection field with `group_expand=True`
it will always return the groups in the definition order of the
selection options.

We achieve this by leveraging the group_expand field attribute which was
designed for changing the groups returned by read_group.

Since this attribute was thought only to be implemented on a
Model-by-Model basis and here we need to use it as a generic function
for all Selection fields (explicit group_expand declarations have higher
precedence), the generic method has been implemented inside
fields.Selection and takes an extra records parameter which holds a
reference to the recordset/model on which read_group was called. This
extra parameter only applies to field implementations of group_expand
and in this case is what allows this specific feature to work with
dynamic Selection fields (function as options).

A side-effect of this implementation is that a read_group call that
groups by a Selection field with `group_expand=True` that uses the
default group_expand will always return all possible groups, even
empty ones.

Task-ID 2635052

closes odoo/odoo#75856

Signed-off-by: Raphael Collet (rco) <rco@openerp.com>
2021-09-06 08:21:34 +00:00
Adrian Torres df110305a1 [FIX] sale_stock_margin: make implicit dependency explicit
Strap yourself in, we're in for a ride:

The module sale_stock_margin overrides
SaleOrderLine._compute_purchase_price and adds some extra dependencies,
the key dependency here is 'move_ids'.

The field move_ids is defined in sale_stock's override of
sale.order.line, however sale_stock is not a dependency of
sale_stock_margin, but since they implicitly share the same dependencies
and are both auto_install=True it is impossible to install
sale_stock_margin without having sale_stock be automatically installed.

However, since sale_stock_margin does not explicitly depend on
sale_stock, if one were to uninstall sale_stock without uninstalling
sale_stock_margin first, the registry would crash because
sale_stock_margin's override of _compute_purchase_price would depend
on a field that no longer exists (move_ids).

This commit changes sale_stock_margin's dependency graph to explicitly
depend on `sale_stock`, this in turn means we can forego the
`stock_account` dependency since it's an explicit dependency of
`sale_stock`.

closes odoo/odoo#75693

Signed-off-by: Raphael Collet (rco) <rco@openerp.com>
2021-08-27 19:49:01 +00:00
Adrian Torres e2f3ac24d0 [IMP] web: allow read_progress_bar to group by m2m fields
The value returned by the search_read in read_progress_bar when passing
a m2m field is a list of ids, which then read_progress_bar tries to use
in a dictionary, list is not a hashable type thus it crashes.

With this commit we convert the group_by_value from list to tuple,
which is hashable, if we're dealing with a many2many field.

task-2508608

Part-of: odoo/odoo#74985
2021-08-26 16:24:59 +00:00
Adrian TorresandRaphael Collet d543f53c33 [FIX] base: avoid recursion depth errors during uninstall
For some reason _logger.info with exc_info=True inside a recursive
function generates RecursionErrors, presumably because the logger uses
recursion itself to generate the stack trace that is logged.

A simple solution would be to remove exc_info=True, but I've decided to
move the log out of the delete function and simply call it once per
uninstall process with all undeletable IDs, so it's kind of a fix +
optimization.

closes odoo/odoo#75445

X-original-commit: 3a04de569bca10a1d1a01f719635de6842e747b2
Signed-off-by: Raphael Collet (rco) <rco@openerp.com>
Signed-off-by: Adrian Torres (adt) <adt@odoo.com>
Co-authored-by: Raphael Collet <rco@odoo.com>
2021-08-23 10:51:57 +00:00
Adrian TorresandRaphael Collet 4f5b2dc550 [FIX] base: perform sanity check on undeletable ir.model.data
This commit adds a sanity check to the part of the ORM that uninstalls
module data, as a recap, here's how module uninstallation works:

- We fetch all data (ir.model.data) that corresponds to the module being
uninstalled (`WHERE module='my_module'`)
- We divide this data according to its type (ir.model, ir.model.field,
constraints, etc.)
- We fetch the corresponding records to each type, we delete them in a
certain order (e.g. ir.model.field before ir.model) and then finally we
delete the ir.model.data as a last step

The data is deleted in batch for maximum performance, if one of the data
cannot be deleted however, we perform a binary search until we find the
culprit(s) and we store these culprits in a list of undeletable_ids.

At the end of the process, we delete all ir.model.data **except** for
the ones that are undeletable, however, it is possible that because of
the multiple-step procedure, an undeletable ir.model.data could have
become deletable.

Imagine that an ir.model.field cannot be deleted, its module data id is
added to the list of undeletable_ids, however if later on its ir.model
is deleted successfully, the ir.model.field is dropped because its table
is dropped, in this case the ir.model.data becomes deletable, but since
we simply ignore it at the end of the process, we potentially end up
with orphaned xmlids.

This can be problematic when we reinstall the module and uninstall it
again, as the system does not expect an orphaned xmlid, will completely
crash and prevent the 2nd uninstallation of the module.

This is the case with CRM and its
crm.lead.scoring.frequency.field.field_id field, its ir.model.field
cannot be deleted because the name field (and display_name) of the same
model depend on it, so it is left as is, then further down the process
the entire model is deleted and as a result so is all of its remaining
fields, however the ir.module.data for the field that could not be
deleted remains.

opw-2575592

closes odoo/odoo#73668

X-original-commit: 75697934b34df882ec03595b876a8a6dadcef4c5
Signed-off-by: Adrian Torres (adt) <adt@odoo.com>
Co-authored-by: Raphael Collet <rco@odoo.com>
2021-07-14 07:47:32 +00:00
Adrian Torres 5a04043546 [FIX] base: log Selection.ondelete ORM bypass at runbot level
This commit changes the warning log for Selection fields that states
that the hook could not go through the ORM for the deletion at uninstall
(because of some business error) and that it had to bypass the ORM (aka
pure sql delete) and turns it into a runbot-level log, meaning that it's
technically a warning but the runbot won't fail because of it.

This is done because for the install/uninstall tests it shows up as an
actual failure but it is not as it is non-blocking, and 99.9% of devs
won't ever see this warning on runbot since it triggers on module
uninstall anyway.

closes odoo/odoo#73332

X-original-commit: c140f545d150da05bd47f2c2dabc28c53cefe912
Signed-off-by: Adrian Torres (adt) <adt@odoo.com>
2021-07-06 16:17:23 +00:00
Adrian TorresandRaphael Collet b4487e8ea4 [IMP] core: allow grouping by m2m fields in read_group
With this commit, it is now possible to group the records of a model by
a Many2many field of said model.

The result of a read_group grouped by a m2m will return as many entries
or "groups" as there are different records in the comodel that are
linked to the model through the m2m, plus a null/false group, for records
of the model that have no linked records of the comodel or for records
of the comodel for which the current user has no read access to due to
ir.rules.

For a more illustrated explanation, the tests should cover all cases in
detail.

Note that this commit only introduces this change at the ORM level, the
frontend does not yet handle grouping by m2m fields but it is planned
in the near future.

Task-2428971

closes odoo/odoo#68958

Signed-off-by: Raphael Collet (rco) <rco@openerp.com>
Co-authored-by: Raphael Collet <rco@odoo.com>
2021-04-20 12:08:35 +00:00
Adrian Torres be19a9e9aa [DOC] orm: document Query._join method
From having to work with it recently, it was not super clear what each
argument did (especially the link argument), and more documentation =
more betterer code
2021-04-20 10:35:07 +00:00
Adrian TorresandRaphael Collet 143cffe001 [FIX] base: reset _rec_name if x_name field is deleted
Commit 6a0028f91944b1d9e4eac026e86ca249ef5bc7ee introduced a change that
allowed field_triggers to be computed lazily per registry, this change
introduced a behavioral change that is not easy to notice, in order to
explain the behavioral change I will use the following example which was
the original bug reported:

    - Install studio
    - Create an app (create a new custom model M)
    - Uninstall studio
    -> Uninstall fails because field display_name of the custom model M
    depends on field M.x_name which has been removed due to the
    uninstall process.

To understand why it didn't happen before the aforementioned commit, we
must first understand what happens with said commit applied:

    - We trigger the uninstall of studio, this triggers the uninstall of
    any modules that depend on it, namely studio_customizations which is
    the module in which all customizations done with studio live in.

    - We gather all data belonging to the studio_customization and we
    start deleting in the following order: ...,
    ir.model.fields.selection, ir.model.fields, ..., ir.model

    - During the unlink process, we first remove the actual fields from
    the model instances before deleting their database reflections, this
    is done in the ir.model.fields._drop_column() method, it is this
    method that will delete the x_name field but **not** the
    display_name field, since it is a base field and not a custom one.

    - After the deletion of the fields in memory, we call modified() to
    mark fields that might've depended on the fields we just modified so
    that they can be recomputed later.

    - The call to modified will in turn access field_triggers, but since
    we're in a new registry and field_triggers is a lazy property, it
    will be computed right at this moment, this means that it will call
    resolve_depends on the display_name field which still exists, and
    this field has a dependency on the x_name field that we just
    deleted! This is what will trigger the crash.

With that context, we can now understand how it didn't crash before
commit 6a0028f91944b1d9e4eac026e86ca249ef5bc7ee:

    - Before the aforementioned commit, the field_triggers attribute was
    computed during the registry's setup_models(), in the case of an
    uninstall this call to setup_models was done way before the
    uninstall step of the registry (Step 3 is the last to call
    setup_models before Step 5).

    - This means that the old behavior was technically a bug, because
    right after removing x_name from the model, the field_triggers still
    contained a dependency from display_name to x_name, the former being
    no-longer present in memory.

With this commit, we simply reset the _rec_name and the dependencies of
the display_name if the x_name field is being removed, this ensures that
the computation of field_triggers won't crash and burn.

opw-2452498
opw-2478589

closes odoo/odoo#67823

X-original-commit: e6d22a43ff9f40e5fc7b8c84cd4dfe43f926d872
Signed-off-by: Adrian Torres (adt) <adt@odoo.com>
Co-authored-by: Raphael Collet <rco@odoo.com>
2021-03-15 09:30:21 +00:00
Adrian Torres ab7b5af3e8 [FIX] base: properly execute Selection field ondelete actions
With this commit, 2 changes are made to the way that the
fields.Selection.ondelete cleanup action works:

1) We manually delete ir.model.fields.selection **before** the deletion
of ir.model.fields purposefully to **avoid** SQL CASCADE deletes, as we
do not know which selections are deleted in cascade and thus we cannot
perform the corresponding ondelete cleanup action (which is implemented
within the ORM).

2) In some actions, namely 'set default' and 'set null', we write a
"safe" value to the records containing the Selection being deleted,
before this commit this would go through the ORM (records.write()) but
this is problematic if there's a write override for the record's model
that raises an error for the field being written to. With this commit,
we first try to go through the ORM but if there's a failure (because of
the raise in a write override) then we will bypass the ORM and set it
with SQL.

opw-2451126

closes odoo/odoo#67616

X-original-commit: f5c7e861ee3ca0cdeb6c2f06d227ada07f923ed0
Signed-off-by: Adrian Torres (adt) <adt@odoo.com>
2021-03-11 06:45:42 +00:00
Adrian Torres 75243d5a43 [FIX] base: do not assign default ondelete policy for base fields
Previously, an ondelete policy would be assigned to any required
Selection fields, the default policy being 'set null'.

Base fields do **not** require an ondelete value, since if they are
deleted there is no "data fix" to apply because the field's column is
dropped, and in fact it's error-prone to do so because during
`_process_ondelete`, the system would attempt to set to null fields that
were required (i.e. NOT NULL).

This issue is hidden by the fact that ir.model.fields are usually
deleted **before** ir.model.fields.selection and thus the ondelete
action won't be applied on the field with the original definition
(because it has probably been deleted by SQL CASCADE)

X-original-commit: 2e238f1e0202d65a98177939dac166dd659bb39c
2021-03-10 16:45:06 +00:00
Adrian Torres 169d383d88 [IMP] test_lint: add checker for raise in unlink overrides
This commit adds a pylint checker that will check every override of
`BaseModel.unlink` to verify that there are no raise statements within
the body of the method, if that is the case, an error will be raised.

closes odoo/odoo#58517

Related: odoo/enterprise#13557
Signed-off-by: Raphael Collet (rco) <rco@openerp.com>
2020-12-04 09:16:16 +00:00
Adrian Torres 679185f3e8 [IMP] *: replace all raises inside unlink by api.ondelete
With this commit, all instances of errors being raised inside
`BaseModel.unlink` overrides are moved into methods decorated with
`api.ondelete` which is safer.
2020-12-04 09:16:16 +00:00
Adrian Torres 1c8a958098 [IMP] core: introduce api.ondelete decorator
With this commit, a new ORM api decorator is introduced:
`api.ondelete(*, at_uninstall)`.

This decorator is to be applied to Model methods that check for specific
business conditions when attempting to unlink a record via the
interface.

E.g. trying to unlink a validated journal entry

This decorator allows this logic to exist outside the `BaseModel.unlink`
method and is automatically bypassed when in uninstall mode, this means
that during an uninstall any and all data related to a module can and
will be removed easily and cleanly while still being able to apply
business logic to manual deletion of records.

This feature opens the gates to solving a very big problem with
uninstalls: records and tables that remain in a database despite the
relevant module being uninstalled, because if an override to unlink
raises an error, the data will never be deleted from the database.

Henceforth, overrides of unlink shall solely be used for data-cleaning
purposes, i.e. deletion or modification of data that is related to the
one currently being deleted but cannot be automatically deleted because
there are no proper SQL relations.

In certain very specific, low-level scenarios an unlink may be
overridden to raise an error, but this should only be done if you know
what the fuck you're doing, most of the time you'll want to resort to
`@api.ondelete`.

Note that this new decorator includes a keyword-only, required argument
called `at_uninstall`, in most business cases this argument shall be
False as this argument dictates whether or not this method should be
executed in the `unlink` call during uninstall. It should only be set to
True if you are certain of all the implications which most likely means
that the records of the model in which this ondelete function is defined
will NOT be removed during uninstall, they will forever linger in the DB
until manual intervention, this in turn can mean a wide range of
undefined problems due to crap left on the database.

Following commits will replace any `unlink` overrides that raise
business errors by methods decorated with `api.ondelete`, another commit
will introduce a pylint checker that will raise a warning any time that
an unlink override raises an error.
2020-12-04 09:15:51 +00:00
Adrian Torres f66640e3d3 [FIX] payment: properly (re)create journals for providers
This commit fixes two bugs, the first one is a reinstall bug that
happens whenever the modules `payment` or `payment_test` are uninstalled
then reinstalled and the second one is a bug in which for some payment
acquirers, a journal is never created.

For the first bug, the problem is that when uninstalling the
aforementioned modules, the journals linked to each provider are not
deleted (which is ok from a business POV) and thus when reinstalling
said modules we simply recreate new journals, but journals have a
unicity constraint on (name, code, company_id), therefore the
re-creation of these journals will most likely fail.

This bug doesn't happen with other payment_* modules because all *main*
acquirers are defined in the payment module, except for payment_test
that defines its own acquirer (thus the fact that it fails to reinstall
too).

The solution chosen is to try to force the creation of the journals,
even if it may fail, within a try/except block: if it does fail, we
simply do a lookup for matching (name, code, company_id) and assign
whatever we find to the acquirer's journal_id. This approach was chosen
because the most common case is that of an install (journal creation),
so checking for existing journals first would make the most common case
less performant.

For the second bug, the part of the code that creates journals for
providers being installed made a false assumption: To find the acquirers
for which to create journals, it would gather the name of the
acquirer/provider from the module name of the modules being installed
(or that were already installed) and would compare them to existing
acquirers whose provider would match the names extracted from the module
name. However, not all payment_ modules contain the actual name of the
provider in the module name! payment_ingenico is one such case, while
the module name contains ingenico, the technical name for the provider
is 'ogone', meaning that the heuristic would never find an existing
acquirer with provider set to 'ingenico', therefore no journal would be
created for that specific payment provider.

The fix is trivial, payment.acquirer has related fields that point to
the modules that implement each provider, meaning that one can simply
check for all payment.acquirer whose module_state is 'to_install' or
'installed'.

This bug does not happen in v12 because when v12 was released ogone was
still ogone and not ingenico, and all payment acquirers actually
followed the convention of module name == provider name, but for
simplicity's sake we keep this change for v12 too since it's in the same
scope as for the first bug and the code is cleaner anyway.

closes odoo/odoo#62563

X-original-commit: 0b70f188ca441d6cf53b2ef63f4856239bbee8e4
Signed-off-by: Raphael Collet (rco) <rco@openerp.com>
Signed-off-by: Adrian Torres (adt) <adt@odoo.com>
2020-11-27 17:47:17 +00:00
Adrian Torres 49bd732de1 [FIX] core: allow writing to delegated m2o fields on records
Before this commit, it was impossible to write to a delegated m2o field
if the length of the recordset was greater than 1.

The reason was because the special case for delegated fields inside
fields.Many2one.convert_to_cache would check for `record.id` which is a
bit misleading, since record can be greater than one.

The solution is to check whether any of the records are real records
instead, as if all of the records are NewRecords, then the parent is a
NewRecord too.

Fixes #62069

closes odoo/odoo#62343

X-original-commit: 76bf9dc94165c79be873a8864bfdb50a0a636084
Signed-off-by: Adrian Torres (adt) <adt@odoo.com>
Signed-off-by: Raphael Collet (rco) <rco@openerp.com>
2020-11-25 15:48:11 +00:00
Adrian TorresandRaphael Collet 64b95a01d0 [FIX] base: do not prefetch fields to be uninstalled
During the uninstall step of the registry, we perform a commit right
after the uninstallation of all module data, this commit is performed
right before the creation of a new registry, this means that said commit
will call `flush_env` and thus `recompute` on an environment that is
based on a stale Registry (memory and database are unsynchronized).

This means that during this specific moment, there's a chance that the
recompute function may have to fetch some fields it depends on to
perform its computations if said fields are not in cache, this in turn
means that it will try to prefetch fields that are potentially no longer
in the database, making the registry crash and preventing the uninstall.

This is what happened with multiple if not all `payment_*` modules, the
main module `payment` depends on a `ir.module.module` record, most
notably the `color` field of the `payment.acquirer` model which depends
on the `state` field which in turn depends on the
`ir.module.module.state` field.

This meant that uninstalling a module such as `payment_paypal` triggered
a recompute of `payment.acquirer.color` during uninstall, and to perform
that computation we need several `payment.acquirer` fields to be fetched
from cache or the database. If in cache, the uninstall would go through
without a hitch, if not in cache, we would fetch the required fields
from the database but we'd also attempt to prefetch the fields
introduced by `payment_paypal` that had just been deleted from the
database!

To avoid this, the `_module_uninstall_data` method of `ir.model.data`
will henceforth guarantee that `ir.model.fields` that are to-be-deleted
will have their prefetch set to False, meaning only existing fields will
be fetched from the database during the uninstall of extending modules.

Fixes #60424
opw-2372598

closes odoo/odoo#62247

X-original-commit: 7bffd2df0d5d7eff841cdf7ba7cb6ea84a5c5c49
Signed-off-by: Raphael Collet (rco) <rco@openerp.com>
Co-authored-by: Raphael Collet <rco@odoo.com>
2020-11-24 13:42:25 +00:00
Adrian Torres ce83249d97 [IMP] test_lint: check for function-redefined error with pylint
With this commit, pylint will now point out when the same
function/class/method is defined more than once in the same scope which
is a recurring mistake.

closes odoo/odoo#60044

Related: odoo/enterprise#14090
Signed-off-by: Adrian Torres (adt) <adt@odoo.com>
2020-10-16 12:56:52 +00:00
Adrian Torres b7017e58cc [FIX] *: adapt business code to function-redefined error
This commit adapts the business code in which
class/module/function/method redefinition took place so that it no
longer happens and the pylint test passes.
2020-10-16 12:56:52 +00:00
Adrian Torres ece40d83fb [FIX] doc: warn about pitfalls of inverse batching
This commit updates the documentation surrounding compute and inverse
methods for fields, explicitly warning API consumers that grouping
multiple compute fields under a single inverse method is error-prone and
could not work at all (it can still work under specific conditions but
henceforth it shall be considered bad practice).

This case was found at https://github.com/odoo/enterprise/pull/13815/commits/a651413df915f166697d26a83084582207405858

closes odoo/odoo#60115

X-original-commit: 1d21924c1da0bed59cd105a3d9bf9b60fa2d7bc4
Signed-off-by: Raphael Collet (rco) <rco@openerp.com>
Signed-off-by: Adrian Torres (adt) <adt@odoo.com>
2020-10-15 15:05:00 +00:00
Adrian Torres f4bd4f7b60 [FIX] base: allow custom fields to use time module
After commit 06a8c5264eb6e87c29ad1d23a14e12dd45aa281c it is no longer
possible to pass bare modules to `safe_eval`'s context, however during
the aforementioned commit only the wrapped datetime and dateutil modules
were updated in ir_model's SAFE_EVAL_BASE context, thus the bare `time`
module was still being passed (and this triggered a traceback whenever a
custom computed field that used the time module was computed).

The fix is simple: pass the wrapped time module to the `safe_eval`
context instead of the bare one.

This commit also introduces a regression test to verify that the passed
modules actually work in custom fields.

opw-2347711

closes odoo/odoo#59560

X-original-commit: 02e816877ec12471a1446ba894b5cfca309979da
Signed-off-by: Olivier Dony (odo) <odo@openerp.com>
Signed-off-by: Adrian Torres (adt) <adt@odoo.com>
2020-10-08 18:41:08 +00:00
Adrian Torres 7a82891c59 [FIX] base_import_module: avoid accessing deleted columns
Commit 59eee6ba5f introduced a cleanup
mechanism for external data modules being uninstalled from a database,
this meant deleting the ir.module.module entry created when first
installing the aforementioned external module.

However, the above patch has one small oversight: the access of the
`imported` field introduced by the `base_import_module` module is done
after the `super().module_uninstall()` call which, in the case of the
uninstall of the `base_import_module` module, will delete the `imported`
column and the following call to filtered will fail because the column
has already been deleted and the registry hasn't been reloaded yet.

The solution to this, as explained in the code comment, is to simply
compute the `modules_to_delete` before the call to `module_uninstall()`.

closes odoo/odoo#59418

X-original-commit: c0226087b82ffda3971bb953d8d00b8c5e67642c
Signed-off-by: Raphael Collet (rco) <rco@openerp.com>
Signed-off-by: Adrian Torres (adt) <adt@odoo.com>
2020-10-07 13:25:32 +00:00
Adrian Torres 811d0a994c [FIX] test_convert, core: correct accesses to Exception.message
The attribute Exception.message is no longer available in Python 3, and
newer versions of pylint check for this and raise an error during
test_lint, with this commit we take care of these codesites in the
following manner:

* For test_convert, the access was in a function that is effectively
    dead code, and thus it has been removed.

With this, we can upgrade pylint to the newest version and fix a
longstanding bug with MRO building in pylint.

closes odoo/odoo#57921

X-original-commit: 6a989445fdda811f854003a62fd564061d088963
Signed-off-by: Adrian Torres (adt) <adt@odoo.com>
2020-09-17 08:56:06 +00:00
Adrian Torres b532e65206 [FIX] base: properly handle orphan xmlids
Before this commit:
    * Add a field with studio to any view of module A
    * Note that the model has to be unique to module A
    * Uninstall module A
    * Try to uninstall the `studio_customizations` module
    -> MissingError, impossible to uninstall `studio_customizations`

This happens because when uninstalling module A, all `ir.model.fields`
for models of said module are deleted in cascade by PostgreSQL when
unlinking the module's `ir.model`, this means that the ORM has no way of
knowing exactly which `ir.model.fields` where deleted and which
corresponding `ir.model.data` should be deleted, so the `ir.model.data`
remain in the database as orphans (that can, and will be cleaned up
later).

However since #34435 the mechanic that avoids the removal of
LOG_ACCESS_COLUMNS performs field access on records that may not exist.

This commit solves this by simply making sure that all records exist
before performing any checks that may require field access (and thus can
trigger a MissingError if the record does not exist).

opw-2316973

closes odoo/odoo#56285

X-original-commit: 08e662823ccf7dbe4d497f3f487287cba285a852
Signed-off-by: Raphael Collet (rco) <rco@openerp.com>
2020-08-21 11:10:50 +00:00
Adrian Torres 786598da02 [IMP] *: avoid overrides of res.config.settings.execute
Execute shouldn't really be overriden because it handles a very touchy
case: module (un)installs, if it is poorly overriden it can create
registry inconsistencies and can make databases crash in really dumb
ways, since the module operations *need* to be performed at the end of
the transaction (last).

Most of the time, an override of set_values() does the same job and is
much safer.

This commit updates the `res.config.settings.execute()` documentation to
reflect this.

closes odoo/odoo#56032

Signed-off-by: Raphael Collet (rco) <rco@openerp.com>
2020-08-21 08:22:57 +00:00
Adrian Torres 422ca9563e [FIX] core: apply post-constraints only if necessary
This is a followup of commit bc2bb5e03c2b32d4ee1b0597ea5889c17d2b0e0e

When a module is updated, a constraint application may (temporarily)
fail because the existing data does not respect the constraint, this is
OK and can be fixed through hooks/migration scripts and was handled by
the aforementioned commit.

However when updating multiple modules, it is possible that an
inheriting module will try to re-apply the failed constraint and
succeed, if that is the case, when processing the `post_constraints` an
already-existing constraint will be applied and raise an error.

To fix this, a check is made before trying to apply the constraint, to
verify that it is not already in _constraint_queue, if it is not, then
we may attempt to apply it, if it is in the queue, then we may safely
ignore it as it will be applied further down the registry cycle.

closes odoo/odoo#55725

X-original-commit: 5225b9ce5178302af05b63029fb184e27b781815
Signed-off-by: Adrian Torres (adt) <adt@odoo.com>
2020-08-11 08:34:37 +00:00
Adrian Torres 9fefa08745 [FIX] core: mapped() does not prefetch as expected
Before this commit, calling `mapped()` on a relational field of a single
record inside a for loop does not correctly propagate the prefetch ids
from the bigger recordset down to the record inside the loop:

    [rec.line_ids.mapped('name') for rec in recs]

To be more precise, `recs` correctly propagates prefetching information
down to `rec.line_ids`, but method `mapped()` does not pass it along to
the record that triggers the prefetching.

This resulted in one query per record in `recs`, so if `recs` were a
1000 records recordset, at least 1000 queries would be necessary.

With this commit, the `mapped()` function correctly propagates the
`_prefetch_ids` of the larger recordset (`rec.line_ids`) so that the
prefetching works properly and the 1000 queries are brought down to 1.

This commit is a followup on #42611.

closes odoo/odoo#54565

X-original-commit: 0e97053fee36c0c76b70cb94be5340c3144b178b
Signed-off-by: Raphael Collet (rco) <rco@openerp.com>
Signed-off-by: Adrian Torres (adt) <adt@odoo.com>
2020-07-16 11:09:05 +00:00
Adrian Torres ce4fcebbbb [FIX] orm: don't log errors on constraint failure unless necessary
Given an scenario in which a field F of model M is defined in module X
as `required=True` and is extended by another module Y as
`required=False` in a database with data not satisfying the original
constraint:

During an upgrade of base the original constraint will be re-applied
on the data (and fail) even though it is no longer necessary because
module Y relaxes the NOT NULL constraint.

This failure in and of itself is non-blocking, the upgrade will go
through but an error and a warning are logged anyway which are not
problematic either except in the case of automated testing
infrastructure (such as runbot), because of this it would be best if
these errors would not be logged at all unless we're 100% sure that the
constraint that was applied is not relaxed downstream.

With this commit, the `finalize_constraints` method will verify that the
constraint is applicable (field is required) before re-applying the NOT
NULL constraint.

opw-2269220

closes odoo/odoo#53529

X-original-commit: f09f4826fbdd1a547503c51a3cfad71285312c49
Signed-off-by: Raphael Collet (rco) <rco@openerp.com>
2020-06-23 15:33:04 +00:00
Adrian Torres 0ab6b20717 [FIX] base: return super().unlink() for ir.ui.view.unlink()
Commit bca2926b01 introduced a fix for
ir.ui.view unlinks during uninstall but forgot to return the result of
the super() call to unlink which broke behaviour downstream.

This commit restores the proper behavior of returning the result of the
call to super.

Closes #52364

closes odoo/odoo#52435

X-original-commit: a3d91cc8691794d8f93ef416830b09fabcf08b66
Signed-off-by: Adrian Torres (adt) <adt@odoo.com>
2020-06-04 13:33:24 +00:00
Adrian Torres bdd87051f6 [FIX] base: allow removal of selection fields during dev
Let field F of model M be a Selection field.

Before this commit, creating a database with field F, removing/renaming
field F by changing its definition within the code then upgrading the
database to reflect the changes would result in a registry crash because
the field would still exist in the database but not in the ORM (memory),
thus trying to fetch the field in DB from the memory would result in a
KeyError.

This is due to the fact that field renames / removals are not stable
operations and should be handled with a migration script when upgrading
an existing database, nevertheless this can be quite an annoying
behavior while developing which is why this commit simply skips the
processing of the field if it does not exist in memory.

The ORM already foresees these cases and logs a warning to the developer
saying that the field was deleted anyway but that it was only a
partial delete and that it should be handled in a migration script in
order for the change to be production ready.

closes odoo/odoo#51099

X-original-commit: a54fecc35386f8e00018ff3d858b383d43f1dded
Signed-off-by: Raphael Collet (rco) <rco@openerp.com>
Signed-off-by: Adrian Torres (adt) <adt@odoo.com>
2020-05-12 12:24:46 +00:00
Adrian Torres ca91e13dee [IMP] testing: forbid module operations during testing
With this commit, module operations such as install, upgrades and
uninstalls are henceforth forbidden inside unit tests and instead such
tests must be performed with standalone Odoo scripts.

This is done because module operations during tests are not
transactional, this can leave the registry in an unclean state and
further tests may be affected by this, it also creates a new registry
which complicates registry cleanup if anything crashes,
because the registry to be cleaned up is not the same one that crashed.

Instead, what should be done is a script that imports odoo as a library,
and loads the database necessary then performs whichever operations
necessary. This script should contain a single function with a single
parameter (env) and should be decorated with
@odoo.tests.common.standalone in order to be executed properly, this
decorator accepts any amount of positional parameters as tags that can
be specified when calling the script in order to execute only a select
subset of scripts.

Special tags are: 'all' and <module_name>, these are generated
automatically, the first will execute ALL scripts available whereas
<module_name> will execute all scripts introduced by said module.

When calling the test_module_operations script, only scripts found in
*installed* modules will be executed, script discovery is only possible
if the code is loaded therefore it is only possible if the module is
installed.

closes odoo/odoo#49669

Signed-off-by: Adrian Torres (adt) <adt@odoo.com>
2020-04-22 07:45:53 +00:00
Adrian Torres 2cb77eb104 [FIX] http: do not redirect to database manager on registry crash
Before this commit if the loading of the registry failed because of an
AttributeError or a psycopg2 error the http dispatcher would redirect
the user to the database manager.

This can be problematic because integrators (e.g. odoo.sh) may choose to
disable / forbid access to the database manager, and when the registry
crashes because of e.g. a migration, the real error will be overshadowed
by an AccessDenied error or somesuch depending on the path taken to
forbid access to the database manager.

With this commit, the real exception is simply reraised

closes odoo/odoo#49238

X-original-commit: de4e67dcc52916337251370387aea6aea893a60e
Signed-off-by: Raphael Collet (rco) <rco@openerp.com>
2020-04-08 13:22:34 +00:00
Adrian Torres 00c82d3121 [FIX] account: ensure cleanup of registry after test_all_l10n
Followup of #48896, ensures that the registry is correctly cleaned up
after the installation of the different l10n_* modules and that the
subsequent setup_models is done with the correct registry

closes odoo/odoo#49197

X-original-commit: bfd6bdcd89724f04442bbe07307177c523bed5f0
Signed-off-by: Raphael Collet (rco) <rco@openerp.com>
2020-04-08 09:11:24 +00:00
Adrian Torres 24b3cf63ff [FIX] crm: make crm.team.write work in batch again
When `_synchronize_alias` was introduced into crm.team.write, the write
method become incompatible with batch writes because
`_synchronize_alias` assumes `self` is a recordset of length 1 and
performs attribute accesses directly on self, this makes the __get__
crash because it performs a `self.ensure_one()`

With this commit, crm.team.write will iterate over the self recordset
and call `_synchronize_alias` on every record of the recordset.

This also solves a uninstallation problem in sale_crm because a write is
performed on crm.team in an uninstall_hook.

closes odoo/odoo#49088

X-original-commit: a9511cd42be365e58371d717b8197aac644eaf33
Signed-off-by: Raphael Collet (rco) <rco@openerp.com>
Signed-off-by: Adrian Torres (adt) <adt@odoo.com>
2020-04-06 16:18:02 +00:00
Adrian Torres 0cb6f111b8 [FIX] payment_*: reset payment acquirers to default on uninstall
The `payment` module introduces a certain amount of payment acquirers,
each one corresponding to a `payment_` module.

When a `payment_` module is installed, this data is updated so that
payments done with the corresponding acquirer change in behaviour using
the provider installed by the `payment_` module.

When a `payment_` module is uninstalled, this data should be reset to
default, more especifically the `view_template_id` and the `provider`
fields of `payment.acquirer`.

This was not possible before this commit, and more importantly it would
make the uninstallation of such `payment_` module impossible as the
`view_template_id` is a required m2o ondelete='set null', which will
make the registry crash. Even if the former wasn't a problem, the
provider field would remain set to a non-existing selection option,
which would make the registry crash (eventually, when checking a record
with such a selection option).

With this commit, we reset these fields to their default value upon
module uninstall.

In 13, the issue with `view_template_id` should be fixed, as required
m2o that are ondelete='set null' are no longer possible. As for the
provider Selection field, a fix should arrive in master soon.

opw-2225333

closes odoo/odoo#48916

X-original-commit: 4f0c1c1bfd71dd1ff6793d0a91b49984c54d1351
Signed-off-by: Raphael Collet (rco) <rco@openerp.com>
Signed-off-by: Adrian Torres (adt) <adt@odoo.com>
2020-04-02 21:47:21 +00:00
Adrian Torres 5952928b42 [REM] *: remove various unused import shims
Before this commit, a lot of leftover import shims existed in the
codebase for py2-py3 compatibility, these are no longer needed since
Odoo 13.0+ doesn't support Python 2 anymore and is (finally) in EOL.

With this commit, these shims are dropped, making the code cleaner,
easier to read and with one less dependency.

Queue -> queue -> py2-py3 compatibility
xmlrpclib -> xmlrpc.client -> py2-py3 compatibility
ConfigParser -> configparser -> py2-py3 compatibility
itertools.izip_longest -> itertools.zip_longest -> py2-py3 compatibility
urllib -> urllib.request -> py2-py3 compatibility
__builtins__ -> builtins -> py2-py3 compatibility
_winreg -> winreg -> py2-py3 compatibility

mock -> unittest.mock -> merged into CPython

The debian/fedora packages and requirements.txt have been updated accordingly

closes odoo/odoo#44601

Related: odoo/enterprise#8141
Signed-off-by: Xavier Morel (xmo) <xmo@odoo.com>
2020-04-01 12:45:40 +00:00
Adrian Torres 1daf8eb127 [FIX] *: set ondelete policy of required Selection fields
With this commit, Selection fields with `required=True` which are
extended via `selection_add` are given proper ondelete policies to
ensure the cleanup of records containing these extended options during
uninstall of the extending module.

This commit also cleans up leftover uninstall hooks that were being used
to handle the same set of problems prior to the ondelete mechanism being
implemented for Selection fields.

closes odoo/odoo#46325

Related: odoo/enterprise#9117
Signed-off-by: Raphael Collet (rco) <rco@openerp.com>
2020-03-30 13:42:04 +00:00
Adrian Torres f0481392c6 [IMP] core: introduce mechanism for selection_add cleanup
- Let A and B be two different modules.
- A defines a required Selection field F of model M and B extends
  it through the `selection_add` argument.
- Create records of model M and have some of them have any of the
  options introduced by module B selected for field F.
- Uninstall module B.

The result will be records of Model M with an option for field F that no
longer exists, this makes the registry inconsistent and prone to
crashing (it is sufficient to access the form view of such a record to
trigger a crash).

This commit introduces a mechanism similar to the `ondelete` argument
found in Many2one fields, the argument name is the same but it's
different both in behaviour and in implementation.

The `ondelete` mechanism for Selection fields is enforced for **any**
Selection field with required set to `True`, this means that the
developer is required to set a cleanup behaviour for when their module
is uninstalled. For possible cleanup options, see fields.Selection's
docstring.

As far as implementation goes, everything is implemented in Python
unlike with Many2one fields where the behaviour is delegated to
PostgreSQL.

The `ondelete` setting will be processed during
`ir.model.fields.selection.unlink()` to ensure that the registry is left
in an appropriate state after module uninstall.
2020-03-30 13:42:04 +00:00
Adrian Torres 96223568c4 [FIX] core: invalidate registry at the start of setup_models
This is necessary in order to create models on-the-fly within tests and
to test improper Model / Field creation: registry.reset_changes will
only perform the reset of the registry if it is invalidated, however if
the setup of models crashes before it is invalidated (as is the case of
a test), the reset_changes will not be triggered and thus the registry
will be left dirty for subsequent tests.
2020-03-30 13:42:04 +00:00
Adrian Torres 975ba27723 [FIX] core: enable logger.runbot() when importing odoo as a library
Before this commit, trying to use Odoo as a library without initializing
the logging features would result in a crash because `Logger.runbot()`
is monkey-patched inside the function `init_logger()`, which is de facto
necessary for using Odoo as a lib.

With this commit, we do the monkey-patch at the module-level so whenever
the `netsvc` package is imported the patch is applied, which should be
well before the loading of the registry starts.

closes odoo/odoo#48396

Signed-off-by: Raphael Collet (rco) <rco@openerp.com>
2020-03-26 09:25:15 +00:00
Adrian Torres 32fcc1b883 [REM] base: deprecate resolve_2many_commands
With the introduction of `new`, the use of `resolve_2many_commands` is
redundant, as it accomplishes the same function with the added benefit
of returning a record-like object whose API is more familiar than
`resolve_2many_commands` API.

closes odoo/odoo#37290

Related: odoo/enterprise#9409
Signed-off-by: Raphael Collet (rco) <rco@openerp.com>
2020-03-23 15:14:11 +00:00
Adrian Torres 9befa3d245 [FIX] *: adapt code for resolve_2many_commands removal
This code adapts all business code instances of calls to
`resolve_2many_commands` and replaces them by calls to `new` which
returns a record-like object whose api is more familiar than the
`resolve_2many_commands` api.
2020-03-23 15:14:10 +00:00
Adrian Torres 02327d8a6b [IMP] base: warn user about the consequences of uninstall
This commit adds a warning banner near the bottom of the uninstall
wizard warning the user of the consequences of module uninstallation and
advising him to try it first on a duplicate database so as to avoid
borking the database.

The Cancel/Confirm buttons were also swapped and the Cancel one was
made primary to avoid mindless "next-clicking"

closes odoo/odoo#33396

Task-id: 1824392
Signed-off-by: Raphael Collet (rco) <rco@openerp.com>
2020-03-23 12:26:56 +00:00
Adrian TorresandRaphael Collet 0453566a76 [ADD] tests: add a script to test module uninstallation
With this commit, a new script "tests-uninstalls.py" is added in order to
test module uninstallation.

This script is a kind of standalone tool. It uses odoo as a library but
the odoo server is not started at all.

In its standard invocation, it tries an install/uninstall/reinstall
cycle for each all module found in the specified database.

By specifying '-U', it only tries to uninstall the comma separated list
of modules following the argument.

Be aware that this tool, will alter the database against which it was
invoked.

Co-authored-by: Raphael Collet <rco@odoo.com>
2020-03-02 16:07:06 +00:00
Adrian Torres 9493f23977 [FIX] core: do not nag about dependencies for modules to remove
Before this commit:
    - Install a module
    - Uninstall the previously installed module
    - The registry will complain that some dependencies may be missing
        for the module being uninstalled

This happens because we check after the installation / upgrade of
modules that none have been left in a transient state to verify that new
dependencies have been properly installed and loaded, this applies to
'to install' and 'to upgrade' states however it's not the same for 'to
remove' states, as the process of uninstall happens much later in the
code.

After this commit, simply uninstalling modules will not trigger this
error log.

Do note that in case of a problem with an uninstall, the function
"reset_module_states" will tackle the case of leftover transient states.

closes odoo/odoo#47495

X-original-commit: c095a28314f78d1d9854e5e9bcb26014922a4f12
Signed-off-by: Xavier Dollé (xdo) <xdo@odoo.com>
2020-03-12 12:26:58 +00:00
Adrian Torres cd4b50de6f [FIX] base: don't uninstall already uninstalled modules
Before this commit:
    - Install some random module that can be uninstalled (so not base)
    - Open the uninstall wizard for said module in two different tabs /
        windows / whatever
    - In one tab, confirm the module uninstall and wait for it to be
        done
    - As soon as the other tab is done with the uninstall, go to the
        second one with the uninstall wizard still open, and proceed
        with the second uninstall
    - Boom, the registry crashes and completely fucks up the DB because
        there's no check at all that prevents the uninstall of already
        uninstalled modules.

After this commit:
    - `ir.module.module.button_uninstall` will check if all the
        modules being uninstalled are in the installed state
        and if not a UserError will be raised, preventing a second
        uninstall of the module which could potentially break the DB

Do note that this fix is LOCAL, the problem is however more or less
global, wherever there's user-actionable buttons that should only be
pressed once there's a potential for bugs / breakage if a similar fix is
not implemented locally. Perhaps a more global fix should be implemented
eventually, but it's generally less annoying for business cases since
those probably won't break the registry, see task 1859014.

opw-2213679
opw-2212594
opw-2206446

closes odoo/odoo#47450

X-original-commit: 8c1bb22ec0222dca652e0649454b986c06cb1368
Signed-off-by: Adrian Torres (adt) <adt@odoo.com>
2020-03-11 18:52:52 +00:00
Adrian Torres 26680badef [FIX] base: handle custom non-stored m2m
Before this commit, one could create a custom, non-stored m2m field
through the web interface BUT the deletion of the custom field would be
impossible after saving the form.

This is due to the fact that the relation would not be stored in DB
since the field is store=False, therefore the value of the relation
would equal to None, and since the unlinking mechanism assumes that
the value is never None, it tries to drop a table of name None, which
does not exist.

closes odoo/odoo#46909

X-original-commit: cdd1439a380810569db80d984c8cb80d213b1bf3
Signed-off-by: Adrian Torres (adt) <adt@odoo.com>
2020-03-04 15:39:18 +00:00
Adrian Torres b08729267b [FIX] stock: reset product.template records to default type
Stock introduces the option 'product' for the field `type` of the
`product.template` model.

If there exists records for this model with the aforementioned option
selected when the stock module is uninstalled, the records will remain
in the database for an indefinite amount of time, all while pointing to
an option that does no longer exist (except if the record was created by
the stock module itself), making the registry inconsistent and
eventually leading to a crash.

This is a known limitation of the ORM regarding Selection fields and
more specifically the `selection_add` mechanism, no "generic" solution
has been chosen thus far because it is not always clear which approach
should be taken:

    1) Delete the record?
    2) Set the option to a fallback, base option?
    3) Something else handled by the module itself?
    ...

In this case the second approach has been chosen and whenever the module
stock is uninstalled, all remaining product.template records of `type`
'product' will be reset to the default option defined by the field,
which as of this commit is 'consu'.

See opw#2193814

closes odoo/odoo#45341

X-original-commit: a84b2829c5fac9e2b0d8cb49a4e7aa73f7e0e2b2
Signed-off-by: Adrian Torres (adt) <adt@odoo.com>
2020-02-13 17:13:03 +00:00
Adrian Torres 9b0e976b9f [IMP] base: raise warning if no field in search view
Before this commit, it was possible (and common) to create search views
without fields and only filters, the consequence was that a search for
that particular search view was not possible. (See
https://github.com/odoo/enterprise/pull/7852)

With this commit, a warning is raised if no field is defined within a
search view, preventing these "common mistakes" from happening again.

closes odoo/odoo#45058

Task-id: 2179521
Signed-off-by: Raphael Collet (rco) <rco@openerp.com>
2020-02-12 13:44:07 +00:00
Adrian Torres a372561497 [FIX] crm: specify at least one field in search views
Without at least one field, performing a search in these models' search
view is outright impossible.
2020-02-12 13:44:00 +00:00
Adrian Torres 97a5093117 [FIX] *: specify at least one field in search views
Without at least one field, performing a search in these models' search
view is outright impossible.

closes odoo/odoo#45177

X-original-commit: 0aabfbff3ce5d68bf37e1734ba7aa43f62803f3a
Related: odoo/enterprise#8379
Signed-off-by: Nicolas Martinelli (nim) <nim@odoo.com>
2020-02-12 11:09:05 +00:00
Adrian Torres f61262eb08 [FIX] core: delay constraint application in case of upgrade
closes odoo/odoo#44800

Co-authored-with: Xavier Dollé <xdo@odoo.com>
X-original-commit: bc2bb5e03c2b32d4ee1b0597ea5889c17d2b0e0e
Signed-off-by: Xavier Dollé (xdo) <xdo@odoo.com>
2020-02-06 18:28:29 +00:00
Adrian Torres 1d13928764 [IMP] core: improve mapped and filtered performance
Previously, mapped was following a very naive approach, which was simply
calling the field name passed as input for every record in a recordset,
sequentially.

The problem with this approach is that we will potentially recompute the
same fields multiple times for differents records, when this could be
done once per field for ALL records, and store this value in cache for
further access.

Another potential problem is that we don't take advantage of the ORM's
prefetching to fetch all the records that are not in cache at once,
instead of doing the same query for every record in the recordset.

Yet another problem is the conversion of each cache value to a record
format and then combining all of the individual records into a single
recordset, which, depending on the size of the recordset, can take an
unbelievable amount of CPU time.

With this new implementation of `mapped()` we take care of all of these
problems:

This is done by first delegating `mapped()` from the model to the field,
this mapped takes a recordset as input and it will try to batch compute
and prefetch as much as possible for the entire recordset, but it will
not keep these values for the actual output, it just stores everything
in cache and then at the end, retrieves everything from the cache to
guarantee the same order.

After the mapped, the conversion from cache format to record format is
delegated to the new `convert_to_record_multi` which will fetch all the
ids and then perform a single browse to encapsulate all of the records
into a single recordset with the least amount of overhead possible.

Part of Task 2170344

closes odoo/odoo#42611

Signed-off-by: Raphael Collet (rco) <rco@openerp.com>
2020-01-21 14:06:50 +00:00
Adrian Torres be01db2f71 [IMP] api: move cache_key to the Environment and cache it
Computing the `cache_key` turned out to be a big factor during the
lifespan of a `BaseModel.mapped` call and a lot of this time is spent
computing the same `cache_key` over and over.

These unnecessary computations can be easily reduced to a couple by
moving the `cache_key` method on the environment (instead of the field)
and by implementing a memo for that method.  The rationale is that the
`cache_key` of a field does not change for a given environment.

The result of this patch is up to 50% faster `Field.__get__` which in
turn means a GLOBAL gain in performance, especially for methods /
functions that rely heavily on `__get__` such as `BaseModel.mapped`.

closes odoo/odoo#42674

Signed-off-by: Adrian Torres (adt) <adt@odoo.com>
2020-01-21 08:28:36 +00:00
Adrian Torres 8645e31863 [FIX] website_sale: do not pollute the environment
Instead of polluting an existing environment, use the method that was
intended for changing the company `with_company` which returns a brand
new environment and leaves the existing one unchanged.
2020-01-21 08:28:36 +00:00
Adrian Torres ee29cb9147 [FIX] tests: make --test-file great again
Sort of but not really, this commit fixes a special case in which
launching a --test-file of a file with at least two SavepointCases would
create a postgresql deadlock and it would be impossible to terminate the
Odoo process without sending a SIGKILL or waiting for the lock to
timeout.

This was introduced at #39368 and happens because of the way that
unittests unwraps suites, to keep it short, when it unwraps the custom
OdooSuite class internally, it ends up with a vanilla TestSuite with
which to run the different test cases, and since #39368 depends on the
overrides added to OdooSuite to function, the class cleanups are not
triggered at the end of a test class (rollback, cache cleanups, env
reset, registry reset, etc.).

The fix is to manually unwrap the suite of tests to keep OdooSuite as
the suite with which to call the tests, which was already done for
--test-enable (although for different reasons, --test-tags?) which is
why --test-enable didn't have any problems.

This commit also fixes a typo I found on the backport, which meant
classCleanups were not being executed if the setUpClass failed, but it
had no effect on classCleanups during tearDownClass.

Task-ID 2160398
Depends on #43135

closes odoo/odoo#43296

X-original-commit: 7a5ded7d40afc29043d356b5dece0dbe1fbd5ab3
Signed-off-by: Adrian Torres (adt) <adt@odoo.com>
2020-01-14 16:26:50 +00:00
Adrian Torres f9eeb3b44e [FIX] base: restrict m2o with ir models as inverses
Before this revision, any m2o with an ir.* model as inverse could have
the ondelete policy as 'restrict', either by default if it was required
or by setting it explicitly in the field declaration.

This is problematic because ir.* models are reflection models and they
*MUST* be deleted during an uninstall of the module that introduces
them, otherwise tables, fields, constraints, etc. are left in the
database, this is why 'restrict' doesn't make sense UNLESS the unlink is
being performed by an user and not by the system (uninstall).

As a solution, this revision sets the default to cascade if the field is
required, otherwise null.

If the programmer explicitly sets a required m2o field with an ir.*
model as an inverse to ondelete='restrict', it is considered an
unsupported use-case and the registry will crash during loading with a
clear error explaining that it is not supported.

closes odoo/odoo#39739

Signed-off-by: Adrian Torres (adt) <adt@odoo.com>
2019-11-07 08:39:17 +00:00
Adrian Torres 3da477dfc5 [FIX] *: adapt code to comply with new ondelete policy
The ondelete policy must be explicitly set for m2o fields whose inverse
is a reflection model (ir.*)
2019-11-07 08:39:17 +00:00
Adrian Torres e0b5a0cdb6 [IMP] tests: use addCleanup and addClassCleanup where useful
This commit takes advantages of the features added in the parent commit
to have better/cleaner tests.

closes odoo/odoo#39368

Signed-off-by: Adrian Torres (adt) <adt@odoo.com>
2019-11-07 08:30:59 +00:00
Adrian Torres ec587297eb [IMP] tests: partially backport classCleanups from CPython 3.8
This commit partially backports bpo-24412, which allows the definition of
class cleanups (addClassCleanup) and module cleanups (omitted),
similar to instance cleanups (addCleanup).

This is useful for tests that override unittest's setUpClass and
could crash during its execution: If this happens, it is possible that a
bunch of crap is left in the database or even worse, the cursor becomes
completely fucked; Thanks to the addClassCleanup, we can undo the damage
done by the setUpClass.

Another benefit is that it is called unconditionally after tearDownClass
is called, so it can also be called as a replacement and/or safer
tearDownClass.
2019-11-06 14:07:04 +00:00
Adrian Torres 13ee8a0373 [FIX] stock: allow unlinking records in uninstall mode
Business cases should not prevent the unlink of module-related records
during an uninstall.

This commit allows the reinstallation of sale_stock with demo data and
also allows the proper removal of tables and records if a db has any
non-draft inventory adjustments.

closes odoo/odoo#39776

X-original-commit: cedafe00941b869d284cd511633d7da15868fc0a
Signed-off-by: Adrian Torres (adt) <adt@odoo.com>
2019-11-04 17:16:38 +00:00
Adrian Torres 7affe69117 [FIX] base: avoid triggering recomputation when deleted selection
In 6b048a8b8 most issue with trigger causing error when uninstalling a
module were solved.

But there was a particular case if the current uninstallation of modules
there was an update of `ir.model.fields.selection` to be removed. The
unlink of `ir.model.fields.selection` would cause a `setup_models` call
that would add all removed triggers back and possibly down the line
cause an trigger recomputation error.

With this changeset, we do as is already done for ir.model.fields and do
not re-initialize the registry during module uninstallation.

opw-2098915
closes #39796

closes odoo/odoo#39824

X-original-commit: 0ccf2ab8e7f3edabb2d3d4c17a20829de21cca49
Signed-off-by: Adrian Torres (adt) <adt@odoo.com>
2019-11-05 18:43:26 +00:00
Adrian Torresandmreficent 73b18b30b5 [FIX] base: don't crash during CRUD if record does not exist
- Let a model M define a Many2oneReference field F1.
- Let a model N define a One2many field F2 whose inverse is M->F1
- Define a computed field N->F3 that depends on N->F2

If, for whatever reason, a recordset of M contains records that have
been unlinked already and we try to unlink them again, the system will
crash with a MissingException error.

This happened because, while most _modified_trigger cases cover the case
of a MissingException (i.e. record not in cache), the case for a
Many2oneReference didn't.

This is fixed by simply ignoring these "stale" records in the
_modified_triggers section for Many2oneReference fields.

closes odoo/odoo#38816

X-original-commit: f3b05032f0576befda4dca870718afd429c21b0f
Signed-off-by: Adrian Torres (adt) <adt@odoo.com>
Co-authored-by: mreficent <miquel.raich@eficent.com>
2019-10-15 14:33:07 +00:00
Adrian Torres af46a5c5a4 [DOC] api: warn about onchange pitfalls
closes odoo/odoo#37836

X-original-commit: a8454381ad2151136ee2a49665f69a12b8dc7daf
Signed-off-by: Adrian Torres (adt) <adt@odoo.com>
2019-10-02 17:45:13 +00:00
Adrian Torres 957c78b98f [IMP] base: optimize create on models with property fields
Before this commit, creating a record of a model with at least one
property/company_dependent field would trigger the field's inverse
method regardless of whether an actual value was being passed in for the
field or not.

This means that if no value was given or if the value was the same
as the default value, we would waste precious time in the property
field's inverse method.

With this commit, we do not call the inverse method if:

1) there is no value for it in the vals dict (use default)
2) the value in the vals dict is the same as the default value

As an example, when creating a res.partner record while having account
installed (which introduces a property field in res.partner), the
creation time goes from 24ms to 14ms.

closes odoo/odoo#36267

Signed-off-by: Raphael Collet (rco) <rco@openerp.com>
2019-08-30 16:16:17 +00:00
Adrian Torres 235a9da5db [FIX] base: oversight of 547118d76f
The aforementioned commit prevented the creation of automatic xmlids for
custom fields, however this change is not exactly stable; if a database
already contains automatic xmlids for custom fields and the module to
which these xmlids belong to is upgraded, the xmlids would get deleted,
along with any records related to it because of the way that
`_update_xmlids` works.

This commit leaves existing automatic xmlids of custom fields while
preventing the creation of *new* ones.

closes odoo/odoo#34563

Signed-off-by: Christophe Simonis <chs@odoo.com>
2019-07-04 06:47:23 +00:00
Adrian Torres 46d12675a7 [FIX] tests: allow False as value for required booleans in SSF
A required boolean accepts two values, True and False, however the SSF
and the web-client assume False to be equal to NULL and treat them
interchangeably.

In the SSF, we verify that a required field is filled by checking that
its value is different from False, however False is a valid value for a
boolean, this means that setting a required boolean to False would never
work in the SSF.

This commit overcomes this issue by simply skipping the check for fields
of type boolean.

closes odoo/odoo#34729

Signed-off-by: Xavier Morel (xmo) <xmo@odoo.com>
2019-07-10 08:58:27 +00:00
Adrian Torres 547118d76f [FIX] base: do not generate auto xmlid for custom fields
For custom fields, the _module attribute is False, this means that
during the reflection of the model, the custom field's xmlid would be
set up as being introduced in the module of its model's original
definition, which is wrong (because it has no module).

E.g. field x_foo introduced in my_mod by extending model bom from
mrp, the resulting xmlid would be `field_mrp_bom__x_foo` when in reality
it should be `field_my_mod_bom__x_foo`, however this patch simply
disables the feature for custom fields as there is little to no use for
them to have an automatic xmlid.

Task-ID: 2025151

closes odoo/odoo#34265

Signed-off-by: Raphael Collet (rco) <rco@openerp.com>
2019-06-20 09:14:51 +00:00
Adrian Torres 73235ad6ae [FIX] purchase: truncate description for stock.move.line
Before this commit, when creating a PO line with a description that was long
enough (~2712 bytes long) and then confirming the PO, a traceback would
pop up with an error from postgres "index row size exceeds maximum".

This is because when confirming a PO, a stock.move is created with a
stock.move.line mirroring the purchase.order.line, however the "name"
field on stock.move.line is indexed (whereas the purchase.order.line is
not), thus triggering the aforementioned error.

This commit solves this issue by truncating the name field on the
stock.move.line to 2000 bytes (for simplicity's sake) upon creation.

Fixes #33549

closes odoo/odoo#33607

Signed-off-by: Raphael Collet (rco) <rco@openerp.com>
2019-05-23 11:34:07 +00:00
Adrian Torres 2971bf6100 [MOV] core: put things where they belong
This commit moves some ir_ui_view specific functions into ir_ui_view.py
and removes some old-api <-> new-api compatibility shims as well as
removes orm.py since it has 0 to do with the Odoo ORM.

closes odoo/odoo#34826

Signed-off-by: Martin Trigaux (mat) <mat@odoo.com>
2019-07-26 09:07:19 +00:00
Adrian Torres bc99500799 [FIX] base: fix oversight
The bug triggers during an upgrade of base (tested while migrating from
12.0 to master).

Introduced at 074074e570

closes odoo/odoo#35153

Signed-off-by: Martin Trigaux (mat) <mat@odoo.com>
2019-07-24 14:52:03 +00:00
Adrian Torres a42ea0195d [IMP] models: remove support for _constraints
Using api.constrains should be used instead

Simply leave a warning in case a model uses a non-empty attribute
`_constraints`.

closes odoo/odoo#34679

Signed-off-by: Martin Trigaux (mat) <mat@odoo.com>
2019-07-18 07:46:26 +00:00
Adrian Torres a8767716cf [REM] core: remove @api.multi
kw_multi is the default API, so the decorator is no longer necessary and
hasn't been for a long time
2019-07-17 14:13:12 +02:00
Adrian Torres 4b38cc6590 [REM] *: calls to @api.multi
Multi is the default api for methods, it is not necessary to explicitly
decorate methods with it, adds clutter and most people use it because
they see that the rest of the code uses it.

Done with `find . -type f -name '*.py' | xargs sed -i '/@api.multi/d'`
2019-07-17 14:13:12 +02: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
Adrian Torres 407f1f6cdb [REM] api: remove helpers one and aggregate
`api.one` has been deprecated since v9 because it often makes the code
less clear and behaves in ways developers and readers may not expect
since functions decorated with it usually expect a `list` of record(s)
instead of the recordset, whereas most modern Odoo code expects `self`
to be a recordset.

The function `aggregate` was solely being used by `api.one` thus it has
also been removed since there doesn't seem to be any other use for it
thus far.

closes odoo/odoo#34555

Signed-off-by: Raphael Collet (rco) <rco@openerp.com>
2019-07-05 09:04:45 +00:00
Adrian Torres 9e71d57d11 [REM] *: remove calls to api.one and adapt code
Adapt all code that was using `api.one` to recordset-style method and
remove any and all calls to `api.one` in preparation for its removal.
2019-07-05 09:04:45 +00:00
Adrian Torres d47f0ddbe4 [FIX] base: force unlink during uninstall
This commit is a followup to #25854

If a custom relational field is in a view and the field it references is
in another module and this module is uninstalled, the registry will
crash.

This is not because the ORM doesn't properly cascade-delete the field,
it tries to, but since the field is in a not-to-be-deleted view, the
unlinking of the field is interrupted and an error is raised to prevent
the breakage of the view.

However, module install/upgrade/uninstall cannot be stopped by such
errors and once the registry is reloaded, it crashes because it cannot
find the referenced field.

The solution, while not ideal, is to force the deletion of the field if
we're in uninstall mode... This means that the view in question will be
broken until the field is manually removed from the view, but a broken
view can be dealt with more easily than a broken registry.

opw-1974362

closes odoo/odoo#33130

Signed-off-by: Raphael Collet (rco) <rco@openerp.com>
2019-05-03 07:50:02 +00:00
Adrian Torres 1aa8663444 [IMP] base: unlink module data in batch
Before this commit, during a module uninstall, module data would be
unlinked in a record-by-record basis, this was done this way for two
elementary reasons:

- To guarantee an unlink order, as some module data being deleted
might only be deleted if some other module data has already been
deleted.
e.g. [a, b, c, a] -> we could batch unlink both `a` in the
list, however the second `a` could depend on the unlink
of either b or c, making the groupby erroneous.

- To create a savepoint before every module data unlink (record), so
that in case it fails, we can simply skip it and proceed with the
uninstall.

After this commit, batch unlink of module data is now possible without
losing the aforementioned key features.

- The first problem is fixed by using `itertools.groupby`, which
will group **adjacent** data of the same type and unlinks them all
in batch while guaranteeing correct unlink order.
e.g. [a, a, a, b, b, c, a, c, c, c] -> this will unlink in
batches of [a, a, a], [b, b], [c], [a], [c, c, c]

- The second problem is fixed by making a savepoint per batch, if
a single element of the batch fails and the recordset contains more
than one element, we split the recordset into two and we repeat the
unlink process recursively until we weed out the undeletable record

Real world example: l10n_mx_edi takes 20min to uninstall because it
contains a lot of master data (~55k records), with this patch the
uninstall takes a measly 20s.

It should generally reduce the uninstallation time for all modules, but
those with lots of master data will see greater benefits in terms of
time.

opw-2025895

closes odoo/odoo#34435

Signed-off-by: Raphael Collet (rco) <rco@openerp.com>
2019-07-01 14:24:39 +00:00
Adrian Torres 3b3a14cb8d [FIX] sale: allow the uninstallation of sale
Commit 1da63f0ab0 removed the
uninstall_hook defined in sale/__init__.py but forgot to remove it from
the __manifest__.py, resulting in a traceback when uninstalling the
`sale` module.

closes odoo/odoo#32804

Signed-off-by: Raphael Collet (rco) <rco@openerp.com>
2019-04-18 13:12:18 +00:00
Adrian Torres 3f3d2b2773 [FIX] base: prevent module operations while cron is running
closes odoo/odoo#32234

Signed-off-by: Raphael Collet (rco) <rco@openerp.com>
2019-04-05 13:10:35 +00:00
Adrian Torres 09af3fac39 [FIX] base: generate field translations based on index key
The ir_translation table has an index on the (name, lang, type and value)
key, however this index was not being used when generating field
translations because the column `type` was missing!

With this commit, we fully utilize the index when generating field
translations.

With the eCommerce with categories and 4k+ products, the time to charge
products per category would be around 4-5s; With this patch it is
reduced to 1-2s.

opw-1961665

closes odoo/odoo#33504

Signed-off-by: Raphael Collet (rco) <rco@openerp.com>
2019-05-20 13:42:17 +00:00
Adrian Torres 353474fba2 [FIX] sale: do create of sale.order.line in batch
With this patch, `sale.order.line` records can now be created in batch,
this means that operations such as:

    - SO copy
    - SO create + commands
    - SO import

will benefit from this patch in terms of performance.

E.g.

For a SO of 2k lines, duplicating the SO takes:
Before patch: 1515s
After patch: 171s

Creating an SO with SOL as o2m commands:
Before patch: 181s
After patch: 43s

closes odoo/odoo#32040

Signed-off-by: Raphael Collet (rco) <rco@openerp.com>
2019-03-22 12:08:35 +00:00
Adrian Torres f4b7dab00e [FIX] account, sale: create invoice lines in batch
This patch changes the create of `account.invoice.line` to use
`model_create_multi`, which allows the batch creation of multiple
invoice lines while only triggering the recompute of all related fields
once.

It also changes the generation of invoices from a SO to take advantage
of the create multi of `account.invoice.line`.

For a SO of 2000 lines:
    Pre-patch:
        ~417s to generate an Invoice
    Post-patch:
        ~30s to generate an Invoice

closes odoo/odoo#31975

Signed-off-by: Raphael Collet (rco) <rco@openerp.com>
2019-03-20 14:34:17 +00:00
Adrian Torres 686e6e10ce Revert "[FIX] ir_model_fields: register selection options"
This reverts commit 5f5bb5e26b.

Not really a fix as this was never implemented, to be done in master...

Closes #31466
2019-02-27 18:00:28 +01:00
Adrian Torres 5f5bb5e26b [FIX] ir_model_fields: register selection options
Before this commit:
        -> Debug mode
        -> Settings
        -> Database structure
        -> Fields
        -> Any selection field
=> The field `selection` of the ir.model.fields form view does not
display the selection options of the field being viewed, this is because
the selection field is not registered at `_reflect_field_params` of
`ir.model.fields`.

After this commit:

The field is properly registered; for Selection fields with static
options, these are shown as-is, for fields with a lambda function as
options, the string 'function' is displayed, and for fields using a
function name as a string, the same string will be displayed.

Fixes #28360

closes odoo/odoo#31207
2019-02-26 13:12:05 +00:00
Adrian Torres 934c001680 [FIX] expression: properly handle {TRUE,FALSE}_LEAF
Before this commit, doing expression.OR() with only FALSE_LEAF would
yield [] which is equivalent to TRUE_LEAF and is therefore not correct.

The same happened (to a lesser extent) with expression.AND() within an
expression.OR(), since the former would return a [] which would be
ignored by expression.OR().

See tests for a clearer view of the use cases.

Fixes #30113, #26540

closes odoo/odoo#31202
2019-02-20 10:45:28 +00:00
Adrian Torres 073b7bb944 [FIX] stock: unlink move_lines in batch
Before this patch, cancelling a PO of 2000 lines would take a **very,
very** long time to process... so long that I terminated it before it
finished... yikes.

This was due to the call to `_do_unreserve` on every single move when
calling `_action_cancel`, this meant that n² calls to unlink where being
made (at least), which also meant n² calls to
`stock.move._recompute_state` were being made... that's a lot of
recomputes... = bad performance

The patch simply calls unlink once, which means `_recompute_state` is
called only once, saving a lot of time.

Cancel time for a PO containing 2000 lines:
        - Before patch: 1h30min+ (process terminated before finishing)
        - After patch: 7min

Other modules making use of stock and `_action_cancel` should also see
benefits from this patch

closes odoo/odoo#31787

Signed-off-by: Arnold Moyaux <amoyaux@users.noreply.github.com>
2019-03-12 15:35:19 +00:00
Adrian Torres 859586b641 [FIX] purchase: recompute in batch after confirmation
For every line in a RFQ, we create a stock.move upon confirmation of the
RFQ. This create, in turn, triggers a lot of recomputes that are not
needed for the creation of the next moves and thus should only be
triggered once.

This commit disables the recompute during the step that creates the
moves and then manually triggers the recompute **once**, after all the
moves have been created.

Confirmation time for a PO with 2000 lines:
        - Before patch: ~1h
        - After patch: ~9min

This patch is similar to the one introduced at [1], however the
previously mentioned patch uses batch create which is not available in
this version, so we manually emulate batch create's behavior.

[1] https://github.com/odoo/odoo/pull/31471
2019-03-12 13:15:40 +00:00
Adrian Torres 21a95bae50 [FIX] website: properly unlink website_id ir.model.field
It is not properly unlinked because it is a m2o required with
ondelete='set null'

Fixes #25078

closes odoo/odoo#30617
2019-02-01 09:56:30 +00:00
Adrian Torres 86ef5d1a46 [FIX] ir_model: properly delete m2m whose comodel is being deleted
Before this patch, if an m2m's relation field pointed to a model that
was to be deleted, the deletion of the aforementioned model wouldn't
trigger the m2m's deletion, which would lead to the registry crashing.

With this patch, whenever an ir.model is being unlinked, a check is
performed to verify that no other fields (m2m or otherwise) have this
model as a comodel, and if they do, they are unlinked as well.

closes odoo/odoo#30230
2019-02-01 10:19:28 +00:00
Adrian Torres f3de712d76 [FIX] ir_model: add constraint on domain field
Previous to this commit, if one were to create an ir.model.field with a
poorly constructed domain (read: SyntaxError), the server would properly
send an error message stating that an Error occurred, however this would
be too late as the registry with the bad code would have already been
reloaded, this meant that the registry would be left in an unstable
state (read: crashed).

With this commit, a constraint on the domain is added so that we confirm
that the code in the domain field is properly constructed, thus no need
to reload the registry and therefore no crash.

closes odoo/odoo#30157
2019-01-14 08:30:56 +00:00
Adrian Torres fecc8156cf [REM] *: remove pycompat py2 helpers/shims
This commit complements the removal of pycompat PY2 helpers and shims
from base by removing / adapting references to these helpers from the
enterprise code.
2018-11-29 08:40:45 +00:00
Adrian Torres 758382b3a7 [REM] pycompat: remove python 2 shims and helpers
Odoo no longer supports python 2, thus some of these helpers can and
have been replaced by python 3 built-ins, therefore there is no need for
them to stay defined.

The removed helpers are:
    * izip, imap and ifilter
    * unichr, text_type
    * implements_to_string, implements_iterator
    * string_types, integer_types
    * to_native

The python 2 shims have also been removed, and only the python 3 helpers
have been kept, because they can still be usable (i.e. accepting
both bytes and str for functions that can only accept one of the two)

[REM] pyjsparser: remove PY3 shims

They're no longer necessary as Odoo doesn't officially support python 2
anymore.

closes odoo/odoo#28519
2018-11-29 09:28:17 +00:00
Adrian Torres 52f5528cfb [REF] *: replace deprecated pycompat helpers for builtins
This commit replaces calls to pycompat helpers that were intended for
python 2 <-> python 3 interoperability for python 3 builtins, as python
2 is no longer officially supported by Odoo.

This includes:
    * calls to imap/izip/ifilter replaced by map/zip/filter
    * uses of text_type replaced by str
    * uses of unichr replaced by chr
    * calls to implements_to_string, implements_iterator removed
    * string_types and integer_types replaced by str, int respectively
    * calls to to_native replaced by calls to to_text

This is done in preparation to the removal of these deprecated helpers
in the following commit.
2018-11-29 09:28:17 +00:00
Adrian Torres e5c864a596 [REM] test_pylint: remove PY2/PY3 restrictions
Since Odoo no longer officially supports python 2, some of the
restrictions previously imposed by test_pylint are no longer necessary,
either because the usage of some builtin functions is legal (zip, map,
filter) or because the concerned function/module would already raise a
relevant error under python 3.
2018-11-29 09:24:26 +00:00
Adrian Torres 407f6e3532 [FIX] stock: do not prefetch stock.move fields
For a SO of 300 lines, when confirmed, it takes 195s to process stock
moves for the SO, which is an insane amount of time for any kind of
process in Odoo.

This happens because stock.move is a heavy model with lots of fields,
prefetching all these fields at once can hinder performance when
processing a lot of stock.moves.

To reduce the processing time, the prefetching is disabled in the
specific scenario of processing stock moves and pickings on SO
confirmation.

This patch increases the performance of SO validation (with stock
installed) by ~30%.

Pre-patch: 195s (300 SOL)
Post-patch: 135s (300 SOL)

closes odoo/odoo#28835
2018-11-20 10:51:07 +00:00