amr_aly

Untitled

May 17th, 2021 (edited)
576
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. def edit_patient(request, id): # Making Update to a Patient
  2.    
  3.     query = Patients.objects.get(id=id)
  4.     form = PatientsForm(request.POST or None, request.FILES or None, instance=query)
  5.     if form.is_valid():
  6.         save_form = form.save(commit=False)
  7.         save_form.save()
  8.         patient_id = save_form.id
  9.         name = save_form.name
  10.         card = save_form.cardid
  11.        
  12.         dup_name = Patients.objects\
  13.                                 .values('name')\
  14.                                 .annotate(ncount=Count('name'))\
  15.                                 .filter(name=name, ncount__gt=1)
  16.         records = Patients.objects\
  17.                                 .filter(name__in=[item['name'] for item in dup_name])
  18.         # print('rec_edit = '+ str(records) + str(name))#(dup_name, records)
  19.         rec = [item.name for item in records]
  20.         reco = any(rec.count(element) > 1 for element in rec)
  21.         # print('patname= '+str(name), 'rec_edit= '+str(rec), 'dupname_edit= ' +str(dup_name),reco)
  22.                
  23.         # check duplicate for cardid
  24.         dup_num = Patients.objects \
  25.                                     .values('cardid') \
  26.                                     .annotate(bcount=Count('cardid')) \
  27.                                     .filter(cardid=card, bcount__gt=1)
  28.                 records_num = Patients.objects.filter(cardid__in=[item['cardid'] for item in dup_num])
  29.                 # print('recnum_edit = '+ str(records_num) + str(num))#(dup_name, records)
  30.                 rec_num = [item.cardid for item in records_num]
  31.                 reco_num = any(rec_num.count(element) > 1 for element in rec_num)
  32.                 # print(rec_num, 'reco_num= ' + str(reco_num))
  33.                 if reco or reco_num:
  34.                     if reco:
  35.                         messages.success(request, 'Patient (' +str(name)+ ') is already exists change the name ..!')
  36.                         Patients.objects.filter(id=patient_id).update(name=query.name)
  37.                         return redirect(reverse('patientdata:edit_patient', kwargs={'id': id}))
  38.                     elif reco_num:
  39.                         messages.success(request, 'Card ID is already exists !, It must not be duplicated')
  40.                         Patients.objects.filter(id=patient_id).update(cardid=query.cardid)
  41.                         return redirect(reverse('patientdata:edit_patient', kwargs={'id':id}))
  42.                 else:
  43.                     messages.success(request, 'Edit changes done successfully')
  44.                     return redirect(reverse('patientdata:edit_patient', kwargs={'id':id}))
  45.                     # return redirect(reverse('patientdata:table_patient'))
  46.        
  47.     context = {
  48.         'patient': patient,
  49.         'patient_id': patient_id,
  50.         'editpatform': form,
  51.         'query': query,
  52.         'barcode': bar,
  53.         'match_pasthist': match_pasthist,
  54.         'patient_visits_table':table,
  55.     }
  56.     return render(request, 'patientdata/edit_patient.html', context)
  57.  
RAW Paste Data