Explorar el Código

selezione utenti - csv

Mauro hace 10 meses
padre
commit
670a26b41f
Se han modificado 3 ficheros con 51 adiciones y 17 borrados
  1. 10 0
      utente/forms.py
  2. 13 2
      utente/templates/utente.welcome.html
  3. 28 15
      utente/views.py

+ 10 - 0
utente/forms.py

@@ -113,3 +113,13 @@ class formIndiceUpload(forms.Form):
     forms.Form.__init__(self,*args,**kwargs)
     self.fields['indice'] = forms.FileField(required=True,initial='')
 
+class formMultipleCheckBox(forms.Form):
+  choices = [(0,0),]
+  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) 
+

+ 13 - 2
utente/templates/utente.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 body %}
   <form name="inputUtente" method="POST">
   <div class='form-control'>
@@ -30,7 +41,7 @@
   	  <th scope='col'>Pin</th>
         {% endif %}
         <th scope='col'>Inserimento</th>
-	<th scope='col'><input class="form-check-input" type="checkbox" id="select" name="select" value='0'/></th>
+	<th scope='col'><input class="form-check-input" type="checkbox" id="selectAll" name="selectAll" value='0' onClick="toggle(this);"/></th>
       </tr>
     </thead>
     <tbody>
@@ -52,7 +63,7 @@
           {% endif %}
           <td> {{ uu.inserimento|date:'d/m/Y' }} </td>
 	  <td>
-		  <input class="form-check-input" type="checkbox" id="select" name="select" value='{{ uu.id }}'/>
+  	  <input class="form-check-input" type="checkbox" id="select-{{ uu.id}}" name="select" value='{{ uu.id }}'/>
           </td>
 	</tr>
       {% endfor %} 

+ 28 - 15
utente/views.py

@@ -85,21 +85,34 @@ def welcome(request):
         else:
           print('request non valida')
       if 'select' in request.POST:
-        select = request.POST.get('select')
-        print('select',select,type(select))
-        tmplist = list()
-        for i in select:
-          print("utente",i)
-          if i == '0': continue
-          u = Utente.objects.get(pk=int(i))
-          tmplist.append([u.codicefiscale,u.nome,u.pin])
-          print("lunghezza tmplist",len(tmplist))
-
-        f = StringIO()
-        csv.writer(f).writerows(tmplist)
-        print(f.getvalue())
-
-          
+        #validiamo i dati passati
+        SelectResponse = formMultipleCheckBox(request.POST)
+        SelectResponse.c(data['utenti'])
+        print("SelectResponse",SelectResponse)
+
+        if SelectResponse.is_valid():
+          select = SelectResponse.cleaned_data.get('select')
+          print('select',select,type(select))
+
+          tmplist = list()
+          tmplist.append(["Codice Fiscale","Nome","Pin"])
+          for i in select:
+            print("utente",i)
+            if i == '0': continue
+            u = Utente.objects.get(pk=int(i))
+            tmplist.append([u.codicefiscale,u.nome,u.pin])
+            print("lunghezza tmplist",len(tmplist))
+
+          f = StringIO()
+          csv.writer(f).writerows(tmplist)
+          print(f.getvalue())
+
+          # scarica il file
+          response =  FileResponse(f.getvalue(),content_type='application/csv',as_attachment=False)
+          response['Content-Disposition'] = 'attachment; filename="lista_dipendenti.csv"'
+          return response
+
+ 
   return render(request,'utente.welcome.html',data)
 
 def edit(request):