def ViewEmployeeWeek(request,employee_id,date): template = 'ViewEmployeeWeek.html' if request.method == 'POST': navform = NavDateForm(request.POST) if navform.is_valid(): # process the data in form.cleaned_data as required #date_external = navform.cleaned_data['navdate'] #dateformat = DateFormat(date_external) #date = dateformat.format('Y-m-d') date = navform.cleaned_data['navdate'] if date: return redirect('OptiSched:ViewEmployeeWeek',employee_id,date.isoformat()) else: #pdb.set_trace() work_day = Date.objects.get( date_user = request.user, date=date, ) employee = Person.objects.get( pk=employee_id, person_user = request.user, ) start_end_week_dates = ScheduleDateTimeUtilities.get_dates_from_week( work_day.date.isocalendar()[0], work_day.date.isocalendar()[1] ) shifts_in_week = Shift.objects.filter( shift_user = request.user, shift_date__gte = start_end_week_dates[0], shift_date__lte = start_end_week_dates[1], employee = employee ) navform = NavDateForm(initial={'navdate': date}) context = { 'employee': employee, 'shifts': shifts_in_week, 'NavDateForm': navform, 'week_start_date':start_end_week_dates[0], 'week_end_date':start_end_week_dates[1], } return render(request,template,context)
def ViewEmployeeWeek(request, employee_id, date): template = 'ViewEmployeeWeek.html' if request.method == 'POST': navform = NavDateForm(request.POST) if navform.is_valid(): # process the data in form.cleaned_data as required #date_external = navform.cleaned_data['navdate'] #dateformat = DateFormat(date_external) #date = dateformat.format('Y-m-d') date = navform.cleaned_data['navdate'] if date: return redirect('OptiSched:ViewEmployeeWeek', employee_id, date.isoformat()) else: #pdb.set_trace() work_day = Date.objects.get( date_user=request.user, date=date, ) employee = Person.objects.get( pk=employee_id, person_user=request.user, ) start_end_week_dates = ScheduleDateTimeUtilities.get_dates_from_week( work_day.date.isocalendar()[0], work_day.date.isocalendar()[1]) shifts_in_week = Shift.objects.filter( shift_user=request.user, shift_date__gte=start_end_week_dates[0], shift_date__lte=start_end_week_dates[1], employee=employee) navform = NavDateForm(initial={'navdate': date}) context = { 'employee': employee, 'shifts': shifts_in_week, 'NavDateForm': navform, 'week_start_date': start_end_week_dates[0], 'week_end_date': start_end_week_dates[1], } return render(request, template, context)
def GetNewEmployee(self,emp_types_needed,datetime): #------ unavailable_workers = self.employees_already_working year_num = self.date_model_obj.date.year week_num = self.date_model_obj.week() # get all employees qs_all_employees = self.employee_all available_employees = [] # get all available employees for emp in qs_all_employees: if not emp in unavailable_workers: available_employees.append(emp) # get just the employee types needed temp_emp_types_needed = [] for emp_type_needed in emp_types_needed: temp_emp_types_needed.append(emp_type_needed.etr_employee_type) # *************************************************************** # Need to weed out all available employees that are not qualified # to work roles for shifts that need to be filled # someone who is only a cook cannot fill a manger's shift temp_available_employees = [] temp_available_employees[:] = [] temp_available_employees = copy.deepcopy(available_employees) for available_employee in temp_available_employees: temp_person_employee_types = [] temp_person_employee_types = PersonEmployeeType.objects.filter(pet_employee = available_employee) temp_employee_types = [] for temp_person_employee_type in temp_person_employee_types: temp_employee_types.append(temp_person_employee_type.pet_employee_type) #----------------------- # check if at least one type exists in each list temp_et_intersect = [] temp_et_intersect = list( set.intersection( set(temp_employee_types),set(temp_emp_types_needed) ) ) if not temp_et_intersect: available_employees.remove(available_employee) #------------------------- # check to see if the available employee has already worked enough hours for a full week week_dates = namedtuple('week_dates', ['start_date','end_date']) week_dates = ScheduleDateTimeUtilities.get_dates_from_week(int(year_num),int(week_num)) temp_emp_existing_shifts_in_week = Shift.objects.filter( shift_date__gte = week_dates[0], shift_date__lte = week_dates[1], employee = available_employee.id) hours_worked_in_week = 0 for existing_shift in temp_emp_existing_shifts_in_week: hours_worked_in_week += existing_shift.hours() # if the employee has worked total hours per week; remove them if (hours_worked_in_week >= available_employee.person_max_hours_per_week): if ( available_employee in available_employees): available_employees.remove(available_employee) # if an employee's hours left to work in a week is less than a minimum shift; remove them elif (available_employee.person_min_hours_per_shift > (available_employee.person_max_hours_per_week - hours_worked_in_week) ): if ( available_employee in available_employees): available_employees.remove(available_employee) # Remove available employees who are available to work based on hours left in the week # but have a restriction coming up in the same day that is sooner than # a minimum shift. # For example, the current hour is 10 they can't work at 12 or later and min shift = 4hrs elif( self.HoursUntilCantWork(available_employee,datetime) < available_employee.person_min_hours_per_shift ): if ( available_employee in available_employees): available_employees.remove(available_employee) # Get all DateTime requests that encompass the current time # and for only potential new employees qs_all_datetime_requests = RequestDateTime.objects.filter( rqst_date_date = datetime.date(), rqst_date_start_time__lte = datetime.time(), rqst_date_end_time__gte = datetime.time() ).exclude( rqst_date_employee__in = unavailable_workers) # Get all DayTime requests that encompass the current time # and for only potential new employees qs_all_daytime_requests = RequestDayTime.objects.filter( day_of_week = self.date_model_obj.date.weekday(), rqst_day_start_time__lte = datetime.time(), rqst_day_end_time__gte = datetime.time() ).exclude( rqst_day_employee__in = unavailable_workers) # TODO Make this a dictionary/map all_datetime_vacation = [] all_datetime_sick = [] all_datetime_preferred = [] all_datetime_skip = [] # group them into their different types for one_request in qs_all_datetime_requests: if (one_request.rqst_date_type == one_request.VACATION): all_datetime_vacation.append(one_request) elif (one_request.rqst_date_type == one_request.SICK): all_datetime_sick.append(one_request) elif (one_request.rqst_date_type == one_request.PREFERRED): all_datetime_preferred.append(one_request) elif (one_request.rqst_date_type == one_request.SKIP): all_datetime_skip.append(one_request) all_daytime_preferred = [] all_daytime_skip = [] # group them into their different types for one_request in qs_all_daytime_requests: if (one_request.rqst_day_type == one_request.PREFERRED): all_daytime_preferred.append(one_request) elif (one_request.rqst_day_type == one_request.SKIP): all_daytime_skip.append(one_request) # **************************************************************************** # *** remove all employees from main list that are in cannot work requests *** # TODO: Maybe dont need this because we get rid of them based on future even # Skip requests (cannot work this day) if all_datetime_skip: for request in all_datetime_skip: if request.rqst_date_employee in available_employees: available_employees.remove(request.rqst_date_employee) if all_daytime_skip: for request in all_daytime_skip: if request.rqst_day_employee in available_employees: available_employees.remove(request.rqst_day_employee) # Sick requests (im sick or am going to be sick potential dr visit) if all_datetime_sick: for sick_request in all_datetime_sick: if sick_request.rqst_date_employee in available_employees: available_employees.remove(sick_request.rqst_date_employee) # Vacation requests (im going to be off this week or this day) if all_datetime_vacation: for vacation_request in all_datetime_vacation: if vacation_request.rqst_date_employee in available_employees: available_employees.remove(vacation_request.rqst_date_employee) # **************************************************************** # *** add in duplicates in main list for emps with preferences *** # Vacation requests (im sick or am going to be sick (dr visit)) if all_datetime_preferred: for request in all_datetime_preferred: if request.rqst_date_employee in available_employees: available_employees.append(request.rqst_date_employee) if all_daytime_preferred: for request in all_daytime_preferred: if ( request.rqst_day_employee in available_employees # if we've aready added them as a date time preference we dont # need to add them again if they normally prefer it..that would make # them 3x as likely to be picked for the shift and available_employees.count(request.rqst_day_employee) < 2 ): available_employees.append(request.rqst_day_employee) return_val = [None]*2 # select random employee from final list and return it if available_employees: # init return val # get employee # TODO: Instead of getting the first get a random available employee # once we have stuff down well return_val[0] = available_employees[0] #return_val[0] = random.choice(available_employees) # get employee types for chosen employee chosen_employee_types = PersonEmployeeType.objects.filter(pet_employee=return_val[0]) temp_chosen_employee_types = [] for chosen_employee_type in chosen_employee_types: temp_chosen_employee_types.append(chosen_employee_type.pet_employee_type) # get list of chosen employee's types and what we need intersected chosen_employee_types_intersect = [] chosen_employee_types_intersect = list(set.intersection(set(temp_chosen_employee_types),set(temp_emp_types_needed))) return_val[1] = chosen_employee_types_intersect[0] #return_val[1] = random.choice(chosen_employee_types_intersect) return return_val