0

I have a custom module in openerp 7 with fields check-in time(date-time) and check-out time(date-time). When I click on save, i want to perform a validation on both fields to ensure check-out time is not less than check-in time. Thanks for any ideas.

2 Answers2

1

As above, use datetime.

In Odoo your dates, times and datetimes are handed to use as strings formatted using

openerp.tools.DEFAULT_SERVER_DATE_FORMAT, DEFAULT_SERVER_TIME_FORMAT and DEFAULT_SERVER_DATETIME_FORMAT.

from datetime import datetime
from openerp.tools import DEFAULT_SERVER_DATETIME_FORMAT

check_in = datetime.strptime(my_object.check_in, DEFAULT_SERVER_DATETIME_FORMAT)
check_out = datetime.strptime(my_object.check_out, DEFAULT_SERVER_DATETIME_FORMAT)

Go nuts with comparisons etc.

A couple of notes:

  1. I highly recommend reading up on the datetime module in the standard library, particularly strftime, strptime and timedelta
  2. Remember you will be getting the dates and datetimes in UTC. The classes that represent the date and datetime fields have methods to return dates and timestamps in the users' timezone but you will not usually need these. Have a look at fields.date.context_today and fields.datetime.context_timestamp
Adrian Merrall
  • 2,509
  • 12
  • 9
0

I would try to use the datetime class from the datetime module.

  1. Import relevant python module

    from datetime import datetime

  2. Retrieve your record via the appropriate method i.e.

    your_record = self.pool.get('your_custom_module').search(cr, uid, domain, offset=0, limit=None, order=None, context=None, count=False)

note: you need to provide proper domain and modify/remove arguments to suit you needs

  1. Create datetime objects from relevant fields (use the strptime method of datetime class : create a date object from a string). Something like :

    check_in = datetime.strptime(your_record[0]['check-in time'], '%Y-%m-%d') check_out = datetime.strptime(your_record[0]['check-out time'], '%Y-%m-%d')

note: you need to adapt the format('%Y-%m-%d') to whatever format your DB returns

  1. Compare both object with a simple expression :

    if check_in < check_out: ... else: ...

  2. Do whatever other operations need to be done.

It's kinda hard to provide more info without additional details about your flow.

Hope this helps, Cheers

zee
  • 174
  • 4