Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def edit_patient(request, id): # Making Update to a Patient
- query = Patients.objects.get(id=id)
- form = PatientsForm(request.POST or None, request.FILES or None, instance=query)
- if form.is_valid():
- save_form = form.save(commit=False)
- save_form.save()
- patient_id = save_form.id
- name = save_form.name
- card = save_form.cardid
- dup_name = Patients.objects\
- .values('name')\
- .annotate(ncount=Count('name'))\
- .filter(name=name, ncount__gt=1)
- records = Patients.objects\
- .filter(name__in=[item['name'] for item in dup_name])
- # print('rec_edit = '+ str(records) + str(name))#(dup_name, records)
- rec = [item.name for item in records]
- reco = any(rec.count(element) > 1 for element in rec)
- # print('patname= '+str(name), 'rec_edit= '+str(rec), 'dupname_edit= ' +str(dup_name),reco)
- # check duplicate for cardid
- dup_num = Patients.objects \
- .values('cardid') \
- .annotate(bcount=Count('cardid')) \
- .filter(cardid=card, bcount__gt=1)
- records_num = Patients.objects.filter(cardid__in=[item['cardid'] for item in dup_num])
- # print('recnum_edit = '+ str(records_num) + str(num))#(dup_name, records)
- rec_num = [item.cardid for item in records_num]
- reco_num = any(rec_num.count(element) > 1 for element in rec_num)
- # print(rec_num, 'reco_num= ' + str(reco_num))
- if reco or reco_num:
- if reco:
- messages.success(request, 'Patient (' +str(name)+ ') is already exists change the name ..!')
- Patients.objects.filter(id=patient_id).update(name=query.name)
- return redirect(reverse('patientdata:edit_patient', kwargs={'id': id}))
- elif reco_num:
- messages.success(request, 'Card ID is already exists !, It must not be duplicated')
- Patients.objects.filter(id=patient_id).update(cardid=query.cardid)
- return redirect(reverse('patientdata:edit_patient', kwargs={'id':id}))
- else:
- messages.success(request, 'Edit changes done successfully')
- return redirect(reverse('patientdata:edit_patient', kwargs={'id':id}))
- # return redirect(reverse('patientdata:table_patient'))
- context = {
- 'patient': patient,
- 'patient_id': patient_id,
- 'editpatform': form,
- 'query': query,
- 'barcode': bar,
- 'match_pasthist': match_pasthist,
- 'patient_visits_table':table,
- }
- return render(request, 'patientdata/edit_patient.html', context)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement