def clean_date(self): now = datetime.datetime.now() data = self.cleaned_data.get('date') if not data: return data # Check that selected date is not in the past if data < now.date(): raise ValidationError( _('Sorry, you can\'t make reservations for past days.')) service_provider = self.service.service_provider # Check if it's overlapping with working days (convert date to weekday and check if it's in the week_days list) workinghrs = WorkingHours.get_for_day(service_provider, data.weekday()) if workinghrs is None: raise ValidationError( _('Sorry, you can\'t make a reservation on a non working day.') ) # Check if it's overlapping with absences if Absence.is_absent_on(service_provider, data): raise ValidationError( _('Sorry, the provider is absent on this day.')) return data
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 getWorkingHours(provider, service_id, date): workinghrs = WorkingHours.get_for_day(provider, date.weekday()) employees_workinghours_on_curr_service = EmployeeWorkingHours.objects.filter(service=service_id) maxTime_from = employees_workinghours_on_curr_service[0].time_from maxTime_to = employees_workinghours_on_curr_service[0].time_to for employee_workinghours in employees_workinghours_on_curr_service: if maxTime_from > employee_workinghours.time_from: maxTime_from = employee_workinghours.time_from if maxTime_to < employee_workinghours.time_to: maxTime_to = employee_workinghours.time_to events = [] # Check if provider is working on this date if workinghrs is None or Absence.is_absent_on(provider, date): return [ { "title": ugettext(EVENT_TITLE_CLOSED_WHOLE_DAY), "start": encodeDatetime(date), "end": encodeDatetime(date + datetime.timedelta(days=1)), "color": EVENT_CLOSED_COLOR, } ] # Start events.append( { "title": ugettext(EVENT_TITLE_CLOSED), "start": encodeDatetime(date), "end": encodeDatetime(datetime.datetime.combine(date, maxTime_from)), "color": EVENT_PAUSE_COLOR, } ) # End events.append( { "title": ugettext(EVENT_TITLE_CLOSED), "start": encodeDatetime(datetime.datetime.combine(date, maxTime_to)), "end": encodeDatetime(date + datetime.timedelta(days=1)), "color": EVENT_PAUSE_COLOR, } ) for wrkbrk in workinghrs.breaks.all(): events.append( { "title": ugettext(EVENT_TITLE_CLOSED), "start": encodeDatetime(datetime.datetime.combine(date, wrkbrk.time_from)), "end": encodeDatetime(datetime.datetime.combine(date, wrkbrk.time_to)), "color": EVENT_PAUSE_COLOR, } ) return events
def getWorkingHours(provider, date): workinghrs = WorkingHours.get_for_day(provider, date.weekday()) events = [] # Check if provider is working on this date if workinghrs is None or Absence.is_absent_on(provider, date): return [{ 'title': ugettext(EVENT_TITLE_CLOSED_WHOLE_DAY), 'start': encodeDatetime(date), 'end': encodeDatetime(date + datetime.timedelta(days=1)), 'color': EVENT_CLOSED_COLOR }] # Start events.append({ 'title': ugettext(EVENT_TITLE_CLOSED), 'start': encodeDatetime(date), 'end': encodeDatetime(datetime.datetime.combine(date, workinghrs.time_from)), 'color': EVENT_PAUSE_COLOR }) # End events.append({ 'title': ugettext(EVENT_TITLE_CLOSED), 'start': encodeDatetime(datetime.datetime.combine(date, workinghrs.time_to)), 'end': encodeDatetime(date + datetime.timedelta(days=1)), 'color': EVENT_PAUSE_COLOR }) for wrkbrk in workinghrs.breaks.all(): events.append({ 'title': ugettext(EVENT_TITLE_CLOSED), 'start': encodeDatetime(datetime.datetime.combine(date, wrkbrk.time_from)), 'end': encodeDatetime(datetime.datetime.combine(date, wrkbrk.time_to)), 'color': EVENT_PAUSE_COLOR }) return events
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 clean_date(self): now = datetime.datetime.now() data = self.cleaned_data.get('date') if not data: return data # Check that selected date is not in the past if data < now.date(): raise ValidationError(_('Sorry, you can\'t make reservations for past days.')) service_provider = self.service.service_provider # Check if it's overlapping with working days (convert date to weekday and check if it's in the week_days list) workinghrs = WorkingHours.get_for_day(service_provider, data.weekday()) if workinghrs is None: raise ValidationError(_('Sorry, you can\'t make a reservation on a non working day.')) # Check if it's overlapping with absences if Absence.is_absent_on(service_provider, data): raise ValidationError(_('Sorry, the provider is absent on this day.')) return data
def getWorkingHours(provider, date): workinghrs = WorkingHours.get_for_day(provider, date.weekday()) events = [] # Check if provider is working on this date if workinghrs is None or Absence.is_absent_on(provider, date): return [{ 'title': ugettext(EVENT_TITLE_CLOSED_WHOLE_DAY), 'start': encodeDatetime(date), 'end': encodeDatetime(date + datetime.timedelta(days=1)), 'color': EVENT_CLOSED_COLOR }] # Start events.append({ 'title': ugettext(EVENT_TITLE_CLOSED), 'start': encodeDatetime(date), 'end': encodeDatetime(datetime.datetime.combine(date, workinghrs.time_from)), 'color': EVENT_PAUSE_COLOR }) # End events.append({ 'title': ugettext(EVENT_TITLE_CLOSED), 'start': encodeDatetime(datetime.datetime.combine(date, workinghrs.time_to)), 'end': encodeDatetime(date + datetime.timedelta(days=1)), 'color': EVENT_PAUSE_COLOR }) for wrkbrk in workinghrs.breaks.all(): events.append({ 'title': ugettext(EVENT_TITLE_CLOSED), 'start': encodeDatetime(datetime.datetime.combine(date, wrkbrk.time_from)), 'end': encodeDatetime(datetime.datetime.combine(date, wrkbrk.time_to)), 'color': EVENT_PAUSE_COLOR }) return events
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 getWorkingHours(provider, service_id, date): workinghrs = WorkingHours.get_for_day(provider, date.weekday()) employees_workinghours_on_curr_service = EmployeeWorkingHours.objects.filter( service=service_id) maxTime_from = employees_workinghours_on_curr_service[0].time_from maxTime_to = employees_workinghours_on_curr_service[0].time_to for employee_workinghours in employees_workinghours_on_curr_service: if maxTime_from > employee_workinghours.time_from: maxTime_from = employee_workinghours.time_from if maxTime_to < employee_workinghours.time_to: maxTime_to = employee_workinghours.time_to events = [] # Check if provider is working on this date if workinghrs is None or Absence.is_absent_on(provider, date): return [{ 'title': ugettext(EVENT_TITLE_CLOSED_WHOLE_DAY), 'start': encodeDatetime(date), 'end': encodeDatetime(date + datetime.timedelta(days=1)), 'color': EVENT_CLOSED_COLOR }] # Start events.append({ 'title': ugettext(EVENT_TITLE_CLOSED), 'start': encodeDatetime(date), 'end': encodeDatetime(datetime.datetime.combine(date, maxTime_from)), 'color': EVENT_PAUSE_COLOR }) # End events.append({ 'title': ugettext(EVENT_TITLE_CLOSED), 'start': encodeDatetime(datetime.datetime.combine(date, maxTime_to)), 'end': encodeDatetime(date + datetime.timedelta(days=1)), 'color': EVENT_PAUSE_COLOR }) for wrkbrk in workinghrs.breaks.all(): events.append({ 'title': ugettext(EVENT_TITLE_CLOSED), 'start': encodeDatetime(datetime.datetime.combine(date, wrkbrk.time_from)), 'end': encodeDatetime(datetime.datetime.combine(date, wrkbrk.time_to)), 'color': EVENT_PAUSE_COLOR }) return events
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 getEmployeeWorkingHours(provider, employee, date, past): sp_workinghrs = WorkingHours.get_for_day(provider, date.weekday()) workinghrs = EmployeeWorkingHours.get_for_day(employee, date.weekday()) events = [] if past: now = datetime.datetime.now() if date < now.date(): return [ { 'title': ugettext('In the past'), 'start': encodeDatetime(date), 'end': encodeDatetime(datetime.datetime.combine(date, datetime.time(23, 59))), 'color': '#444444' } ] elif date == now.date(): events.append( { 'title': ugettext('In the past'), 'start': encodeDatetime(date), 'end': encodeDatetime(datetime.datetime.combine(date, now.time())), 'color': '#444444' } ) # TODO add employee absence support if sp_workinghrs is None or Absence.is_absent_on(provider, date): return [ { 'title': ugettext(EVENT_TITLE_CLOSED_WHOLE_DAY), 'start': encodeDatetime(date), 'end': encodeDatetime(date + datetime.timedelta(days=1)), 'color': EVENT_CLOSED_COLOR } ] if workinghrs is None: return [ { 'title': ugettext(EVENT_TITLE_NOT_WORKING_WHOLE_DAY), 'start': encodeDatetime(date), 'end': encodeDatetime(date + datetime.timedelta(days=1)), 'color': EVENT_CLOSED_COLOR } ] events.append({ 'title': ugettext(EVENT_TITLE_NOT_WORKING), 'start': encodeDatetime(date), 'end': encodeDatetime(datetime.datetime.combine(date, workinghrs.time_from)), 'color': EVENT_PAUSE_COLOR }) events.append({ 'title': ugettext(EVENT_TITLE_NOT_WORKING), 'start': encodeDatetime(datetime.datetime.combine(date, workinghrs.time_to)), 'end': encodeDatetime(date + datetime.timedelta(days=1)), 'color': EVENT_PAUSE_COLOR }) for wrkbrk in sp_workinghrs.breaks.all(): events.append({ 'title': ugettext(EVENT_TITLE_CLOSED), 'start': encodeDatetime(datetime.datetime.combine(date, wrkbrk.time_from)), 'end': encodeDatetime(datetime.datetime.combine(date, wrkbrk.time_to)), 'color': EVENT_PAUSE_COLOR }) return events
def getWorkingHours(service, provider, date, past): workinghrs = WorkingHours.get_for_day(provider, date.weekday()) events = [] if past: now = datetime.datetime.now() if date < now.date(): return [ { 'title': ugettext('In the past'), 'start': encodeDatetime(date), 'end': encodeDatetime(datetime.datetime.combine(date, datetime.time(23, 59))), 'color': '#444444' } ] elif date == now.date(): events.append( { 'title': ugettext('In the past'), 'start': encodeDatetime(date), 'end': encodeDatetime(datetime.datetime.combine(date, now.time())), 'color': '#444444' } ) # Check if provider is working on this date if workinghrs is None or Absence.is_absent_on(provider, date): return [ { 'title': ugettext(EVENT_TITLE_CLOSED_WHOLE_DAY), 'start': encodeDatetime(date), 'end': encodeDatetime(date + datetime.timedelta(days=1)), 'color': EVENT_CLOSED_COLOR } ] # Start events.append({ 'title': ugettext(EVENT_TITLE_CLOSED), 'start': encodeDatetime(date), 'end': encodeDatetime(datetime.datetime.combine(date, workinghrs.time_from)), 'color': EVENT_PAUSE_COLOR }) # End events.append({ 'title': ugettext(EVENT_TITLE_CLOSED), 'start': encodeDatetime(datetime.datetime.combine(date, workinghrs.time_to)), 'end': encodeDatetime(date + datetime.timedelta(days=1)), 'color': EVENT_PAUSE_COLOR }) for wrkbrk in workinghrs.breaks.all(): events.append({ 'title': ugettext(EVENT_TITLE_CLOSED), 'start': encodeDatetime(datetime.datetime.combine(date, wrkbrk.time_from)), 'end': encodeDatetime(datetime.datetime.combine(date, wrkbrk.time_to)), 'color': EVENT_PAUSE_COLOR }) if service is not None: employees = Employee.objects.filter(id__in=service.employees.all(), employer=provider.id) else: employees = Employee.objects.filter(employer=provider.id).all() first_arrive = datetime.time(23, 59) last_gone = datetime.time(0) for e in employees: if e.working_hours.all(): cwh = e.working_hours.all()[0].get_for_day(e, date.weekday()) if cwh: if cwh.time_to > last_gone: last_gone = cwh.time_to if cwh.time_from < first_arrive: first_arrive = cwh.time_from if employees: if first_arrive == datetime.time(23, 59) and last_gone == datetime.time(0): return [{ 'title': ugettext('No employees scheduled but we are still open. Huh.'), 'start': encodeDatetime(date), 'end': encodeDatetime(date + datetime.timedelta(days=1)), 'color': EVENT_CLOSED_COLOR }] else: if first_arrive > workinghrs.time_from: events.append({ 'title': ugettext('No employees here yet'), 'start': encodeDatetime(datetime.datetime.combine(date, workinghrs.time_from)), 'end': encodeDatetime(datetime.datetime.combine(date, first_arrive)), 'color': EVENT_PAUSE_COLOR }) if last_gone < workinghrs.time_to: events.append({ 'title': ugettext('All employees have left'), 'start': encodeDatetime(datetime.datetime.combine(date, last_gone)), 'end': encodeDatetime(datetime.datetime.combine(date, workinghrs.time_to)), 'color': EVENT_PAUSE_COLOR }) return events