· 4 years ago · Jun 03, 2021, 11:44 AM
1########## views.py
2
3def edit_patient(request, id): # Making Update to a Patient
4 qs = Visits.objects.filter(patient=id).order_by('-id')
5
6 ### we will explain the next 2 lines later on the (Django Egypt Group)
7 table = VisitsTable(qs, exclude='patient, addpresent')
8 table.paginate(page=request.GET.get("page", 1), per_page=10)
9
10 query = Patients.objects.get(id=id) # get(birth_date=birth_date)
11 patient = Patients.objects.values('id').filter(id=id).first()
12 barcode = Patients.objects.values('barcode').filter(id=id).first()
13 # patient = Patients.objects.filter(id=id)
14 patient_id = patient['id']
15 bar = barcode['barcode']
16 # print(query, patient)
17 match_pasthist = PastHistory.objects.filter(patient=id).exists()
18
19 form = PatientsForm(request.POST or None, request.FILES or None, instance=query)
20 if form.is_valid():
21 barcode_value = request.POST.get('barurl')
22 if barcode_value == None or barcode_value == '':
23 messages.success(request, 'Create barcode without value is not valid')
24 elif barcode_value != None:
25 qr = pyqrcode.create(barcode_value)
26 name = request.POST.get('name')
27 split_name = name.split()
28 img_name = '-'.join(split_name)
29 db_name = Patients.objects.get(id=id)
30 file_path = 'media_root/patients/' + str(img_name) + '.png'
31
32 if os.path.exists(file_path):
33 os.remove(file_path)
34 qr.png('media_root/patients/' + str(img_name) + '.png', scale=8)
35 save_form = form.save(commit=False)
36 save_form.barimg = 'patients/' + str(img_name) + '.png'
37 save_form.barurl = barcode_value
38 save_form.save()
39
40 patient_id = save_form.id
41 name = save_form.name
42 card = save_form.cardid
43 dup_name = Patients.objects\
44 .values('name')\
45 .annotate(ncount=Count('name'))\
46 .filter(name=name, ncount__gt=1)
47 records = Patients.objects\
48 .filter(name__in=[item['name'] for item in dup_name])
49 # print('rec_edit = '+ str(records) + str(name))#(dup_name, records)
50 rec = [item.name for item in records]
51 reco = any(rec.count(element) > 1 for element in rec)
52 # print('patname= '+str(name), 'rec_edit= '+str(rec), 'dupname_edit= ' +str(dup_name),reco)
53
54 # check duplicate for cardid
55 dup_num = Patients.objects \
56 .values('cardid') \
57 .annotate(bcount=Count('cardid')) \
58 .filter(cardid=card, bcount__gt=1)
59 records_num = Patients.objects.filter(cardid__in=[item['cardid'] for item in dup_num])
60 # print('recnum_edit = '+ str(records_num) + str(num))#(dup_name, records)
61 rec_num = [item.cardid for item in records_num]
62 reco_num = any(rec_num.count(element) > 1 for element in rec_num)
63 # print(rec_num, 'reco_num= ' + str(reco_num))
64 if reco or reco_num:
65 if reco:
66 messages.error(request, 'Patient (' +str(name)+ ') is already exists change the name ..!')
67 Patients.objects.filter(id=patient_id).update(name=db_name.name)
68 return redirect(reverse('patientdata:edit_patient', kwargs={'id': id}))
69 elif reco_num:
70 messages.error(request, 'Card ID is already exists !, It must not be duplicated')
71 return redirect(reverse('patientdata:edit_patient', kwargs={'id':id}))
72 else:
73 messages.success(request, 'Edit changes done successfully')
74 return redirect(reverse('patientdata:edit_patient', kwargs={'id':id}))
75 # return redirect(reverse('patientdata:table_patient'))
76 elif not os.path.exists(file_path):
77 qr.png('media_root/patients/' + str(img_name) + '.png', scale=8)
78 save_form = form.save(commit=False)
79 save_form.barimg = 'patients/' + str(img_name) + '.png'
80 save_form.barurl = barcode_value
81 save_form.save()
82
83 name = save_form.name
84 card = save_form.cardid
85 dup_name = Patients.objects\
86 .values('name')\
87 .annotate(ncount=Count('name'))\
88 .filter(name=name, ncount__gt=1)
89 records = Patients.objects\
90 .filter(name__in=[item['name'] for item in dup_name])
91 # print('rec_edit = '+ str(records) + str(name))#(dup_name, records)
92 rec = [item.name for item in records]
93 reco = any(rec.count(element) > 1 for element in rec)
94 # print('patname= '+str(name), 'rec_edit= '+str(rec), 'dupname_edit= ' +str(dup_name),reco)
95
96 # check duplicate for cardid
97 dup_num = Patients.objects \
98 .values('cardid') \
99 .annotate(bcount=Count('cardid')) \
100 .filter(cardid=card, bcount__gt=1)
101 records_num = Patients.objects.filter(cardid__in=[item['cardid'] for item in dup_num])
102 # print('recnum_edit = '+ str(records_num) + str(num))#(dup_name, records)
103 rec_num = [item.cardid for item in records_num]
104 reco_num = any(rec_num.count(element) > 1 for element in rec_num)
105 # print(rec_num, 'reco_num= ' + str(reco_num))
106 if reco or reco_num:
107 if reco:
108 messages.success(request, 'Patient (' +str(name)+ ') is already exists change the name ..!')
109 return redirect(reverse('patientdata:edit_patient', kwargs={'id': id}))
110 elif reco_num:
111 messages.success(request, 'Card ID is already exists !, It must not be duplicated')
112 return redirect(reverse('patientdata:edit_patient', kwargs={'id':id}))
113 else:
114 messages.success(request, 'Edit changes done successfully')
115 return redirect(reverse('patientdata:edit_patient', kwargs={'id':id}))
116 # return redirect(reverse('patientdata:table_patient'))
117
118 context = {
119 'patient': patient,
120 'patient_id': patient_id,
121 'editpatform': form,
122 'query': query,
123 'barcode': bar,
124 'match_pasthist': match_pasthist,
125 'patient_visits_table':table,
126 }
127 return render(request, 'patientdata/edit_patient.html', context)
128
129
130################## edit_patient.html
131{% extends 'index.html' %}
132{% load static %}
133{% load render_table from django_tables2 %}
134{% load widget_tweaks %}
135<!-- -->
136{% block content %}
137
138<div class="container" style="background-color: white;" id="editPatient">
139 <article class="message is-{{ message.tags }}">
140 <div class="message-body alert {{ message.tags }}" style="color: red;" role="alert">
141 {{ editpatform.non_field_errors }}
142 </div>
143 </article>
144
145 <solid>
146 <h1 style="color:black">Edit Patient</h1>
147 </solid>
148 <hr>
149 <div class="container">
150 <a class="button is-dark" href="{% url 'visits:pass_patient_id' patient_id %}" role="button">
151 New Visit</a>
152 <a class="button is-dark" href="{% url 'patientdata:save_patient' %}" role="button">
153 New Patient</a>
154 </div>
155 <hr>
156
157 <br><br>
158 <form enctype="multipart/form-data" method="POST" id="edit-pat-form">{% csrf_token %}
159
160 <div class="float-left" style="background-color:transparent;height: 100%; width: 35%;" id="left-editpat-div">
161
162 {{editpatform.name.label_tag}} {{editpatform.name}} <br>
163
164 {{editpatform.address.label_tag}} {{editpatform.address}}
165 <br>
166
167 {{editpatform.birth_date.label_tag}}
168 {% render_field editpatform.birth_date v-model="dob" id="dob" %} <br>
169
170 {{editpatform.age.label_tag}}
171 {% render_field editpatform.age ::value="getAge" id="age" %} <br>
172
173 {{editpatform.phone.label_tag}} {{editpatform.phone}} <br>
174
175 {{editpatform.mobile.label_tag}} {{editpatform.mobile}} <br>
176
177 {{editpatform.cardid.label_tag}}
178 {% render_field editpatform.cardid v-model="cardid" v-on::change="cardValue" id="cardid" %} <br>
179
180 {{editpatform.barcode.label_tag.hidden }}
181 {% render_field editpatform.barcode ::value="barcodeChanged + cardValue" hidden="true" %} <br>
182
183 {% render_field editpatform.barurl ::value="urlChanged" hidden="true" %}
184
185 {% if query.barimg %}
186 <div>
187 <a href="{% url 'patientdata:patient_details' barcode %}">
188 <img class="image" src="{{ query.barimg.url }}">
189 </a>
190 </div> <br>
191 {% endif %}
192 <br>
193 <button type="submit" class="button is-primary">Update</button>
194
195 <hr>
196 </div>
197
198 ### we will explain (how to make a designed table by django_tables2) later on the (Django Egypt Group)
199 <div class="float-right" style="float:right; height: 100%; width: 55%;background-color:white; border-style: outset; border-
200 radius: 20px; border-color: #aab5af; box-shadow: 2px 2px 4px 4px grey;" id="edit-pat-table-div">
201 {% render_table patient_visits_table %}
202 </div>
203
204 </form>
205</div>
206
207{% endblock %}
208
209{% block scripts %}
210 <script>
211 // Using Vue.js Here
212 var editPatient = new Vue ({
213 el: '#editPatient',
214 delimiters: ['[[', ']]'],
215 // data () {
216 // return
217 data: {
218 patient_name: '' ,
219 title: "Add New Patient",
220 message: '{{ non_field_errors }}' ,
221 id:'{{ patient_id }}',
222 name:'',
223 cardid:'',
224 dob: new Date(),
225 age: '',
226 url:'http://192.168.1.120:8000/patientdata/patient/details/by/barcode/',
227 barcode:'',
228 sign:'_',
229 },
230 computed: {
231 cardValue(){
232 this.cardid = document.getElementById('cardid').value;
233 return this.cardid;
234 },
235 barcodeChanged(){
236 this.barcode = this.id + this.sign + this.dob + this.sign ;//+ this.cardid;
237 return this.barcode;
238 },
239 urlChanged(){
240 this.barurl = this.url + this.id + this.sign + this.dob + this.sign + this.cardid;
241 return this.barurl;
242 },
243 }
244 })
245
246 </script>
247
248{% endblock %}
249