Shravy
Shravy

Reputation: 666

How to make previous day's date and today's date only accessible in date time picker in Odoo?

I am trying to check on a condition where the today's date is only accessible to get selected in the date time picker in Odoo and the code is as follows.

def onchange_date(self, cr, uid, ids, fdate, context=None):
if fdate:
if datetime.strptime(fdate, "%Y-%m-%d %H:%M:%S").date() <> 
datetime.now().date():
return { 'value': { 'fdate': False } }
return fdate

I also wanted to make the previous day i.e yesterday's date to be available to the user to get selected along with today's date.

Upvotes: 1

Views: 2598

Answers (2)

Juan Salcedo
Juan Salcedo

Reputation: 1668

If you only use Dates, you should use date instead of datetime, and if first if you only want a user use the date of today, get automatically the date of today and put it readonly, something like this:

...
import time
...
#here you got a field with the date of today.
date_today = fields.Date('Today is: ', default = lambda *a: time.strftime("%Y-%m-%d"), readonly = True)

And if you want what one user only be able to choose the days yesterday and today, it's a little bit more laborious, I know this is not the best way to do it but it work fine.

...
from openerp.exceptions import ValidationError
...

chosen_date = fields.Date('Chosen day: ', default = lambda *a: time.strftime("%Y-%m-%d"))

@api.onchange('chosen_date'):
def _onchange_chosen_date(self):
    yesterday = str( int(time.strftime("%d")) - 1 )
    if len(yesterday) == 1:
        yesterday = '0' + yesterday
    yesterday_date = time.strftime("%Y-%m-" + yesterday)
    if self.chosen_date != time.strftime("%Y-%m-%d") or self.chosen_date != yesterday_date:
        return { 'warning': {'title':"Warning", 'message':"You are only able to choose only yesterday and today...!"}}

EDIT

I only use V8, but I know something of V7 syntax!

1th.

...
import time
...
#here you got a field with the date of today.
date_today = fields.date('Today is: ', default = lambda *a: time.strftime("%Y-%m-%d"), readonly = True)

2th.

...
from openerp.osv import osv
...

chosen_date = fields.date('Chosen day: ', default = lambda *a: time.strftime("%Y-%m-%d"))

def _onchange_chosen_date(self, cr, uid, ids, chosen_date, context=None):
    yesterday = str( int(time.strftime("%d")) - 1 )
    if len(yesterday) == 1:
        yesterday = '0' + yesterday
    yesterday_date = time.strftime("%Y-%m-" + yesterday)
    if self.chosen_date != time.strftime("%Y-%m-%d") or self.chosen_date != yesterday_date:
        raise osv.except_osv(("Warning"),("You are only able to choose only yesterday and today...!"))

And in the XML:

<field name="chosen_date" on_change="_onchange_chosen_date(chosen_date)" />

I hope this can be helpful for you!!!

Upvotes: 1

Vigneshwaran Thenraj
Vigneshwaran Thenraj

Reputation: 708

selected_date = datetime.strptime(fdate, "%Y-%m-%d %H:%M:%S").date()
yes_date = datetime.strptime(fdate, "%Y-%m-%d %H:%M:%S").relativedelta(days = -1).date()
today = datetime.now().date()
if selected_date <> today and selected_date <> yes_date

And add the import file for relative delte

Upvotes: 1

Related Questions