Skip to Content
Menu
This question has been flagged

Hello,


is it possible to use a domain filter in a record rule that works with a datetime field to filter all entries from "today - 4 weeks" to "today"?


Greetings

RSK

Avatar
Discard
Best Answer

I don't think you can do this in a Record Rule, but you could add a filter in an Extension Search View and use that in the Windows Action.

Something like this:

<?xml version="1.0"?>
<xpath expr="//filter[@name='my_sale_orders_filter']" position="after">
  <filter string="Last 4 weeks" name="four_weeks"
      domain="[('date_order','&gt;=',(context_today() +
                relativedelta(weeks=-4, weekday=0)).strftime('%Y-%m-%d'))]"
        />
</xpath>
Then this new filter needs to be set as context in the Windows Action:

{'search_default_four_weeks': 1}]
But users can remove the filter.


=====

Another idea is to create a new boolean field on Sales Orders, and run a scheduled job every day to set this ON when an order is 4 weeks old.  Then you can create a Record Rule to limit access to older orders.

This is an example of the Python code, but you'd need to create a new module to run it (Scheduled Actions can't run Server Actions):

for rec in records:
  rec['x_recent_order'] = False
  if rec.date_order > (datetime.datetime.today() - datetime.timedelta(weeks=4)):
    rec['x_recent_order'] = True 

x_recent_order is the new boolean field.  Hope this helps!

Avatar
Discard
Author

Will this filter be applied and forced for every user?

No, it will be a filter that users can select.

Author

Then it's not what I'm looking for. Thanks anyway

Yes, filters won't do that. I updated my answer and added another idea.

Author

Sounds like the best way for me. I already got the record rule and the boolean field going. This is working as expected, now I just need to get the scheduled task working. Do you have the python code example for that?

I'm not really a Python programmer, but I have added some code that I tested in a Server Action. Should be straightforward for a developer to add this to a Scheduled Action.

Best Answer

Refer Odoo developer documentation https://www.odoo.com/documentation/13.0/reference/security.html

domain used to check whether a given record matches the rule (and is accessible) or does not (and is not accessible). The domain is evaluated with two variables in context: user is the current user’s record and time is the time module

You can use only two predefined variables in the record rule. 

user

    It's the browsable object of res.users model. 

time

    is the time module which provides various time-related functions

Avatar
Discard
Related Posts Replies Views Activity
2
Oct 22
9950
3
Jan 24
624
1
Dec 23
2047
2
May 21
1568
0
Mar 15
2967