def test_absence_by_parent(self): Absence.objects.create(child_link=self.child, from_date=date.today() + timedelta(days=10), created_by=self.user) self.assertEqual(1, len(Absence.get_absence_queryset_by_user(self.user2))) for absence in Absence.get_absence_queryset_by_user(self.user2): self.assertEqual(absence.child_link, self.child) self.assertEqual( 1, len(Absence.get_open_absence_queryset_by_user(self.user2)))
def test_absence_only_open(self): Absence.objects.create(child_link=self.child, from_date=date.today() - timedelta(days=10), created_by=self.user) self.assertEqual(1, len(Absence.get_absence_queryset_by_user(self.user))) self.assertEqual( 0, len(Absence.get_open_absence_queryset_by_user(self.user))) Absence.objects.create(child_link=self.child, from_date=date.today() + timedelta(days=10), created_by=self.user, deleted=True) self.assertEqual( 0, len(Absence.get_open_absence_queryset_by_user(self.user))) self.assertEqual(1, len(Absence.get_absence_queryset_by_user(self.user)))
def test_get_holiday(self): Absence.objects.create(child_link=self.child, from_date=date.today() + timedelta(days=2), created_by=self.user) Absence.objects.create(child_link=self.child, from_date=date.today() + timedelta(days=3), created_by=self.user) Absence.objects.create(child_link=self.child, from_date=date.today() + timedelta(days=1), created_by=self.user) Absence.objects.create(child_link=self.child, from_date=date.today(), created_by=self.user) Absence.objects.create(child_link=self.child, from_date=date.today() + timedelta(days=4), created_by=self.user) Absence.objects.create(child_link=self.child, from_date=date.today() + timedelta(days=5), created_by=self.user) holidays = Absence.get_holidays(self.user) self.assertEqual(1, len(holidays)) self.assertEqual(holidays[0]['from_date'], date.today()) self.assertEqual(holidays[0]['to_date'], date.today() + timedelta(days=5))
def delete_holiday(request, year, month, day, to_year, to_month, to_day): if hasattr(request, 'user') and request.user.is_authenticated: for absence in Absence.get_open_absence_queryset_by_user(request.user): if date(year, month, day) <= absence.from_date <= date( to_year, to_month, to_day): absence.deleted_by = request.user absence.deleted = True absence.save() return HttpResponseRedirect(reverse_lazy('list-absences'))
def form_valid(self, form): delta = form.cleaned_data['to_date'] - form.cleaned_data['from_date'] absences = Absence.get_open_absence_queryset_by_user(self.request.user) for day_delta in range(0, delta.days + 1): the_day = form.cleaned_data['from_date'] + timedelta( days=day_delta) if the_day not in [x.from_date for x in absences]: Absence.objects.create( from_date=the_day, created_by=self.request.user, child_link=form.cleaned_data['children'], remark=form.cleaned_data['remark']) return super().form_valid(form)
def test_absence_ordering(self): Absence.objects.create(child_link=self.child, from_date=date.today() + timedelta(days=10), created_by=self.user) Absence.objects.create(child_link=self.child, from_date=date.today(), created_by=self.user) index = 0 for absence in Absence.get_open_absence_queryset_by_user(self.user): index += 1 if index == 1: self.assertEqual(absence.from_date, date.today()) if index == 2: self.assertNotEqual(absence.from_date, date.today()) if index > 2: self.assertFalse(1 == 1, 'more than expected absences in')
def get_context_data(self, object_list=None, **kwargs): context = super().get_context_data(object_list=object_list, **kwargs) context['today'] = date.today() context['holidays'] = Absence.get_holidays(self.request.user) return context
def get_queryset(self): return Absence.get_open_absence_queryset_by_user(self.request.user)