Skip to content

Commit

Permalink
DateTime syntax for storing date and time information. Minor function…
Browse files Browse the repository at this point in the history
…ality changes
  • Loading branch information
Jacob Daniel Bennett committed Sep 17, 2020
1 parent ffb77be commit 4ab1efa
Showing 1 changed file with 28 additions and 105 deletions.
133 changes: 28 additions & 105 deletions api/ECNQueue.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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")])
Expand All @@ -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,
Expand All @@ -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}
)

Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -410,35 +365,24 @@ 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)

#elif sectionType == "":
elif sectionType == "replyFromUser":

#replyMetaDataDelimiters = ["Subject: ", "From: ", "Date: "]

formattedDate = ""
formattedTime = ""
formattedDateTime = ""
repliedByName = ""
repliedByEmail = ""
subject = ""
ccRecipientsList = []
#ccRecipientDict = {}

"""
ccRecipientDict Format:
Expand All @@ -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
Expand All @@ -466,27 +407,18 @@ 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])
#repliedByName = (re.search("(?<=From: )(.*)(?= <)", items)).group()
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])
Expand All @@ -496,19 +428,14 @@ 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()

#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()
sections.append(
{"type": sectionType,
"date": formattedDate,
"time": formattedTime,
"datetime": formattedDateTime,
"subject": subject,
"userName": repliedByName,
"userEmail": repliedByEmail,
Expand Down Expand Up @@ -707,7 +634,3 @@ def getQueues() -> list:
queues.append(Queue(file))

return queues

if __name__ == "__main__":
item = Item("ce", 11)
print()

0 comments on commit 4ab1efa

Please sign in to comment.