From 27b1ed83cc16c3db820093a553b7afc6a209e81c Mon Sep 17 00:00:00 2001 From: Aevann1 Date: Mon, 11 Jul 2022 15:14:34 +0200 Subject: [PATCH] refactor json a little bit and fix https://chapotraphouse.club/post/18459/marseycapywalking-megathread-for-bugs-and-suggestions/2278188?context=8#context --- files/classes/comment.py | 65 +++++++++++++++---------------------- files/classes/submission.py | 52 +++++++++++++---------------- files/classes/user.py | 31 ++++++++---------- 3 files changed, 62 insertions(+), 86 deletions(-) diff --git a/files/classes/comment.py b/files/classes/comment.py index 66f246835..4052a2a63 100644 --- a/files/classes/comment.py +++ b/files/classes/comment.py @@ -242,40 +242,6 @@ class Comment(Base): if self.ghost: return '👻' else: return self.author.username - @property - @lazy - def json_raw(self): - flags = {} - for f in self.flags: flags[f.user.username] = f.reason - - data= { - 'id': self.id, - 'level': self.level, - 'author_name': self.author_name, - 'body': self.body, - 'body_html': self.body_html, - 'is_bot': self.is_bot, - 'created_utc': self.created_utc, - 'edited_utc': self.edited_utc or 0, - 'is_banned': bool(self.is_banned), - 'deleted_utc': self.deleted_utc, - 'is_nsfw': self.over_18, - 'permalink': f'/comment/{self.id}', - 'stickied': self.stickied, - 'distinguish_level': self.distinguish_level, - 'post_id': self.post.id if self.post else 0, - 'score': self.score, - 'upvotes': self.upvotes, - 'downvotes': self.downvotes, - 'is_bot': self.is_bot, - 'flags': flags, - } - - if self.ban_reason: - data["ban_reason"]=self.ban_reason - - return data - @lazy def award_count(self, kind, v): if v and v.poor: return 0 @@ -300,13 +266,36 @@ class Comment(Base): 'parent': self.parent_fullname } else: + flags = {} + for f in self.flags: flags[f.user.username] = f.reason - data=self.json_raw + data= { + 'id': self.id, + 'level': self.level, + 'author_name': self.author_name, + 'body': self.body, + 'body_html': self.body_html, + 'is_bot': self.is_bot, + 'created_utc': self.created_utc, + 'edited_utc': self.edited_utc or 0, + 'is_banned': bool(self.is_banned), + 'deleted_utc': self.deleted_utc, + 'is_nsfw': self.over_18, + 'permalink': f'/comment/{self.id}', + 'stickied': self.stickied, + 'distinguish_level': self.distinguish_level, + 'post_id': self.post.id if self.post else 0, + 'score': self.score, + 'upvotes': self.upvotes, + 'downvotes': self.downvotes, + 'is_bot': self.is_bot, + 'flags': flags, + } - if self.level>=2: data['parent_comment_id']= self.parent_comment_id - if "replies" in self.__dict__: - data['replies']=[x.json_core for x in self.replies(None)] + if self.level >= 2: data['parent_comment_id'] = self.parent_comment_id + + data['replies'] = [x.json_core for x in self.replies()] return data diff --git a/files/classes/submission.py b/files/classes/submission.py index 63578f2a5..da106c467 100644 --- a/files/classes/submission.py +++ b/files/classes/submission.py @@ -216,11 +216,30 @@ class Submission(Base): @property @lazy - def json_raw(self): + def json_core(self): + + if self.is_banned: + return {'is_banned': True, + 'deleted_utc': self.deleted_utc, + 'ban_reason': self.ban_reason, + 'id': self.id, + 'title': self.title, + 'permalink': self.permalink, + } + + if self.deleted_utc: + return {'is_banned': bool(self.is_banned), + 'deleted_utc': True, + 'id': self.id, + 'title': self.title, + 'permalink': self.permalink, + } + + flags = {} for f in self.flags: flags[f.user.username] = f.reason - data = {'author_name': self.author_name if self.author else '', + return {'author_name': self.author_name if self.author else '', 'permalink': self.permalink, 'shortlink': self.shortlink, 'is_banned': bool(self.is_banned), @@ -249,33 +268,6 @@ class Submission(Base): 'club': self.club, } - if self.ban_reason: - data["ban_reason"]=self.ban_reason - - return data - - @property - @lazy - def json_core(self): - - if self.is_banned: - return {'is_banned': True, - 'deleted_utc': self.deleted_utc, - 'ban_reason': self.ban_reason, - 'id': self.id, - 'title': self.title, - 'permalink': self.permalink, - } - elif self.deleted_utc: - return {'is_banned': bool(self.is_banned), - 'deleted_utc': True, - 'id': self.id, - 'title': self.title, - 'permalink': self.permalink, - } - - return self.json_raw - @property @lazy def json(self): @@ -288,7 +280,7 @@ class Submission(Base): data["author"]='👻' if self.ghost else self.author.json_core data["comment_count"]=self.comment_count - + if "replies" in self.__dict__: data["replies"]=[x.json_core for x in self.replies] diff --git a/files/classes/user.py b/files/classes/user.py index 3a7c3e7c8..ad28b757a 100644 --- a/files/classes/user.py +++ b/files/classes/user.py @@ -619,8 +619,19 @@ class User(Base): @property @lazy - def json_raw(self): - data = {'username': self.username, + def json_core(self): + + if self.is_suspended: + return {'username': self.username, + 'url': self.url, + 'is_banned': True, + 'is_permanent_ban': not bool(self.unban_utc), + 'ban_reason': self.ban_reason, + 'id': self.id + } + + + return {'username': self.username, 'url': self.url, 'is_banned': bool(self.is_banned), 'created_utc': self.created_utc, @@ -633,22 +644,6 @@ class User(Base): 'flair': self.customtitle } - return data - - @property - @lazy - def json_core(self): - - now = int(time.time()) - if self.is_suspended: - return {'username': self.username, - 'url': self.url, - 'is_banned': True, - 'is_permanent_ban': not bool(self.unban_utc), - 'ban_reason': self.ban_reason, - 'id': self.id - } - return self.json_raw @property @lazy