[IMP]GOLEM Resource check : better performance with pre-calculation and restricted domain search for conflicts

This commit is contained in:
Fabien BOURGEOIS 2018-03-17 08:39:43 +01:00
parent d5cc394967
commit 398a3e7f63

View File

@ -176,22 +176,25 @@ class GolemResourceReservation(models.Model):
is_day_allowed = True is_day_allowed = True
#only check if the day hasn't a 24 availibility #only check if the day hasn't a 24 availibility
if not timetable.availibility_24: if not timetable.availibility_24:
hour_start = 0.0 reservation_day_date = reservation_day.date()
hour_stop = 0.0 day_start = date_start.date()
if (reservation_day.date() == date_start.date() and day_stop = date_stop.date()
reservation_day.date() == date_stop.date()): if reservation_day_date == day_start and \
reservation_day_date == day_stop:
hour_start = date_start.hour + date_start.minute / 60.0 hour_start = date_start.hour + date_start.minute / 60.0
hour_stop = date_stop.hour + date_stop.minute / 60.0 hour_stop = date_stop.hour + date_stop.minute / 60.0
elif reservation_day.date() == date_start.date(): elif reservation_day_date == day_start:
hour_start = date_start.hour + date_start.minute / 60.0 hour_start = date_start.hour + date_start.minute / 60.0
hour_stop = 23.98 hour_stop = 23.98 # Just before 23:59
elif reservation_day.date() == date_stop.date(): elif reservation_day_date == day_stop:
hour_start = 0.0 hour_start = 0.0
hour_stop = date_stop.hour + date_stop.minute / 60.0 hour_stop = date_stop.hour + date_stop.minute / 60.0
else: else:
#if the day is not a start nor stop it's not allowed unless #if the day is not a start nor stop it
#availibility_24 is True #should be covered on all day
is_day_allowed = False #strange, as availibility_24 is not True
hour_start = 0.0
hour_stop = 23.98
if is_day_allowed and (hour_start < timetable.time_start or \ if is_day_allowed and (hour_start < timetable.time_start or \
hour_stop > timetable.time_stop): hour_stop > timetable.time_stop):
@ -201,10 +204,15 @@ class GolemResourceReservation(models.Model):
raise ValidationError(verr) raise ValidationError(verr)
if not is_day_allowed: if not is_day_allowed:
verr = _('Not allowed, the resource is not available ' verr = _('Not allowed, the resource is not available '
'this day. Please choose another date.') 'this day : {}. Please choose another '
'date.'.format(reservation_day.strftime('%A')))
raise ValidationError(verr) raise ValidationError(verr)
# Check if the resource is already taken during this period # Check if the resource is already taken during this period
# PERF : check for res that can be in conflict,
# do not iterate over all reservations
domain = [('resource_id', '=', reservation.resource_id.id), domain = [('resource_id', '=', reservation.resource_id.id),
('date_start', '<=', reservation.date_stop),
('date_stop', '>=', reservation.date_start),
('state', 'in', ('confirmed', 'validated')), ('state', 'in', ('confirmed', 'validated')),
('id', '!=', reservation.id)] ('id', '!=', reservation.id)]
reservations = self.env['golem.resource.reservation'].search(domain) reservations = self.env['golem.resource.reservation'].search(domain)