From 89ad2a1174fe3f07000af53788f121baf0473858 Mon Sep 17 00:00:00 2001 From: Justin Campbell Date: Sun, 26 Jul 2020 13:21:34 -0400 Subject: [PATCH] Implement getUserEmail in Item class --- api/ECNQueue.py | 29 +++++++++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/api/ECNQueue.py b/api/ECNQueue.py index a282140..03d0695 100644 --- a/api/ECNQueue.py +++ b/api/ECNQueue.py @@ -1,4 +1,4 @@ - # TODO: Add ECNQueue module documentation +# TODO: Add ECNQueue module documentation #------------------------------------------------------------------------------# # Imports @@ -33,6 +33,7 @@ def __init__(self, queue: str, number: int) -> None: self.headers = self.__parseHeaders() self.content = self.__getContent() self.isLocked = self.__isLocked() + self.userEmail = self.__getUserEmail() def __getLastUpdated(self) -> float: """Returns last modified time of item reported by the filesystem in mm-dd-yy hh:mm am/pm format. @@ -114,7 +115,7 @@ def __parseHeaders(self) -> list: # TODO: Implement attachment parsing def __getContent(self) -> list: - """Returns a dictionary lines of the item body. + """Returns a dictionary of lines of the item body. Example: "Hello. I need help.\\n\\n*** Status updated by: campb303 at: 6/23/2020 13:26:55 ***\\nDont Delete" becomes @@ -156,6 +157,30 @@ def __isLocked(self) -> Union[str, bool]: else: return False + def __getMostRecentHeaderByType(self, headerType: str) -> Union[dict, None]: + """Return most recent header of the given type. + If no header of that type exists, returns None. + + Args: + headerType (str): Type of header to return. + + Returns: + Union[dict, None]: dict of header if it exists or None + """ + for header in self.headers: + if header["type"] == headerType: return header + return None + + def __getUserEmail(self) -> str: + """Returns the email address that the item originated from. + + Returns: + str: Email address that the item originated from. + """ + fromHeader = self.__getMostRecentHeaderByType("From") + userEmail = email.utils.parseaddr(fromHeader["content"])[1] + return userEmail + def __repr__(self) -> str: return self.queue + str(self.number)