diff --git a/downloader.py b/downloader.py index db2b51d..f77eb0a 100644 --- a/downloader.py +++ b/downloader.py @@ -5,8 +5,6 @@ import os import subprocess import shutil import tempfile -import requests -import requests_ntlm import openpyxl import PyPDF2 import wx @@ -47,8 +45,6 @@ def download_invoices(parent): invoices_info = get_invoices_info(parent.input_file_path) invoices = invoices_info[1] - session = requests.Session() - session.auth = requests_ntlm.HttpNtlmAuth("sr\\"+parent.login_dlg.username.GetValue(), parent.login_dlg.password.GetValue()) parent.log_dialog.log_text.AppendText("Inizio download fatture dal portale CCSR\n") wx.Yield() @@ -58,7 +54,7 @@ def download_invoices(parent): downloaded_count = 0 for invoice_id, invoice in invoices.items(): - resp = session.get(invoice["url"]) + resp = parent.session.get(invoice["url"]) if resp.status_code == 200: with open(tmp_dir+"/"+invoice_id+".pdf", "wb") as output_file: output_file.write(resp.content) diff --git a/fatture_ccsr.py b/fatture_ccsr.py index a62d581..2f917eb 100644 --- a/fatture_ccsr.py +++ b/fatture_ccsr.py @@ -1,18 +1,20 @@ -"""This utility is used for downloading or converting to TRAF2000 invoices from a .csv or .xml report file""" +"""This utility is used for downloading or converting to TRAF2000 invoices from a .xlsx, .csv or .xml report file""" import os +import tempfile import wx import wx.adv import requests import requests_ntlm -import tempfile import downloader import traf2000_converter import exc -DOWNLOAD_ACTION = 1 -CONVERT_ACTION = 2 +LOGIN_ACTION = 0 +LOGOUT_ACTION = 1 +DOWNLOAD_ACTION = 10 +CONVERT_ACTION = 20 def file_extension(file_path: str, allowed_ext: set = None) -> str: """Return the file extension if that's in the allowed extension set""" @@ -32,7 +34,7 @@ class LogDialog(wx.Dialog): main_sizer = wx.BoxSizer(wx.VERTICAL) - self.log_text = wx.TextCtrl(self, wx.ID_ANY, size=(300, 200), style=wx.TE_MULTILINE|wx.TE_READONLY|wx.HSCROLL|wx.EXPAND) + self.log_text = wx.TextCtrl(self, wx.ID_ANY, size=(500, 200), style=wx.TE_MULTILINE|wx.TE_READONLY|wx.HSCROLL|wx.EXPAND) log_sizer = wx.BoxSizer(wx.HORIZONTAL) log_sizer.Add(self.log_text, 1, wx.ALL|wx.EXPAND, 2) @@ -86,95 +88,21 @@ class LoginDialog(wx.Dialog): self.SetSizerAndFit(main_sizer) + def disconnect(self): + """close session and reset input fields""" + self.username.SetValue('') + self.password.SetValue('') + self.GetParent().session.close() + self.logged_in = False + def on_login(self, _): """check credentials and login""" if self.username.GetValue() not in ("", None) and self.password.GetValue() not in ("", None): - self.logged_in = True - self.Close() - -# class FilePickerDialog(wx.Dialog): -# """file picker dialog for downloaded ccsr files""" -# def __init__(self, parent, title): -# super(FilePickerDialog, self).__init__(parent, wx.ID_ANY, title, style=wx.DEFAULT_DIALOG_STYLE) - -# self.input_file_path = None -# self.input_file_ext = None -# self.output_file_path = None -# self.log_dialog = None - -# main_sizer = wx.BoxSizer(wx.VERTICAL) -# input_sizer = wx.BoxSizer(wx.HORIZONTAL) -# btn_sizer = wx.BoxSizer(wx.HORIZONTAL) - -# input_file_text = wx.StaticText(self, wx.ID_ANY, "Seleziona il file scaricato dal portale della CCSR") -# input_file_text.SetFont(wx.Font(10, wx.FONTFAMILY_DEFAULT, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_BOLD)) - -# input_file_doc = wx.StaticText(self, wx.ID_ANY, "Per scaricare le fatture selezionare il file .xlsx mentre per convertire i record in TRAF2000 selezionare il file .xml o .csv") - -# self.input_file_picker = wx.FilePickerCtrl(self, 101, "", "Seleziona il .xlsx, .csv o .xml", "*.xlsx;*.csv;*.xml", style=wx.FLP_DEFAULT_STYLE) -# input_sizer.Add(self.input_file_picker, 1, wx.ALL|wx.EXPAND, 2) -# self.input_file_picker.Bind(wx.EVT_FILEPICKER_CHANGED, self.file_picker_changed) - -# self.download_btn = wx.Button(self, DOWNLOAD_ACTION, "Scarica Fatture") -# btn_sizer.Add(self.download_btn, 0, wx.ALL|wx.CENTER, 2) -# self.download_btn.Bind(wx.EVT_BUTTON, self.btn_onclick) -# self.download_btn.Disable() - -# self.traf2000_btn = wx.Button(self, CONVERT_ACTION, "Genera TRAF2000") -# btn_sizer.Add(self.traf2000_btn, 0, wx.ALL|wx.CENTER, 2) -# self.traf2000_btn.Bind(wx.EVT_BUTTON, self.btn_onclick) -# self.traf2000_btn.Disable() - -# main_sizer.Add(input_file_text, 0, wx.ALL|wx.CENTER, 2) -# main_sizer.Add(input_file_doc, 0, wx.ALL|wx.CENTER, 2) -# main_sizer.Add(input_sizer, 0, wx.ALL|wx.EXPAND, 2) -# main_sizer.Add(btn_sizer, 0, wx.ALL|wx.CENTER, 2) - -# self.SetSizerAndFit(main_sizer) - -# def file_picker_changed(self, event): -# """event raised when the input file path is changed""" -# self.input_file_path = event.GetEventObject().GetPath() -# try: -# self.input_file_ext = file_extension(self.input_file_path, (".xml", ".csv", ".xlsx")) -# except exc.NoFileError as handled_exception: -# print(handled_exception.args[0]) -# except exc.NoFileExtensionError as handled_exception: -# print(handled_exception.args[0]) -# except exc.WrongFileExtensionError as handled_exception: -# print(handled_exception.args[0]) -# if self.input_file_ext == ".xlsx": -# self.download_btn.Enable() -# self.traf2000_btn.Disable() -# elif self.input_file_ext in (".csv", ".xml"): -# self.traf2000_btn.Enable() -# self.download_btn.Disable() -# else: -# self.download_btn.Disable() -# self.traf2000_btn.Disable() - -# def btn_onclick(self, event): -# """event raised when a button is clicked""" -# btn_id = event.GetEventObject().GetId() -# if btn_id == DOWNLOAD_ACTION: -# self.login_dlg.ShowModal() -# if self.login_dlg.logged_id: -# self.log_dialog = LogDialog(self, "Download delle fatture dal portale CCSR", DOWNLOAD_ACTION) -# self.log_dialog.Show() -# downloader.download_invoices(self) -# self.log_dialog.btn.Enable() -# elif btn_id == CONVERT_ACTION: -# self.log_dialog = LogDialog(self, "Conversione delle fatture in TRAF2000", CONVERT_ACTION) -# self.log_dialog.Show() -# try: -# traf2000_converter.convert(self) -# except exc.NoFileError as handled_exception: -# print(handled_exception.args[0]) -# except exc.NoFileExtensionError as handled_exception: -# print(handled_exception.args[0]) -# except exc.WrongFileExtensionError as handled_exception: -# print(handled_exception.args[0]) -# self.log_dialog.btn.Enable() + session = self.GetParent().session + session.auth = requests_ntlm.HttpNtlmAuth("sr\\"+self.username.GetValue(), self.password.GetValue()) + if session.get('https://report.casadicurasanrossore.it:8443/Reports/browse/').status_code == 200: + self.logged_in = True + self.Close() class FattureCCSRFrame(wx.Frame): """main application frame""" @@ -185,23 +113,41 @@ class FattureCCSRFrame(wx.Frame): self.input_file_ext = None self.output_file_path = None self.log_dialog = None - self.session = None + self.session = requests.Session() - super(FattureCCSRFrame, self).__init__(parent, wx.ID_ANY, title, size=(650, 150)) + super(FattureCCSRFrame, self).__init__(parent, wx.ID_ANY, title, size=(520, 180)) panel = wx.Panel(self) - main_sizer = wx.BoxSizer(wx.VERTICAL) + self.main_sizer = wx.BoxSizer(wx.VERTICAL) date_sizer = wx.BoxSizer(wx.HORIZONTAL) btn_sizer = wx.BoxSizer(wx.HORIZONTAL) - title_text = wx.StaticText(panel, wx.ID_ANY, "Utility per scaricare le fatture dal portale CCSR e generare il tracciato TeamSystem TRAF2000") + self.login_dlg = LoginDialog(self, "Inserisci le credenziali di login al portale della CCSR") + self.output_traf2000_dialog = wx.FileDialog(panel, "Scegli dove salvare il file TRAF2000", defaultFile="TRAF2000", style=wx.FD_SAVE|wx.FD_OVERWRITE_PROMPT) + self.output_pdf_dialog = wx.FileDialog(panel, "Scegli dove salvare il .pdf con le fatture scaricate", defaultFile="fatture.pdf", style=wx.FD_SAVE|wx.FD_OVERWRITE_PROMPT) + + title_text = wx.StaticText(panel, wx.ID_ANY, "Utility Fatture Casa di Cura San Rossore") title_text.SetFont(wx.Font(10, wx.FONTFAMILY_DEFAULT, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_BOLD)) - desc_text = wx.StaticText(panel, wx.ID_ANY, "Seleziona le date di inizio e fine periodo delle fatture da gestire") + desc_text = wx.StaticText(panel, wx.ID_ANY, "Effettua il login poi seleziona le date di inizio e fine periodo delle fatture da gestire ed esegui un'azione") - self.start_date_picker = wx.adv.DatePickerCtrl(panel, dt=wx.DefaultDateTime) - self.end_date_picker = wx.adv.DatePickerCtrl(panel, dt=wx.DefaultDateTime) - date_sizer.Add(self.start_date_picker, 0, wx.ALL|wx.CENTER, 2) - date_sizer.Add(self.end_date_picker, 0, wx.ALL|wx.CENTER, 2) + start_date_lbl = wx.StaticText(self, wx.ID_ANY, "Dal:") + end_date_lbl = wx.StaticText(self, wx.ID_ANY, "Al:") + self.start_date_picker = wx.adv.DatePickerCtrl(panel, dt=wx.DateTime.Today().SetDay(1)) + self.end_date_picker = wx.adv.DatePickerCtrl(panel, dt=wx.DateTime.Today()) + date_sizer.Add(start_date_lbl, 0, wx.ALL|wx.CENTER, 2) + date_sizer.Add(self.start_date_picker, 1, wx.ALL|wx.CENTER, 2) + date_sizer.Add(end_date_lbl, 0, wx.ALL|wx.CENTER, 2) + date_sizer.Add(self.end_date_picker, 1, wx.ALL|wx.CENTER, 2) + self.start_date_picker.Disable() + self.end_date_picker.Disable() + + self.login_btn = wx.Button(panel, LOGIN_ACTION, "Login") + self.login_btn.SetDefault() + self.login_btn.Bind(wx.EVT_BUTTON, self.btn_onclick) + + self.logout_btn = wx.Button(panel, LOGOUT_ACTION, "Logout") + self.logout_btn.Hide() + self.logout_btn.Bind(wx.EVT_BUTTON, self.btn_onclick) self.download_btn = wx.Button(panel, DOWNLOAD_ACTION, "Scarica Fatture") btn_sizer.Add(self.download_btn, 0, wx.ALL|wx.CENTER, 2) @@ -213,78 +159,98 @@ class FattureCCSRFrame(wx.Frame): self.traf2000_btn.Bind(wx.EVT_BUTTON, self.btn_onclick) self.traf2000_btn.Disable() - self.login_dlg = LoginDialog(self, "Inserisci le credenziali di login al portale della CCSR") - self.output_traf2000_dialog = wx.FileDialog(panel, "Scegli dove salvare il file TRAF2000", defaultFile="TRAF2000", style=wx.FD_SAVE|wx.FD_OVERWRITE_PROMPT) - self.output_pdf_dialog = wx.FileDialog(panel, "Scegli dove salvare il .pdf con le fatture scaricate", defaultFile="fatture.pdf", style=wx.FD_SAVE|wx.FD_OVERWRITE_PROMPT) + self.main_sizer.Add(title_text, 0, wx.ALL|wx.CENTER, 2) + self.main_sizer.Add(desc_text, 0, wx.ALL|wx.CENTER, 2) + self.main_sizer.Add(self.login_btn, 0, wx.ALL|wx.CENTER, 4) + self.main_sizer.Add(self.logout_btn, 0, wx.ALL|wx.CENTER, 4) + self.main_sizer.Add(date_sizer, 0, wx.ALL|wx.EXPAND, 2) + self.main_sizer.Add(btn_sizer, 0, wx.ALL|wx.CENTER, 2) - main_sizer.Add(title_text, 0, wx.ALL|wx.CENTER, 2) - main_sizer.Add(desc_text, 0, wx.ALL|wx.CENTER, 2) - main_sizer.Add(date_sizer, 0, wx.ALL|wx.EXPAND, 2) - main_sizer.Add(btn_sizer, 0, wx.ALL|wx.CENTER, 2) - - panel.SetSizer(main_sizer) + panel.SetSizerAndFit(self.main_sizer) self.Show() - self.login_dlg.ShowModal() - if self.login_dlg.logged_in: - self.download_btn.Enable() - self.traf2000_btn.Enable() + def enable_on_login(self): + """enable and show what needed after login""" + self.download_btn.Enable() + self.traf2000_btn.Enable() + self.start_date_picker.Enable() + self.end_date_picker.Enable() + self.login_btn.Hide() + self.logout_btn.Show() + self.main_sizer.Layout() + + def disable_on_logout(self): + """disable and hide what needed after logout""" + self.download_btn.Disable() + self.traf2000_btn.Disable() + self.start_date_picker.Disable() + self.end_date_picker.Disable() + self.login_dlg.disconnect() + self.logout_btn.Hide() + self.login_btn.Show() + self.main_sizer.Layout() def btn_onclick(self, event): """event raised when a button is clicked""" btn_id = event.GetEventObject().GetId() - if not self.login_dlg.logged_in: - return None - self.session = requests.Session() - self.session.auth = requests_ntlm.HttpNtlmAuth("sr\\"+self.login_dlg.username.GetValue(), self.login_dlg.password.GetValue()) + if btn_id == LOGIN_ACTION: + self.login_dlg.ShowModal() + if self.login_dlg.logged_in: + self.enable_on_login() + elif btn_id == LOGOUT_ACTION: + self.disable_on_logout() + else: + if not self.login_dlg.logged_in: + #TODO: error + return None - start_date = self.start_date_picker.GetValue().Format("%d/%m/%Y") - end_date = self.end_date_picker.GetValue().Format("%d/%m/%Y") - input_file_url = 'https://report.casadicurasanrossore.it:8443/reportserver?/STAT_FATTURATO_CTERZI&dataI='+start_date+'&dataF='+end_date+'&rs:Format='+('EXCELOPENXML' if btn_id == DOWNLOAD_ACTION else 'XML') - print(input_file_url) - downloaded_input_file = self.session.get(input_file_url) - if downloaded_input_file.status_code != 200: - #TODO: error - print(downloaded_input_file.status_code) - print(downloaded_input_file.content) - return - input_file_descriptor, self.input_file_path = tempfile.mkstemp(suffix=('.xlsx' if btn_id == DOWNLOAD_ACTION else '.xml')) - with open(input_file_descriptor, 'wb') as input_file: - input_file.write(downloaded_input_file.content) + start_date = self.start_date_picker.GetValue().Format("%d/%m/%Y") + end_date = self.end_date_picker.GetValue().Format("%d/%m/%Y") + input_file_url = 'https://report.casadicurasanrossore.it:8443/reportserver?/STAT_FATTURATO_CTERZI&dataI='+start_date+'&dataF='+end_date+'&rs:Format=' + input_file_url += ('EXCELOPENXML' if btn_id == DOWNLOAD_ACTION else 'XML' if btn_id == CONVERT_ACTION else None) - try: - self.input_file_ext = file_extension(self.input_file_path, (".xml", ".csv", ".xlsx")) - except exc.NoFileError as handled_exception: - print(handled_exception.args[0]) - return - except exc.NoFileExtensionError as handled_exception: - print(handled_exception.args[0]) - return - except exc.WrongFileExtensionError as handled_exception: - print(handled_exception.args[0]) - return + downloaded_input_file = self.session.get(input_file_url) + if downloaded_input_file.status_code != 200: + #TODO: error + return None - if btn_id == DOWNLOAD_ACTION: - self.log_dialog = LogDialog(self, "Download delle fatture dal portale CCSR", DOWNLOAD_ACTION) - self.log_dialog.Show() - downloader.download_invoices(self) - self.log_dialog.btn.Enable() + input_file_descriptor, self.input_file_path = tempfile.mkstemp(suffix=('.xlsx' if btn_id == DOWNLOAD_ACTION else '.xml' if btn_id == CONVERT_ACTION else None)) + with open(input_file_descriptor, 'wb') as input_file: + input_file.write(downloaded_input_file.content) - elif btn_id == CONVERT_ACTION: - self.log_dialog = LogDialog(self, "Conversione delle fatture in TRAF2000", CONVERT_ACTION) - self.log_dialog.Show() - #TODO: error frame try: - traf2000_converter.convert(self) + self.input_file_ext = file_extension(self.input_file_path, (".xml", ".csv", ".xlsx")) except exc.NoFileError as handled_exception: print(handled_exception.args[0]) + return except exc.NoFileExtensionError as handled_exception: print(handled_exception.args[0]) + return except exc.WrongFileExtensionError as handled_exception: print(handled_exception.args[0]) - self.log_dialog.btn.Enable() + return + + if btn_id == DOWNLOAD_ACTION: + self.log_dialog = LogDialog(self, "Download delle fatture dal portale CCSR", DOWNLOAD_ACTION) + self.log_dialog.Show() + downloader.download_invoices(self) + self.log_dialog.btn.Enable() + + elif btn_id == CONVERT_ACTION: + self.log_dialog = LogDialog(self, "Conversione delle fatture in TRAF2000", CONVERT_ACTION) + self.log_dialog.Show() + #TODO: error frame + try: + traf2000_converter.convert(self) + except exc.NoFileError as handled_exception: + print(handled_exception.args[0]) + except exc.NoFileExtensionError as handled_exception: + print(handled_exception.args[0]) + except exc.WrongFileExtensionError as handled_exception: + print(handled_exception.args[0]) + self.log_dialog.btn.Enable() if __name__ == "__main__": app = wx.App()