|
|
@@ -21,12 +21,15 @@ from codicefiscale import codicefiscale
|
|
|
|
|
|
import os
|
|
|
|
|
|
+# come sono gestiti i file nella configurazione:
|
|
|
+# MEDIA_ROOT=os.path.join(BASE_DIR,'static/upload')
|
|
|
+# che diventa = os.path.join(BASE_DIR,getConfig('DocPath'))
|
|
|
+
|
|
|
def delete_file(documento):
|
|
|
# questo blocco rimuove fisicamente il file prima della sua rimozione logica nel db
|
|
|
- fl_path = '/'.join((settings.MEDIA_ROOT,documento.utente.azienda.partitaiva,documento.utente.codicefiscale))
|
|
|
- print('Path completa:',fl_path)
|
|
|
- posizione = '/'.join((fl_path,documento.storage))
|
|
|
- if os.path.isfile(posizione):
|
|
|
+ file_path = os.path.join(BASE_DIR,getConfig('DocPath'),documento.utente.azienda.partitaiva,documento.utente.codicefiscale,documento.storage)
|
|
|
+ print('Path completa:',file_path)
|
|
|
+ if os.path.isfile(file_path):
|
|
|
print('rimozione fisica file:',posizione)
|
|
|
try:
|
|
|
os.remove(posizione)
|
|
|
@@ -34,35 +37,37 @@ def delete_file(documento):
|
|
|
print('errore nella rimozione del file:',ose)
|
|
|
|
|
|
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))):
|
|
|
+ utente = Utenti.objects.get(pk=uid)
|
|
|
+ file_path = os.path.join(BASE_DIR,getConfig('DocPath'),documento.utente.azienda.partitaiva,documento.utente.codicefiscale,utente.storage)
|
|
|
+ print('Path completa da Basedir:',file_path)
|
|
|
+ if utente.storage and os.path.isfile(file_path):
|
|
|
try:
|
|
|
- fl = open('/'.join((fl_path,codicefiscale,u.storage)), 'rb')
|
|
|
+ fl = open(file_path, 'rb')
|
|
|
except Exception as er:
|
|
|
print('errore',er)
|
|
|
data=dict()
|
|
|
data['errore']="File non esistente o non ancora disponibile"
|
|
|
- data['utente'] = u
|
|
|
+ data['utente'] = utente
|
|
|
return render(request,'welcome.error.html',data)
|
|
|
else:
|
|
|
- print('il file non esiste')
|
|
|
+ print('il file non esiste',file_path)
|
|
|
data=dict()
|
|
|
data['errore']='File non esistente o non ancora disponibile'
|
|
|
- data['utente'] = u
|
|
|
+ data['utente'] = utente
|
|
|
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)
|
|
|
+ mime_type, _ = mimetypes.guess_type(file_path)
|
|
|
+ print('file',file_path,mime_type)
|
|
|
+ response = FileResponse(open(file_path,'rb'),content_type='application/pdf',as_attachment=False)
|
|
|
#response['Content-Disposition'] = "attachment; filename=%s" % u.documento
|
|
|
return response
|
|
|
|
|
|
def upload_file(uploaded_file,utente,originale=False):
|
|
|
+ '''
|
|
|
+ caricamento files
|
|
|
+ '''
|
|
|
stored_file = ''.join((str(time.time()),'.saved'))
|
|
|
- fl_path = settings.MEDIA_ROOT
|
|
|
+ fl_path = os.path.join(BASE_DIR,getConfig('DocPath'))
|
|
|
print('nome file',uploaded_file.name,stored_file)
|
|
|
nomefile=None
|
|
|
if originale:
|
|
|
@@ -78,21 +83,20 @@ def upload_file(uploaded_file,utente,originale=False):
|
|
|
# utente.codicefiscale (prima era "record" e mi sono deciso a cambiarlo)
|
|
|
|
|
|
try:
|
|
|
- os.mkdir('/'.join((fl_path,utente.azienda.partitaiva)))
|
|
|
+ os.mkdir(os.path.join(fl_path,utente.azienda.partitaiva))
|
|
|
except FileExistsError as fee:
|
|
|
- print('/'.join((fl_path,utente.azienda.partitaiva)),"esistente")
|
|
|
-
|
|
|
+ print(os.path.join(fl_path,utente.azienda.partitaiva),"esistente")
|
|
|
try:
|
|
|
- os.mkdir('/'.join((fl_path,utente.azienda.partitaiva,utente.codicefiscale)))
|
|
|
+ os.mkdir(os.path.join(fl_path,utente.azienda.partitaiva,utente.codicefiscale))
|
|
|
except FileExistsError as fee:
|
|
|
- print('/'.join((fl_path,utente.azienda.partitaiva,utente.codicefiscale)),"esistente")
|
|
|
+ print(os.path.join(fl_path,utente.azienda.partitaiva,utente.codicefiscale),"esistente")
|
|
|
|
|
|
- with open('/'.join((fl_path,utente.azienda.partitaiva,utente.codicefiscale,nomefile)),'wb+') as d:
|
|
|
+ with open(os.path.join(fl_path,utente.azienda.partitaiva,utente.codicefiscale,nomefile),'wb+') as d:
|
|
|
for chunk in uploaded_file.chunks():
|
|
|
d.write(chunk)
|
|
|
|
|
|
if originale:
|
|
|
- return '/'.join((fl_path,utente.azienda.partitaiva.utente.codicefiscale,uploaded_file.name))
|
|
|
+ return os.path.join(fl_path,utente.azienda.partitaiva,utente.codicefiscale,nomefile)
|
|
|
return stored_file
|
|
|
|
|
|
def save_and_load_file_multiple(listadocumenti,request,utente,azienda,descrizione):
|
|
|
@@ -144,20 +148,20 @@ def save_and_load_file_single(listadocumenti,request,utente,azienda,descrizione)
|
|
|
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
|
|
|
+ fl_path = getConfig('DocPath')
|
|
|
|
|
|
risultato = None
|
|
|
- with open(''.join((fl_path,'/',fileindice.name)),'rb') as ind:
|
|
|
+ with open(os.path.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:
|
|
|
+ with open(os.path.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:
|
|
|
+ with open(os.path.join(fl_path,'/',fileindice.name,'.cvtd'),'rt') as ind:
|
|
|
spamreader = csv.reader(ind,delimiter=';')
|
|
|
errori = list()
|
|
|
count = 0
|
|
|
@@ -205,10 +209,11 @@ def save_and_load_file_indice(fileindice,request,documento):
|
|
|
print('presenti documenti da allegare')
|
|
|
listadocumenti = filecaricati.cleaned_data['allegati']
|
|
|
print(listadocumenti)
|
|
|
- u = Utenti.objects.all()
|
|
|
+
|
|
|
+ utenti = Utenti.objects.all()
|
|
|
for singolodocumento in listadocumenti:
|
|
|
print('singolodocumento',singolodocumento)
|
|
|
- for utente in u:
|
|
|
+ for utente in utenti:
|
|
|
if utente.codicefiscale.strip() in singolodocumento.name:
|
|
|
print('match utente-documento')
|
|
|
risultato = upload_file(singolodocumento,utente)
|