forked from rDrama/rDrama
1
0
Fork 0

refactor json again

master
Aevann1 2022-07-11 19:33:26 +02:00
parent bc4074f9aa
commit ae45b45bfb
5 changed files with 27 additions and 85 deletions

View File

@ -248,8 +248,7 @@ class Comment(Base):
return len([x for x in self.awards if x.kind == kind]) return len([x for x in self.awards if x.kind == kind])
@property @property
@lazy def json(self):
def json_core(self):
if self.is_banned: if self.is_banned:
data= {'is_banned': True, data= {'is_banned': True,
'ban_reason': self.ban_reason, 'ban_reason': self.ban_reason,
@ -290,28 +289,11 @@ class Comment(Base):
'downvotes': self.downvotes, 'downvotes': self.downvotes,
'is_bot': self.is_bot, 'is_bot': self.is_bot,
'flags': flags, '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 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 return data

View File

@ -216,7 +216,7 @@ class Submission(Base):
@property @property
@lazy @lazy
def json_core(self): def json(self):
if self.is_banned: if self.is_banned:
return {'is_banned': True, return {'is_banned': True,
@ -239,7 +239,7 @@ class Submission(Base):
flags = {} flags = {}
for f in self.flags: flags[f.user.username] = f.reason 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, 'permalink': self.permalink,
'shortlink': self.shortlink, 'shortlink': self.shortlink,
'is_banned': bool(self.is_banned), 'is_banned': bool(self.is_banned),
@ -266,27 +266,14 @@ class Submission(Base):
'voted': self.voted if hasattr(self, 'voted') else 0, 'voted': self.voted if hasattr(self, 'voted') else 0,
'flags': flags, 'flags': flags,
'club': self.club, '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__: if "replies" in self.__dict__:
data["replies"]=[x.json for x in self.replies] data["replies"]=[x.json for x in self.replies]
if "voted" in self.__dict__:
data["voted"] = self.voted
return data return data
@lazy @lazy

View File

@ -620,8 +620,7 @@ class User(Base):
@property @property
@lazy @lazy
def json_core(self): def json(self):
if self.is_suspended: if self.is_suspended:
return {'username': self.username, return {'username': self.username,
'url': self.url, 'url': self.url,
@ -642,22 +641,14 @@ class User(Base):
'bannerurl': self.banner_url, 'bannerurl': self.banner_url,
'bio': self.bio, 'bio': self.bio,
'bio_html': self.bio_html_eager, '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): def ban(self, admin=None, reason=None, days=0):
if days: if days:

View File

@ -25,24 +25,15 @@ class Vote(Base):
def __repr__(self): def __repr__(self):
return f"<Vote(id={self.id})>" return f"<Vote(id={self.id})>"
@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 @property
@lazy @lazy
def json(self): def json(self):
data=self.json_core return {"user_id": self.user_id,
data["user"]=self.user.json_core "submission_id":self.submission_id,
data["post"]=self.post.json_core "vote_type":self.vote_type,
"user":self.user.json,
return data "post":self.post.json
}
class CommentVote(Base): class CommentVote(Base):
@ -64,21 +55,12 @@ class CommentVote(Base):
def __repr__(self): def __repr__(self):
return f"<CommentVote(id={self.id})>" return f"<CommentVote(id={self.id})>"
@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 @property
@lazy @lazy
def json(self): def json(self):
data=self.json_core return {"user_id": self.user_id,
data["user"]=self.user.json_core "submission_id":self.submission_id,
data["comment"]=self.comment.json_core "vote_type":self.vote_type,
"user":self.user.json,
return data "comment":self.comment.json
}

View File

@ -30,7 +30,7 @@ def unread(v):
n.read = True n.read = True
g.db.add(n) g.db.add(n)
return {"data":[x[1].json_core for x in listing]} return {"data":[x[1].json for x in listing]}