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 test_appointment_cancelled_by_practitioner_unbooked(self): user = User.objects.create_user(username='******', password='******', first_name="Robert", last_name="Greener", email="*****@*****.**") user.save() practitioner = Practitioner(user=user, mobile="+447476605233", bio="ABC", address_line_1="XXX", address_line_2="XXXXX", is_approved=True) practitioner.save() appointment = Appointment(practitioner=practitioner, patient=None, start_date_and_time=datetime(year=2018, month=4, day=17, hour=15, minute=10), length=timedelta(hours=1)) appointment.save() appointment_cancelled_by_practitioner(appointment) self.assertEqual(len(mail.outbox), 1)
def _deal_with_appointments(self, request, app_ids, practitioner_id): valid_appointments = Appointment.check_validity(selected_appointments_id=app_ids, selected_practitioner=practitioner_id) if valid_appointments is not False: overlap_free, appointments_to_book = Appointment.get_appointment_overlaps(valid_appointments, patient=self.patient) if overlap_free is False: # valid appointments but overlap exists clashes = appointments_to_book return render(request, self.get_template_names(), context={"clashes": clashes}) else: # all valid bookable_appointments, merged_appointments = Appointment.merge_appointments(appointments_to_book) if len(merged_appointments) > 1: messages.success(request, str(len(merged_appointments)) + " appointments were merged") # add to session data - used by the checkout request.session['bookable_appointments'] = Appointment.appointments_to_dictionary_list( bookable_appointments) request.session['merged_appointments'] = Appointment.appointments_to_dictionary_list( merged_appointments) request.session['patient_id'] = self.patient.id return render(request, self.get_template_names(), {"bookable_appointments": bookable_appointments, "merged_appointments": merged_appointments, "practitioner_id": practitioner_id}) else: # appointments not valid invalid_appointments = True return render(request, self.get_template_names(), context={"invalid_appointments": invalid_appointments})
def test_one_appointment(self): a1 = Appointment(start_date_and_time=datetime(year=2018, month=3, day=2, hour=14, minute=20), length=timedelta(hours=3)) a1.save() overlaps = Appointment._get_overlaps([a1]) self.assertEqual(len(overlaps), 0)
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 get(self, request, *args, **kwargs): appointments_to_book = [] try: appointment_dictionary = request.session['bookable_appointments'] except KeyError: return render(request, self.get_template_names(), {"appointments": appointments_to_book}) appointments_to_book = Appointment.convert_dictionaries_to_appointments(appointment_dictionary) return render(request, self.get_template_names(), {"appointments": appointments_to_book})
def test__str__(self): u = User(first_name="John", last_name="Smith") u.save() practitioner = Practitioner(user=u, address_line_1="My home", postcode="EC12 1CV", mobile="+447577293232", bio="Hello") practitioner.save() appointment = Appointment(practitioner=practitioner, start_date_and_time=datetime(year=2018, month=3, day=2, hour=15, minute=16), length=timedelta(hours=1)) appointment.save() self.assertEqual(str(appointment), 'John Smith - 2018-03-02 15:16:00 for 1:00:00')
def test_appointment_cancelled_by_patient(self): user = User.objects.create_user(username='******', password='******', first_name="Robert", last_name="Greener", email="*****@*****.**") user.save() practitioner = Practitioner(user=user, mobile="+447476605233", bio="ABC", address_line_1="XXX", address_line_2="XXXXX", is_approved=True) practitioner.save() user2 = User.objects.create_user(username='******', password='******', first_name="Woof", last_name="Meow", email="*****@*****.**") user2.save() patient = Patient(user=user2, gender='M', mobile="+447476605233", date_of_birth=date(year=1995, month=1, day=1)) patient.save() appointment = Appointment(practitioner=practitioner, patient=patient, start_date_and_time=datetime(year=2018, month=4, day=17, hour=15, minute=10), length=timedelta(hours=1)) appointment.save() appointment_cancelled_by_patient(patient, appointment, False) self.assertEqual(len(mail.outbox), 2) appointment_cancelled_by_patient(patient, appointment, True) self.assertEqual(len(mail.outbox), 4)
def test_two_spread_over_lapping(self): a1 = Appointment(start_date_and_time=datetime(year=2018, month=3, day=2, hour=14, minute=20), length=timedelta(hours=1)) a1.save() a2 = Appointment(start_date_and_time=datetime(year=2018, month=3, day=2, hour=14, minute=40), length=timedelta(hours=1)) a2.save() overlaps = Appointment._get_overlaps([a1, a2]) self.assertEquals(len(overlaps), 1)
def test_is_live_when_too_early(self): u = User(first_name="John", last_name="Smith") u.save() patient = Patient(user=u, gender='M', mobile="+447476666555", date_of_birth=date(year=1995, month=1, day=1)) patient.save() practitioner = Practitioner(user=u, address_line_1="My home", postcode="EC12 1CV", mobile="+447577293232", bio="Hello") practitioner.save() appointment = Appointment(practitioner=practitioner, patient=patient, start_date_and_time=timezone.now() + timedelta(minutes=10), length=timedelta(hours=1)) appointment.save() self.assertFalse(appointment.is_live())
def post(self, request, pk, **kwargs): self.object = self.get_object() form = AppointmentDateSelectForm(request.POST) if form.is_valid(): date = form.cleaned_data['date'] # pk = practitioner id appointments = Appointment.get_valid_appointments(date, pk) return render(self.request, self.template_name, context={"form": form, "appointments": appointments, "object": self.get_object()}) else: return self.get(request)
def test_reminders(self): user = User.objects.create_user(username='******', password='******', first_name="Robert", last_name="Greener", email="*****@*****.**") user.save() practitioner = Practitioner(user=user, mobile="+447476605233", bio="ABC", address_line_1="XXX", address_line_2="XXXXX", is_approved=True) practitioner.save() user2 = User.objects.create_user(username='******', password='******', first_name="Woof", last_name="Meow", email="*****@*****.**") user2.save() patient = Patient(user=user2, gender='M', mobile="+447476605233", date_of_birth=date(year=1995, month=1, day=1)) patient.save() appointment1 = Appointment(practitioner=practitioner, patient=patient, start_date_and_time=timezone.now(), length=timedelta(hours=1)) appointment1.save() appointment2 = Appointment(practitioner=practitioner, patient=patient, start_date_and_time=timezone.now(), length=timedelta(minutes=30)) appointment2.save() reminders() self.assertEqual(len(mail.outbox), 4)
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 post(self, request, *args, **kwargs): if 'delete' in request.POST: apps = request.session['bookable_appointments'] app_list = Appointment.convert_dictionaries_to_appointments(apps) for app in app_list: if app.session_id == request.POST['delete']: app_list.remove(app) # delete the appointment and update the session data request.session['bookable_appointments'] = Appointment.appointments_to_dictionary_list(app_list) return self.get(request, args, kwargs) return self.get(request, args, kwargs) elif 'checkout' in request.POST: # TODO: Add payment gateway stuff here...probably try: appointment_dictionary = request.session['bookable_appointments'] except KeyError: return self.get(request, *args, **kwargs) del request.session['bookable_appointments'] # empty the shopping basket appointments_to_book = Appointment.convert_dictionaries_to_appointments(appointment_dictionary) # first delete the appointments we merged, if any merged_dictionary = request.session['merged_appointments'] del request.session['merged_appointments'] # delete the merged appointments if merged_dictionary is not None: merged_appointment_list = Appointment.convert_dictionaries_to_appointments(merged_dictionary) Appointment.delete_appointments(merged_appointment_list) # go ahead and book those appointments if Appointment.book_appointments(appointments_to_book, self.patient): notifications.multiple_appointments_booked(appointments_to_book) # call method from notifications.py return render(request, "connect_therapy/patient/bookings/booking-success.html", { 'appointment': appointments_to_book}) else: return HttpResponse("Failed to book. Patient object doesnt exist.")
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()))
def test_three_consecutive_one_overlap(self): a1 = Appointment(start_date_and_time=datetime(year=2018, month=3, day=2, hour=14, minute=20), length=timedelta(hours=1)) a1.save() a2 = Appointment(start_date_and_time=datetime(year=2018, month=3, day=2, hour=15, minute=20), length=timedelta(hours=1)) a2.save() a3 = Appointment(start_date_and_time=datetime(year=2018, month=3, day=2, hour=16, minute=20), length=timedelta(hours=1)) a3.save() a4 = Appointment(start_date_and_time=datetime(year=2018, month=3, day=2, hour=17, minute=19), length=timedelta(hours=1)) a4.save() overlaps = Appointment._get_overlaps([a4, a2, a3, a1]) self.assertEquals(len(overlaps), 1)
def test_four_disjoint(self): a1 = Appointment(start_date_and_time=datetime(year=2018, month=3, day=2, hour=14, minute=20), length=timedelta(hours=1)) a1.save() a2 = Appointment(start_date_and_time=datetime(year=2018, month=3, day=2, hour=15, minute=30), length=timedelta(hours=1)) a2.save() a3 = Appointment(start_date_and_time=datetime(year=2018, month=3, day=2, hour=17, minute=00), length=timedelta(hours=1)) a3.save() a4 = Appointment(start_date_and_time=datetime(year=2018, month=3, day=2, hour=18, minute=10), length=timedelta(hours=1)) a4.save() overlaps = Appointment._get_overlaps([a4, a2, a3, a1]) self.assertEquals(len(overlaps), 0)
def test_over_lap(self): a1 = Appointment(start_date_and_time=datetime(year=2018, month=3, day=2, hour=14, minute=20), length=timedelta(hours=0, minutes=2)) a1.save() a2 = Appointment(start_date_and_time=datetime(year=2018, month=3, day=2, hour=14, minute=21), length=timedelta(hours=0, minutes=1)) a2.save() a3 = Appointment(start_date_and_time=datetime(year=2018, month=3, day=2, hour=14, minute=22), length=timedelta(hours=0, minutes=1)) a3.save() a4 = Appointment(start_date_and_time=datetime(year=2018, month=3, day=2, hour=14, minute=23), length=timedelta(hours=0, minutes=1)) a4.save() overlaps = Appointment._get_overlaps([a4, a2, a3, a1]) self.assertEquals(len(overlaps), 1)
def test_four_spread_over_lapping(self): a1 = Appointment(start_date_and_time=datetime(year=2018, month=3, day=2, hour=14, minute=20), length=timedelta(hours=1)) a1.save() a2 = Appointment(start_date_and_time=datetime(year=2018, month=3, day=2, hour=14, minute=50), length=timedelta(hours=1)) a2.save() a3 = Appointment(start_date_and_time=datetime(year=2018, month=3, day=2, hour=15, minute=10), length=timedelta(hours=1)) a3.save() a4 = Appointment(start_date_and_time=datetime(year=2018, month=3, day=2, hour=16, minute=00), length=timedelta(hours=1)) a4.save() overlaps = Appointment._get_overlaps([a1, a2, a3, a4]) self.assertEquals(len(overlaps), 3)
def test_same_start_different_duration(self): a1 = Appointment(start_date_and_time=datetime(year=2018, month=3, day=2, hour=14, minute=20), length=timedelta(minutes=1)) a1.save() a2 = Appointment(start_date_and_time=datetime(year=2018, month=3, day=2, hour=14, minute=20), length=timedelta(minutes=2)) a2.save() a3 = Appointment(start_date_and_time=datetime(year=2018, month=3, day=2, hour=14, minute=20), length=timedelta(minutes=3)) a3.save() a4 = Appointment(start_date_and_time=datetime(year=2018, month=3, day=2, hour=14, minute=20), length=timedelta(minutes=4)) a4.save() overlaps = Appointment._get_overlaps([a4, a2, a3, a1]) self.assertEquals(len(overlaps), 3)