From c4e8532c63e71398610d6ae0ce7cfddb550f1775 Mon Sep 17 00:00:00 2001 From: Justin Campbell Date: Mon, 21 Jun 2021 11:09:36 -0400 Subject: [PATCH] Auto populate Item.json_data --- src/webqueue2api/parser/item.py | 63 +++++++++++++++++++++++---------- 1 file changed, 44 insertions(+), 19 deletions(-) diff --git a/src/webqueue2api/parser/item.py b/src/webqueue2api/parser/item.py index 592fe6f..a9daa4d 100644 --- a/src/webqueue2api/parser/item.py +++ b/src/webqueue2api/parser/item.py @@ -72,26 +72,51 @@ def __init__(self, queue: str, number: int) -> None: self.department = self.__get_most_recent_header_by_type("Department") self.building = self.__get_most_recent_header_by_type("Building") self.date_received = self.__get_formatted_date(self.__get_most_recent_header_by_type("Date")) + self.json_data = self.__generate_json_data() - # TODO: Autopopulate jsonData w/ __dir__() command. Exclude `^_` and `jsonData`. - self.json_data = { - "queue": self.queue, - "number": self.number, - "lastUpdated": self.last_updated, - "headers": self.headers, - "content": self.content, - "isLocked": self.is_locked, - "userEmail": self.user_email, - "userName": self.user_name, - "userAlias": self.user_alias, - "assignedTo": self.assigned_to, - "subject": self.subject, - "status": self.status, - "priority": self.priority, - "department": self.department, - "building": self.building, - "dateReceived": self.date_received - } + def __generate_json_data(self) -> dict: + """Generates a JSON serializable data dictionary of class attributes. + + Example: + __generate_json_data() returns: + { + "assigned_to": 'bekuma', + "building": '', + "content": [...], + "date_received": '2020-08-20T22: 06: 08+0000', + "departmen"': '', + "headers": [...], + "is_locked"': False, + "last_updated": '2021-06-04T11: 01: 00-0400', + "number": 1, + "path": '/home/pier/e/queue/Mail/ce/1', + "priority": '', + "queue": 'ce', + "status": 'archiving from zsite', + "subject": 'RE: Updating my computer', + "user_alias": 'govind', + "user_email": 'govind@purdue.edu', + "user_name": 'Govindaraju, Rao S' + } + + Returns: + dict: JSON serializable data dictionary of class attributes. + """ + json_data = {} + + # List of attributes that need processing before being added to json_data + attributes_to_process = ["path"] + for attribute in attributes_to_process: + if attribute == "path": + json_data[attribute] = str(self.path) + + # List of attributes to be ignored + attributes_to_ignore = ["to_json", "json_data"] + attributes_to_process + for attribute in self.__dir__(): + if not attribute.startswith("_") and attribute not in attributes_to_ignore: + json_data[attribute] = self.__getattribute__(attribute) + + return json_data def __get_time_last_updated(self) -> str: """Returns last modified time of item reported by the filesystem in mm-dd-yy hh:mm am/pm format.