From ae45b45bfb0a3ef69c626e8942d40c576b171a41 Mon Sep 17 00:00:00 2001 From: Aevann1 Date: Mon, 11 Jul 2022 19:33:26 +0200 Subject: [PATCH] refactor json again --- files/classes/comment.py | 24 +++----------------- files/classes/submission.py | 23 +++++-------------- files/classes/user.py | 21 +++++------------- files/classes/votes.py | 42 ++++++++++------------------------- files/routes/notifications.py | 2 +- 5 files changed, 27 insertions(+), 85 deletions(-) diff --git a/files/classes/comment.py b/files/classes/comment.py index d4d26b41ea..761384f987 100644 --- a/files/classes/comment.py +++ b/files/classes/comment.py @@ -248,8 +248,7 @@ class Comment(Base): return len([x for x in self.awards if x.kind == kind]) @property - @lazy - def json_core(self): + def json(self): if self.is_banned: data= {'is_banned': True, 'ban_reason': self.ban_reason, @@ -290,28 +289,11 @@ class Comment(Base): 'downvotes': self.downvotes, 'is_bot': self.is_bot, 'flags': flags, + 'author': '👻' if self.ghost else self.author.json, + 'replies': [x.json for x in self.replies()] } - if self.level >= 2: data['parent_comment_id'] = self.parent_comment_id - - return data - - @property - def json(self): - - data=self.json_core - - if self.deleted_utc or self.is_banned: - return data - - data["author"]='👻' if self.ghost else self.author.json_core - data["post"]=self.post.json_core if self.post else '' - - if self.level >= 2: - data["parent"]=self.parent.json_core - - 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 68344f642a..72477ca634 100644 --- a/files/classes/submission.py +++ b/files/classes/submission.py @@ -216,7 +216,7 @@ class Submission(Base): @property @lazy - def json_core(self): + def json(self): if self.is_banned: return {'is_banned': True, @@ -239,7 +239,7 @@ class Submission(Base): flags = {} for f in self.flags: flags[f.user.username] = f.reason - return {'author_name': self.author_name if self.author else '', + data = {'author_name': self.author_name if self.author else '', 'permalink': self.permalink, 'shortlink': self.shortlink, 'is_banned': bool(self.is_banned), @@ -266,27 +266,14 @@ class Submission(Base): 'voted': self.voted if hasattr(self, 'voted') else 0, 'flags': flags, 'club': self.club, + 'author': '👻' if self.ghost else self.author.json, + 'comment_count': self.comment_count, + 'voted': self.voted } - @property - @lazy - def json(self): - - data=self.json_core - - if self.deleted_utc or self.is_banned: - return data - - 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 for x in self.replies] - if "voted" in self.__dict__: - data["voted"] = self.voted - return data @lazy diff --git a/files/classes/user.py b/files/classes/user.py index 59782e57ba..32bb7d27ce 100644 --- a/files/classes/user.py +++ b/files/classes/user.py @@ -620,8 +620,7 @@ class User(Base): @property @lazy - def json_core(self): - + def json(self): if self.is_suspended: return {'username': self.username, 'url': self.url, @@ -642,22 +641,14 @@ class User(Base): 'bannerurl': self.banner_url, 'bio': self.bio, 'bio_html': self.bio_html_eager, - 'flair': self.customtitle + 'flair': self.customtitle, + 'badges': [x.json for x in self.badges], + 'coins': self.coins, + 'post_count': self.post_count, + 'comment_count': self.comment_count } - @property - @lazy - def json(self): - data = self.json_core - - data["badges"] = [x.json for x in self.badges] - data['coins'] = self.coins - data['post_count'] = self.post_count - data['comment_count'] = self.comment_count - - return data - def ban(self, admin=None, reason=None, days=0): if days: diff --git a/files/classes/votes.py b/files/classes/votes.py index f0c54d3d5d..1f72b4f8d2 100644 --- a/files/classes/votes.py +++ b/files/classes/votes.py @@ -25,24 +25,15 @@ class Vote(Base): def __repr__(self): return f"" - @property - @lazy - def json_core(self): - data={ - "user_id": self.user_id, - "submission_id":self.submission_id, - "vote_type":self.vote_type - } - return data - @property @lazy def json(self): - data=self.json_core - data["user"]=self.user.json_core - data["post"]=self.post.json_core - - return data + return {"user_id": self.user_id, + "submission_id":self.submission_id, + "vote_type":self.vote_type, + "user":self.user.json, + "post":self.post.json + } class CommentVote(Base): @@ -64,21 +55,12 @@ class CommentVote(Base): def __repr__(self): return f"" - @property - @lazy - def json_core(self): - data={ - "user_id": self.user_id, - "comment_id":self.comment_id, - "vote_type":self.vote_type - } - return data - @property @lazy def json(self): - data=self.json_core - data["user"]=self.user.json_core - data["comment"]=self.comment.json_core - - return data \ No newline at end of file + return {"user_id": self.user_id, + "submission_id":self.submission_id, + "vote_type":self.vote_type, + "user":self.user.json, + "comment":self.comment.json + } \ No newline at end of file diff --git a/files/routes/notifications.py b/files/routes/notifications.py index bf0d0b5b65..601f7a0d4b 100644 --- a/files/routes/notifications.py +++ b/files/routes/notifications.py @@ -30,7 +30,7 @@ def unread(v): n.read = True g.db.add(n) - return {"data":[x[1].json_core for x in listing]} + return {"data":[x[1].json for x in listing]}