Ver Fonte

attivata dowload documenti da admin

mauro há 10 meses atrás
pai
commit
1c5b7391d2

+ 10 - 0
documento/forms.py

@@ -28,3 +28,13 @@ class DeleteOldDocuments(forms.Form):
   def __init(self,*args,**kwargs):
     forms.Form.__init__(self,*args,**kwargs)
     self.fields['limite'] = forms.CharField(required=True)
+
+class formMultipleCheckBox(forms.Form):
+  choices = []
+  def __init__(self,*args,**kwargs):
+    forms.Form.__init__(self,*args,**kwargs)
+    self.fields['select'] = forms.MultipleChoiceField(required=False,widget=forms.CheckboxSelectMultiple(),choices=self.choices)
+  def c(self,lista):
+    for u in lista:
+      self.choices.append((u.id,u.id),)
+    print('choices',self.choices)

+ 24 - 1
documento/managefile.py

@@ -15,6 +15,7 @@ from random import randint
 import csv
 import time
 import datetime
+import zipfile
 
 #pip install python-codicefiscale
 from codicefiscale import codicefiscale
@@ -92,7 +93,7 @@ def upload_file(uploaded_file,utente,originale=False):
     print(os.path.join(fl_path,utente.azienda.partitaiva),"esistente")
   try:
     os.mkdir(os.path.join(fl_path,utente.azienda.partitaiva,utente.codicefiscale))
-  except FileExistsError as fee:
+  except fileexistserror as fee:
     print(os.path.join(fl_path,utente.azienda.partitaiva,utente.codicefiscale),"esistente")
 
   with open(os.path.join(fl_path,utente.azienda.partitaiva,utente.codicefiscale,nomefile),'wb+') as d:
@@ -262,3 +263,25 @@ def save_and_load_file_indice(request,fileindice,azienda=None,sede=None):
 
               utente.pin = str(randint(100000,999999))
               utente.save()
+
+def PrepareZipFile(listafile=[]):
+  zip_path = os.path.join(settings.BASE_DIR,getConfig('DocZipFile'))
+  print("zip_path:",zip_path)
+
+  # verifica l'esistenza della directory
+  try:
+    os.mkdir(zip_path)
+  except FileExistsError as fee:
+    print(zip_path,"esistente")
+  filename = ''.join((str(time.time()),'.zip'))
+
+  with zipfile.ZipFile(os.path.join(zip_path,filename),'w') as zip:
+    for lf in listafile:
+      d = Documento.objects.get(pk=lf)
+      file2zip = os.path.join(settings.BASE_DIR,getConfig('DocPath'),d.utente.azienda.partitaiva,d.utente.codicefiscale,d.storage)
+      zip.write(file2zip,arcname=d.documento)
+    print('path completa',file2zip)
+    print('file completo',os.path.join(zip_path,filename))
+  return (zip_path,filename)
+
+

+ 11 - 0
documento/templates/documento.welcome.html

@@ -1,5 +1,16 @@
 {% extends "base.html" %}
 
+{% block headersupplement %}
+<script language="JavaScript">
+function toggle(source) {
+  checkboxes = document.getElementsByName('select');
+  for(var i=0, n=checkboxes.length;i<n;i++) {
+    checkboxes[i].checked = source.checked;
+  }
+}
+</script>
+{% endblock %}
+
 {% block top %}
     <div class='form-outline mb-3 text-center btn-primary h3'>
            {{ HeaderTitle }}

+ 12 - 4
documento/templates/documento.welcome.lista.html

@@ -1,4 +1,8 @@
   {% if ElencoDocumenti %}
+    <form name="ElencoDocumenti" id="ElencoDocumenti" method="POST">
+    {% csrf_token %}
+      <input type='submit' class="btn btn-primary" name='ScaricaSelezionati' value="Scarica Selezionati"/>
+    </form>
     <table class='table table-striped table-hover'>
       <thead class='thead-dark'>
       <tr>
@@ -8,6 +12,7 @@
         <th scope='col'>Documento</th>
         <th scope='col'>Descrizione</th>
         <th scope='col'>Inserimento</th>
+	<th scope='col'><input class="form-check-input" type="checkbox" id="selectAll" name="selectAll" value='0' onClick="toggle(this);"/>&nbsp;<label for="selectAll" class="form-check-label">Tutti</label></th>
         {% if admin.edit_documento %} <th scope='col'>Cancella</th> {% endif %}
       </tr>
       </thead>
@@ -16,10 +21,13 @@
         <tr>
           <td><input type='checkbox' class='form-check-input' value='{{ documento.id }}' name='selezione'></td>
           <td> <button type='button' class='btn btn-primary btn-block btn-lg mb-2 active' onclick="window.open('{% url "documento:finalize" documento.id documento.utente.id%}')">Vedi</button></td>
-          <td> {{ documento.utente.nome }} </td>
-	  <td> {{ documento.documento }} </td>
-          <td> {{ documento.descrizione }} </td>
-          <td> {{ documento.dataupload | date:'d/m/Y' }} </td>
+          <td>{{ documento.utente.nome }} </td>
+	  <td>{{ documento.documento }} </td>
+          <td>{{ documento.descrizione }} </td>
+          <td>{{ documento.dataupload | date:'d/m/Y' }} </td>
+	  <td><input class="form-check-input" type="checkbox" form="ElencoDocumenti" id="select-{{ documento.id }}" name="select" value='{{ documento.id }}'/>
+          </td>
+
           {% if admin.edit_documento %}
             <td><button type='button' class='btn btn-primary' data-bs-toggle='modal' data-bs-target='#ConfirmDeleteModal{{ documento.id }}' value='{{ documento.id }}'>Elimina</button></td>
           {% endif %}

+ 22 - 0
documento/views.py

@@ -193,6 +193,28 @@ def welcome(request,utente=None):
 
   data['ElencoDocumenti'] = ElencoDocumenti
 
+  if request.method == "POST":
+    if 'ScaricaSelezionati' in request.POST:
+      print('Premuto tasto Scarica Selezionati')
+      print('request',request.POST)
+      if 'select' in request.POST:
+        SelectResponse = formMultipleCheckBox(request.POST)
+        SelectResponse.c(data['ElencoDocumenti'])
+        print("SelectResponse",SelectResponse)
+
+        if SelectResponse.is_valid():
+          select = SelectResponse.cleaned_data.get('select')
+          print('select',select,type(select))
+          (pathzipfile,filePreparato) = PrepareZipFile(select)
+          # scarica il file
+          response = None
+          with open(os.path.join(pathzipfile,filePreparato),'rb') as f:
+            response =  FileResponse(f.read(),content_type='application/zip',as_attachment=False)
+            response['Content-Disposition'] = 'attachment; filename={}'.format(filePreparato)
+          os.unlink(os.path.join(pathzipfile,filePreparato))
+          return response
+       
+
   print("Numero documenti associati",len(ElencoDocumenti),len(data['ElencoDocumenti']))
 
   return render(request,'documento.welcome.html',data)

+ 22 - 3
utente/templates/utente.download.html

@@ -1,5 +1,16 @@
 {% extends 'base.html' %}
 
+{% block headersupplement %}
+<script language="JavaScript">
+function toggle(source) {
+  checkboxes = document.getElementsByName('select');
+  for(var i=0, n=checkboxes.length;i<n;i++) {
+    checkboxes[i].checked = source.checked;
+  }
+}
+</script>
+{% endblock %}
+
 {% block top %}
     <div class='form-outline mb-3 text-center btn-primary h3'>
            {{ HeaderTitle }}
@@ -37,23 +48,31 @@
    </div>
 
    <div>
+    <form name="ElencoDocumenti" method="POST">
+    {% csrf_token %}
     <table class='table table-striped table-hover'>
       <thead class='thead-dark'>
         <tr>
           <th scope='col'>Documento</th>
           <th scope='col'>Descrizione</th>
+	  <th scope='col'>Data Caricamento</th>
+	  <th scope='col'><input class="form-check-input" type="checkbox" id="selectAll" name="selectAll" value='0' onClick="toggle(this);"/>&nbsp;<label for='selectAll' class='form-check-label'>Tutti</label></th>
+
         </tr>
       </thead>
       <tbody>
         {% for documento in documentiUtente %}
           <tr>
-            <!--<td> <button type='button' class='btn btn-primary btn-block mb-2 active' onclick="window.open('{% url "documento:finalize" documento.id utente.id %}')">{{ documento.documento }}</button></td>-->
-              <td><a href={% url "documento:finalize" documento.id utente.id %}>{{ documento.documento}}</td>
-              <td> {{ documento.descrizione }} </td>
+            <td> <button type='button' class='btn btn-primary btn-block mb-2 active' onclick="window.open('{% url "documento:finalize" documento.id utente.id %}')">{{ documento.documento }}</button></td>
+            <td> {{ documento.descrizione }} </td>
+	    <td> {{ documento.dataupload }} </td>
+	    <td><input class="form-check-input" type="checkbox" id="select-{{ documento.id}}" name="select" value='{{ documento.id }}'/></td>
             </tr>
           {% endfor %}
         </tbody>
       </table>
+      <input type='submit' class="btn btn-primary" name='ScaricaSelezionati' value="ScaricaSelezionati"/>
+      </form>
     </div>
   </div>
 

+ 10 - 1
utente/templates/utente.edit.html

@@ -1,7 +1,16 @@
 {% extends 'base.html' %}
 
-{% block body %}
+{% block headersupplement %}
+<script language="JavaScript">
+function toggle(source) {
+  checkboxes = document.getElementsByName('select');
+  for(var i=0, n=checkboxes.length;i<n;i++) {
+    checkboxes[i].checked = source.checked;
+  }
+}
+</script>
 
+{% block body %}
 <!-- Nav tabs -->
 <ul class="nav nav-tabs" id="myTab" role="tablist">
   <li class="nav-item" role="presentation">

+ 1 - 1
utente/templates/utente.welcome.html

@@ -17,7 +17,7 @@ function toggle(source) {
     {% if admin.crea_utente %} 
       <input type='submit' class="btn btn-primary" name='scelta' value='Nuovo Dipendente'>
       <button type='button' class="btn btn-primary" name='TastoCaricaIndici' data-bs-toggle='modal' data-bs-target='#CaricaIndiceModal'>Carica Lista Nuovi Dipendenti</button>
-      <input type='submit' class="btn btn-primary" name='ScaricaSelezionati' value="ScaricaSelezionati"/>
+      <input type='submit' class="btn btn-primary" name='ScaricaSelezionati' value="Scarica Selezionati"/>
     {% endif %}
     <input type='submit' class="btn btn-primary" name="indietro" value="Indietro">
   </div>