From e500167d275df21c6ef75a5883fb09c7c777cd2e Mon Sep 17 00:00:00 2001 From: asdf Date: Thu, 22 Jul 2021 16:47:50 +1000 Subject: [PATCH] Category details screen now sorted by new activity --- data.py | 40 ++++++++++++++++++++++++++++++++++++++++ linkulator.py | 27 +++++++++++---------------- 2 files changed, 51 insertions(+), 16 deletions(-) diff --git a/data.py b/data.py index d192b99..05785e5 100644 --- a/data.py +++ b/data.py @@ -237,3 +237,43 @@ class LinkData: search_results.add(tuple(parent_record)) return sorted(search_results, key=lambda x: x[0], reverse=True) + + def list_category_details(self, selected_category: str) -> list: + """returns a sorted list of posts belonging to the specified category""" + links = [] + + for record in self.link_data: + category = record[4] + if category == selected_category: + postid = record[0] + userid = record[1] + timestamp = record[2] + parent_id = userid + "+" + str(timestamp) + description = record[6] + + replies = [i for i in self.link_data if i[3] == parent_id] + new_replies = [i for i in replies if i[2] >= config.USER.lastlogin] + if replies: + last_modified_timestamp = str( + max(float(timestamp), max([float(i[2]) for i in replies if i[2]])) + ) + else: + last_modified_timestamp = timestamp + + has_new_replies = ( + True if new_replies or timestamp >= config.USER.lastlogin else False + ) + + links.append( + { + "postid": postid, + "link_timestamp": timestamp, + "link_author": userid, + "reply_count": len(replies), + "description": description, + "has_new_replies": has_new_replies, + "last_modified_timestamp": last_modified_timestamp, + } + ) + + return sorted(links, key=lambda x: x["last_modified_timestamp"], reverse=True) diff --git a/linkulator.py b/linkulator.py index 92e9508..c4d2ff2 100755 --- a/linkulator.py +++ b/linkulator.py @@ -69,22 +69,17 @@ def print_category_details(view_cat): out = "" link_count = 0 thread_index = {} + category_details = LinkData.list_category_details(view_cat["name"]) - for line in link_data: - if line[4] == view_cat["name"]: - link_count += 1 - thread_index[link_count] = line[0] - parent_id = line[1] + "+" + str(line[2]) - replies = [line for line in link_data if line[3] == parent_id] - new_replies = len( - [line for line in replies if line[2] >= config.USER.lastlogin] - ) - desc = textwrap.shorten(line[6], width=desclen, placeholder="...") - newmarker = "*" if new_replies or line[2] >= config.USER.lastlogin else "" - _dt = datetime.fromtimestamp(float(line[2])).strftime("%Y-%m-%d") - out += " {:3d} {:>10s} {:<{namelen}s} [{:3d}] {:s}{}\n".format( - link_count, _dt, line[1], len(replies), desc, newmarker, namelen=namelen - ) + for link in category_details: + link_count += 1 + thread_index[link_count] = link["postid"] + desc = textwrap.shorten(link["description"], width=desclen, placeholder="...") + newmarker = "*" if link["last_modified_timestamp"] >= config.USER.lastlogin else "" + _dt = datetime.fromtimestamp(float(link["link_timestamp"])).strftime("%Y-%m-%d") + out += " {:3d} {:>10s} {:<{namelen}s} [{:3d}] {:s}{}\n".format( + link_count, _dt, link["link_author"], link["reply_count"], desc, newmarker, namelen=namelen + ) if len(out) > 0: print(header) @@ -274,7 +269,7 @@ def is_valid_input(entry: str) -> bool: return True def is_correct_category(entry: str) -> bool: - """Make sure the user purposefully created a new category and not by + """Make sure the user purposefully created a new category and not by accident (mistyped, tried to use category number instead of name)""" if entry not in [ record["name"] for record in categories ]: question = "Do you want to create a new category '{}'? Y/[N]".format(entry)