Beispiel #1
0
 def test_no_constraints_should_be_valid(self):
     form = ShiftSlotForm(
         self.dummy_slot,
         business=Business.objects.get(business_name='dummy'))
     if not form.is_valid():
         print form.errors
     self.assertTrue(form.is_valid())
Beispiel #2
0
 def test_not_enough_total_waiters_should_fail(self):
     test_emp = EmployeeProfile.objects.get(
         user__username=self.emp_credentials['username'])
     test_emp.role = 'CO'
     test_emp.save()
     form = ShiftSlotForm(
         self.dummy_slot,
         business=Business.objects.get(business_name='dummy'))
     self.assertFalse(form.is_valid())
Beispiel #3
0
    def test_not_enough_old_emps_should_fail(self):
        lookup = 'waiter_age'
        self.dummy_slot[lookup + '__operation_constraint'] = 'gte'
        self.dummy_slot[lookup + '__value_constraint'] = '28'
        self.dummy_slot[lookup + '__applyOn_constraint'] = '1'

        form = ShiftSlotForm(
            self.dummy_slot,
            business=Business.objects.get(business_name='dummy'))
        self.assertFalse(form.is_valid())
Beispiel #4
0
    def test_enough_males_should_success(self):
        lookup = 'waiter_gender'
        self.dummy_slot[lookup + '__operation_constraint'] = 'eq'
        self.dummy_slot[lookup + '__value_constraint'] = 'M'
        self.dummy_slot[lookup + '__applyOn_constraint'] = '1'

        form = ShiftSlotForm(
            self.dummy_slot,
            business=Business.objects.get(business_name='dummy'))
        self.assertTrue(form.is_valid())
Beispiel #5
0
 def test_overlap_slots_should_fail(self):
     ShiftSlot.objects.create(
         business=Business.objects.get(business_name='dummy'),
         week=get_next_week_num(),
         day='3',
         start_hour='12:00:00',
         end_hour='13:00:00')
     form = ShiftSlotForm(
         self.dummy_slot,
         business=Business.objects.get(business_name='dummy'))
     self.assertFalse(form.is_valid())
Beispiel #6
0
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()
            })
Beispiel #7
0
def update_shift_slot(request, slot_id):
    updated_slot = get_object_or_404(ShiftSlot, id=slot_id)

    if not updated_slot.is_next_week():
        return HttpResponseBadRequest(
            '<h3>this shift is not at next week</h3>')

    business = request.user.profile.business

    if request.method == 'POST':
        logger.info('in post, is is %s', slot_id)
        slot_form = ShiftSlotForm(request.POST, business=business)
        if slot_form.is_valid():
            data = slot_form.cleaned_data
            slot_constraint_json = SlotConstraintCreator(data).create()

            logger.info('new end hour is %s', str(data['end_hour']))
            ShiftSlot.objects.filter(id=slot_id).update(
                business=request.user.profile.business,
                day=data['day'],
                start_hour=data['start_hour'],
                end_hour=data['end_hour'],
                constraints=json.dumps(slot_constraint_json),
                is_mandatory=data['mandatory'])
            messages.success(request, 'slot updated')
            return HttpResponseRedirect('/')
        else:
            return render(request, 'manager/new_shift.html',
                          {'form': slot_form})
    else:  # GET
        logger.info('in get, ID is %s', slot_id)
        day = int(updated_slot.day)
        start_hour = str(updated_slot.start_hour)
        end_hour = str(updated_slot.end_hour)
        is_mandatory = updated_slot.is_mandatory
        form = ShiftSlotForm(initial={
            'day': day,
            'start_hour': start_hour.replace('-', ':'),
            'end_hour': end_hour.replace('-', ':'),
            'mandatory': is_mandatory
        },
                             business=business)
        return render(
            request, 'manager/update_shift.html', {
                'form': form,
                'week_range': get_next_week_string(),
                'id': slot_id,
                'holiday': updated_slot.holiday,
                'logo_conf': get_logo_conf()
            })