amr_aly

Save_Barcode

May 27th, 2021 (edited)
760
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ######################################
  2. ####### views.py
  3. import os
  4. import pyqrcode
  5. from pyzbar.pyzbar import decode
  6. from PIL import Image
  7.  
  8.  
  9. def save_patient(request):
  10.     """ Collecting data for patients and save patient data to database """
  11.     if request.method == 'POST':
  12.         form = PatientsForm(request.POST or None, request.FILES or None)
  13.         if form.is_valid():
  14.             barcode_value = request.POST.get('barurl')
  15.             if barcode_value == None or barcode_value == '':
  16.                 messages.success(request, 'Create barcode without value is not valid')
  17.             elif barcode_value != None:
  18.                 qr = pyqrcode.create(barcode_value)
  19.                 name = request.POST.get('name')
  20.                 split_name = name.split()
  21.                 img_name = '-'.join(split_name)
  22.  
  23.                 file_path = 'media_root/patients/' + str(img_name) + '.png'
  24.                 # print(img_name)
  25.                 match = Patients.objects.filter(name=name).exists()
  26.                 if not os.path.exists(file_path) and not match:
  27.                     qr.png('media_root/patients/' + str(img_name) + '.png', scale=8)
  28.                     save_form = form.save(commit=False)
  29.                     save_form.barimg = 'patients/' + str(img_name) + '.png'
  30.                     save_form.barurl = barcode_value
  31.                     save_form.save()
  32.                     pat_id = save_form.id
  33.                     Visits.objects.create(patient_id=pat_id, visitdate=date.today(),
  34.                                     complain="any comp", sign="any sign",
  35.                                     amount=0, intervention="any intervention")
  36.                     messages.success(request, 'Saving process done ... ')
  37.                     return redirect('patientdata:table_patient')
  38.                 else:
  39.                     messages.error(request, 'Barcode is already exists or Patient name is repeated')
  40.                     return redirect(reverse('patientdata:save_patient'))
  41.     else:
  42.         form = PatientsForm()
  43.  
  44.     lastid = Patients.objects.values('id').last()
  45.     if lastid is not None:
  46.         patid = lastid['id'] + 1
  47.     else:
  48.         patid = 1
  49.     # print(patid)
  50.     label2 = "Save"
  51.     context = {
  52.         'savepatform': form,
  53.         'lastid': patid,
  54.         # 'button_lable': label,
  55.         'lable2': label2,
  56.     }
  57.     return render(request, 'patientdata/save_patient.html', context)
  58.  
  59. ######################################
  60. ###### save_patient.html
  61. <form method="POST" enctype="multipart/form-data">{% csrf_token %}
  62.         <div class="float-left" id="left-savepat-div" style="overflow-x: auto; width: 45%; height: 100%;">
  63.             <!-- {{savepatform.id.label_tag}} {{savepatform.id}} <br> -->
  64.  
  65.             {{savepatform.name.label_tag}}
  66.             {% render_field savepatform.name v-model="name" %} <br>
  67.             <!--  -->
  68.             {{savepatform.address.label_tag}} {{savepatform.address}}
  69.             <br>
  70.             <!--  -->
  71.             {{savepatform.birth_date.label_tag}}
  72.             {% render_field savepatform.birth_date v-model="dob" id="dob" %}
  73.             <br>
  74.             {{savepatform.age.label_tag}}
  75.             {% render_field savepatform.age ::value="getAge" id="age" %}
  76.             <br>
  77.             <hr>
  78.  
  79.             <button type="submit" class="btn btn-dark" id="savepatform-btn">
  80.                 {{lable2}}
  81.             </button>
  82.            
  83.         </div>
  84.         <div class="float-right" id="right-savepat-div" style="overflow-x: auto; width: 45%; height: 100%;">
  85.             {{savepatform.cardid.label_tag}}
  86.             {% render_field savepatform.cardid v-model="cardid" %} <br>
  87.            
  88.             {{savepatform.phone.label_tag}} {{savepatform.phone}} <br>
  89.            
  90.             {{savepatform.mobile.label_tag}} {{savepatform.mobile}} <br>
  91.            
  92.             {{savepatform.barcode.label_tag}}
  93.             {% render_field savepatform.barcode ::value="this.id + this.sign + this.dob + this.sign + this.cardid" %}
  94.             <br>
  95.             <!-- {{savepatform.barimg.label_tag}} -->
  96.             <!-- {% render_field savepatform.barimg %} -->
  97.             {% render_field savepatform.barurl ::value="this.url + this.id + this.sign + this.dob + this.sign + this.cardid" hidden="true" %}
  98.            
  99.             <!-- <img class="image" src="{{ Patients.barimg.url }}"> -->
  100.             <hr>
  101.         </div>
  102.  
  103.     </form>
  104.  
  105. {% block scripts %}
  106. <script>
  107.     // Using Vue.js Here
  108.     var savePatient = new Vue ({
  109.     el: '#savePatient',
  110.     delimiters: ['[[', ']]'],
  111.     data: {
  112.        patient_name: '' ,
  113.        title: "Add New Patient",
  114.        message: '{{ non_field_errors }}' ,
  115.        id:'{{ lastid }}', // from views
  116.        name:'',
  117.        cardid:'',
  118.        dob: new Date(),
  119.        age: '',
  120.        url:'http://192.168.1.120:8000/pat.../patient/details/by/barcode/',
  121.        barcode:'',
  122.        sign:'_',
  123. },
  124. </script>
  125. {% endblock %}
RAW Paste Data