Odoo Help

Welcome!

This community is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.

1

Datetime Constarints

By
Logicious
on 9/3/15, 3:21 AM 543 views

i have one problem , i have one class named as room details and second one is room booking. we can go to book a room by room booking model. I want to create a constraint to it . Problem is that if one person can book one room at one date&time if second person can book that room may be after that time or before that time.previously i askd this question , and there are some answers also i got but i cant figure it out.I cant able to find out the solution fromr that, please help me.

i will give my code


class room_management(osv.Model):

_name = 'room.management'

_columns = {

'name': fields.char('Name',requierd=True, help='Put a Name for your Room example: Interview room,board room,conference Hall...etc '),

'location':fields.char('Location',requierd=True,help="give the location as city name or street name..etc"),

'floar':fields.char('Floor Details',help='Enter the Foar no or name,like Foar 4B6 or somthing should identification'),

'address':fields.text('Address',requierd=True,help='Detailed Address'),

'no_seats':fields.integer('No of Seats',requierd=True,help='Number of seats occupied in this room'),

'room_no':fields.char('Room No',requierd=True,help='Should be unique',),

}


class room_booking(osv.Model):

_name = 'room.booking'

_columns = {

'room_id' : fields.many2one('room.management', string="Room Booking"),

'duration': fields.integer('Duration'),

'reason': fields.char('Reason',requierd=True ,help="short deatails about booking,Example:Simons Hr interview"),

'start': fields.datetime('Start At',requierd=True),

'end': fields.datetime('End At',requierd=True),

}


1
Marvin Taboada
On 9/4/15, 2:34 AM

Hello Logicious,

This kind of constraints are applicable for creation but also for edition. If you really want to perform input validation overwriting `create` also overwrite `write` as well.

A cleaner solution to cover both cases is to create Python based constraints like the one you can see in `hr_holidays`, please see here:

https://github.com/odoo/odoo/blob/8.0/addons/hr_holidays/hr_holidays.py#L208

* https://github.com/odoo/odoo/blob/8.0/addons/hr_holidays/hr_holidays.py#L152

Constraints are automatically invoked during creation or edition:

* https://github.com/odoo/odoo/blob/8.0/openerp/models.py#L3949 

* https://github.com/odoo/odoo/blob/8.0/openerp/models.py#L4273 

* https://github.com/odoo/odoo/blob/8.0/openerp/models.py#L1239 

Please also note tha Odoo 8 introduced a new way to declare constraints (and deprecated the above):

https://www.odoo.com/documentation/8.0/reference/orm.html#openerp.models.Model._inherits

https://www.odoo.com/documentation/8.0/reference/orm.html#openerp.api.constrains

Regards.

2
pks
On 9/3/15, 9:23 AM

you just try this i think its will work fine as your wish :-

def create(self, cr, uid, vals, context=None):

all_room_ids=self.search(cr,uid,[('room_id','=',vals['room_id'])])

for room_id in all_room_ids     

       room_data=self.browse(cr,uid,room_id)          

                      if ((vals['start'] >= room_data.start) and (vals['end'] <= room_data.start)) or (vals['start'] >= room_data.end):

                                  print'yyyyyyyyyyyyy',room_data

                      else:

                              raise osv.except_osv(_('Error!'),_("Room Already booked."))

0
Logicious
On 9/3/15, 5:26 AM

@prasanth: thanks prasanth. its working perfectly

0
Akhilesh Gupta
On 9/3/15, 3:39 AM

Hi,

You should make room booking constraints on the basis of room status Or you can create warning if it already booked.

May it will help you.

Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

About This Community

This community is for professionals and enthusiasts of our products and services. Read Guidelines

Question tools

1 follower(s)

Stats

Asked: 9/3/15, 3:21 AM
Seen: 543 times
Last updated: 9/24/15, 12:53 AM