[Openerp-community] Reclaiming context

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

[Openerp-community] Reclaiming context

Sandy Carter (http://www.savoirfairelinux.com)
Hello community,


While debugging yet another translation error in Odoo 7.0, I came up
with a simple way to reclaim lost contexts in functions.

The bug I was fighting is one where workflow actions do not pass any
contexts. This bug manifests as chattr messages in english when using
the workflows.

As I don't have the time to fix it in the server, nor am I ready to jump
through the hoops to get that fix integreated in the Odoo, I found a way
to fix this bug in my module.

The way I countered the bug in my module is in the write function where
I would usually start with

    if context is None:
        context = {}

or

    context = context or {}

In this case it was None since the workflow mechanism would not have a
context. I saw that since I have the user id, I can still get the
context for this user.

I replaced the familiar context check with:

    context = context or self.pool['res.users'].context_get(cr, uid)

I find this simple enough. The line is under 80 characters and it can
replace the usual context check.

Furthermore it assures that everything done within this function and its
parent functions have at least the language in the context, something I
have been struggling with a lot lately.


What does the community think of this as a possible standard for
modules, replacing the usual if context is None lines.

Are there possible drawbacks to using this line?

Thank you for you input!

--
Sandy Carter
Consultant en logiciel libre
Développeur OpenERP
Savoir-faire Linux Inc.
514-276-5468 poste 142
http://www.savoirfairelinux.com
[hidden email]


_______________________________________________
Mailing list: https://launchpad.net/~openerp-community
Post to     : [hidden email]
Unsubscribe : https://launchpad.net/~openerp-community
More help   : https://help.launchpad.net/ListHelp

signature.asc (484 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: [Openerp-community] Reclaiming context

Raphael Valyi
Hello Sandy,

I think this is a great idea.

It has been debated a few times if it was a bug that the workflow engine doesn't propagate the context. Well in my opinion that's valid to say the context is a compound object made by the user settings, overridden eventually by the parameters of the HTTP request scope. These last parameters stand for user interface details like from which menu you are trying to act on a record or what are the currently selected records. It's normal that the impact of these parameters is limited and it's normal that you can also imagine load the persisted data right from the database instead and still get the right business logic.
So in a word, even in the workflow which is supposed to be able to act right on persisted data, it's okay to loose the track of the HTTP request params but as you mentioned this is not a reason to loose the context related to the user settings (language, timezone...)

I personally like your proposal for v7. As for v8 the similar logic should happen in the @api decorator providing the context on self and I am not sure, but may be it's already implemented like that.

Thanks for the proposition!

-- 
Raphaël Valyi
Founder and consultant
+55 21 3942-2434



On Thu, Aug 28, 2014 at 1:04 PM, Sandy Carter <[hidden email]> wrote:
Hello community,


While debugging yet another translation error in Odoo 7.0, I came up
with a simple way to reclaim lost contexts in functions.

The bug I was fighting is one where workflow actions do not pass any
contexts. This bug manifests as chattr messages in english when using
the workflows.

As I don't have the time to fix it in the server, nor am I ready to jump
through the hoops to get that fix integreated in the Odoo, I found a way
to fix this bug in my module.

The way I countered the bug in my module is in the write function where
I would usually start with

    if context is None:
        context = {}

or

    context = context or {}

In this case it was None since the workflow mechanism would not have a
context. I saw that since I have the user id, I can still get the
context for this user.

I replaced the familiar context check with:

    context = context or self.pool['res.users'].context_get(cr, uid)

I find this simple enough. The line is under 80 characters and it can
replace the usual context check.

Furthermore it assures that everything done within this function and its
parent functions have at least the language in the context, something I
have been struggling with a lot lately.


What does the community think of this as a possible standard for
modules, replacing the usual if context is None lines.

Are there possible drawbacks to using this line?

Thank you for you input!

--
Sandy Carter
Consultant en logiciel libre
Développeur OpenERP
Savoir-faire Linux Inc.
<a href="tel:514-276-5468" value="+15142765468">514-276-5468 poste 142
http://www.savoirfairelinux.com
[hidden email]


_______________________________________________
Mailing list: https://launchpad.net/~openerp-community
Post to     : [hidden email]
Unsubscribe : https://launchpad.net/~openerp-community
More help   : https://help.launchpad.net/ListHelp



_______________________________________________
Mailing list: https://launchpad.net/~openerp-community
Post to     : [hidden email]
Unsubscribe : https://launchpad.net/~openerp-community
More help   : https://help.launchpad.net/ListHelp
Reply | Threaded
Open this post in threaded view
|

Re: [Openerp-community] Reclaiming context

Pedro Manuel Baeza Romero
It looks good to me also, but one detail, put:

if context is None:
    context = self.pool['res.users'].context_get(cr, uid)


To not make the query when context dictionary is already {} (maybe you can be interested in not propagating user context and you pass explicitly context={} in your call to the function).

Regards.


2014-08-28 18:35 GMT+02:00 Raphael Valyi <[hidden email]>:
Hello Sandy,

I think this is a great idea.

It has been debated a few times if it was a bug that the workflow engine doesn't propagate the context. Well in my opinion that's valid to say the context is a compound object made by the user settings, overridden eventually by the parameters of the HTTP request scope. These last parameters stand for user interface details like from which menu you are trying to act on a record or what are the currently selected records. It's normal that the impact of these parameters is limited and it's normal that you can also imagine load the persisted data right from the database instead and still get the right business logic.
So in a word, even in the workflow which is supposed to be able to act right on persisted data, it's okay to loose the track of the HTTP request params but as you mentioned this is not a reason to loose the context related to the user settings (language, timezone...)

I personally like your proposal for v7. As for v8 the similar logic should happen in the @api decorator providing the context on self and I am not sure, but may be it's already implemented like that.

Thanks for the proposition!

-- 
Raphaël Valyi
Founder and consultant
<a href="tel:%2B55%2021%203942-2434" value="+552139422434" target="_blank">+55 21 3942-2434



On Thu, Aug 28, 2014 at 1:04 PM, Sandy Carter <[hidden email]> wrote:
Hello community,


While debugging yet another translation error in Odoo 7.0, I came up
with a simple way to reclaim lost contexts in functions.

The bug I was fighting is one where workflow actions do not pass any
contexts. This bug manifests as chattr messages in english when using
the workflows.

As I don't have the time to fix it in the server, nor am I ready to jump
through the hoops to get that fix integreated in the Odoo, I found a way
to fix this bug in my module.

The way I countered the bug in my module is in the write function where
I would usually start with

    if context is None:
        context = {}

or

    context = context or {}

In this case it was None since the workflow mechanism would not have a
context. I saw that since I have the user id, I can still get the
context for this user.

I replaced the familiar context check with:

    context = context or self.pool['res.users'].context_get(cr, uid)

I find this simple enough. The line is under 80 characters and it can
replace the usual context check.

Furthermore it assures that everything done within this function and its
parent functions have at least the language in the context, something I
have been struggling with a lot lately.


What does the community think of this as a possible standard for
modules, replacing the usual if context is None lines.

Are there possible drawbacks to using this line?

Thank you for you input!

--
Sandy Carter
Consultant en logiciel libre
Développeur OpenERP
Savoir-faire Linux Inc.
<a href="tel:514-276-5468" value="+15142765468" target="_blank">514-276-5468 poste 142
http://www.savoirfairelinux.com
[hidden email]


_______________________________________________
Mailing list: https://launchpad.net/~openerp-community
Post to     : [hidden email]
Unsubscribe : https://launchpad.net/~openerp-community
More help   : https://help.launchpad.net/ListHelp



_______________________________________________
Mailing list: https://launchpad.net/~openerp-community
Post to     : [hidden email]
Unsubscribe : https://launchpad.net/~openerp-community
More help   : https://help.launchpad.net/ListHelp



_______________________________________________
Mailing list: https://launchpad.net/~openerp-community
Post to     : [hidden email]
Unsubscribe : https://launchpad.net/~openerp-community
More help   : https://help.launchpad.net/ListHelp
Reply | Threaded
Open this post in threaded view
|

Re: [Openerp-community] Reclaiming context

Nicolas Bessi - Camptocamp
Hello, 

+1 for Pedro proposition

As the function is decorated with "@tools.ormcache(skiparg=2)" it should not have a too big negative impact on performance.
We may initialise an empty context if it really causes performance issues.

context_get may also be optimized to use a read instead of a browse.


Regards

Nicolas



2014-08-28 18:45 GMT+02:00 Pedro Manuel Baeza Romero <[hidden email]>:
It looks good to me also, but one detail, put:

if context is None:
    context = self.pool['res.users'].context_get(cr, uid)


To not make the query when context dictionary is already {} (maybe you can be interested in not propagating user context and you pass explicitly context={} in your call to the function).

Regards.


2014-08-28 18:35 GMT+02:00 Raphael Valyi <[hidden email]>:

Hello Sandy,

I think this is a great idea.

It has been debated a few times if it was a bug that the workflow engine doesn't propagate the context. Well in my opinion that's valid to say the context is a compound object made by the user settings, overridden eventually by the parameters of the HTTP request scope. These last parameters stand for user interface details like from which menu you are trying to act on a record or what are the currently selected records. It's normal that the impact of these parameters is limited and it's normal that you can also imagine load the persisted data right from the database instead and still get the right business logic.
So in a word, even in the workflow which is supposed to be able to act right on persisted data, it's okay to loose the track of the HTTP request params but as you mentioned this is not a reason to loose the context related to the user settings (language, timezone...)

I personally like your proposal for v7. As for v8 the similar logic should happen in the @api decorator providing the context on self and I am not sure, but may be it's already implemented like that.

Thanks for the proposition!

-- 
Raphaël Valyi
Founder and consultant
<a href="tel:%2B55%2021%203942-2434" value="+552139422434" target="_blank">+55 21 3942-2434



On Thu, Aug 28, 2014 at 1:04 PM, Sandy Carter <[hidden email]> wrote:
Hello community,


While debugging yet another translation error in Odoo 7.0, I came up
with a simple way to reclaim lost contexts in functions.

The bug I was fighting is one where workflow actions do not pass any
contexts. This bug manifests as chattr messages in english when using
the workflows.

As I don't have the time to fix it in the server, nor am I ready to jump
through the hoops to get that fix integreated in the Odoo, I found a way
to fix this bug in my module.

The way I countered the bug in my module is in the write function where
I would usually start with

    if context is None:
        context = {}

or

    context = context or {}

In this case it was None since the workflow mechanism would not have a
context. I saw that since I have the user id, I can still get the
context for this user.

I replaced the familiar context check with:

    context = context or self.pool['res.users'].context_get(cr, uid)

I find this simple enough. The line is under 80 characters and it can
replace the usual context check.

Furthermore it assures that everything done within this function and its
parent functions have at least the language in the context, something I
have been struggling with a lot lately.


What does the community think of this as a possible standard for
modules, replacing the usual if context is None lines.

Are there possible drawbacks to using this line?

Thank you for you input!

--
Sandy Carter
Consultant en logiciel libre
Développeur OpenERP
Savoir-faire Linux Inc.
<a href="tel:514-276-5468" value="+15142765468" target="_blank">514-276-5468 poste 142
http://www.savoirfairelinux.com
[hidden email]


_______________________________________________
Mailing list: https://launchpad.net/~openerp-community
Post to     : [hidden email]
Unsubscribe : https://launchpad.net/~openerp-community
More help   : https://help.launchpad.net/ListHelp



_______________________________________________
Mailing list: https://launchpad.net/~openerp-community
Post to     : [hidden email]
Unsubscribe : https://launchpad.net/~openerp-community
More help   : https://help.launchpad.net/ListHelp



_______________________________________________
Mailing list: https://launchpad.net/~openerp-community
Post to     : [hidden email]
Unsubscribe : https://launchpad.net/~openerp-community
More help   : https://help.launchpad.net/ListHelp




--
Nicolas Bessi
Senior ERP consultant
Business Solution technical manager

Camptocamp SA
PSE A
CH-1015 Lausanne

_______________________________________________
Mailing list: https://launchpad.net/~openerp-community
Post to     : [hidden email]
Unsubscribe : https://launchpad.net/~openerp-community
More help   : https://help.launchpad.net/ListHelp
Reply | Threaded
Open this post in threaded view
|

Re: [Openerp-community] Reclaiming context

Alexandre Fayolle - camptocamp
In reply to this post by Sandy Carter (http://www.savoirfairelinux.com)
On 28/08/2014 18:04, Sandy Carter wrote:

> Hello community,
>
>
> While debugging yet another translation error in Odoo 7.0, I came up
> with a simple way to reclaim lost contexts in functions.
>
> The bug I was fighting is one where workflow actions do not pass any
> contexts. This bug manifests as chattr messages in english when using
> the workflows.
>
> As I don't have the time to fix it in the server, nor am I ready to jump
> through the hoops to get that fix integreated in the Odoo, I found a way
> to fix this bug in my module.
>
> The way I countered the bug in my module is in the write function where
> I would usually start with
>
>     if context is None:
>         context = {}
>
> or
>
>     context = context or {}
>
> In this case it was None since the workflow mechanism would not have a
> context. I saw that since I have the user id, I can still get the
> context for this user.
>
> I replaced the familiar context check with:
>
>     context = context or self.pool['res.users'].context_get(cr, uid)
>

Ideally this would be done by the workflow engine, but I'm pretty sure
this won't make it to official 7.0 and not sure for OCB. But I do like
the version you propose *a lot*. Let's use it more!

--
Alexandre Fayolle
Chef de Projet
Tel : + 33 (0)4 79 26 57 94

Camptocamp France SAS
Savoie Technolac, BP 352
73377 Le Bourget du Lac Cedex
http://www.camptocamp.com



_______________________________________________
Mailing list: https://launchpad.net/~openerp-community
Post to     : [hidden email]
Unsubscribe : https://launchpad.net/~openerp-community
More help   : https://help.launchpad.net/ListHelp
Reply | Threaded
Open this post in threaded view
|

Re: [Openerp-community] Reclaiming context

sebastien beau
Really good idea Sandy !
I totaly approve this change


2014-08-29 9:34 GMT+02:00 Alexandre Fayolle <[hidden email]>:
On 28/08/2014 18:04, Sandy Carter wrote:
> Hello community,
>
>
> While debugging yet another translation error in Odoo 7.0, I came up
> with a simple way to reclaim lost contexts in functions.
>
> The bug I was fighting is one where workflow actions do not pass any
> contexts. This bug manifests as chattr messages in english when using
> the workflows.
>
> As I don't have the time to fix it in the server, nor am I ready to jump
> through the hoops to get that fix integreated in the Odoo, I found a way
> to fix this bug in my module.
>
> The way I countered the bug in my module is in the write function where
> I would usually start with
>
>     if context is None:
>         context = {}
>
> or
>
>     context = context or {}
>
> In this case it was None since the workflow mechanism would not have a
> context. I saw that since I have the user id, I can still get the
> context for this user.
>
> I replaced the familiar context check with:
>
>     context = context or self.pool['res.users'].context_get(cr, uid)
>

Ideally this would be done by the workflow engine, but I'm pretty sure
this won't make it to official 7.0 and not sure for OCB. But I do like
the version you propose *a lot*. Let's use it more!

--
Alexandre Fayolle
Chef de Projet
Tel : <a href="tel:%2B%2033%20%280%294%2079%2026%2057%2094" value="+33479265794">+ 33 (0)4 79 26 57 94

Camptocamp France SAS
Savoie Technolac, BP 352
73377 Le Bourget du Lac Cedex
http://www.camptocamp.com



_______________________________________________
Mailing list: https://launchpad.net/~openerp-community
Post to     : [hidden email]
Unsubscribe : https://launchpad.net/~openerp-community
More help   : https://help.launchpad.net/ListHelp


_______________________________________________
Mailing list: https://launchpad.net/~openerp-community
Post to     : [hidden email]
Unsubscribe : https://launchpad.net/~openerp-community
More help   : https://help.launchpad.net/ListHelp