|
|
@@ -0,0 +1,203 @@
|
|
|
+from django.shortcuts import render
|
|
|
+from django.http import HttpResponse,HttpResponseRedirect,FileResponse
|
|
|
+from django.contrib.auth.models import User
|
|
|
+from django.urls import reverse
|
|
|
+from django.db import IntegrityError
|
|
|
+
|
|
|
+from .models import *
|
|
|
+from config.views import *
|
|
|
+
|
|
|
+
|
|
|
+from django.conf import settings
|
|
|
+from django.conf.urls.static import static
|
|
|
+from django.contrib.auth import authenticate
|
|
|
+import mimetypes
|
|
|
+from random import randint
|
|
|
+import csv
|
|
|
+import time
|
|
|
+import datetime
|
|
|
+
|
|
|
+#pip install python-codicefiscale
|
|
|
+from codicefiscale import codicefiscale
|
|
|
+
|
|
|
+import os
|
|
|
+
|
|
|
+def download_file(request,uid):
|
|
|
+ u = Utenti.objects.get(pk=uid)
|
|
|
+ fl_path = '/'.join((settings.MEDIA_ROOT,azienda.codicefiscale,))
|
|
|
+ print('path completa','/'.join((fl_path,u.codicefiscale,u.storage)))
|
|
|
+ if u.storage and os.path.isfile('/'.join((fl_path,codicefiscale,u.storage))):
|
|
|
+ try:
|
|
|
+ fl = open('/'.join((fl_path,codicefiscale,u.storage)), 'rb')
|
|
|
+ except Exception as er:
|
|
|
+ print('errore',er)
|
|
|
+ data=dict()
|
|
|
+ data['errore']="File non esistente o non ancora disponibile"
|
|
|
+ data['utente'] = u
|
|
|
+ return render(request,'welcome.error.html',data)
|
|
|
+ else:
|
|
|
+ print('il file non esiste')
|
|
|
+ data=dict()
|
|
|
+ data['errore']='File non esistente o non ancora disponibile'
|
|
|
+ data['utente'] = u
|
|
|
+ return render(request,'welcome.error.html',data)
|
|
|
+
|
|
|
+ mime_type, _ = mimetypes.guess_type('/'.join((fl_path,codicefiscale,u.storage)))
|
|
|
+ fl = '/'.join((fl_path,codicefiscale,u.storage))
|
|
|
+ print('fl',fl,mime_type)
|
|
|
+ response = FileResponse(open(fl,'rb'),content_type='application/pdf',as_attachment=False)
|
|
|
+ #response['Content-Disposition'] = "attachment; filename=%s" % u.documento
|
|
|
+ return response
|
|
|
+
|
|
|
+def upload_file(uploaded_file,record,originale=False): #record=utente
|
|
|
+ stored_file = ''.join((str(time.time()),'.saved'))
|
|
|
+ fl_path = settings.MEDIA_ROOT
|
|
|
+ print('nome file',uploaded_file.name,stored_file)
|
|
|
+ nomefile=None
|
|
|
+ if originale:
|
|
|
+ nomefile = uploaded_file.name
|
|
|
+ else:
|
|
|
+ nomefile = stored_file
|
|
|
+
|
|
|
+ # vediamo se caricare o meno.
|
|
|
+ # in questo caso, se il documento esiste gia', non lo carica.
|
|
|
+ # come facciamo a stabilire se il documento esiste gia'?
|
|
|
+
|
|
|
+ try:
|
|
|
+ os.mkdir('/'.join((fl_path,record.azienda.partitaiva)))
|
|
|
+ except FileExistsError as fee:
|
|
|
+ print('/'.join((fl_path,record.azienda.partitaiva)),"esistente")
|
|
|
+ try:
|
|
|
+ os.mkdir('/'.join((fl_path,record.azienda.partitaiva,record.codicefiscale)))
|
|
|
+ except FileExistsError as fee:
|
|
|
+ print('/'.join((fl_path,record.azienda.partitaiva,record.codicefiscale)),"esistente")
|
|
|
+
|
|
|
+ with open('/'.join((fl_path,record.azienda.partitaiva,record.codicefiscale,nomefile)),'wb+') as d:
|
|
|
+ for chunk in uploaded_file.chunks():
|
|
|
+ d.write(chunk)
|
|
|
+
|
|
|
+ if originale:
|
|
|
+ return '/'.join((fl_path,record.azienda.partitaiva.record.codicefiscale,uploaded_file.name))
|
|
|
+ return stored_file
|
|
|
+
|
|
|
+def save_and_load_file_multiple(listadocumenti,request,u,azienda,descrizione):
|
|
|
+ listaok = list()
|
|
|
+ listanotok = list()
|
|
|
+ for singolodocumento in listadocumenti:
|
|
|
+ print('singolodocumento',singolodocumento)
|
|
|
+ for utente in u:
|
|
|
+ if utente.codicefiscale.strip() in singolodocumento.name:
|
|
|
+ print('match utente-documento')
|
|
|
+ risultato = upload_file(singolodocumento,utente)
|
|
|
+ documento = Documento()
|
|
|
+ documento.dataupload = datetime.datetime.now()
|
|
|
+ documento.utente = utente
|
|
|
+ documento.azienda = azienda
|
|
|
+ documento.documento = singolodocumento.name
|
|
|
+ documento.storage = risultato
|
|
|
+ documento.descrizione = descrizione
|
|
|
+ try:
|
|
|
+ documento.save()
|
|
|
+ listaok.append(documento.documento)
|
|
|
+ except IntegrityError as ie:
|
|
|
+ print("problema di integrita', il file esiste",ie)
|
|
|
+ listanotok.append(documento.documento)
|
|
|
+ print(listaok,listanotok)
|
|
|
+ return (listaok,listanotok)
|
|
|
+
|
|
|
+def save_and_load_file_single(listadocumenti,request,utente,azienda,descrizione):
|
|
|
+ listaok = list()
|
|
|
+ listanotok = list()
|
|
|
+ for singolodocumento in listadocumenti:
|
|
|
+ print('singolodocumento',singolodocumento,descrizione)
|
|
|
+ risultato = upload_file(singolodocumento,utente)
|
|
|
+ documento = Documento()
|
|
|
+ documento.dataupload = datetime.datetime.now()
|
|
|
+ documento.utente = utente
|
|
|
+ documento.azienda = azienda
|
|
|
+ documento.documento = singolodocumento.name
|
|
|
+ documento.storage = risultato
|
|
|
+ documento.descrizione = descrizione
|
|
|
+ try:
|
|
|
+ documento.save()
|
|
|
+ listaok.append(documento.documento)
|
|
|
+ except IntegritiError as ie:
|
|
|
+ print("problema di integrita', il file esiste",ie)
|
|
|
+ listanotok.append(documento.documento)
|
|
|
+ return (listaok,listanotok)
|
|
|
+
|
|
|
+def save_and_load_file_indice(fileindice,request,documento):
|
|
|
+ print('nome indice caricato',fileindice.name)
|
|
|
+ upload_file(fileindice,request.FILES['indice'],True)
|
|
|
+ fl_path = settings.MEDIA_ROOT
|
|
|
+
|
|
|
+ risultato = None
|
|
|
+ with open(''.join((fl_path,'/',fileindice.name)),'rb') as ind:
|
|
|
+ risultato = ind.read()
|
|
|
+ risultato = risultato.decode('utf-8',errors='replace')
|
|
|
+ if isinstance(risultato,str):
|
|
|
+ risultato = risultato.encode('utf8')
|
|
|
+
|
|
|
+ with open(''.join((fl_path,'/',fileindice.name,'.cvtd')),'wb') as ind:
|
|
|
+ ind.write(risultato)
|
|
|
+ print('una volta convertito, passiamo oltre')
|
|
|
+
|
|
|
+ with open(''.join((fl_path,'/',fileindice.name,'.cvtd')),'rt') as ind:
|
|
|
+ spamreader = csv.reader(ind,delimiter=';')
|
|
|
+ errori = list()
|
|
|
+ count = 0
|
|
|
+ for sr in spamreader:
|
|
|
+ salvare = False
|
|
|
+ count +=1
|
|
|
+ if count==1: continue
|
|
|
+ print(count,'sr',sr,len(sr))
|
|
|
+
|
|
|
+ '''
|
|
|
+ rispetto a farma, questo tracciato e' diverso
|
|
|
+ 0: 0308 - BOX3 S.R.L.;
|
|
|
+ 1: Elaborazioni - Lul Libro Unico del Lavoro lavoratore;
|
|
|
+ 2: ALESSE ANTONIO - LSSNTN63A25H501Y;
|
|
|
+ 3: DA0308_L.U.L._LAVORATORE_#_ALESSE_ANTONIO_#_LSSNTN63A25H501Y_#_06.2024_12.07.2024_10.18.43.pdf;
|
|
|
+ 4: ="06.2024";
|
|
|
+ 5: 12.07.2024 10:34:17;
|
|
|
+ 6: Cliente;
|
|
|
+ 7: 2024.07.15 11:40:33;
|
|
|
+ 8: 12.07.2024;
|
|
|
+ 9: 12.07.2024;
|
|
|
+ '''
|
|
|
+
|
|
|
+ if len(sr) > 1: # la lunghezza del record segnala qualcosa
|
|
|
+ utente = None
|
|
|
+ try:
|
|
|
+ utente = Utente.objects.get(codicefiscale=sr[2].split(' - ')[1].strip().upper())
|
|
|
+ except Utente.DoesNotExist as dne:
|
|
|
+ print('Utente non trovato')
|
|
|
+ utente = Utente()
|
|
|
+ utente.azienda = azienda
|
|
|
+ utente.nome = sr[2].capitalize()
|
|
|
+ try:
|
|
|
+ data_convertita = datetime.datetime.strptime(sr[8], "%d.%m.%Y")
|
|
|
+ print('data convertita',data_convertita)
|
|
|
+ utente.datanascita = data_convertita
|
|
|
+ except ValueError as ve:
|
|
|
+ print(ve)
|
|
|
+ utente.codicefiscale = sr[2].split(' - ')[1].strip().upper()
|
|
|
+ utente.password = str(randint(100000,999999))
|
|
|
+ utente.save()
|
|
|
+
|
|
|
+ # caricamento documenti
|
|
|
+ if filecaricati.cleaned_data.get('allegati'):
|
|
|
+ print('presenti documenti da allegare')
|
|
|
+ listadocumenti = filecaricati.cleaned_data['allegati']
|
|
|
+ print(listadocumenti)
|
|
|
+ u = Utenti.objects.all()
|
|
|
+ for singolodocumento in listadocumenti:
|
|
|
+ print('singolodocumento',singolodocumento)
|
|
|
+ for utente in u:
|
|
|
+ if utente.codicefiscale.strip() in singolodocumento.name:
|
|
|
+ print('match utente-documento')
|
|
|
+ risultato = upload_file(singolodocumento,utente)
|
|
|
+ # risultato: nome del file memorizzato (nome interno)
|
|
|
+ # singolodocumento.name = nome del file originale
|
|
|
+
|
|
|
+
|