Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FIX] validate field names in attributes of views #20893

Merged
merged 2 commits into from
Dec 7, 2017

Conversation

rco-odoo
Copy link
Member

OPW 779494

@rco-odoo
Copy link
Member Author

@sle-odoo I had to fix some views in mrp and stock.
Can you please have a look? Thanks

@C3POdoo C3POdoo added the OE the report is linked to a support ticket (opw-...) label Nov 13, 2017
Copy link
Contributor

@mgeubelle mgeubelle left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not confident enough with my Python knowledge to approve but that's a great improvement ! Thanks.

@@ -121,7 +121,7 @@
<field name="needs_lots" readonly="1" groups="stock.group_production_lot"/>
<field name="is_done" invisible="1"/>
<field name="sequence" invisible="1"/>
<field name="location_id" domain="[('id', 'child_of', parent.location_id)]" invisible="1"/>
<field name="location_id" invisible="1"/>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you sure you can just delete the domain? Maybe the field has been renamed? I see production_location_id on the parent, could it be it?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are 3 fields referring to locations in the parent model...

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't care since it's invisible

@@ -79,7 +79,7 @@
<span><field name="product_uom_qty" readonly="1" nolabel="1"/></span>
<span><field name="product_uom_id" attrs="{'readonly': [('id', '!=', False)]}" nolabel="1"/></span>
</div>
<field name="lot_id" string="Lot/Serial number" groups="stock.group_production_lot" readonly="1" attrs="{'invisible': [('lots_visible', '=', False)]}"/>
<field name="lot_id" string="Lot/Serial number" groups="stock.group_production_lot" readonly="1"/>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lots_visible still exists on the model ; invisible isn't needed anymore?

Copy link
Contributor

@amoyaux amoyaux Nov 14, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add the field lots_visible as invisible instead.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removing the attrs keeps the behavior as is. We can also add the field lots_visible in the view...

})

@mute_logger('odoo.addons.base.ir.ir_ui_view')
def test_context_in_subview(self):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would have added a test if model is not in the subview but in the main view.

</form>
""",
})
with self.assertRaises(ValidationError):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Idem (error if model in main view but not in subview).

@pimodoo
Copy link
Collaborator

pimodoo commented Nov 14, 2017

@sle-odoo

@@ -9,7 +9,7 @@
<field name="product_id"/>
<field name="product_uom_qty"/>
<field name="product_uom" options="{'no_open': True, 'no_create': True}" string="Unit of Measure" groups="product.group_uom"/>
<field name="product_packaging" domain="[('product_tmpl_id','=',product_tmpl_id)]" groups="product.group_stock_packaging"/>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should replace it by domain="[('product_id','=',product_id)]" instead of remove it

@@ -183,7 +183,7 @@
<tree editable="bottom" decoration-muted="state == 'done' and is_locked == True" decoration-success="product_uom_qty==qty_done" decoration-danger="qty_done &gt; product_uom_qty and state != 'done'">
<field name="picking_id" invisible="1"/>
<field name="product_id" invisible="1"/>
<field name="location_id" invisible="not context.get('show_source_location')" domain="[('id', 'child_of', parent.location_id)])" groups="stock.group_stock_multi_locations"/>
<field name="location_id" invisible="not context.get('show_source_location')" groups="stock.group_stock_multi_locations"/>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What should we do? Do we need to duplicate twice this view and add them in two inline views instead of using tree_view_ref? Or create a related field parent_location_id on stock_move_line?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@amoyaux We can also add the missing field location_id in the parent view(s).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually this domain had a typo (extra closing parenthesis).
So I just fixed the typo 😄

@rco-odoo rco-odoo force-pushed the 11.0-opw-779494-rco branch 3 times, most recently from 76a2c9c to 6d7a408 Compare November 15, 2017 09:22
@nhomar
Copy link
Collaborator

nhomar commented Nov 15, 2017

please my friends, do not even think put this in master PLEASE ;-)

@rco-odoo
Copy link
Member Author

@nhomar why? Can you elaborate on this?

The main purpose of this validation is to prevent people from screwing up views with Studio. We recently got examples of users removing fields from views, and making domains impossible to evaluate. The issue was totally invisible while in Studio, but the view kept on crashing when used for real ☹️

@nhomar
Copy link
Collaborator

nhomar commented Nov 15, 2017

@rco-odoo I mean I love it in 11.0 as it is, but the point is that the stable policy does not allow fix this kind of big things in stable and this PR is very nice, this was the reason of my comments.

# retrieve subfields of 'parent'
get = partial(get_parent, get)
for child in node:
yield from node_names(child, get)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That doesn't seem like a hill worth removing Python 2 support on

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not funny. I wanted to break some stuff ;-)

return ['self', 'parent', 'id', 'uid', 'context',
'active_id', 'active_ids', 'active_model',
'time', 'datetime', 'relativedelta',
'context_today', 'current_date']
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could return a set directly.

return (name
for node in ast.walk(ast.parse(expr.strip(), mode='eval'))
for name in [get(node)]
if name)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why make these inner functions? That seems unnecessary.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Readability. It took me a while to get the algorithm right.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I understand having them as functions (that seems fair), just not inner ones, why not toplevel?

attrs = node.get('attrs')
# collect field names in left-hand-side of conditions
for domain in safe_eval(attrs).values():
if isinstance(domain, list):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there any case where the domain could be a non-list?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It once crashed with a boolean domain. Maybe we should fix the domain...

@rco-odoo
Copy link
Member Author

@nhomar I understand and I always keep this aspect in mind.
On the other hand, we need something to validate the views modified by Studio. We have recently encountered support tickets where people screw up views with Studio without noticing...

I was thinking about using this when actually validating the view, but not when building the view for the client (via fields_view_get). This would ensure validation when you modify views, but not when you simply use them.

@wtaferner
Copy link
Contributor

wtaferner commented Nov 16, 2017

@rco-odoo
I am not surprised that Odoo Studio is screwing up a lot even though I think you guys did a great job that this kind of marketing tool can be helpful, even for devs at some point (now I am trying to keep my customers in bounce that they do not screw up on their new freedom too fast).
A lot of work still to do and ahead as we talked on Odoo experience to have all different data sources reasonably under control, aware to performance through recommendations and hints and most of all flexible and endurable over versions (still ask myself how Odoo will handle studio customization in migrations...deactivating breaking views and removing fields is probably not the smartest move when you sold it as a killer feature), but keep on rockin :-)

@rco-odoo rco-odoo force-pushed the 11.0-opw-779494-rco branch 2 times, most recently from 50f5217 to c4566f4 Compare November 21, 2017 09:16
@rco-odoo rco-odoo force-pushed the 11.0-opw-779494-rco branch 2 times, most recently from 50537cc to 3ac561a Compare November 23, 2017 16:22
@rco-odoo
Copy link
Member Author

@mart-e I add a new translated term here (error message).
Should I regenerate the .pot file?

@rco-odoo
Copy link
Member Author

@odony this adds some extra validation in views, to answer a request from the Studio team.
We recently got several support tickets for users that screw up views with Studio: they remove a field that is used in a domain in another field, for instance. The idea is, in the view validation, to check that all fields appearing in domains, contexts, etc. are effectively present in the view.

I would very much appreciate your review. Implementing the validation brought a few surprises: some views in Odoo 11 are not valid. Hopefully they do not seem to break anything so far, because of domains never being evaluated or so.

@rco-odoo rco-odoo requested a review from odony November 24, 2017 11:11
@rco-odoo
Copy link
Member Author

rco-odoo commented Dec 1, 2017

ping @odony

Copy link
Contributor

@odony odony left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks very good and elegant, considering the complexity of the verification, thanks!

It will be excellent with the small improvement to the error message, as discussed.

@rco-odoo rco-odoo merged commit 0109c9e into odoo:11.0 Dec 7, 2017
@rco-odoo rco-odoo deleted the 11.0-opw-779494-rco branch December 7, 2017 13:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
OE the report is linked to a support ticket (opw-...)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

10 participants