summaryrefslogtreecommitdiff
path: root/odoo/fields.py
AgeCommit message (Collapse)Author
2020-10-21[FIX] core: validation of field parametersRaphael Collet
The feature was actually not working as expected. It has been fixed, and a test now ensures it does work as expected. This commit also fixes some invalid parameters, hopefully nothing critical. closes odoo/odoo#60402 Related: odoo/enterprise#14273 Signed-off-by: Raphael Collet (rco) <rco@openerp.com>
2020-10-13[FIX] core: assignment of binary field on new record with originRaphael Collet
The assignment of a binary field on a new record with origin did actually modify the attachment storing the value of the field on the origin record! The fix is simply to avoid messing up with attachments when updating new records. closes odoo/odoo#59918 X-original-commit: 6778c4c10fa68c46bde6fae176f547f905e9c02f Signed-off-by: Raphael Collet (rco) <rco@openerp.com>
2020-10-13[FIX] core: assignment of size to new recordsRaphael Collet
Upon onchange, the web client sends unchanged image fields on one2many lines as "bin size" values, while changed image fields are sent as their full value. As the value is not base64-encoded, the assignment of that value crashes. Fix it by making assignment on new records not crash when decoding the image: the value is simply not assigned to the field. In the situation described above, the record being assigned is a new record with a real record as origin. Because the field is actually not assigned, its value is implicitly the value of the field on the origin record, which is consistent with the fact that the field was not modified in the form. X-original-commit: 4442d6f0f29a2bf6e992515cd535c88215855a36
2020-10-02[FIX] core: cache consistency of one2many field with computed inverseXavier-Do
Consider a one2many field with a corresponding many2one field that is computed. After modifying the many2one field's dependencies, the cached value of the one2many field is inconsistent until the many2one field has been recomputed. Therefore, one has to force the computation of the many2one field before accessing the cached value of the one2many field. closes odoo/odoo#59031 X-original-commit: b9201ebdd65eaabab9257cf97c58410681783fa6 Signed-off-by: Xavier Dollé (xdo) <xdo@odoo.com> Signed-off-by: Raphael Collet (rco) <rco@openerp.com>
2020-09-28[FIX] core: compute on batches of maximum PREFETCH_MAX recordsRaphael Collet
Calling a compute method on 1M records inevitably brings a worst case in cache prefetching: the code that determines which records to fetch has time complexity O(N²). We avoid this situation by calling the compute method on maximum 1K records. Each computed batch has a "prefetch set" of maximum 1K records too, which avoids the worst-case scenario above. closes odoo/odoo#58647 X-original-commit: 0b1aa660b1d94fe46dea09670b875f2d81ca36a3 Signed-off-by: Raphael Collet (rco) <rco@openerp.com>
2020-09-21[FIX] core: always evaluate field.depends upon setupRaphael Collet
This is necessary when a field's dependencies are given by a callable. This functionality was broken with the support for an explicit parameter `depends` in the fields' definition, because the implementation could not distinguish between the field parameter `depends` and the `depends` evaluated from compute functions. The fix consists in storing the field parameter `depends` into `field._depends`, and the result of the setup into `field.depends`. closes odoo/odoo#58127 X-original-commit: d4d7925d69c9a37f099ddd3a8929004f17e162eb Signed-off-by: Raphael Collet (rco) <rco@openerp.com>
2020-09-17[FIX] core: non-stored binary fields should have attachment=FalseRaphael Collet
This prevents some code (in controllers) to retrieve an attachment for a field that is not stored, as the code only relies on `field.attachment`. This also makes the field definition more consistent. closes odoo/odoo#57969 X-original-commit: 13397943d9e953815ae5ce9b01ce812a92903361 Signed-off-by: Raphael Collet (rco) <rco@openerp.com>
2020-08-21[IMP] core: error message for computed field left unassignedRaphael Collet
Non-stored readonly computed fields must be assigned by compute method. Make the error message more explicit. closes odoo/odoo#56269 Signed-off-by: Raphael Collet (rco) <rco@openerp.com>
2020-08-20[FIX] core: first onchange on new one2many lineRaphael Collet
Consider models `M` and `L`, with a one2many field `M.l_ids` with inverse `L.m_id`. Consider also a computed field `L.foo` that depends on `L.m_id`. The field `L.foo` is not computed correctly by `onchange` on a new line in the form view of `M`: its value is forced to `False` because it has no default, and is not recomputed by `onchange` since the field `L.m_id` is never triggered an `onchange`. Modify the method `onchange` to not force fields without a default to `False`, and return all fields.
2020-08-17[FIX] base: docstring typoRémi Rahir
closes odoo/odoo#55983 X-original-commit: edacc0fc920e27b9759408887762fcba284ee391 Signed-off-by: Victor Feyens (vfe) <vfe@odoo.com>
2020-08-17[IMP] core: clarify making fields inaccessibleXavier Morel
* add a constant to `fields` for that purpose * add a test to ensure that it works as expected * fix the formatter so it handles the pattern correctly
2020-07-20[FIX] core: update of one2many field on new record with originRaphael Collet
When setting the inverse many2one field on a record, the record is not removed from its inverse relation if the field's current value is not in cache. Fix this by setting the field's value in cache before updating. closes odoo/odoo#54711 X-original-commit: df12c85816dfa59f298c3e6a077bd88e4b7aa6a6 Signed-off-by: Xavier Morel (xmo) <xmo@odoo.com>
2020-07-16[FIX] core: mapped() does not prefetch as expectedAdrian Torres
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[FIX] core: update of one2many field on new record with originRaphael Collet
An onchange method can remove an existing line from a one2many field of a new record with command 2, such as: move.line_ids = [(2, <NewId origin=42>), ...] The processing of the command 2 above incorrectly tries to remove the new line from the origin record, instead of the new record. closes odoo/odoo#54545 X-original-commit: 7d138a445acbd9f2e122d9c30ca81877bae6a245 Signed-off-by: Raphael Collet (rco) <rco@openerp.com>
2020-06-23[FIX] orm: don't log errors on constraint failure unless necessaryAdrian Torres
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-17[FIX] core: make `state` special case overridableXavier Morel
`state` was special cased in `copy_data` such that it would always be reset to its default value if it had one. While this special case can be useful (maybe) and is certainly ingrained in odoo, having it not be overridable can be problematic. Move the special case to the fields, so it's possible to explicitly mark state fields as copy. closes odoo/odoo#53113 Signed-off-by: Xavier Morel (xmo) <xmo@odoo.com>
2020-06-11[FIX] core: do not compute a field on a mix of new and real recordsRaphael Collet
This fix looks like good practice to avoid confusing developers when writing compute methods for fields, and should not be necessary. But it actually fixes an extremely obscure bug in the computation of the field `sale.order.invoice_status` when dealing with timesheets (module sale_timesheet_enterprise). The bug seems triggered by the fact that the field is computed on both a given sales order and a new record with that sales order as origin. X-original-commit: f9c128c6f567dfaab5cfb0080e4fdc847942911a
2020-05-26[IMP] *: make ir.property methods privateMartin Trigaux
Should only interact with them via python code in a controlled environment, no direct call with RPC
2020-05-14[IMP] doc: Image field documentationVictor Feyens
It was considered important to precise that image fields without limitations weren't checked at all and could therefore contain non image content (and precise some other things on Image fields). Forward-port of #51200 closes odoo/odoo#51259 Signed-off-by: Victor Feyens (vfe) <vfe@odoo.com>
2020-05-14[FIX] core: selection override with str or callableRaphael Collet
If the original field is defined with a selection list, overrides with method or methods names fall back on the list. closes odoo/odoo#51232 X-original-commit: 272ba8d2238d1d1b87f47b114b55e6eed25314a8 Signed-off-by: Raphael Collet (rco) <rco@openerp.com>
2020-05-13[FIX] base: default check_company domain.Victor Feyens
The supported case of check_company on `res_company` model fields was not considered when specifying a default domain on `check_company=True` fields. closes odoo/odoo#48674 Nb: It is already correctly supported in the `_check_company()` method. Signed-off-by: Raphael Collet (rco) <rco@openerp.com>
2020-05-08[FIX] core: partial unlink in recordsetRaphael Collet
The following loop crashes on the second iteration: for record in records: record.display_name # access non-stored computed field record.unlink() # delete record The cache has been invalidated by `unlink` on the first iteration, so the field is computed. The computation is done on the whole recordset, which fails with a `MissingError` because of the first record. The fix is to retry the recomputation on the single record in this case. closes odoo/odoo#50910 X-original-commit: 7f33ebf528b1415452ce549e3329c3fe89be0ad8 Signed-off-by: Raphael Collet (rco) <rco@openerp.com>
2020-05-06[FIX] core: unexpected cache invalidation when updating one2manyRaphael Collet
When setting a many2one field, the corresponding one2many fields are updated in cache. When such a one2many field has a domain, the evaluation of the domain may require to fetch some fields from the database. If the `towrite` cache has not been updated yet, the many2one field is overridden by the database value. Fix by setting the `towrite` cache before updating inverse fields. closes odoo/odoo#50788 X-original-commit: c1ebfe05a66cfebc7ced36e25db5f6ff4bfb2d46 Signed-off-by: Raphael Collet (rco) <rco@openerp.com>
2020-04-30[FIX] core: improve cache consistency of translated fieldsRaphael Collet
X-original-commit: 7460d65387edd9d8578e048da6ee634ef6a2e71e Co-authored-by: Christophe Simonis <chs@odoo.com>
2020-04-18[FIX] fields: screwing up cache when rounding monetary valueRaphael Collet
Consider a sales order with a single line. Edit the sales order: remove the line, and add another line with the same subtotal. When saving, the total of the sales order is 0.0. Here is the explanation: the form view performs a `write` on the sales order, and modifies the lines with a command `2` (remove line) and a command `0` (create line). After deleting the first order line, the cache is emptied, and a call to `flush()` forces the recomputation of the total. The value is computed to be 0, and assigned to the field. The assignment converts the value for the cache, without prefetching the currency field (optimization), and puts 0.0 in cache. The assignment then converts the value for the database, which prefetches most fields on the sales order: the cache is now inconsistent and contains the old value V, while the database is then updated with 0.0. After creating the new order line, the total is once again recomputed. Its value is V, and because the cache also contains that value, no update is performed to the database, which remains at 0.0! The fix consists in avoiding the prefetching of fields when accessing the currency field to round a monetary value. opw-2223134 closes odoo/odoo#49741 X-original-commit: 048ea2f20a0a2fa6d629cbe262cbbe765248d36f Signed-off-by: Raphael Collet (rco) <rco@openerp.com>
2020-04-08[IMP] base: remove redundant base64 back and forth in attachmentsXavier Morel
* add a `raw` computed field, though only update some of base to use it (addons for which that makes sense can be migrated progressively) * avoid working with base64 data when it's possible to work with the actual data * improve datas (base64 encoded content): should depend on bin_size closes odoo/odoo#47212 Signed-off-by: Raphael Collet (rco) <rco@openerp.com>
2020-04-07[FIX] fields: add support for parameter `invisible` (actually used)Raphael Collet
2020-04-07[IMP] fields: add validation of parameter namesRaphael Collet
2020-04-07[IMP] base: document all field types.Victor Feyens
closes odoo/odoo#49135 X-original-commit: cc653d3f1a49e2b7b2cc0ec833097bd7cf3528a1 Signed-off-by: Victor Feyens (vfe) <vfe@odoo.com>
2020-04-03[FIX] core: add NOT NULL constraints in post-initRaphael Collet
The method `_init_column` for the field `alias_id` of `mail.alias.mixin` requires the reflection of models, because `mail.alias` records have a many2one field to `ir.model`. The actual initialization of the column is thus performed in post-init phase (after reflection). Therefore, the NOT NULL constraint cannot be added right away. X-original-commit: 2e3400f149ce4ed4c00bc49a519f58993b49bc51
2020-04-03[IMP] core: add/update foreign keys in batchRaphael Collet
This greatly reduces the number of queries to add foreign keys (notably for many2one fields). This saves 6.5% of the total installation time. X-original-commit: 13a2666b4da09fc0ec7608a974dadde45f76fd7a
2020-04-03[IMP] core: create/drop indexes all at onceRaphael Collet
Reduce the number of queries to create and drop indexes. This saves 1.5% of the total installation time. X-original-commit: 0e1e480da9d971575feba64038c4fa518ebe102b
2020-04-03[IMP] core: compute field_computed lazilyRaphael Collet
This is a simple refactoring. X-original-commit: 634775bff6d9eba9d4548cc801071a942895a719
2020-04-03[IMP] core: compute field triggers lazily on registryRaphael Collet
This saves 2% of the total installation time. X-original-commit: 6a0028f91944b1d9e4eac026e86ca249ef5bc7ee
2020-04-03[IMP] core: optimize `field._setup_related_full`Raphael Collet
Simply avoid browsing records for models, and use classes instead. This saves 1% of the total installation time. X-original-commit: 35ebaea098edeb3d34f77305b09cb36e56ea5a92
2020-04-03[IMP] core: remove `Field._slots`Raphael Collet
This optimization is no longer necessary. Thanks to Python 3.6's new implementation of dicts, the memory footprint difference between slots and dicts is now around 5%, which is no longer worth the complexity and performance cost. This saves 2% of the total installation time. X-original-commit: c7f17770803744cbbb741fc11265ee914641aed7
2020-04-03[IMP] core: optimize `model._setup_base()`Raphael Collet
Simplify the code to retrieve fields on a class. Simplify the class attribute that lists the class' proper fields. Optimize `model._add_inherited_fields()`. Optimize `resolve_mro` by using classes instead of recordsets. This saves 3.5% of the total installation time. X-original-commit: 89ab71905cb6c5d3beda5eac357f8592e31aaef8
2020-04-03[IMP] core: avoid duplicates in `depends` and `depends_context`Raphael Collet
Repeated calls to `registry.setup_models()` will eventually introduce many duplicates in the those attributes, which may slow down the determination of computation triggers. X-original-commit: 735e65f7ca45616881336121d10a874412446dcf Co-authored-by: Xavier Morel <xmo@odoo.com>
2020-04-01[REM] *: remove various unused import shimsAdrian Torres
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-03-31[IMP] base, *: allow html fields not to sanitize formsfja-odoo
* = event, website_event_track, website_sale, website_hr_recruitment, website_livechat, website_sale, website_slides The option to sanitize or not the forms was not available, this will allow better flexibility on whether forms should be sanitized or not on an HTML field. Also we use this new param to allow forms to be added on some already existing html fields where forms where sanitized out. task-2209554 closes odoo/odoo#47318 Signed-off-by: Jérémy Kersten (jke) <jke@openerp.com>
2020-03-30[IMP] core: introduce mechanism for selection_add cleanupAdrian Torres
- 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-25[FIX] core: computed fields can be copied if editableThibault Delavallée
Purpose of this commit is to let computed stored editable fields being copied if their field class allows it. Indeed the value of those fields is computed based on some triggers but can also be updated manually by users. When copying a record, it makes sense to consider that this value is what the user expects and allow its copy, if the original field allows it. Either it was computed, and copied value will be correct without having to call computation again (well, provided all dependencies have been copied, too), or it was updated and the copied value will be the one the user entered. Without this fix, an edited field is not copied, and will be recomputed, which may look like an inconsistent value. Task ID 2209163 closes odoo/odoo#48383 X-original-commit: 4b274d3b4101fbae154a572cdf40d23838899773 Signed-off-by: Olivier Dony (odo) <odo@openerp.com> Signed-off-by: Raphael Collet (rco) <rco@openerp.com> Co-authored-by: Raphael Collet <rco@odoo.com>
2020-03-23[IMP] core: do not recompute stored field on new recordRaphael Collet
The use case is a cache miss of a stored field with compute on a new record with origin. The field should be computed only when explicitly triggered, i.e., when a dependency has been modified. Otherwise it should be fetched from the origin record. closes odoo/odoo#47353 Signed-off-by: Raphael Collet (rco) <rco@openerp.com>
2020-03-18[FIX] fields: XLSX files mistaken as SVG filesJorge Pinna Puissant
Before this commit, when a user try to import data with a XLSX file, the file was mistaken by an SVG file. This issue arises because a XLSX file from import isn't encoded in base64, and for testing if the file is an SVG file it will be decoded. base64.b64decode by default (when validate is False), will remove all characters that are in the base-64 alphabet from the input prior to decode. So in our case, when the non encoded XSLX file is force decoded the results starts, unluckily, with '<' and it's mistaken by an XML/SVG file. Note that, the XSLX file is wrongly tested because a XSLX file is just a ZIP file, and all the ZIP files start with PK\x03\x04, and P is the first byte of a base64 encoded XML file. Now, only files that were previously encoded into base64 are decoded to be tested. The validate = True parameter in base64.b64decode will raise a binascii.Error if there are a non-base64-alphabet characters in the input, this will allow us to know if the input was or wasn't base64 prior encoded. As base64.b64decode with validate = False, removed the non-base64-alphabet characters this allows to decode input files compatible with RFC 2045 (MIME). The files compatible with this standard will have a newline character (b'\n') after every 76 bytes of the output, and end with a newline. To keep backwards compatibility, we remove the newlines and the carriage return from the input before the decoding. opw-2194468 closes #36081 closes #31849 closes #33543 closes odoo/odoo#47906 X-original-commit: 65d709c9ab386d646f682c494cfb21cb06ec8034 Signed-off-by: Olivier Dony (odo) <odo@openerp.com> Signed-off-by: Jorge Pinna Puissant (jpp) <jpp@odoo.com> Co-authored-by: Xavier Morel <xmo@odoo.com>
2020-03-04[FIX] orm: fields x2m are not sortableLucas Perais (lpe)
Before this commit, x2m fields were described as 'sortable' This was odd since: - When actually sorting on one of those fields through the webclient the sorting was gibbrish - Even the orm silently warned in the log that the field was not a sql column and therefore was not sortable After this commit, only a field which is column (and a few other conditions) can be sorted Task 1863492 closes odoo/odoo#46921 X-original-commit: dd3094378fc322447d1aef994f6bace3f0c24288 Signed-off-by: Lucas Perais (lpe) <lpe@odoo.com>
2020-02-26[FIX] core: compute fields in batch on new recordsRaphael Collet
Remove a leftover special case of the ORM from a former version. This fixes a performance issue, where an onchange recomputes a field on one2many lines one by one. X-original-commit: 16a75d85741db8a1f87c1d26503a9bb75197cf83
2020-02-24[IMP] fields: inherited field on new record are set on parent recordsRaphael Collet
This makes the behavior of `onchange()` consistent in the case of inherited models (with `_inherits`). closes odoo/odoo#45910 Signed-off-by: Raphael Collet (rco) <rco@openerp.com>
2020-02-20[IMP] fields: log warning for ignored selectionmreficent
When a field is related, defining a selection or selection_add will have no effect and the paramater is ignored. Log a warning and fix all fields badly definied Closes odoo/odoo#45716 closes odoo/odoo#45832 Related: odoo/enterprise#8613 Signed-off-by: Martin Trigaux (mat) <mat@odoo.com>
2020-02-07[IMP] doc: some precisions/fixesVictor Feyens
closes odoo/odoo#44869 X-original-commit: c8c311b8d463d1db7f3700c1bb5dac076abab905 Signed-off-by: Victor Feyens (vfe) <vfe@odoo.com>
2020-02-06[FIX] core: delay constraint application in case of upgradeAdrian Torres
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>