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 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.")