Revert "Revert "reorganize upload files and images""
This reverts commit 30518e9e88.
This commit is contained in:
@@ -296,50 +296,22 @@ def upload_doc(request):
|
||||
docs_attaches = get_docs_attaches(request, nodossier, 0, '')
|
||||
|
||||
if 'form.submitted' in request.params:
|
||||
# récupère le fichier lui-même
|
||||
# récupère le fichier download dans le dossier /tmp
|
||||
input_file = request.POST['filename'].file
|
||||
# récupère son nom
|
||||
input_name = request.POST['filename'].filename
|
||||
# récupère son extension
|
||||
input_ext = input_name.split('.')[-1]
|
||||
|
||||
# controler l'extension
|
||||
input_name = request.POST['filename'].filename
|
||||
ext_allowed = ['jpeg','jpg','png','pdf']
|
||||
if input_ext.lower() not in ext_allowed :
|
||||
request.session.flash("Le format de ce fichier n'est pas valide. Téléchargement impossible.", 'warning')
|
||||
else:
|
||||
# récupère le nom du fichier et ajouter le no de dossier
|
||||
filename = '%s-DD%s-%s' % (societe, nochantier, request.POST['filename'].filename)
|
||||
# créer le répertoire du chantier s'il n'existe pas encore
|
||||
path = '%s/%s/%s' % (request.registry.settings['mondumas.devfac_dir'],societe,nochantier)
|
||||
os.makedirs(path, exist_ok=True)
|
||||
|
||||
file_path = os.path.join(path, filename)
|
||||
# We first write to a temporary file to prevent incomplete files
|
||||
temp_file_path = file_path + '~'
|
||||
temp_file = downloadFile2Temp(input_file, input_name, ext_allowed)
|
||||
if temp_file[:8] == 'ERREUR: ':
|
||||
request.session.flash(temp_file, 'danger')
|
||||
return HTTPFound(location=url)
|
||||
|
||||
# Finally write the data to a temporary file
|
||||
input_file.seek(0)
|
||||
with open(temp_file_path, 'wb') as output_file:
|
||||
shutil.copyfileobj(input_file, output_file)
|
||||
|
||||
# controler la taille du fichier < 4 Mo
|
||||
filesize = round(os.path.getsize(temp_file_path) / 1024)
|
||||
if filesize > 4096 :
|
||||
os.remove(temp_file_path)
|
||||
request.session.flash("La taille de ce fichier dépasse la limite autorisée. Téléchargement impossible.", 'warning')
|
||||
else:
|
||||
# Now that we know the file has been fully saved to disk move it into place.
|
||||
try:
|
||||
os.rename(temp_file_path, file_path)
|
||||
except OSError:
|
||||
os.remove(temp_file_path)
|
||||
request.session.flash('%s : Ce fichier existe déjà dans le rapport.' % input_name, 'danger')
|
||||
else:
|
||||
insert_dossier_attaches(request, nodossier, 0, filename, '%s Ko' % str(filesize), logged_in)
|
||||
request.session.flash('%s : Ce fichier est téléchargé avec succès.' % input_name, 'success')
|
||||
# lire tous les documents attachés
|
||||
docs_attaches = get_docs_attaches(request, nodossier, 0, '')
|
||||
# fabriquer le nom du document
|
||||
filename = '%s-DD%s-%s' % (societe, nochantier, input_name)
|
||||
tempFile2Dossier(request, societe, nochantier, '0', temp_file, filename, logged_in)
|
||||
|
||||
request.session.flash('%s : Ce fichier est téléchargé avec succès.' % input_name, 'success')
|
||||
# lire tous les documents attachés
|
||||
docs_attaches = get_docs_attaches(request, nodossier, 0, '')
|
||||
|
||||
return {
|
||||
'page_title': u"Télécharger un document",
|
||||
@@ -378,54 +350,22 @@ def upload_img(request):
|
||||
photos = get_photos(request, nochantier, norapport)
|
||||
|
||||
if 'form.submitted' in request.params:
|
||||
# récupère le fichier lui-même
|
||||
# récupère le fichier download dans le dossier /tmp
|
||||
input_file = request.POST['filename'].file
|
||||
# récupère son nom
|
||||
input_name = request.POST['filename'].filename
|
||||
# récupère son extension
|
||||
input_ext = input_name.split('.')[-1]
|
||||
|
||||
# controler l'extension
|
||||
ext_allowed = ['jpeg','jpg','png']
|
||||
if input_ext.lower() not in ext_allowed :
|
||||
request.session.flash("Le format de ce fichier n'est pas valide. Téléchargement impossible.", 'warning')
|
||||
else:
|
||||
# récupère le nom du fichier et ajouter le no de rapport
|
||||
filename = '%s-RDF%s-%s' % (societe, norapport, input_name)
|
||||
# créer le répertoire du chantier s'il n'existe pas encore
|
||||
path = '%s/%s/%s/%s' % (request.registry.settings['mondumas.devfac_dir'], societe, nochantier, norapport)
|
||||
os.makedirs(path, exist_ok=True)
|
||||
|
||||
file_path = os.path.join('%s/%s' % (path, filename))
|
||||
# We first write to a temporary file to prevent incomplete files
|
||||
temp_file_path = file_path + '~'
|
||||
|
||||
# Finally write the data to a temporary file
|
||||
input_file.seek(0)
|
||||
with open(temp_file_path, 'wb') as output_file:
|
||||
shutil.copyfileobj(input_file, output_file)
|
||||
|
||||
# controler la taille du fichier < 4 Mo
|
||||
filesize = round(os.path.getsize(temp_file_path) / 1024)
|
||||
if filesize > 4096 :
|
||||
os.remove(temp_file_path)
|
||||
request.session.flash("La taille de ce fichier dépasse la limite autorisée. Téléchargement impossible.", 'warning')
|
||||
else:
|
||||
try:
|
||||
# using the Python Image Library (PIL) to resize an image
|
||||
resize_photos(temp_file_path, file_path)
|
||||
filesize = round(os.path.getsize(file_path) / 1024)
|
||||
# Now that we know the file has been fully saved to disk move it into place.
|
||||
os.remove(temp_file_path)
|
||||
|
||||
except OSError:
|
||||
os.remove(temp_file_path)
|
||||
request.session.flash('%s : Ce fichier existe déjà dans le rapport.' % input_name, 'danger')
|
||||
else:
|
||||
insert_dossier_attaches(request, 'PL-%s' % nochantier, norapport, filename, '%s Ko' % str(filesize), logged_in)
|
||||
request.session.flash('%s : Ce fichier est téléchargé avec succès.' % input_name, 'success')
|
||||
# lire tous les photos attachées
|
||||
photos = get_photos(request, nochantier, norapport)
|
||||
temp_file = downloadFile2Temp(input_file, input_name, ext_allowed)
|
||||
if temp_file[:8] == 'ERREUR: ':
|
||||
request.session.flash(temp_file, 'danger')
|
||||
return HTTPFound(location=url)
|
||||
|
||||
# fabriquer le nom du rapport
|
||||
filename = '%s-RDF%s-%s' % (societe, norapport, temp_filename)
|
||||
tempFile2Dossier(request, societe, nochantier, norapport, temp_file, filename, logged_in)
|
||||
|
||||
request.session.flash('%s : Ce fichier est téléchargé avec succès.' % input_name, 'success')
|
||||
# lire tous les photos attachées
|
||||
photos = get_photos(request, nochantier, norapport)
|
||||
|
||||
return {
|
||||
'page_title': u"Gérer les photos",
|
||||
@@ -747,8 +687,11 @@ def demandes(request):
|
||||
url = request.route_url('demandes')
|
||||
|
||||
# lire les demandes d'interventions arrivées par email
|
||||
societe = 'PE'
|
||||
mbx_name = 'peinture-dumas@entreprise-dumas.com'
|
||||
mbx_pwd = 'sasdumas'
|
||||
mbx_search1 = 'FROM gestionsinistre@maif.fr SUBJECT "Missionnement r"'
|
||||
mbx_search2 = 'FROM service.sinistres@domus-services.fr SUBJECT "Ordre de mission DOMUS - Dossier"'
|
||||
conn = imaplib.IMAP4_SSL('imap.entreprise-dumas.com')
|
||||
try:
|
||||
# se connecter à la mailbox
|
||||
@@ -760,16 +703,15 @@ def demandes(request):
|
||||
# lire le INBOX
|
||||
rv, data = conn.select('INBOX', readonly =True)
|
||||
|
||||
if 'form.submitted' in request.params:
|
||||
# demandes_generer(request, conn, societe, mbx_search1, liste, logged_in)
|
||||
demandes_generer(request, conn, societe, mbx_search2, liste, logged_in)
|
||||
|
||||
liste=[]
|
||||
# lire demandes de la MAIF
|
||||
mbx_search = 'FROM gestionsinistre@maif.fr SUBJECT "Missionnement r"'
|
||||
if 'form.submitted' in request.params:
|
||||
demandes_generer(request, conn, mbx_name, mbx_search, liste, logged_in)
|
||||
demandes_afficher(conn, mbx_name, mbx_search, liste)
|
||||
|
||||
# demandes_afficher(conn, mbx_name, mbx_search1, liste)
|
||||
# lire demandes de DOMUS
|
||||
# mbx_search = 'FROM service.sinistres@domus-services.fr SUBJECT "Ordre de mission DOMUS - Dossier"'
|
||||
# demandes_afficher(conn, mbx_name, mbx_search, liste)
|
||||
demandes_afficher(conn, mbx_name, mbx_search2, liste)
|
||||
|
||||
conn.logout()
|
||||
|
||||
@@ -808,58 +750,9 @@ def demandes_afficher(conn, mbx_name, search_criteria, liste):
|
||||
liste.append(d)
|
||||
return liste
|
||||
|
||||
def demandes_generer(request, conn, mbx_name, mbx_search, liste, logged_in):
|
||||
def demandes_generer(request, conn, societe, mbx_search, liste, logged_in):
|
||||
|
||||
def download_pdf_to_tmp(email_message):
|
||||
# downloading attachments
|
||||
for part in email_message.walk():
|
||||
# this part comes from the snipped I don't understand yet...
|
||||
if part.get_content_maintype() == 'multipart':
|
||||
continue
|
||||
if part.get('Content-Disposition') is None:
|
||||
continue
|
||||
fileName = part.get_filename()
|
||||
if bool(fileName):
|
||||
# copier le fichier PDF dans le dossier /tmp
|
||||
temp_file_path = os.path.join('/tmp/', fileName)
|
||||
if not os.path.isfile(temp_file_path) :
|
||||
fp = open(temp_file_path, 'wb')
|
||||
fp.write(part.get_payload(decode=True))
|
||||
fp.close()
|
||||
return temp_file_path
|
||||
|
||||
def convert_pdf_to_txt(path):
|
||||
resource_manager = PDFResourceManager()
|
||||
laparams = LAParams()
|
||||
converter = PDFPageAggregator(resource_manager, laparams=laparams)
|
||||
page_interpreter = PDFPageInterpreter(resource_manager, converter)
|
||||
|
||||
extracted_text = ""
|
||||
with open(path, 'rb') as fh:
|
||||
|
||||
for page in PDFPage.get_pages(fh,
|
||||
caching=True,
|
||||
check_extractable=True):
|
||||
page_interpreter.process_page(page)
|
||||
# The converter renders the layout from interpreter
|
||||
layout = converter.get_result()
|
||||
# Out of the many LT objects within layout, we are interested in LTTextBox and LTTextLine
|
||||
for lt_obj in layout:
|
||||
if isinstance(lt_obj, LTTextBox) or isinstance(lt_obj, LTTextLine):
|
||||
extracted_text += lt_obj.get_text()
|
||||
|
||||
# close open handles
|
||||
converter.close()
|
||||
fh.close()
|
||||
# ecrire le texte dans un fichier
|
||||
extracted_file = '/tmp/log_file.txt'
|
||||
with open(extracted_file, "w") as my_log:
|
||||
my_log.write(extracted_text)
|
||||
my_log.close()
|
||||
|
||||
return extracted_text, extracted_file
|
||||
|
||||
def generer_dossier(request, mbx_name, mbx_search, extracted_file, temp_file_path, logged_in):
|
||||
def generer_dossier(request, societe, mbx_search, extracted_file, temp_file_path, logged_in):
|
||||
# parcourir les lignes pour retrouver les infos utiles
|
||||
with open(extracted_file) as fp:
|
||||
cnt = 1
|
||||
@@ -900,7 +793,7 @@ def demandes_generer(request, conn, mbx_name, mbx_search, liste, logged_in):
|
||||
i2 = line.find(' € ')
|
||||
c_obs = line[i1:i2+2]
|
||||
if ' pour un montant de ' in line:
|
||||
i1 = line.find('pour un montant de ')
|
||||
i1 = line.find('pour un montsant de ')
|
||||
tx_trav = line[i1:-2].replace(',', '.')
|
||||
|
||||
# lire ligne suivante
|
||||
@@ -909,25 +802,14 @@ def demandes_generer(request, conn, mbx_name, mbx_search, liste, logged_in):
|
||||
|
||||
fp.close()
|
||||
|
||||
# créer un dem_devis et récupèrer son no_id
|
||||
nochantier = insert_dossier(request, mbx_name, mbx_search, logged_in, c_nom, c_adr, c_adr2, c_cp, c_ville, c_tel1, no_sinistre, c_obs, tx_trav)
|
||||
societe = mbx_name[0:2].upper()
|
||||
# créer une dem_devis et récupèrer son no_id
|
||||
nochantier = insert_dossier(request, societe, 'MAIF', logged_in, c_nom, c_adr, c_adr2, c_cp, c_ville, c_tel1, no_sinistre, c_obs, tx_trav)
|
||||
nodossier = "%s-%s" % (societe, nochantier)
|
||||
|
||||
# récupère le nom du fichier et ajouter le no de dossier
|
||||
filename = os.path.basename(temp_file_path)
|
||||
filename = '%s-DD%s-%s' % (societe, nochantier, filename)
|
||||
# créer le répertoire du chantier s'il n'existe pas encore
|
||||
path = '%s/%s/%s' % (request.registry.settings['mondumas.devfac_dir'],societe,nochantier)
|
||||
os.makedirs(path, exist_ok=True)
|
||||
|
||||
file_path = os.path.join(path, filename)
|
||||
# Now that we know the file has been fully saved to disk move it into place.
|
||||
shutil.move(temp_file_path, file_path)
|
||||
# get filz size
|
||||
filesize = round(os.stat(file_path).st_size / 1000)
|
||||
|
||||
insert_dossier_attaches(request, nodossier, 0, filename, '%s Ko' % str(filesize), logged_in)
|
||||
tempFile2Dossier(request, societe, nochantier, '0', temp_file, filename, logged_in)
|
||||
|
||||
return
|
||||
|
||||
@@ -956,18 +838,18 @@ def demandes_generer(request, conn, mbx_name, mbx_search, liste, logged_in):
|
||||
temp_file_path = download_pdf_to_tmp(email_message)
|
||||
|
||||
# convertir le fichier pdf en texte
|
||||
texte, extracted_file = convert_pdf_to_txt(temp_file_path)
|
||||
texte, extracted_file = pdf_convert_to_txt(temp_file_path)
|
||||
# mission annulée
|
||||
if 'Objet : ANNULATION MISSION' in texte:
|
||||
# supprime le pdf
|
||||
os.remove(temp_file_path)
|
||||
else:
|
||||
# genere le dossier d'après le mail
|
||||
generer_dossier(request, mbx_name, mbx_search, extracted_file, temp_file_path, logged_in)
|
||||
generer_dossier(request, societe, mbx_search, extracted_file, temp_file_path, logged_in)
|
||||
|
||||
return
|
||||
|
||||
def resize_photos(image_file, new_image_file):
|
||||
def resize_photos(image_file):
|
||||
# using the Python Image Library (PIL) to resize an image
|
||||
img_org = Image.open(image_file)
|
||||
# get the size of the original image
|
||||
@@ -975,7 +857,7 @@ def resize_photos(image_file, new_image_file):
|
||||
# set the resizing factor so the aspect ratio can be retained
|
||||
# factor > 1.0 increases size
|
||||
# factor < 1.0 decreases size
|
||||
factor = 0.75
|
||||
factor = 1
|
||||
width = int(width_org * factor)
|
||||
height = int(height_org * factor)
|
||||
# best down-sizing filter
|
||||
@@ -983,6 +865,105 @@ def resize_photos(image_file, new_image_file):
|
||||
# split image filename into name and extension
|
||||
name, ext = os.path.splitext(image_file)
|
||||
# create a new file name for saving the result
|
||||
img_anti.save(new_image_file)
|
||||
img_anti.save(image_file)
|
||||
|
||||
return
|
||||
|
||||
def downloadFile2Temp(input_file, input_name, ext_allowed):
|
||||
# récupère son extension
|
||||
input_extension = input_name.split('.')[-1]
|
||||
|
||||
# extensions autorisées ?
|
||||
if input_extension.lower() not in ext_allowed :
|
||||
return "ERREUR: Le format du fichier n'est pas valide. Téléchargement impossible."
|
||||
|
||||
# Finally write the data to a temporary file
|
||||
temp_file_path = os.path.join('/tmp/', input_name)
|
||||
# supprimer le fichier s'il existe déjà
|
||||
if os.path.exists(temp_file_path):
|
||||
os.remove(temp_file_path)
|
||||
|
||||
input_file.seek(0)
|
||||
with open(temp_file_path, 'wb') as output_file:
|
||||
shutil.copyfileobj(input_file, output_file)
|
||||
|
||||
# controler la taille du fichier < 4 Mo
|
||||
filesize = round(os.path.getsize(temp_file_path) / 1024)
|
||||
if filesize > 4096 :
|
||||
os.remove(temp_file_path)
|
||||
return "ERREUR: La taille du fichier dépasse la limite autorisée. Téléchargement impossible."
|
||||
|
||||
if input_extension in ['jpeg','jpg','png']:
|
||||
# using the Python Image Library (PIL) to resize an image
|
||||
resize_photos(temp_file_path)
|
||||
|
||||
return temp_file_path
|
||||
|
||||
def tempFile2Dossier(request, societe, nochantier, norapport, temp_file, filename, logged_in):
|
||||
# créer le répertoire du chantier
|
||||
if norapport == '0':
|
||||
path = '%s/%s/%s' % (request.registry.settings['mondumas.devfac_dir'], societe, nochantier)
|
||||
else:
|
||||
path = '%s/%s/%s/%s' % (request.registry.settings['mondumas.devfac_dir'], societe, nochantier, norapport)
|
||||
os.makedirs(path, exist_ok=True)
|
||||
|
||||
filepath = os.path.join('%s/%s' % (path, filename))
|
||||
# supprimer le fichier s'il existe déjà
|
||||
if os.path.exists(filepath):
|
||||
os.remove(filepath)
|
||||
|
||||
# Finally move the temporary file to folder
|
||||
shutil.move(temp_file, filepath)
|
||||
|
||||
filesize = round(os.path.getsize(filepath) / 1024)
|
||||
insert_dossier_attaches(request, '%s-%s' % (societe, nochantier), norapport, filename, '%s Ko' % str(filesize), logged_in)
|
||||
|
||||
def download_pdf_to_tmp(email_message):
|
||||
# downloading attachments
|
||||
for part in email_message.walk():
|
||||
# this part comes from the snipped I don't understand yet...
|
||||
if part.get_content_maintype() == 'multipart':
|
||||
continue
|
||||
if part.get('Content-Disposition') is None:
|
||||
continue
|
||||
fileName = part.get_filename()
|
||||
if bool(fileName):
|
||||
# copier le fichier PDF dans le dossier /tmp
|
||||
temp_file_path = os.path.join('/tmp/', fileName)
|
||||
if not os.path.isfile(temp_file_path) :
|
||||
fp = open(temp_file_path, 'wb')
|
||||
fp.write(part.get_payload(decode=True))
|
||||
fp.close()
|
||||
return temp_file_path
|
||||
|
||||
def pdf_convert_to_txt(path):
|
||||
resource_manager = PDFResourceManager()
|
||||
laparams = LAParams()
|
||||
converter = PDFPageAggregator(resource_manager, laparams=laparams)
|
||||
page_interpreter = PDFPageInterpreter(resource_manager, converter)
|
||||
|
||||
extracted_text = ""
|
||||
with open(path, 'rb') as fh:
|
||||
|
||||
for page in PDFPage.get_pages(fh,
|
||||
caching=True,
|
||||
check_extractable=True):
|
||||
page_interpreter.process_page(page)
|
||||
# The converter renders the layout from interpreter
|
||||
layout = converter.get_result()
|
||||
# Out of the many LT objects within layout, we are interested in LTTextBox and LTTextLine
|
||||
for lt_obj in layout:
|
||||
if isinstance(lt_obj, LTTextBox) or isinstance(lt_obj, LTTextLine):
|
||||
extracted_text += lt_obj.get_text()
|
||||
|
||||
# close open handles
|
||||
converter.close()
|
||||
fh.close()
|
||||
# ecrire le texte dans un fichier
|
||||
extracted_file = '/tmp/log_file.txt'
|
||||
with open(extracted_file, "w") as my_log:
|
||||
my_log.write(extracted_text)
|
||||
my_log.close()
|
||||
|
||||
return extracted_text, extracted_file
|
||||
|
||||
return
|
||||
Reference in New Issue
Block a user