def clean_time(self): now = datetime.datetime.now() data = self.cleaned_data.get('time') if not data or not self.cleaned_data.get('date'): return data start = datetime.datetime.combine(self.cleaned_data['date'], data) end = start + datetime.timedelta(minutes=self.service.duration) if start.date() != end.date(): raise ValidationError( _('Sorry, reservation can\'t span over multiple days.')) # Check current time with date if start < now: raise ValidationError( _('Sorry, you can\'t make a reservation in the past.')) service_provider = self.service.service_provider # Check working hours wrk = WorkingHours.get_for_day(service_provider, start.weekday()) if start.time() < wrk.time_from: raise ValidationError( _('Sorry, the service isn\'t available before ' + str(wrk.time_from)[:-3])) elif end.time() > wrk.time_to: raise ValidationError( _('Sorry, the service is closed from ' + str(wrk.time_to)[:-3])) # Check pauses for wrkBr in wrk.breaks.all(): if is_overlapping(start.time(), end.time(), wrkBr.time_from, wrkBr.time_to): raise ValidationError( _('Sorry, the service isn\'t available at specified time.') ) # Check reservations service = self.service employee = self.service_provider_employee reservations = Reservation.objects.filter( service=service, service_provider_employee=employee, service_provider=service_provider, date=self.cleaned_data.get('date')) for res in reservations: resDt = datetime.datetime.combine(res.date, res.time) if is_overlapping( start, end, resDt, resDt + datetime.timedelta(minutes=res.service_duration)): raise ValidationError( _('Sorry, your reservation is overlapping with another reservation.' )) return data
def check_if_there_is_free_employee(service, start, end, date): service_employees = ServiceProviderEmployee.objects.filter(service=service) free_employees = [] if service_employees <= 1: return None else: for employee in service_employees: employee_reservations = Reservation.objects.filter( service_provider_employee=service_employees, date=date) if not employee_reservations: free_employees.append(employee) else: its_free = True for reservation in employee_reservations: if is_overlapping( start, end, date, date + datetime.timedelta( minutes=reservation.service_duration)): its_free = False break if its_free: free_employees.append(employee) if not free_employees: return None else: return free_employees
def clean(self): intervals = [] for form in self.forms: time_from = form.cleaned_data.get('time_from') time_to = form.cleaned_data.get('time_to') if time_from and time_to: for time_from2, time_to2 in intervals: if is_overlapping(time_from, time_to, time_from2, time_to2): raise ValidationError(_('Breaks can\'t overlap.')) intervals.append((time_from, time_to))
def clean(self): intervals = [] for form in self.forms: valid_from = form.cleaned_data.get('valid_from') valid_to = form.cleaned_data.get('valid_to') if valid_from and valid_to: for valid_from2, valid_to2 in intervals: if is_overlapping(valid_from, valid_to, valid_from2, valid_to2): raise ValidationError(_('Discounts can\'t overlap.')) intervals.append((valid_from, valid_to))
def clean_time(self): now = datetime.datetime.now() data = self.cleaned_data.get('time') if not data or not self.cleaned_data.get('date'): return data start = datetime.datetime.combine(self.cleaned_data['date'], data) end = start + datetime.timedelta(minutes=self.service.duration) if start.date() != end.date(): raise ValidationError(_('Sorry, reservation can\'t span over multiple days.')) # Check current time with date if start < now: raise ValidationError(_('Sorry, you can\'t make a reservation in the past.')) service_provider = self.service.service_provider # Check working hours wrk = WorkingHours.get_for_day(service_provider, start.weekday()) if start.time() < wrk.time_from: raise ValidationError(_('Sorry, the service isn\'t available before ' + str(wrk.time_from)[:-3])) elif end.time() > wrk.time_to: raise ValidationError(_('Sorry, the service is closed from ' + str(wrk.time_to)[:-3])) # Check pauses for wrkBr in wrk.breaks.all(): if is_overlapping(start.time(), end.time(), wrkBr.time_from, wrkBr.time_to): raise ValidationError(_('Sorry, the service isn\'t available at specified time.')) # Check reservations service = self.service employee = self.service_provider_employee reservations = Reservation.objects.filter(service=service, service_provider_employee=employee, service_provider=service_provider, date=self.cleaned_data.get('date')) for res in reservations: resDt = datetime.datetime.combine(res.date, res.time) if is_overlapping(start, end, resDt, resDt + datetime.timedelta(minutes=res.service_duration)): raise ValidationError(_('Sorry, your reservation is overlapping with another reservation.')) return data
def check_if_there_is_free_employee(service, start, end, date): service_employees = ServiceProviderEmployee.objects.filter(service=service) free_employees = [] if service_employees <=1: return None else: for employee in service_employees: employee_reservations = Reservation.objects.filter(service_provider_employee=service_employees, date=date) if not employee_reservations: free_employees.append(employee) else: its_free = True for reservation in employee_reservations: if is_overlapping(start, end, date, date + datetime.timedelta(minutes=reservation.service_duration)): its_free = False break if its_free: free_employees.append(employee) if not free_employees: return None else: return free_employees
def clean_time(self): employee_id = 0 try: employee_id = self.data['service_provider_employee'] except: print "bubu" now = datetime.datetime.now() data = self.cleaned_data.get('time') if not data or not self.cleaned_data.get('date'): return data start = datetime.datetime.combine(self.cleaned_data['date'], data) end = start + datetime.timedelta(minutes=self.service.duration) if start.date() != end.date(): raise ValidationError(_('Sorry, reservation can\'t span over multiple days.')) # Check current time with date if start < now: raise ValidationError(_('Sorry, you can\'t make a reservation in the past.')) service_provider = self.service.service_provider # Check working hours if employee_id != u'': wrk = EmployeeWorkingHours.get_for_day(self.service.id,employee_id, start.weekday()) if wrk: if start.time() < wrk.time_from: messages.warning(self.request,_('Sorry, the service isn\'t available at this time')) raise ValidationError("") elif end.time() > wrk.time_to: messages.warning(self.request,_('Sorry, the service isn\'t available at this time')) raise ValidationError("") # Check pauses wrk = WorkingHours.get_for_day(self.service.service_provider, start.weekday()) for wrkBr in wrk.breaks.all(): if is_overlapping(start.time(), end.time(), wrkBr.time_from, wrkBr.time_to): raise ValidationError(_('Sorry, the service isn\'t available at specified time.')) # Check reservations service = self.service if employee_id ==u'': reservations = Reservation.objects.filter(service=service, service_provider=service_provider, date=self.cleaned_data.get('date')) else: reservations = Reservation.objects.filter(service=service, service_provider=service_provider,service_provider_employee_id=employee_id, date=self.cleaned_data.get('date')) for res in reservations: resDt = datetime.datetime.combine(res.date, res.time) if is_overlapping(start, end, resDt, resDt + datetime.timedelta(minutes=res.service_duration)): free_employees = check_if_there_is_free_employee(service, start, end, resDt) str='' for e in free_employees: str +=e.first_name+' '+e.last_name+', ' if free_employees: if free_employees.__len__()==1: messages.info(self.request, str +'is free at this time') raise ValidationError(_('Sorry, your reservation is overlapping with another reservation.')) else: messages.info(self.request, str +'are free at this time') raise ValidationError(_('Sorry, your reservation is overlapping with another reservation./n')) else: raise ValidationError(_('Sorry, your reservation is overlapping with another reservation.')) return data
def clean_time(self): employee_id = 0 try: employee_id = self.data['service_provider_employee'] except: print "bubu" now = datetime.datetime.now() data = self.cleaned_data.get('time') if not data or not self.cleaned_data.get('date'): return data start = datetime.datetime.combine(self.cleaned_data['date'], data) end = start + datetime.timedelta(minutes=self.service.duration) if start.date() != end.date(): raise ValidationError( _('Sorry, reservation can\'t span over multiple days.')) # Check current time with date if start < now: raise ValidationError( _('Sorry, you can\'t make a reservation in the past.')) service_provider = self.service.service_provider # Check working hours if employee_id != u'': wrk = EmployeeWorkingHours.get_for_day(self.service.id, employee_id, start.weekday()) if wrk: if start.time() < wrk.time_from: messages.warning( self.request, _('Sorry, the service isn\'t available at this time')) raise ValidationError("") elif end.time() > wrk.time_to: messages.warning( self.request, _('Sorry, the service isn\'t available at this time')) raise ValidationError("") # Check pauses wrk = WorkingHours.get_for_day(self.service.service_provider, start.weekday()) for wrkBr in wrk.breaks.all(): if is_overlapping(start.time(), end.time(), wrkBr.time_from, wrkBr.time_to): raise ValidationError( _('Sorry, the service isn\'t available at specified time.') ) # Check reservations service = self.service if employee_id == u'': reservations = Reservation.objects.filter( service=service, service_provider=service_provider, date=self.cleaned_data.get('date')) else: reservations = Reservation.objects.filter( service=service, service_provider=service_provider, service_provider_employee_id=employee_id, date=self.cleaned_data.get('date')) for res in reservations: resDt = datetime.datetime.combine(res.date, res.time) if is_overlapping( start, end, resDt, resDt + datetime.timedelta(minutes=res.service_duration)): free_employees = check_if_there_is_free_employee( service, start, end, resDt) str = '' for e in free_employees: str += e.first_name + ' ' + e.last_name + ', ' if free_employees: if free_employees.__len__() == 1: messages.info(self.request, str + 'is free at this time') raise ValidationError( _('Sorry, your reservation is overlapping with another reservation.' )) else: messages.info(self.request, str + 'are free at this time') raise ValidationError( _('Sorry, your reservation is overlapping with another reservation./n' )) else: raise ValidationError( _('Sorry, your reservation is overlapping with another reservation.' )) return data