From b8e5ae6baaea858948b2e77ec698f43fa0a3bbf1 Mon Sep 17 00:00:00 2001 From: Dan Dembinski Date: Mon, 24 Jun 2019 16:48:40 -0400 Subject: [PATCH] Broke report appending into a separate function/file. Started working on Column Format Checking Function --- WriteReport.py | 31 ++++++++++++++++++++ XMLTesting.py | 79 +++++++++++++++++++++++++++++++++++++++++++++++--- main.py | 74 ++++++++++++++++++++++++++-------------------- 3 files changed, 149 insertions(+), 35 deletions(-) create mode 100644 WriteReport.py diff --git a/WriteReport.py b/WriteReport.py new file mode 100644 index 0000000..72e6a80 --- /dev/null +++ b/WriteReport.py @@ -0,0 +1,31 @@ +def Write_Report(y, ws, columnFormat, SSheader): + results = [] + + count = 0 + for child in y.report: + for childs in child: + print(columnFormat[SSheader[count]]) + count = count + 1 + if '$' in childs.text: + convert = childs.text + apply = convert[1:] + results.append(float(apply.replace(',', ''))) + else: + results.append(childs.text) + ws.append(results) + results = [] + count = 0 + +def Detail_Write_Report(y, ws): + results = [] + + for child in y.report: + for childs in child: + if '$' in childs.text: + convert = childs.text + apply = convert[1:] + results.append(float(apply.replace(',', ''))) + else: + results.append(childs.text) + ws.append(results) + results = [] diff --git a/XMLTesting.py b/XMLTesting.py index 662d881..e271fde 100644 --- a/XMLTesting.py +++ b/XMLTesting.py @@ -2,11 +2,82 @@ from bs4 import BeautifulSoup as bs # import csv from openpyxl import workbook -external_ids = ['G-ADA-13B0D9D1', 'G-ADA-13A8D9CF', 'G-ADA-13B0D9CF', 'G-ADA-13B0D9CE', 'G-ADA-13A7D9D5', 'G-ADA-13ACD9D5', 'G-ADA-13B2D9D5', 'G-ADA-13AED9D4', 'G-ADA-13B1D9D4', 'G-ADA-13ACD9D3', 'G-ADA-13B1D9D3', 'G-ADA-13AED9D2', 'G-ADA-9F17D9CB', 'G-ADA-9F15D9CB', 'G-ADA'] -for z in external_ids: - report_filter = """<?xml version="1.0"?> <PFWebFilter:UserFilter xmlns:PFWebFilter="http://www.pageflex.com/schemas/2004/Storefront/UserFilters/20040817" filterClass="Items"> <PFWebFilter:Step publicFieldName="Order ID" query="ExactEquals" minValue=""""" + z + """"" maxValue="" /> </PFWebFilter:UserFilter>""" - print(report_filter) + +###REPORT ColumnFormat TESTING##### + +with open("StorefrontUtilitiesConfig.xml") as f: + r = f.read() + +y = bs(r, "lxml") + +ClientName = [] +SF_URL = [] +SF_HOST = [] +CLIENT_SUMMARY_TAB = [] +SUMMARY_TAB = [] +client_headers = [] +headers = [] + +CLIENT_COLUMN_FORMAT = [] +COLUMN_FORMAT = [] + +for x in y.find_all('name'): + # print(x.text) + ClientName.append(x.text) +# print(ClientName) + +for w in y.find_all('client'): + for x in w.find_all('monthlyreport'): + for g in x.find_all('summarytab'): + for z in g.find_all('columnname'): + z.name = "string" + CLIENT_SUMMARY_TAB.append(z) + client_headers.append(z.text) + SUMMARY_TAB.append(CLIENT_SUMMARY_TAB) + headers.append(client_headers) + CLIENT_SUMMARY_TAB = [] + client_headers = [] + + +for w in y.find_all('client'): + for x in w.find_all('monthlyreport'): + for g in x.find_all('summarytab'): + for z in g.find_all('columnformat'): + CLIENT_COLUMN_FORMAT.append(z.text) + COLUMN_FORMAT.append(CLIENT_COLUMN_FORMAT) + CLIENT_COLUMN_FORMAT = [] + + +option = 0 +columnformat = dict.fromkeys(headers[option]) + + +for w in y.find_all('client'): + for x in w.find_all('name'): + if x.text == ClientName[option]: + for x in w.find_all('monthlyreport'): + for g in x.find_all('summarytab'): + for z in g.find_all('columnformat'): + CLIENT_COLUMN_FORMAT.append(z.text) + +count = 0 + + +# print(columnformat) +for key in columnformat: + columnformat[key] = CLIENT_COLUMN_FORMAT[count] + count = count + 1 +print(columnformat) + + + + +# external_ids = ['G-ADA-13B0D9D1', 'G-ADA-13A8D9CF'] +# +# for z in external_ids: +# report_filter = """<?xml version="1.0"?> <PFWebFilter:UserFilter xmlns:PFWebFilter="http://www.pageflex.com/schemas/2004/Storefront/UserFilters/20040817" filterClass="Items"> <PFWebFilter:Step publicFieldName="Order ID" query="ExactEquals" minValue=""""" + z + """"" maxValue="" /> </PFWebFilter:UserFilter>""" +# print(report_filter) # diff --git a/main.py b/main.py index eb6323c..1ba35f4 100644 --- a/main.py +++ b/main.py @@ -4,6 +4,8 @@ from bs4 import BeautifulSoup as bs from openpyxl import workbook from openpyxl.styles import Font +from WriteReport import * + bold = Font(bold=True) def Obtain_Ticket(URL, HOST): @@ -24,7 +26,7 @@ def Obtain_Ticket(URL, HOST): temp1 = response.split('') temp2 = temp1[1].split('') ticket = temp2[0] - # print("got ticket: " + ticket) + print("got ticket: " + ticket) return ticket def Release_Ticket(ticket, URL, HOST): @@ -38,13 +40,13 @@ def Release_Ticket(ticket, URL, HOST): """ headers = {'Host': HOST, 'Content-Type': 'application/soap+xml; charset=utf-8', 'Content-Length': 'length'} response = requests.post(url=URL, data=ReleaseTicket, headers=headers).text - # print("released ticket "+ticket) + print("released ticket "+ticket) def Westfield(): print("Westfield") exit() -def Run_Report(ticket, URL, HOST, REPORT_FIELDS, wb, ws, ws2, DETAIL_REPORT_FIELDS): +def Run_Report(ticket, URL, HOST, REPORT_FIELDS, wb, ws, ws2, DETAIL_REPORT_FIELDS, columnFormat, SSheader): ExternalID = [] id = [] @@ -78,19 +80,9 @@ def Run_Report(ticket, URL, HOST, REPORT_FIELDS, wb, ws, ws2, DETAIL_REPORT_FIEL print("working") y = bs(report, "lxml") - results = [] # Opens report file and writes each row - for child in y.report: - for childs in child: - if '$' in childs.text: - convert = childs.text - apply = convert[1:] - results.append(float(apply.replace(',',''))) - else: - results.append(childs.text) - ws.append(results) - results = [] + Write_Report(y, ws, columnFormat, SSheader) # Grabs all the Order IDs from Column A for cell in ws['A']: @@ -170,12 +162,13 @@ def Run_Report(ticket, URL, HOST, REPORT_FIELDS, wb, ws, ws2, DETAIL_REPORT_FIEL for row in ws: value = "=H"+str(count)+"-G"+str(count)+"-F"+str(count) ws.cell(column=5, row=count, value=value) - count =+ 1 + count += 1 + ############# Order Details tab ############# print("Getting Item Detail") - # ExternalID = ['G-ADA-13B0D9D1', 'G-ADA-13A8D9CF'] + for z in ExternalID: report_filter = """<?xml version="1.0"?> <PFWebFilter:UserFilter xmlns:PFWebFilter="http://www.pageflex.com/schemas/2004/Storefront/UserFilters/20040817" filterClass="Items"> <PFWebFilter:Step publicFieldName="Order ID" query="ExactEquals" minValue=\"""" + z + """\" maxValue="" /> </PFWebFilter:UserFilter>""" send = """ @@ -201,20 +194,10 @@ def Run_Report(ticket, URL, HOST, REPORT_FIELDS, wb, ws, ws2, DETAIL_REPORT_FIEL y = bs(report, "lxml") - results = [] - # Opens report file and writes each row - for child in y.report: - for childs in child: - if '$' in childs.text: - convert = childs.text - apply = convert[1:] - results.append(float(apply.replace(',', ''))) - else: - results.append(childs.text) - ws2.append(results) - results = [] +# Opens report file and writes each row + Detail_Write_Report(y, ws) # Finalize Spreadsheet and save @@ -240,6 +223,8 @@ def loadMenu(): DETAIL_TAB = [] client_detail_headers = [] detail_headers = [] + CLIENT_COLUMN_FORMAT = [] + COLUMN_FORMAT = [] # Open the config file with open(ConfigFile) as f: @@ -278,6 +263,14 @@ def loadMenu(): CLIENT_SUMMARY_TAB = [] client_headers = [] +# Get Monthly Report Column Format + for w in y.find_all('client'): + for x in w.find_all('monthlyreport'): + for g in x.find_all('summarytab'): + for z in g.find_all('columnformat'): + CLIENT_COLUMN_FORMAT.append(z.text) + COLUMN_FORMAT.append(CLIENT_COLUMN_FORMAT) + CLIENT_COLUMN_FORMAT = [] # Get Monthly Report Item Detail Fields for w in y.find_all('client'): @@ -292,7 +285,6 @@ def loadMenu(): CLIENT_DETAIL_TAB = [] client_detail_headers = [] - choice = 0 for x in ClientName: print(choice, x) @@ -303,6 +295,22 @@ def loadMenu(): if ClientName[option] == "Westfield": Westfield() + columnformat = dict.fromkeys(headers[option]) + +# Get specific Column Types for chosen client + for w in y.find_all('client'): + for x in w.find_all('name'): + if x.text == ClientName[option]: + for x in w.find_all('monthlyreport'): + for g in x.find_all('summarytab'): + for z in g.find_all('columnformat'): + CLIENT_COLUMN_FORMAT.append(z.text) +# set Column Types Header Dictionary + count = 0 + for key in columnformat: + columnformat[key] = CLIENT_COLUMN_FORMAT[count] + count = count + 1 + # create new report file and add headers based on config file wb = workbook.Workbook() ws = wb.active @@ -317,7 +325,7 @@ def loadMenu(): REPORT_FIELDS = '\n'.join(map(str, SUMMARY_TAB[option])) DETAIL_REPORT_FIELDS = '\n'.join(map(str, DETAIL_TAB[option])) - URL_HOST = [SF_URL[option], SF_HOST[option], REPORT_FIELDS, wb, ws, ws2, DETAIL_REPORT_FIELDS] + URL_HOST = [SF_URL[option], SF_HOST[option], REPORT_FIELDS, wb, ws, ws2, DETAIL_REPORT_FIELDS, columnformat, headers[option]] return URL_HOST def main(): @@ -329,8 +337,12 @@ def main(): ws = URL_HOST[4] ws2 = URL_HOST[5] DETAIL_REPORT_FIELDS = URL_HOST[6] + columnFormat = URL_HOST[7] + + SSheader = URL_HOST[8] + ticket = Obtain_Ticket(URL, HOST) - Run_Report(ticket, URL, HOST, REPORT_FIELDS, wb, ws, ws2, DETAIL_REPORT_FIELDS) + Run_Report(ticket, URL, HOST, REPORT_FIELDS, wb, ws, ws2, DETAIL_REPORT_FIELDS, columnFormat, SSheader) Release_Ticket(ticket, URL, HOST) main() \ No newline at end of file