def post(self, request, **kwargs):
        hour = 0
        minute = (Appointment._meta.get_field('length').get_default().seconds %
                  3600) // 60
        form = PractitionerDefineAppointmentForm(request.POST)
        print(form.is_valid())
        if form.is_valid():
            duration = decompress_duration(form.cleaned_data['length'])
            hour = duration[0]
            minute = duration[1]

            appointment = Appointment(
                patient=None,
                practitioner=self.request.user.practitioner,
                start_date_and_time=form.cleaned_data['start_date_and_time'],
                length=timedelta(hours=hour, minutes=minute))

            over_lap_free, over_laps = Appointment.get_appointment__practitioner_overlaps(
                appointment, self.request.user.practitioner)
            if not over_lap_free:
                clashes = over_laps
                return render(
                    self.request,
                    'connect_therapy/practitioner/appointment-overlap.html',
                    context={"clashes": clashes})
            else:
                Appointment.split_merged_appointment(
                    appointment
                )  # This method will split if needed and then save the appointment
                return super().post(request)

        context = self.get_context_data()
        context['form_was_valid'] = False
        return render(request, self.get_template_names(), context=context)
    def form_valid(self, form):
        # Here, we would record the user's interest using the message
        # passed in form.cleaned_data['message']
        notifications.appointment_cancelled_by_patient(
            self.object.patient,
            self.object,
            self.object.start_date_and_time < timezone.now() + timedelta(hours=24)
        )
        self.object.patient = None
        self.object.save()
        Appointment.split_merged_appointment(self.object)

        return super(PatientCancelAppointmentView, self).form_valid(form)
 def test_split_merged_appointment_when_no_splits_should_happen(self):
     start_date_and_time = datetime(year=2018,
                                    month=3,
                                    day=11,
                                    hour=12,
                                    minute=00)
     appointment = Appointment(start_date_and_time=start_date_and_time,
                               length=timedelta(minutes=30))
     appointment.save()
     Appointment.split_merged_appointment(appointment)
     new_appointments = Appointment.objects.filter(
         start_date_and_time__gte=start_date_and_time,
         start_date_and_time__lte=start_date_and_time +
         timedelta(minutes=30))
     self.assertEqual(len(new_appointments), 1)
     for appointment in new_appointments:
         self.assertEqual(appointment.length, timedelta(minutes=30))
    def test_split_merged_appointment_into_6(self):
        start_date_and_time = datetime(year=2018,
                                       month=3,
                                       day=11,
                                       hour=12,
                                       minute=00)
        appointment = Appointment(start_date_and_time=start_date_and_time,
                                  length=timedelta(hours=3))

        appointment.save()
        Appointment.split_merged_appointment(appointment)
        new_appointments = Appointment.objects.filter(
            start_date_and_time__gte=start_date_and_time,
            start_date_and_time__lte=start_date_and_time + timedelta(hours=3))
        self.assertEqual(len(new_appointments), 6)

        for appointment in new_appointments:
            self.assertEqual(appointment.length, timedelta(minutes=30))

            self.assertEquals(
                appointment.price,
                Decimal(Appointment._meta.get_field('price').get_default()))