Browse Source

confluence final stage

Akash Singh 3 years ago
parent
commit
d12afcb38c

+ 25 - 22
baangt/base/ExportResults/ExportConfluence.py

@@ -22,31 +22,30 @@ class ExportConfluence:
 
     def makeBody(self):
         summary = self.xlsx2html(self.fileNameAndPathToResultXLSX, sheet="Summary")
-        if not self.CreateSubPagesForEachXXEntries:
+        if not self.CreateSubPagesForEachXXEntries:  # If not subPages then create a single page
             output = self.xlsx2html(self.fileNameAndPathToResultXLSX, sheet="Output")
-            html = "<h1>Summary</h1>" + summary + "<br /><br /><h1>Output</h1>" + output
+            html = "<h1>Summary</h1>" + summary + "<br /><br /><h1>Output</h1>" + output  # joining output in main page
         else:
-            html = "<h1>Summary</h1>" + summary + "<br />"
+            html = "<h1>Summary</h1>" + summary + "<br />"  # Main page without output tab data
         html = html.replace('\\',  '\\\\')
         return html
 
     def update_confluence(self):
         confluence = Confluence(url=self.url, username=self.username, password=self.password)  # Confluence login
-        if self.uploadOriginalFile:
+        if self.uploadOriginalFile:  # if original xlsx_file is to be attach on the page
             file = self.attach_file(confluence)
             html = file + "<br /><br />" + self.html
         else:
             html = self.html
         new_page = confluence.create_page(
             self.space, self.pageTitle, html, parent_id=self.rootPage, type='page', representation='storage'
-        )
-        if self.CreateSubPagesForEachXXEntries:
-            try:
+        )  # creating main page
+        if self.CreateSubPagesForEachXXEntries:  # if we want subpages for output
+            try:  # getting page if of main page. With help of this id, sub pages are created
                 parent_page = new_page["id"]
-            except KeyError:
+            except KeyError: # if key is not present then it is most probably inside a list with key "results"
                 parent_page = new_page["results"]["id"]
-            self.create_child_pages(confluence, parent_page)
-
+            self.create_child_pages(confluence, parent_page)  # creating child pages
 
     def xlsx2html(self, filePath, sheet):
         wb = xlrd.open_workbook(filePath)
@@ -76,52 +75,56 @@ class ExportConfluence:
         return html
 
     def attach_file(self, confluence):
-        fileName = os.path.basename(self.fileNameAndPathToResultXLSX)
+        fileName = os.path.basename(self.fileNameAndPathToResultXLSX)  # getting basename of xlsx file for title
+        # Attaching basefile
         attach = confluence.attach_file(self.fileNameAndPathToResultXLSX, name=fileName, content_type=None,
                                page_id=self.rootPage, title=self.pageTitle, space=self.space, comment=None)
         try:
             url = "/confluence"+attach["_links"]["download"].split(".xlsx?")[0] + ".xlsx"
-        except KeyError:
+        except KeyError:  # if key is not present then it is most probably inside a list with key "results"
             url = "/confluence"+attach["results"][0]["_links"]["download"].split(".xlsx?")[0] + ".xlsx"
         link = f'<a href="{url}">{fileName}</a>'
         html = "<h1>Original file</h1>"+link
         return html
 
     def create_child_pages(self, confluence, parent_page):
+        # Creating child pages if required
         wb = xlrd.open_workbook(self.fileNameAndPathToResultXLSX)
         output_xlsx = wb.sheet_by_name("Output")
+        # Getting starting points for each subpage
         starting_points = [x for x in range(1, output_xlsx.nrows, self.CreateSubPagesForEachXXEntries)]
-        header = []
-        remove_index = []
+        header = []  # header used in every subpage
+        remove_index = []  # headers column number which are not to be used are stored in it.
         for x in range(output_xlsx.ncols):
             value = output_xlsx.cell_value(0, x)
             if value.lower() not in self.remove_headers:
                 header.append(value)
             else:
-                remove_index.append(x)
-        header = '<tr><th>' + '</th>\n<th>'.join(header) + '</th></tr>'
+                remove_index.append(x)  # if header is to be removed its column number is stored here
+        header = '<tr><th>' + '</th>\n<th>'.join(header) + '</th></tr>'  # html table headers are formatted here
         for starting in starting_points:
-            if starting + self.CreateSubPagesForEachXXEntries < output_xlsx.nrows:
-                ending = starting + self.CreateSubPagesForEachXXEntries
+            if starting + self.CreateSubPagesForEachXXEntries < output_xlsx.nrows:  # if it is not last sub page
+                ending = starting + self.CreateSubPagesForEachXXEntries - 1  # ending point is only used for title
             else:
                 ending = output_xlsx.nrows
             title = ((len(str(output_xlsx.nrows)) - len(str(starting))) * "0") + str(starting
                     ) + " - " + ((len(str(output_xlsx.nrows)) - len(str(ending))) * "0") + str(ending
-                    ) + " " + self.pageTitle
+                    ) + " " + self.pageTitle  # generating title for subpage
             data = []
             for row in range(starting, ending):
                 dt = []
                 for column in range(output_xlsx.ncols):
                     value = output_xlsx.cell_value(row, column)
-                    if type(value) == float:
+                    if type(value) == float:  # coverting int and float into string
                         if repr(value)[-2:] == '.0':
                             value = int(value)
                     value = str(value)
                     if column not in remove_index:  # if column is not in remove_header list than add the data in html
                         dt.append(escape(value))
-                data.append('<td>' + '</td>\n<td>'.join(dt) + '</td>')
+                data.append('<td>' + '</td>\n<td>'.join(dt) + '</td>')  # generating html table row and appending it in list
+            # html table row tag is added for every row which are stored in list
             html = '<table><tbody>' + header + '<tr>' + '</tr>\n<tr>'.join(data) + '</tr>' + '</tbody></table>'
             confluence.create_page(
                 self.space, title, html, parent_id=parent_page, type='page', representation='storage'
-            )
+            )  # subpage is created here with the help of parent page id
 

+ 23 - 0
baangt/base/SendReports/__init__.py

@@ -10,6 +10,7 @@ import configparser
 from slack_webhook import Slack
 from baangt.base.PathManagement import ManagedPaths
 from baangt.base.SendReports.mailer import SendMail
+from baangt.base.ExportResults.ExportConfluence import ExportConfluence
 
 logger = logging.getLogger('pyC')
 
@@ -79,6 +80,22 @@ class Sender:
             if test:
                 return messages
 
+    def sendConfluence(self):
+        self.set_globalSettings(self.defaultSettings, "Confluence-Base-Url")
+        if self.globalSettings["Confluence-Base-Url"]:
+            args = {}
+            args['url'] = self.globalSettings["Confluence-Base-Url"]
+            args['space'] = self.globalSettings["Confluence-Space"]
+            args['pageTitle'] = self.globalSettings["Confluence-Pagetitle"]
+            args['fileNameAndPathToResultXLSX'] = self.xlsx_file
+            args['username'] = self.globalSettings["Confluence-Username"]
+            args['password'] = self.globalSettings["Confluence-Password"]
+            args['rootPage'] = self.globalSettings["Confluence-Rootpage"]
+            args['remove_headers'] = [x.strip() for x in self.globalSettings["Confluence-Remove_Headers"].split()]
+            args['uploadOriginalFile'] = self.globalSettings["Confluence-Uploadoriginalfile"]
+            args['CreateSubPagesForEachXXEntries'] = int(self.globalSettings["Confluence-Createsubpagesforeachxxentries"])
+            ExportConfluence(**args)
+
     def set_globalSettings(self, setting, word_to_look):
         if word_to_look in setting:
             self.globalSettings = setting
@@ -108,6 +125,8 @@ class Sender:
         self.globalSettings["TelegramChannel"] = config["Default"].get("TelegramChannel"
                                                                     ) or self.write_config(config_file, "TelegramChannel",
                                                                                            "")
+        self.globalSettings["Confluence-Base-Url"] = config["Default"].get("Confluence-Base-Url") or self.write_config(
+            config_file, "Confluence-Base-Url", "")
 
     def write_config(self, config_file, key=None, value=None):
         if key:
@@ -200,5 +219,9 @@ class Sender:
             send_stats.sendTelegram()
         except Exception as ex:
             logger.debug(ex)
+        try:
+            send_stats.sendConfluence()
+        except Exception as ex:
+            logger.debug(ex)
 
 

+ 31 - 0
examples/globalsConfluence.json

@@ -0,0 +1,31 @@
+{
+    "TC.Lines": "",
+    "TC.dontCloseBrowser": "False",
+    "TC.slowExecution": "False",
+    "TC.NetworkInfo": "False",
+    "TX.DEBUG": "False",
+    "TC.Browser": "FF",
+    "TC.ParallelRuns": "1",
+    "TC.BrowserWindowSize": "1024x768",
+    "TC.LogLevel": "",
+    "TC.BrowserAttributes": "",
+    "Stage": "Test",
+    "SendMailTo": "",
+    "NotificationWithAttachment": "True",
+    "MsWebHook": "",
+    "SlackWebHook": "",
+    "TelegramBot": "",
+    "TelegramChannel": "",
+    "DeactivateStatistics": "False",
+    "TC.UseRotatingProxies": "False",
+    "TC.ReReadProxies": "False",
+    "Confluence-Base-Url" : "",
+    "Confluence-Space" : "",
+    "Confluence-Username" : "",
+    "Confluence-Password" : "",
+    "Confluence-Rootpage" : "",
+    "Confluence-Pagetitle" : "",
+    "Confluence-Remove_Headers" : "",
+    "Confluence-Uploadoriginalfile" : "",
+    "Confluence-Createsubpagesforeachxxentries" : 0
+}