def add_shift_slot(request): business = get_curr_business(request) slot_names_generator = ((name, name) for name in get_business_slot_names(business)) if request.method == 'POST': slot_form = ShiftSlotForm(request.POST, business=business, names=slot_names_generator) if slot_form.is_valid(): data = slot_form.cleaned_data constraint_creator = SlotConstraintCreator(data) slot_creator = SlotCreator(business, data, constraint_creator) try: slot_creator.create() messages.success(request, 'slot form was ok') return HttpResponseRedirect('/') except ObjectDoesNotExist as e: logger.error('object does not exist: %s', e) return HttpResponseBadRequest('object does not exist: %s' % str(e)) else: day = request.POST.get('day') slot_holiday = get_holiday(get_curr_year(), day, get_next_week_num()) return render(request, 'manager/new_shift.html', { 'form': slot_form, 'week_range': get_next_week_string(), 'holiday': slot_holiday }, status=400) else: day = request.GET.get('day', '') start_hour = request.GET.get('startTime', '') slot_holiday = get_holiday(get_curr_year(), day, get_next_week_num()) form = ShiftSlotForm(initial={ 'day': day, 'start_hour': start_hour.replace('-', ':') }, business=business, names=slot_names_generator) return render( request, 'manager/new_shift.html', { 'form': form, 'week_range': get_next_week_string(), 'holiday': slot_holiday, 'logo_conf': get_logo_conf() })
def create_slots_for_next_week(business, waiter='1', bartender='1', cook='1', num=1): slots = [] for i in range(num): s = ShiftSlot.objects.create(business=business, year=get_curr_year(), week=get_next_week_num(), day=str(i + 1), start_hour=get_time_from_str('16:00'), end_hour=get_time_from_str('17:00')) s.constraints = json.dumps({ 'waiter': { 'num': waiter }, 'bartender': { 'num': bartender }, 'cook': { 'num': cook } }) s.save() slots.append(s) return slots
def test_should_be_next_week(self): slot = ShiftSlot.objects.create(business=self.test_business, year=get_curr_year(), week=get_next_week_num(), day='1', start_hour='12:30:00', end_hour='13:00:00') self.assertTrue(slot.is_next_week())
def test_attributes_should_be_copied_from_saved_slot(self): saved_slot = SavedSlot.objects.create(name='test_saved_slot', constraints='{}') slot = ShiftSlot.objects.create(business=self.test_business, year=get_curr_year(), week=get_next_week_num(), day='1', start_hour='12:30:00', end_hour='13:00:00', saved_slot=saved_slot) self.assertEqual(slot.name, saved_slot.name) self.assertEqual(slot.constraints, saved_slot.constraints) self.assertEqual(slot.is_mandatory, saved_slot.is_mandatory)
def _create_new_slot(self): slot_constraint_json = self.constraint_creator.create() slot_holiday = get_holiday(get_curr_year(), self.slot_data['day'], get_next_week_num()) new_slot = ShiftSlot(business=self.business, day=self.slot_data['day'], start_hour=self.slot_data['start_hour'], end_hour=self.slot_data['end_hour'], week=get_next_week_num(), holiday=slot_holiday) if self.slot_data.get('save_as') != '': self._create_slot_with_name(new_slot, slot_constraint_json) else: self._create_new_slot_without_name(new_slot, slot_constraint_json)