diff --git a/sbc_email/models/import_letters_history.py b/sbc_email/models/import_letters_history.py index ee7ae2b46..88928ecd7 100644 --- a/sbc_email/models/import_letters_history.py +++ b/sbc_email/models/import_letters_history.py @@ -151,6 +151,82 @@ def button_import(self): return super(ImportLettersHistory, self).button_import() + @api.multi + def button_save(self): + """ + When saving the import_line as correspondences, move pdf file in the + done folder on the NAS and remove image attachment (Web letter case) + """ + for import_letters in self: + if import_letters.config_id.name == \ + self.env.ref('sbc_email.web_letter').name: + for line in import_letters.import_line_ids: + # build part of filename, corresponding to this web import + # letters remove part after '-' caracter (including) + # corresponding to the page number + part_filename = (line.letter_image.name[:-4]).split('-')[0] + + share_nas = self.env.ref('sbc_email.share_on_nas').value + + smb_conn = self._get_smb_connection() + if smb_conn and smb_conn.connect( + SmbConfig.smb_ip, SmbConfig.smb_port): + imported_letter_path = self.env.ref( + 'sbc_email.scan_letter_imported').value + listPaths = smb_conn.listPath( + share_nas, + imported_letter_path) + + # loop in import folder and find pdf corresponding to + # this web letter and eventual attached image + image_ext = None + file_to_save = False + for sharedFile in listPaths: + ext = sharedFile.filename[-4:] + if part_filename == sharedFile.filename[:-4]: + if ext == '.pdf': + file_to_save = True + else: + image_ext = ext + + # move web letter on 'Done' folder on the NAS + if file_to_save: + filename = part_filename + '.pdf' + file_obj = BytesIO() + smb_conn.retrieveFile( + share_nas, imported_letter_path + filename, + file_obj) + file_obj.seek(0) + self._copy_imported_to_done_letter(filename, + file_obj, + False) + # delete files corresponding to web letter in + # 'Import' + try: + smb_conn.deleteFiles(share_nas, + imported_letter_path + + filename) + except Exception as inst: + logger.info('Failed to delete pdf web letter' + .format(inst)) + + # image is attached to this letter so we remove it + if image_ext: + try: + smb_conn.deleteFiles(share_nas, + imported_letter_path + + part_filename + + image_ext) + except Exception as inst: + logger.info('Failed to delete attached\ + image {}'.format(inst)) + + if import_letters.manual_import: + self._manage_all_imported_files() + + super(ImportLettersHistory, self).button_save() + return True + ######################################################################### # PRIVATE METHODS # ######################################################################### @@ -224,7 +300,8 @@ def _run_analyze(self): # remove all the files (now they are inside import_line_ids) self.data.unlink() - self._manage_all_imported_files() + if not self.manual_import: + self._manage_all_imported_files() self.import_completed = True logger.info("Imported letters analysis completed.") @@ -281,17 +358,20 @@ def _manage_all_imported_files(self): listPaths = smb_conn.listPath(share_nas, imported_letter_path) for sharedFile in listPaths: if func.check_file(sharedFile.filename) == 1: - file_obj = BytesIO() - smb_conn.retrieveFile( - share_nas, - imported_letter_path + - sharedFile.filename, - file_obj) - file_obj.seek(0) - self._copy_imported_to_done_letter( - sharedFile.filename, file_obj, True) + # when this is manual import we don't have to copy all + # files, web letters are stock in the same folder... + if not self.manual_import or self.is_in_list_letter( + sharedFile.filename): + file_obj = BytesIO() + smb_conn.retrieveFile( + share_nas, + imported_letter_path + + sharedFile.filename, + file_obj) + file_obj.seek(0) + self._copy_imported_to_done_letter( + sharedFile.filename, file_obj, True) elif func.isZIP(sharedFile.filename): - list_zip_to_delete.append(sharedFile.filename) zip_file = BytesIO() smb_conn.retrieveFile( @@ -303,10 +383,17 @@ def _manage_all_imported_files(self): zip_ = zipfile.ZipFile( zip_file, 'r') - # loop over files inside zip + zip_to_remove = False for f in zip_.namelist(): - self._copy_imported_to_done_letter( - f, BytesIO(zip_.read(f)), False) + # when this is manual import we are not sure that this + # zip contains current letters treated + if not self.manual_import or self.is_in_list_letter(f): + self._copy_imported_to_done_letter( + f, BytesIO(zip_.read(f)), False) + zip_to_remove = True + + if zip_to_remove: + list_zip_to_delete.append(sharedFile.filename) # delete zip file from origin import folder on the NAS for filename in list_zip_to_delete: @@ -320,6 +407,15 @@ def _manage_all_imported_files(self): .format(inst)) smb_conn.close() + def is_in_list_letter(self, filename): + """ + Check if given filename is in import letter list + """ + for line in self.import_line_ids: + if filename[:-4] in line.letter_image.name: + return True + return False + def _copy_imported_to_done_letter( self, filename, file_to_copy, deleteFile): """ @@ -384,7 +480,8 @@ def _get_smb_connection(self): @api.one def import_web_letter(self, child_code, sponsor_ref, language_name, - original_text, template_name, pdf_letter): + original_text, template_name, pdf_letter, + attachment, ext): """ Call when a letter is set on web site: - add web letter to an import set with import letter config @@ -477,14 +574,30 @@ def import_web_letter(self, child_code, sponsor_ref, language_name, logger.info("Try to copy file {} !".format(filename)) # Copy file in attachment in the done letter folder share_nas = self.env.ref('sbc_email.share_on_nas').value - done_letter_path = self.env.ref( - 'sbc_email.scan_letter_done').value + filename + import_letter_path = self.env.ref( + 'sbc_email.scan_letter_imported').value + filename file_pdf = BytesIO(pdf_letter) smb_conn = self._get_smb_connection() if smb_conn and smb_conn.connect( SmbConfig.smb_ip, SmbConfig.smb_port): - smb_conn.storeFile(share_nas, done_letter_path, file_pdf) + smb_conn.storeFile(share_nas, import_letter_path, file_pdf) + + # save eventual attachment + if attachment: + filename_attachment = filename.replace(".pdf", "." + ext) + logger.info("Try save attachment {} !" + .format(filename_attachment)) + + import_letter_path = self.env.ref( + 'sbc_email.scan_letter_imported').value + \ + filename_attachment + + file_attachment = BytesIO(base64.b64decode(attachment)) + smb_conn.storeFile( + share_nas, + import_letter_path, + file_attachment) smb_conn.close() else: return False diff --git a/sbc_translation/models/import_letters_history.py b/sbc_translation/models/import_letters_history.py index 006c8487b..1c563a146 100644 --- a/sbc_translation/models/import_letters_history.py +++ b/sbc_translation/models/import_letters_history.py @@ -34,8 +34,8 @@ def button_save(self): """ try: super(ImportLettersHistory, self).button_save() - except Exception: - logger.info("Exception during letter import !!!") + except Exception as e: + logger.info("Exception during letter import: {}", e) # bug during import, so we remove letter sent on translation # platform for letters in self: