def doc_accept(request, email, id): teider = User.objects.get(email=email) doctor = User.objects.get(email=request.user.email) query = Query.objects.get(id=id) price = query.price print(query) if request.method == 'POST': form = AppointmentForm(request.POST) if form.is_valid(): print(form.cleaned_data) data = form.cleaned_data appointment_time = data['appointment_time'] query.status = 'accepted' query.save() Appointment.objects.create(teider=teider, doctor=doctor, price=price, appointment_time=appointment_time, status='booked') return redirect('doc-dashboard') else: form = AppointmentForm() context = { 'form': form, } return render(request, 'doctors/accept.html', context)
def appointment_add(request): if request.method == 'POST': form = AppointmentForm(request.POST) if form.is_valid(): form.save() return index(request) else: return render(request, 'appointment_add.html', {'form': form}) else: return render(request, 'appointment_add.html')
def process_add_event_form(request): form = AppointmentForm(request.POST or None) form.fields['venue'].queryset = Venue.objects.filter(customer=request.user.userprofile.customer) if form.is_valid(): form.create_appointment(request.user) messages.add_message( request, messages.SUCCESS, _('Successfully added appointment')) return { 'success': True, } form_html = render_crispy_form(form) return {'success': False, 'form_html': form_html}
def process_add_event_form(request): form = AppointmentForm(request.POST or None) form.fields['venue'].queryset = Venue.objects.filter( customer=request.user.userprofile.customer) if form.is_valid(): form.create_appointment(request.user) messages.add_message(request, messages.SUCCESS, _('Successfully added appointment')) return { 'success': True, } form_html = render_crispy_form(form) return {'success': False, 'form_html': form_html}
def post(self,request): form = AppointmentForm(request.POST) if form.is_valid(): print 1 print form.cleaned_data['time'] with transaction.atomic(): appointment = Appointment.objects.create( time = form.cleaned_data['time'], status = form.cleaned_data['status'], physician_id = form.cleaned_data['physician'], patient_id = form.cleaned_data['patient'], ) else: print 2 return HttpResponseRedirect('/appointments/')
def edit_appointment(request, appointment_id): appointment = get_object_or_404(Appointment, id=appointment_id) # if freelancers is trying access project he is not working on => forbidden if Freelancer.objects.filter(email=request.user.email).exists() and \ Freelancer.objects.get(email=request.user.email).current_project != appointment.project_number: return HttpResponseForbidden() # if client is not owner of the appointment => forbidden if not Freelancer.objects.filter(email=request.user.email).exists() and appointment.email != request.user.email: return HttpResponseForbidden() try: freelancer = Freelancer.objects.get(current_project=appointment.project_number) except: freelancer = False if request.method == 'POST': form = AppointmentForm(request.POST, instance=appointment) if form.is_valid(): form.save() return redirect('profile') else: return render(request, 'public_user/index.html') form = AppointmentForm(instance=appointment) context = { 'paid_for':appointment.paid_for, 'form': form, 'appointment_id': appointment_id, 'email': appointment.email, 'freelancer': freelancer } return render(request, 'appointments/edit_appointment.html', context)
def post(self, request, uuid=None, *args, **kwargs): client = get_object_or_404(Client, uuid=uuid) if not request.user.has_perm(Client.CAN_VIEW, client): raise PermissionDenied form = AppointmentForm(request.POST or None) qs = User.objects.filter(Q(is_owner=True) | Q(is_team=True)) form.fields['assigned_to'].choices = [ (user.username, user.first_name + ' ' + user.last_name) for user in qs.all() ] if form.is_valid(): appointment = Appointment() appointment.client = client appointment.appointment_date = form.cleaned_data[ 'appointment_date'] appointment.appointment_time = form.cleaned_data[ 'appointment_time'] appointment.appointment_duration = form.cleaned_data[ 'appointment_duration'] appointment.appointment_reason = form.cleaned_data[ 'appointment_reason'] appointment.assigned_to = get_object_or_404( User, username=form.cleaned_data['assigned_to']) appointment.set_aware_appointment_datetime_utc( request.user.timezone) if form.cleaned_data['send_confirmation_email']: emailer.send_appointment_created_email(appointment, request.user) appointment.save() messages.success(request, 'Appointment deleted successfully') ActivityStream( customer=self.request.user.customer, actor=self.request.user.username, verb='created_appointment', action_object=appointment.get_human_readable_datetime(), target=client.__str__()).save() return redirect('clients:client-appointments', uuid=client.uuid) messages.error(request, 'There was an error Creating appointment') context = {'form': form} return render(request, self.template_name, context)
def save_appointment(request, doctor_id): doctor_details = get_object_or_404(ClinicianProfile, id=doctor_id) appt_form = AppointmentForm(data=request.POST) booked = False if request.method == 'POST': if appt_form.is_valid(): appt = appt_form.save(commit=False) appt.doctor_id = doctor_details.pk appt.user_id = request.user.userprofile.pk appt.save() else: print(appt_form.errors) return render_to_response('appointments/appointment_confirmation.html', {'appt':appt,'doctor':doctor_details,}, RequestContext(request))
def get_context_data(self, **kwargs): context = super(AddEventView, self).get_context_data(**kwargs) client_form = SelectClientForm() client_form.fields['client'].queryset = Client.objects.filter( customer=self.request.user.userprofile.customer) context['SelectClientForm'] = client_form context['AddClientForm'] = AddClientForm() appointment_form = AppointmentForm() appointment_form.fields['venue'].queryset = Venue.objects.filter( customer=self.request.user.userprofile.customer) context['AppointmentForm'] = appointment_form return context
def get(self, request, uuid=None, *args, **kwargs): client = get_object_or_404(Client, uuid=uuid) if not request.user.has_perm(Client.CAN_VIEW, client): raise PermissionDenied if not self.request.user.is_scheduler: raise PermissionDenied qs = User.objects.filter(Q(is_owner=True) | Q(is_team=True)) form = AppointmentForm() form.fields['assigned_to'].choices = [ (user.username, user.first_name + ' ' + user.last_name) for user in qs.all() ] form.fields['appointment_status'] = 'scheduled' context = { 'form': form, 'client': client, } return render(request, self.template_name, context)
def get(self, request, *args, **kwargs): add_form = AppointmentForm(self.request.GET or None) search_form = AppointmentSearchForm(self.request.GET or None) data = self.get_context_data(**kwargs) data['add_form'] = add_form data['search_form'] = search_form ajax_request = request.GET.copy() keyword = ajax_request.get('keyword', None) print request.GET.get('keyword', None), "AJAX", self.request.is_ajax(), keyword if self.request.is_ajax(): self.template_name = "search_results.html" keyword = ajax_request.get('keyword', None) if keyword: appointments, count = AppointmentsListResource( ).get_matching_appointments(keyword) else: # Returns all appointments because keyword is None today = datetime.datetime.today() appointments, count = AppointmentsListResource()._get(today) print "stuff", appointments, count data['appointments'] = appointments data['count'] = count return self.render_to_response(data)
def test_appointment_model_form_has_onchange_behavior_for_veterinary_physician(self): form = AppointmentForm() self.assertTrue(form.Meta.widgets.get('veterinary_physician').attrs.get('onchange') is not None)
def new_appointment_w_date(request, date_id): """ This view manages the creation of a new appointment """ if request.user.is_authenticated(): title = "Create Appointment" user_id = request.user.id # used to check which type of user is creating a new appointment isPatient = (Patient.objects.filter( profile__user_id=user_id).count() == 1) isDoctor = (Doctor.objects.filter( profile__user_id=user_id).count() == 1) isNurse = (Nurse.objects.filter(profile__user_id=user_id).count() == 1) year = date_id[0:4] month = date_id[4:6] day = date_id[6:8] date_string = year + "-" + month + "-" + day userType = "" if isPatient: userType = "Patient" #initialize the from the the passed in date initdata = {"date": date_string} # create a new form form = AppointmentForm(request.POST or None, initial=initdata) # if the form is completed, log the action and save the new appointment if form.is_valid(): appointment = form.save(commit=False) appointment.patient = Patient.objects.get( profile__user_id=request.user.id) #appointment.date = date_string appointment.save() log = Log(username=appointment.patient.profile.user.username, action=" created a new appointment ") log.save() return redirect('/calendar') elif isDoctor: userType = "Doctor" # initialize the from the the passed in date initdata = {"date": date_string} # create a new form form = AppointmentForm(request.POST or None, initial=initdata) if form.is_valid(): appointment = form.save(commit=False) appointment.doctor = Doctor.objects.get( profile__user_id=request.user.id) #appointment.date = date_string appointment.save() log = Log(username=appointment.doctor.profile.user.username, action=" created a new appointment ") log.save() return redirect('/calendar') elif isNurse: userType = "Nurse" nurse = (Nurse.objects.filter(profile__user_id=user_id))[0] # initialize the from the the passed in date initdata = {"date": date_string} # create a new form form = AppointmentForm(request.POST or None, initial=initdata) if form.is_valid(): appointment = form.save(commit=False) #appointment.date = date_string appointment.save() log = Log(username=nurse.profile.user.username, action=" created a new appointment ") log.save() return redirect('/calendar') context = {"form": form, "title": title, "usertype": userType} return render(request, "appointments/create_appointment.html", context) else: return redirect('/login') #-------------------------------------------------------------------------------------------------------
def new_appointment(request): """ This view manages the creation of a new appointment """ if request.user.is_authenticated(): title = "Create Appointment" user_id = request.user.id # used to check which type of user is creating a new appointment isPatient = (Patient.objects.filter( profile__user_id=user_id).count() == 1) isDoctor = (Doctor.objects.filter( profile__user_id=user_id).count() == 1) isNurse = (Nurse.objects.filter(profile__user_id=user_id).count() == 1) userType = "" if isPatient: userType = "Patient" # create a new form form = AppointmentForm(request.POST or None) # if the form is completed, log the action and save the new appointment if form.is_valid(): appointment = form.save(commit=False) appointment.patient = Patient.objects.get( profile__user_id=request.user.id) appointment.save() log = Log(username=appointment.patient.profile.user.username, action=" created a new appointment ") log.save() msg = "Appointment with %s successfully created on %s" % ( appointment.doctor, appointment.date) return account.views.index(request, msg) elif isDoctor: userType = "Doctor" form = AppointmentFormDoctor(request.POST or None) if form.is_valid(): appointment = form.save(commit=False) appointment.doctor = Doctor.objects.get( profile__user_id=request.user.id) appointment.save() log = Log(username=appointment.doctor.profile.user.username, action=" created a new appointment ") log.save() msg = "Appointment with patient %s successfully created on %s" % ( appointment.patient, appointment.date) return account.views.index(request, msg) elif isNurse: userType = "Nurse" nurse = (Nurse.objects.filter(profile__user_id=user_id))[0] form = AppointmentFormNurse(request.POST or None) if form.is_valid(): appointment = form.save(commit=False) appointment.save() log = Log(username=nurse.profile.user.username, action=" created a new appointment ") log.save() msg = "Appointment successfully created with patient %s seeing %s on %s" % ( appointment.patient, appointment.doctor, appointment.date) return account.views.index(request, msg) context = {"form": form, "title": title, "usertype": userType} return render(request, "appointments/create_appointment.html", context) else: return redirect('/login')
def edit_appointment(request, appointment_id): # this view is used for editing appointments with the appointmentform if request.user.is_authenticated(): title = "Edit Appointment" user_id = request.user.id isPatient = (Patient.objects.filter( profile__user_id=user_id).count() == 1) isDoctor = (Doctor.objects.filter( profile__user_id=user_id).count() == 1) isNurse = (Nurse.objects.filter(profile__user_id=user_id).count() == 1) app_instance = Appointment.objects.get(id=appointment_id) if isPatient: # get the patient instance pat = Patient.objects.filter(profile__user_id=request.user.id)[0] # tests if the patient is viewing only their appointments isValid = (Appointment.objects.filter(id=appointment_id, patient=pat)) # redirect if trying to access others appointments if not isValid: return redirect('/profile') # used to fill form with current values initdata = { 'doctor': app_instance.doctor, "date": app_instance.date, 'time': app_instance.time, 'reason': app_instance.reason, 'short_reason': app_instance.short_reason } # create the form form = AppointmentForm(request.POST, instance=app_instance, initial=initdata) if request.POST: """ modifications to appointments is completed in a bit of a messy way. The issue is that appointments should not be able to be edited to be during the same time of another if the doctor is the same. This test is done by getting a list of all appointments and checking if any match the current form submission. The problem with this is when an appointment is under edit, it already exists, and the checker flags the operation as illegal. The way around this was to save all the instance's current fields, and delete it. If the form fails, a new instance is created with the old forms and the user can try again. If the form is successful a new instance is created with the new values and the user is redirected. """ # these are the appointment fields being saved pk = Appointment.objects.filter(id=appointment_id)[0].id date = Appointment.objects.filter(id=appointment_id)[0].date time = Appointment.objects.filter(id=appointment_id)[0].time doc = Appointment.objects.filter(id=appointment_id)[0].doctor reason = Appointment.objects.filter( id=appointment_id)[0].reason s_reason = Appointment.objects.filter( id=appointment_id)[0].short_reason # deletes the current appointment instance Appointment.objects.filter(id=appointment_id)[0].delete() # if the form is valid, log it and save the new data if form.is_valid(): log = Log(username=pat.profile.user.username, action=" edited an appointment ") log.save() form.save() msg = "Appointment with %s on %s successfully modified" % ( doc, date) return account.views.index(request, msg) # if the form fails, create a new appointment instance and save it and send a new form else: app = Appointment(id=pk, patient=pat, date=date, time=time, doctor=doc, reason=reason, short_reason=s_reason) app.save() else: form = AppointmentForm(instance=app_instance, initial=initdata) return render(request, 'appointments/edit_appointment.html', { 'form': form, 'usertype': 'patient' }) elif isDoctor: doc = Doctor.objects.filter(profile__user_id=request.user.id)[0] # tests if the doctor is viewing only their appointments isValid = (Appointment.objects.filter(id=appointment_id, doctor=doc)) if not isValid: return redirect('/profile') initdata = { 'patient': app_instance.patient, "date": app_instance.date, 'time': app_instance.time, 'reason': app_instance.reason, 'short_reason': app_instance.short_reason } form = AppointmentFormDoctor(request.POST, instance=app_instance, initial=initdata) if request.POST: date = Appointment.objects.filter(id=appointment_id)[0].date time = Appointment.objects.filter(id=appointment_id)[0].time pat = Appointment.objects.filter(id=appointment_id)[0].patient reason = Appointment.objects.filter( id=appointment_id)[0].reason s_reason = Appointment.objects.filter( id=appointment_id)[0].short_reason pk = Appointment.objects.filter(id=appointment_id)[0].id Appointment.objects.filter(id=appointment_id)[0].delete() if form.is_valid(): log = Log(username=doc.profile.user.username, action=" edited an appointment ") log.save() form.save() return redirect('/') else: app = Appointment(id=pk, patient=pat, date=date, time=time, doctor=doc, reason=reason, short_reason=s_reason) app.save() else: form = AppointmentFormDoctor(instance=app_instance, initial=initdata) return render(request, 'appointments/edit_appointment.html', { 'form': form, 'usertype': 'Doctor' }) elif isNurse: nurse = Nurse.objects.filter(profile__user_id=request.user.id) initdata = { 'patient': app_instance.patient, "doctor": app_instance.doctor, "date": app_instance.date, 'time': app_instance.time, 'reason': app_instance.reason, 'short_reason': app_instance.short_reason } form = AppointmentFormNurse(request.POST, instance=app_instance, initial=initdata) if request.POST: pk = Appointment.objects.filter(id=appointment_id)[0].id date = Appointment.objects.filter(id=appointment_id)[0].date time = Appointment.objects.filter(id=appointment_id)[0].time pat = Appointment.objects.filter(id=appointment_id)[0].patient doc = Appointment.objects.filter(id=appointment_id)[0].doctor reason = Appointment.objects.filter( id=appointment_id)[0].reason s_reason = Appointment.objects.filter( id=appointment_id)[0].short_reason Appointment.objects.filter(id=appointment_id)[0].delete() if form.is_valid(): form.save() log = Log(username=nurse[0].profile.user.username, action=" edited an appointment ") log.save() return redirect('/') else: app = Appointment(id=pk, patient=pat, date=date, time=time, doctor=doc, reason=reason, short_reason=s_reason) app.save() else: form = AppointmentFormNurse(instance=app_instance, initial=initdata) return render(request, 'appointments/edit_appointment.html', { 'form': form, 'usertype': 'Nurse' }) else: return redirect('/login')
def appointments(request): template = 'appointments/appointment.html' context = {} # customer creating appointment if request.method == 'POST': def user_name_present(name): if User.objects.filter(username=name).exists(): return True return False def user_email_present(email): if User.objects.filter(email=email).exists(): return True return False form_data = { 'name': request.POST['name'], 'email': request.POST['email'], 'phone_num': request.POST['phone_num'], 'site_type': request.POST['site_type'], 'time_slot': request.POST['time_slot'], 'project': request.POST['project'], 'notes': request.POST['notes'], 'done': 'done' in request.POST, } appointment_form = AppointmentForm(form_data) context = { 'form': appointment_form } # user already registered with his email if not request.user.is_authenticated and user_email_present(request.POST['email']): messages.error(request, 'It appears that you already registered.Please login with ' ' your credentials to manage your account. Thank you! ' ) return render(request, template, context) # username used already, Django won't create new user with the same name if not request.user.is_authenticated and user_name_present(request.POST['name']): messages.error(request, 'User name taken. Please use different user name!') return render(request, template, context) if appointment_form.is_valid(): try: """ IF FORM IS VALID, WE WILL SAVE IT TO DB AND WILL CREATE NEW USER WITH CREDENTIALS FROM THE FORM SO THAT HE CAN LOG IN INTO HIS ACCOUNT AND CHANGE HIS CONSULTATION """ appointment_form = appointment_form.save() if user_email_present(request.POST['email']): messages.info(request, 'It appears that you already registered. We added this ' ' consultation to your dashboard. Thank you! ' ) else: # create new user if not registered yet new_user = User.objects.create_user(request.POST['name'], request.POST['email'], request.POST['password']) new_user.save() messages.success(request, f'Your account was created successfully. Please login with your ' 'credentials.') # user can sign in with his credentials # sending email to owner of the website, informing him about new appointment send_mail( { 'to': '*****@*****.**', 'subject': 'New appointment', 'template': 'marcellidesigns_appointment', 'template_vars': {'welcome': 'New appointment : ' + request.POST['site_type'], 'body_1': request.POST['email'] + ' - ' + request.POST['phone_num'] + ' - ' + request.POST[ 'name'], 'body_2': request.POST['project'], 'question': request.POST['time_slot'], 'sign-in': 'Site', 'welcome_team': 'Marcelli Designs', } }) # sending email to customer, informing him about new appointment send_mail( { 'to': request.POST['email'], 'subject': 'Your appointment', 'template': 'marcellidesigns_customer_appointment', 'template_vars': {'welcome': 'Your appointment', 'customer_name': request.POST['name'], 'time': request.POST['time_slot'], 'phone': request.POST['phone_num'], 'project': request.POST['project'], 'site_type': request.POST['site_type'], 'welcome_team': 'Marcelli Designs', } }) return redirect(reverse('account_login')) except: messages.error(request, 'There was an error with your form. \ Please double check your information') else: messages.error(request, 'There was an error with your form. \ Please double check your information') context['consultation_form'] = appointment_form return render(request, template, context)