def edit_patient(request, id): if request.method == 'POST': dentist = request.user.dentist patient = get_object_or_404( Patient, id=id, dentist=dentist ) patient_form = PatientForm(request.POST, instance=patient) if patient_form.is_valid(): patient_form.save() patient_info = PatientForm(instance=patient) return TemplateResponse( request, 'register/patient_data/_form.html', {'patient_info_form': patient_info, 'patient': patient} ) else: return JsonResponse({'status': 'ERROR', 'errors': patient_form.errors})
def patients(request): from person.function import get_position dentist = request.user.dentist if request.method == 'GET': patients = Patient.objects.filter(dentist=dentist, active=True).order_by('-id') import operator from django.db.models import Q text_search = request.GET.get('text_search', None) if text_search is not None: terms = text_search.split(' ') qs1 = reduce(operator.or_, (Q(last_name__icontains=n) for n in terms)) qs2 = reduce(operator.or_, (Q(first_name__icontains=n) for n in terms)) qs3 = reduce(operator.or_, (Q(social_work__initial__icontains=n) for n in terms)) qs4 = reduce(operator.or_, (Q(social_work__name__icontains=n) for n in terms)) qs5 = reduce(operator.or_, (Q(subsidiary_number__icontains=n) for n in terms)) patients = patients.filter(Q(qs1) | Q(qs2) | Q(qs3) | Q(qs4) | Q(qs5) ) page = request.GET.get('page', 1) nro_row = request.GET.get('nro_row', '10') paginator = Paginator(patients, nro_row) try: patients = paginator.page(page) except PageNotAnInteger: patients = paginator.page(1) except EmptyPage: patients = paginator.page(paginator.num_pages) if request.is_ajax(): return TemplateResponse( request, 'person/patients/table.html', { 'patients': patients, 'nro_row': nro_row, } ) else: form = PatientForm() return render_to_response( 'person/patients.html', { 'template': 'patient', 'patient_form': form, 'patients': patients, 'text_search': text_search, 'nro_row': nro_row, }, RequestContext(request) ) else: form = PatientForm(request.POST) sub_num = request.POST.get('subsidiary_number', None) sub_num_exist = Patient.objects.filter(dentist=dentist, subsidiary_number=sub_num).exists() if sub_num == '' or not sub_num_exist: if form.is_valid(): new_patient = form.save(commit=False) new_patient.dentist = dentist odontogram = Odontogram() odontogram.save() if not odontogram.get_teeth().exists(): for e1, e2 in ELEMENTS: x, y = get_position(e2) tooth = Tooth(odontogram=odontogram, number=e1, position_x=x, position_y=y) if (e1, e2) in MILK_TEETH: tooth.work_type = 1 tooth.color = 1 tooth.save() for l1, l2 in LOCATIONS: Sector(tooth=tooth, location=l1, points=l1).save() new_patient.odontogram = odontogram new_patient.code = 'P%04d' % (dentist.number_of_patients + 1) new_patient.save() return JsonResponse( {'status': 'OK', 'url': reverse('person:profile_patient', kwargs={'id': new_patient.id}) } ) else: return JsonResponse({'status': 'ERROR', 'errors': form.errors}) else: form.errors['subsidiary_number'] = [u'Ya existe un/a Paciente con este/a Numero de afiliado.'] return JsonResponse({'status': 'ERROR', 'errors': form.errors})