Commit Graph

63 Commits

Author SHA1 Message Date
Raphael Collet bc8c7596a5 [IMP] models: rework the API that deals with dirty fields on records 2014-11-24 15:31:18 +01:00
Denis Ledoux 999ed04c40 [FIX] api: avoid to return all fields *2many in onchanges
When an onchange returns a change in a 2many field line (a '1' tuple, update), avoid to return all fields of the *2many field but only the altered field.

Otherwise, the web client regard all the fields of the 2many as dirty, and try to write on all fields (even if the value is the same, thus)

opw-615062
2014-11-14 14:24:11 +01:00
Raphael Collet e038fec696 [IMP] models: improve performance of _setup_fields()
There was an issue in _setup_fields(): the method invokes _inherits_reload(),
which recomputes inherited fields, and invokes itself recursively on children
models.  This may be problematic if the children models have already been set
up.

This optimization avoids recursive calls of method _inherits_reload().  In
_setup_fields(), first all parent models are set up, then their fields are
inspected to determine inherited fields, and their setup is done.  This scheme
guarantees that inherited fields are computed once per model.
2014-11-13 14:54:04 +01:00
Raphael Collet 54140331f3 [FIX] fields: fix the translations of the labels of inherited fields 2014-11-06 16:42:41 +01:00
Raphael Collet 5cc863245b [IMP] fields: improve _determine_default() and add test for inherited fields 2014-11-06 12:20:39 +01:00
Raphael Collet f2e4a10e1a [IMP] use model._fields instead of model._all_columns to cover all fields
The old-api model._all_columns contains information about model._columns and
inherited columns.  This dictionary is missing new-api computed non-stored
fields, and the new field objects provide a more readable api...

This commit contains the following changes:

 - adapt several methods of BaseModel to use fields instead of columns and
   _all_columns

 - copy all semantic-free attributes of related fields from their source

 - add attribute 'group_operator' on integer and float fields

 - base, base_action_rule, crm, edi, hr, mail, mass_mailing, pad,
   payment_acquirer, share, website, website_crm, website_mail: simply use
   _fields instead of _all_columns

 - base, decimal_precision, website: adapt qweb rendering methods to use fields
   instead of columns
2014-11-04 13:47:57 +01:00
Richard Mathot ef862084c9 [DOC] fields.py: oldname key 2014-11-03 18:09:16 +05:30
Raphael Collet 5eb6e58156 [FIX] fields: make field.store=False on old-style function fields
Clarify the semantics of field attributes:
 - field.store is True when the field is actually stored in the database;
 - field.column is the column corresponding to field or None.

The various field definitions correspond to:
 - new-style stored field: field.store and field.column
 - new-style non-stored field: not field.store and not field.column
 - old-style regular field: field.store and field.column
 - old-style function field: not field.store and field.column
2014-10-30 13:29:21 +01:00
Raphael Collet 8db5b84fc1 [FIX] fields: inherited fields should get 'string' from their parent field
Because some parameters of a field may be determined during its setup, we have
to update the corresponding column after the setup, and recompute _all_columns
to make it consistent.
2014-10-28 09:12:31 +01:00
Raphael Collet 4a65b61f2f [IMP] fields: remove unused imports 2014-10-23 10:35:27 +02:00
Raphael Collet ba1369fbef [IMP] fields: infer required=True on related fields
One can infer related=True on a non-stored related field if all fields on the
path are related.  This cannot be done if the related field is stored: when you
create a record, the database row is created first, and the related field is
computed and stored afterwards.  Making the field required in that case would
trigger a non-null constraint violation.
2014-10-23 10:28:13 +02:00
Raphael Collet 0873613767 [IMP] fields: initialized computed fields to a null value instead of a failed one
This should ease compute methods that assign a list to "update" a
one2many/many2many field.  With a failed value, the update crashes.
2014-10-23 10:14:24 +02:00
Raphael Collet 8e6d5beb35 [IMP] fields: reuse column objects when possible, instead of recreating them
This is a memory optimization: it reduces the memory footprint of each
registry.  We have observed a reduction of 10Mb on a database with modules crm,
sale, purchase, stock.
2014-10-22 16:22:39 +02:00
Denis Ledoux 0a82397da3 [FIX] fields: selection, do not try to translate label if label is empty
- translate with no source returns first translation for this field, whatever the source
 - performance
2014-10-22 11:54:22 +02:00
Raphael Collet 89031f5de6 [FIX] models: simplify partial setup of fields, let it crash silently
The setup of relational fields may be problematic, as they may refer to unknown
models via custom relational fields.  In a partial setup, do not try to skip
the field setup, but let it go and silently catch any exception if it crashes.
2014-10-15 11:39:12 +02:00
Raphael Collet 43abcb02ba [FIX] models: in _add_field(), set the field as an attr before setting it up
In the case of custom fields, the field's parameters were set up without the
field being present in the class hierarchy.  Because of this, the parameter
inheritance mechanism was missing the field itself.  As a consequence, custom
selection fields ended up without selection, for instance :-/
2014-10-14 11:56:59 +02:00
Raphael Collet 5db84cb07e [IMP] models: do not use compute methods to determine default values anymore
Compute methods could give results that should not be considered as default
values.  For instance, a related field usually defaults to a null value, which
is then set to the field with its inverse method by create().  This may violate
a non-null constraint if the original field is required.  Therefore, compute
methods are no longer used to determine default values.
2014-10-13 13:44:07 +02:00
Raphael Collet 8a9027edd1 [IMP] fields: do not force required=True on related fields
This may cause issues if the field is stored: non-null constraint will prevent
saving records when no value is provided.
2014-10-09 17:18:28 +02:00
Raphael Collet 1c9ed9f534 [IMP] fields: rename attribute 'copyable' into 'copy' 2014-10-09 15:05:15 +02:00
Raphael Collet 8ddf145559 [IMP] fields: do not copy field objects anymore, but make new instances instead
Because of the parameter overriding mechanism implemented by fields, it is no
longer necessary to copy field objects.  It is even better to no copy them in
the case of related fields.
2014-10-09 15:05:15 +02:00
Raphael Collet be894166ef [IMP] fields: add assertions to check parameters (comodel_name, size, digits) 2014-10-09 10:50:40 +02:00
Raphael Collet 36174fcc6e [IMP] fields: set the default value to the closest field.default or _defaults
This solves a subtle issue: in the following case, the class Bar should
override the default value set by Foo.  But in practice it was not working,
because _defaults is looked up before field.default.

    class Foo(models.Model):
        _name = 'foo'
        _columns = {
            'foo': fields.char('Foo'),
        }
        _defaults = {
            'foo': "Foo",
        }

    class Bar(models.Model):
        _inherit = 'foo'
        foo = fields.Char(default="Bar")

The change makes field.default and the model's _defaults consistent with each
other.
2014-10-09 09:18:02 +02:00
Raphael Collet 3f31081bc2 [IMP] fields: make attribute 'default' callable 2014-10-09 09:18:02 +02:00
Raphael Collet 619a844428 [FIX] fields: in to_column(), returning self.column is generally not correct
Consider the following example:

    class Foo(models.Model):
        _name = 'foo'
        _columns = {
            'state': fields.selection([('a', 'A')]),
        }

    class Bar(models.Model):
        _inherit = 'foo'
        state = fields.Selection(selection_add=[('b', 'B')])

The attribute 'column' of the field does not have the full selection list,
therefore the column object cannot not be reused, even a copy of it.  The
solution is to systematically recreate the column from the field's final
specification, except for function fields that have no sensible way for being
recreated.
2014-10-08 16:39:59 +02:00
Raphael Collet afb91fde81 [FIX] models: fields_get() shall not return info about fields not set up yet
When processing data files during a module installation/upgrade, not all fields
are set up yet, in particular relational custom fields.  Make fields_get()
ignore those fields, so that views can be created/updated and validated,
provided they do not refer to those fields...
2014-10-06 11:56:03 +02:00
Raphael Collet 53ff84493f [IMP] fields: allow sorting on inherited field in list views 2014-10-01 16:00:44 +02:00
Raphael Collet a69996b50c [IMP] fields: split multi-purpose '_origin' into 'column' and 'inherited'
This makes it easier to determine when a field interfaces a column, and when it
implements an inherited field (with _inherits).
2014-10-01 16:00:44 +02:00
Raphael Collet 34ba5154e2 [IMP] fields: split the default value from the compute function
This separation allows to set a default value on a related field, for instance.
2014-10-01 09:24:09 +02:00
Denis Ledoux 30a0814159 [FIX] fields: copy origin to avoid sharing field objects between registries
Fix a bug introduced in revision f229974, where shared columns objects are
systematically reintroduced in registries.
2014-09-30 14:32:35 +02:00
Raphael Collet 1f15055de3 [FIX] fields: a function field with fnct_search should be searchable 2014-09-24 18:41:20 +02:00
Raphael Collet 764c5acd81 [IMP] fields: reset decimal precision on new-style fields
When a decimal_precision record is created/modified, the float fields of the
models in the registry must be reset.  This was done on old-API columns only.
It is now handled by the new-API fields.
2014-09-18 11:11:19 +02:00
Raphael Collet 6e0c73d2ad [FIX] fields: make convert_to_cache() more robust with inverse one2many
When a one2many field uses an integer field as inverse, the onchange method on
the second model may receive a dictionary for the value of the integer field.
This is because the client expects that field to be a many2one.
2014-09-17 16:01:02 +02:00
Denis Ledoux d6741c8b54 [FIX] fields: convert_to_read, handle missing many2one foreign keys 2014-09-16 12:29:37 +02:00
Raphael Collet e724f3adec [FIX] fields: in *2many.convert_to_cache, fix the effect of command "1" 2014-09-15 15:31:30 +02:00
Raphael Collet 7c0387c952 [FIX] fields: in computed fields, handle AccessError and MissingError
When computing a field on a recordset, a subset of the records may be missing
or forbidden by access rules.  In that case, evaluate the compute method record
by record, and mark failed records as such in cache.
2014-09-11 11:57:18 +02:00
Christophe Simonis e9a4603cb4 [FIX] fields.py: One2many fields only set inverse field on Many2one fields 2014-09-09 13:28:48 +02:00
Raphael Collet 68777c5860 [IMP] models: inherited fields are related fields read as the current user
Add an attribute 'related_sudo' (True by default) for related fields.
A related field is computed as superuser if related_sudo is True.

Add explicit related fields 'name' and 'email' on 'res.users', as these should
be readable by the public user with module website_forum.
2014-09-04 16:10:48 +02:00
Raphael Collet e7b8e107f1 [IMP] fields: update inverse fields lazily for performance
When a relational field is assigned in an onchange, its inverse field is
updated in cache.  Reading the current value of the inverse field may be
costly, for instance in the case of a one2many field with thousands of records
as a value.  Instead, put in cache a SpecialValue that reads and updates the
field; it will be triggered only when it is accessed.
2014-09-02 14:07:03 +02:00
Raphael Collet 2ad092b5e5 [ADD] doc: new documentation, with training tutorials, and new scaffolding 2014-08-22 17:51:20 +02:00
Raphael Collet dce5228da7 [FIX] fields: add missing option 'sanitize' on Html fields 2014-08-21 13:27:30 +02:00
Denis Ledoux 0fce350cf4 [FIX] fields: *2many related fields should not be read as superuser
one2many and many2many fields depends on the security rules.
For instance, on products, with the taxes_id many2many fields, you only see the taxes of your own company, thanks to the multi company security rule
With related *2many fields, if you browse it with superuser, you will have all records of the one2many fields, even those you are not allowed to see, as superuser ignores security rules.

For instance, taxes_id of product.product is a related of taxes_id of product_template (through the inherits on product.template), and you should see the same taxes on the product template and on the product product (variant). This is not the case if the fields is read using the superuser
2014-08-20 14:08:25 +02:00
Olivier Dony bd3df7bfa1 [FIX] orm field (new api): preserve empty values instead of folding to False
The new API introduced a small behavior change where empty
string values written or stored in a char/text field were
replaced by False (i.e. as if they were NULL).
This was done to mimic the web client behavior, but introduces
a very surprising effect: a.name = ""; assert a.name == "";
would fail. It would also require many more tests in the
code when reading existing required values from the database,
as they could still be False when an empty string value
had previously been stored, for some reason.
2014-08-13 10:44:05 +02:00
Raphael Collet 9363dfe1ef [FIX] fields: generalize inverse_field to a list of inverse fields
Some many2one fields happen to have several corresponding one2many fields,
typically with different domains. It is also the case for the field 'res_id' of
mail.message, where each model inheriting from mail.thread defines a one2many
based on that field. The fix ensures that when a relational field is updated,
all its inverse fields are invalidated.
2014-08-08 14:57:00 +02:00
Olivier Dony 4c8c722401 [IMP] fields.Date[Time]: convert_to_cache: avoid costly parsing when not validating
The extra parsing check is not necessary when we're
not validating inputs, because in that case the
values come from the database and are valid.
The validation is quite expensive due to calls
to strptime() + strftime().
2014-08-07 18:24:29 +02:00
Olivier Dony 935c476664 [IMP] orm.Field: doc for @copy attribute 2014-08-07 18:03:08 +02:00
Christophe Simonis 5bd3c02bec [FIX] fields.py: handle "change_default" and "deprecated" attributes in new fields 2014-08-07 13:04:26 +02:00
Olivier Dony f062eec639 [FIX] orm: _compute_related: ensure prefetch is done in batch
Due to the use of a sudo env, the records
were being added to the sudo cache one by
one instead of all at once. This meant the
prefetching was not able to load all
records at once, leading to prohibitive
times when processing thousands of
records.
2014-08-06 18:48:35 +02:00
Raphael Collet e95fc488db [IMP] fields: add mechanism to extend a selection field
If a selection field is defined by a list as selection, such as:

    state = fields.Selection([('a', 'A'), ('b', 'B')])

one can extend it by inheritance by redefining the field, as:

    state = fields.Selection(selection_add=[('c', 'C')])

The result is that the selection field will have the list
[('a', 'A'), ('b', 'B'), ('c', 'C')] as selection.
2014-08-06 15:20:07 +02:00
Christophe Simonis dc9bcf479d [FIX] Allow search on `_inherits` fields.
As `_inherits` fields are now handled via `related`
fields (not stored, obviously), a new descriptor
`searchable` has been added to `fields_get()` result
to indicated if the field is searchable or not.
2014-08-06 15:01:01 +02:00
Raphael Collet 4c18c5fb6e [FIX] fields: convert_to_cache() on *2many fields must take record's current value
The existing code was buggy when writing on *2many fields with a list of
commands: the value was converted for the cache, but taking an empty recordset
as the current value of the field.
2014-08-06 09:07:57 +02:00