From 4ab1efa12584cc1c87c6e8839d77e5658449d453 Mon Sep 17 00:00:00 2001 From: Jacob Daniel Bennett Date: Thu, 17 Sep 2020 15:48:50 -0400 Subject: [PATCH] DateTime syntax for storing date and time information. Minor functionality changes --- api/ECNQueue.py | 133 ++++++++++-------------------------------------- 1 file changed, 28 insertions(+), 105 deletions(-) diff --git a/api/ECNQueue.py b/api/ECNQueue.py index 82b0e7e..6c29c1f 100644 --- a/api/ECNQueue.py +++ b/api/ECNQueue.py @@ -54,7 +54,7 @@ def __init__(self, queue: str, number: int) -> None: self.priority = self.__getMostRecentHeaderByType("Priority") self.department = self.__getMostRecentHeaderByType("Department") self.building = self.__getMostRecentHeaderByType("Building") - self.dateReceived = self.__getParsedDate(self.__getMostRecentHeaderByType("Date")) + self.dateReceived = self.__getFormattedDate(self.__getMostRecentHeaderByType("Date")) self.jsonData = { "queue": self.queue, @@ -214,45 +214,23 @@ def __parseSections(self) -> list: directoryInfo = [] # Stores line numbers for directory info that can be removed from initial content - linesToRemove = [] - - lineCounter = 0 + # Directory Info is stored for lineContents in initialMessageContent: for itemsindirectory in directoryInfoPattern: if lineContents.startswith(itemsindirectory): directoryInfo.append(lineContents) - linesToRemove.append(lineCounter) - # Breaks loop early if a non new line is ecnountered and if a directory Item wasn't found - elif lineContents != "\n": - break - - lineCounter = lineCounter + 1 # Appends Directory Information into the sections array sections.append( {"type": "directoryInformation", "content": directoryInfo} ) - - # Sorts the linesToRemove array in reverse and deletes the specified lines from the initial message - for lineNums in sorted(linesToRemove, reverse=True): - initialMessageContent.pop(lineNums) # Gets the initial message date from the header initialMessageDateStr = self.__getMostRecentHeaderByType("Date") - initialMessageFormattedDate = "" - initialMessageFormattedTime = "" + initialMessageFormattedDate = self.__getFormattedDate(initialMessageDateStr) - # Attempts to format the date and time into utc format - try: - dateObject = parse(initialMessageDateStr) - initialMessageFormattedDate = dateObject.strftime("%Y-%m-%dT%H:%M:%S%z") - initialMessageFormattedTime = dateObject.strftime("%H:%M:%S%z") - except: - initialMessageFormattedDate = "invalid" - initialMessageFormattedTime = "Invalid" - # Parses the header looking for CC recipients of the initial message initialMessageCCSection =[] initialMessageCCList = email.utils.getaddresses([self.__getMostRecentHeaderByType("CC")]) @@ -265,8 +243,7 @@ def __parseSections(self) -> list: # Appends the initial message information to the sections array sections.append( {"type": "initialMessage", - "date": initialMessageFormattedDate, - "time": initialMessageFormattedTime, + "datetime": initialMessageFormattedDate, "userName": self.__parseFromData(data="userName"), "userEmail": self.__parseFromData(data="userEmail"), "ccRecipients": initialMessageCCSection, @@ -276,34 +253,31 @@ def __parseSections(self) -> list: # Stores all assignment history in a list of dictionaries assignmentHistoryList = [] assignedBy = "" - assignedDate = "" - assignedTime = "" + assignedDateTime = "" assignedTo = "" # Parses the header looking for assignment delimeters and stores info into their respective variables for headerContent in range(0, contentStart): line = self.__rawItem[headerContent] + + # Gets who the Item was assigned to if line.startswith("Assigned-To: "): assignedTo = (re.search("(?<=Assigned-To: )(.*)", line)).group() + + # Gets the date the Item was assigned elif line.startswith("Assigned-To-Updated-Time: "): + dateFromLine = (re.search("(?<=Assigned-To-Updated-Time: )(.*)", line)).group() + assignedDateTime = self.__getFormattedDate(dateFromLine) - # Attempts to format the date and time into utc format - try: - dateFromLine = (re.search("(?<=Assigned-To-Updated-Time: )(.*)", line)).group() - dateObject = parse(dateFromLine) - assignedDate = dateObject.strftime("%Y-%m-%dT%H:%M:%S%z") - assignedTime = dateObject.strftime("%H:%M:%S%z") - except: - initialMessageFormattedDate = "invalid" - initialMessageFormattedTime = "Invalid" - - # Checks for the ending delimiter for an assignmnet event and appends it to the assignment history list + # Gets who assigned the Item elif line.startswith("Assigned-To-Updated-By: "): assignedBy = (re.search("(?<=Assigned-To-Updated-By: )(.*)", line)).group() + + # Aissignment_Updated_By signifies the end of the assignment event + # and all information is appended in a dictionary to assignment history assignmentHistoryList.append( {"by": assignedBy, - "date": assignedDate, - "time": assignedTime, + "datetime": assignedDateTime, "to": assignedTo} ) @@ -343,8 +317,7 @@ def __parseSections(self) -> list: sectionContent = self.__rawItem[boundary["start"] : boundary["end"]] if sectionType == "edit": - formattedDate = "" - formattedTime = "" + formattedDateTime = "" editedBy = "" # Parses for the author of the edit, which is located between the "*** Edited by: " and " at:" substrings @@ -354,27 +327,17 @@ def __parseSections(self) -> list: dateTimeString = (re.search("(?<= at: )(.*)(?= \*\*\*\n)", line)).group() # Attempts to format the date and time into utc format - try: - dateObject = parse(dateTimeString) - formattedDate = dateObject.strftime("%Y-%m-%dT%H:%M:%S%z") - formattedTime = dateObject.strftime("%H:%M:%S%z") - except: - formattedDate = "invalid" - formattedTime = "Invalid" + formattedDateTime = self.__getFormattedDate(dateTimeString) sections.append( {"type": sectionType, "by": editedBy, - "date": formattedDate, - "time": formattedTime, + "datetime": formattedDateTime, "content": sectionContent,} ) - - sectionContent.remove(line) elif sectionType == "replyToUser": - formattedDate = "" - formattedTime = "" + formattedDateTime = "" repliedBy = "" #parses for the author of the reply, which is located between the "*** Replied by: " and " at:" substrings @@ -383,25 +346,17 @@ def __parseSections(self) -> list: #parses for the date and time of the reply, which is located between the " at: " and "***\n" substrings dateTimeString = (re.search("(?<= at: )(.*)(?= \*\*\*\n)", line)).group() - try: - dateObject = parse(dateTimeString) - formattedDate = dateObject.strftime("%Y-%m-%dT%H:%M:%S%z") - formattedTime = dateObject.strftime("%H:%M:%S%z") - except: - formattedDate = "invalid" - formattedTime = "Invalid" + formattedDateTime = self.__getFormattedDate(dateTimeString) sections.append( {"type": sectionType, "by": repliedBy, - "date": formattedDate, - "time": formattedTime, + "datetime": formattedDateTime, "content": sectionContent} ) elif sectionType == "status": - formattedDate = "" - formattedTime = "" + formattedDateTime = "" updatedBy = "" #parses for the author of the status change, which is located between the "*** Status updated by: " and " at:" substrings @@ -410,19 +365,12 @@ def __parseSections(self) -> list: #parses for the date and time of the status change, which is located between the " at: " and "***\n" substrings dateTimeString = (re.search("(?<= at: )(.*)(?= \*\*\*\n)", line)).group() - try: - dateObject = parse(dateTimeString) - formattedDate = dateObject.strftime("%Y-%m-%dT%H:%M:%S%z") - formattedTime = dateObject.strftime("%H:%M:%S%z") - except: - formattedDate = "invalid" - formattedTime = "Invalid" + formattedDateTime = self.__getFormattedDate(dateTimeString) sections.append( {"type": sectionType, "by": updatedBy, - "date": formattedDate, - "time": formattedTime, + "datetime": formattedDateTime, "content": sectionContent} ) #sectionContent.remove(line) @@ -430,15 +378,11 @@ def __parseSections(self) -> list: #elif sectionType == "": elif sectionType == "replyFromUser": - #replyMetaDataDelimiters = ["Subject: ", "From: ", "Date: "] - - formattedDate = "" - formattedTime = "" + formattedDateTime = "" repliedByName = "" repliedByEmail = "" subject = "" ccRecipientsList = [] - #ccRecipientDict = {} """ ccRecipientDict Format: @@ -448,9 +392,6 @@ def __parseSections(self) -> list: } """ - #Counter for line number and list of lines that can be deleted from the content - lineNum = 0 - linesToBeDeleted = [0] newLineCounter = 0 #Parses the section content looking for any line that starts with a metadata @@ -466,7 +407,6 @@ def __parseSections(self) -> list: #Checks for lines starting with these delimiters if items.startswith("Subject: "): subject = (re.search("(?<=Subject: )(.*)", items)).group() - linesToBeDeleted.append(lineNum) elif items.startswith("From: "): emailList = email.utils.getaddresses([items]) @@ -474,19 +414,11 @@ def __parseSections(self) -> list: repliedByName = emailList[0][0] #repliedByEmail = (re.search("(?<= <)(.*)(?=>)", items)).group() repliedByEmail = emailList[0][1] - linesToBeDeleted.append(lineNum) elif items.startswith("Date: "): dateStr = (re.search("(?<=Date: )(.*)", items)).group() - try: - dateObject = parse(dateStr) - formattedDate = dateObject.strftime("%Y-%m-%dT%H:%M:%S%z") - formattedTime = dateObject.strftime("%H:%M:%S%z") - linesToBeDeleted.append(lineNum) - except: - formattedDate = "invalid" - formattedTime = "Invalid" + formattedDateTime = self.__getFormattedDate(dateStr) elif items.startswith("Cc: "): recipientsList = email.utils.getaddresses([items]) @@ -496,10 +428,6 @@ def __parseSections(self) -> list: "email":cc[1]} ) - lineNum = lineNum + 1 - - for lineNumbers in sorted(linesToBeDeleted, reverse=True): - sectionContent.pop(lineNumbers) #parses for the author of the status change, which is located between the "*** Status updated by: " and " at:" substrings #updatedBy = (re.search("(?<=\*{3} Status updated by: )(.*)(?= at:)", line)).group() @@ -507,8 +435,7 @@ def __parseSections(self) -> list: #dateTimeString = (re.search("(?<= at: )(.*)(?= \*\*\*\n)", line)).group() sections.append( {"type": sectionType, - "date": formattedDate, - "time": formattedTime, + "datetime": formattedDateTime, "subject": subject, "userName": repliedByName, "userEmail": repliedByEmail, @@ -707,7 +634,3 @@ def getQueues() -> list: queues.append(Queue(file)) return queues - -if __name__ == "__main__": - item = Item("ce", 11) - print()