def patient(request, pat_id=None): """ Displays the patient info and orders """ # Grab patient from the database patient_rec = Patient.objects.get(pk=pat_id) # Check if it is a post request. If so, build our form with the post data. if request.method == 'POST': form = PatientInfoForm(data=request.POST, instance=patient_rec) # Ensure form is valid. If so, save. If not, show error. if form.is_valid(): form.save() else: messages = { 'headline1': 'Invalid Form', 'headline2': 'Please try again.', 'headline3': f"{form.errors}" } return show_message(request, messages) # Set up variables for our template and render it context = { 'patient_info': patient_rec, 'form': PatientInfoForm(instance=patient_rec), 'active_orders': patient_rec.orders.filter(level_id__lt=4), 'complete_orders': patient_rec.orders.filter(level_id__gte=4), } return render(request, 'patient.html', context)
def new_patient(request): """ Handles creation of a new patient """ # if not post request, redirect to 404 if not request.method == 'POST': raise Http404 # set up new patient request form with POST data new_form = PatientInfoForm(data=request.POST) # Check if form is valid. If so, assign doctor and save, the redir to a new order. Otherwise, show error. if new_form.is_valid(): new_patient = new_form.save() new_patient.doctor_id = request.user.pk new_patient.save() return redirect('new_order', pat_id=new_patient.pk) else: context = { 'patient_list': None, 'date_selected': None, 'new_patient_form': new_form, 'show_modal': True, } return render(request, 'patient_lookup.html', context)
def new_patient(request): """ Handles creation of a new patient """ # if not post request, redirect to 404 if not request.method == 'POST': raise Http404 # set up new patient request form with POST data new_form = PatientInfoForm(data=request.POST) # Check if form is valid. If so, assign doctor and save, the redir to a new order. Otherwise, show error. if new_form.is_valid(): new_patient = new_form.save() new_patient.doctor_id = request.user.pk new_patient.save() return redirect('new_order', pat_id=new_patient.pk)
def new_patient(request): """ Handles creation of a new patient """ # if not post request, redirect to 404 if not request.method == 'POST': raise Http404 # set up new patient request form with POST data new_form = PatientInfoForm(data=request.POST) # Check if form is valid. If so, assign doctor and save, the redir to a new order. Otherwise, show error. if new_form.is_valid(): new_patient = new_form.save(commit=False) new_patient.set_password(new_form.cleaned_data['password']) new_user = User.objects.create_user( username=new_form.cleaned_data['email_info'], email=new_form.cleaned_data['email_info'], password=new_form.cleaned_data['password']) new_user.first_name = new_form.cleaned_data['first_name'] new_user.last_name = new_form.cleaned_data['last_name'] new_user.save() new_patient.user = new_user new_patient.save() patient_group = Group.objects.get(name="Patient") patient_group.user_set.add(new_patient.user) new_patient.doctor_id = request.user.pk new_patient.save() return redirect('new_order', pat_id=new_patient.pk) else: context = { 'patient_list': None, 'date_selected': None, 'new_patient_form': new_form, 'show_modal': True, } return render(request, 'patient_lookup.html', context)