diff --git a/drama/classes/comment.py b/drama/classes/comment.py index 99cd58c63..6a9280fd2 100644 --- a/drama/classes/comment.py +++ b/drama/classes/comment.py @@ -135,6 +135,9 @@ class Comment(Base, Age_times, Scores, Stndrd, Fuzzing): @property def json_raw(self): + flags = {} + for f in self.flags: flags[f.user.username] = f.reason + data= { 'id': self.base36id, 'fullname': self.fullname, @@ -157,7 +160,7 @@ class Comment(Base, Age_times, Scores, Stndrd, Fuzzing): 'downvotes': self.downvotes_fuzzed, #'award_count': self.award_count, 'is_bot': self.is_bot, - 'flags': self.flags, + 'flags': flags, } if self.ban_reason: diff --git a/drama/classes/submission.py b/drama/classes/submission.py index ac330635c..de5b5d5fc 100644 --- a/drama/classes/submission.py +++ b/drama/classes/submission.py @@ -214,6 +214,9 @@ class Submission(Base, Stndrd, Age_times, Scores, Fuzzing): @property def json_raw(self): + flags = {} + for f in self.flags: flags[f.user.username] = f.reason + data = {'author_name': self.author.username, 'permalink': self.permalink, 'is_banned': bool(self.is_banned), @@ -241,8 +244,9 @@ class Submission(Base, Stndrd, Age_times, Scores, Fuzzing): 'meta_title': self.meta_title, 'meta_description': self.meta_description, 'voted': self.voted, - 'flags': self.flags, + 'flags': flags, } + if self.ban_reason: data["ban_reason"]=self.ban_reason diff --git a/drama/classes/user.py b/drama/classes/user.py index e2c22fb81..c66bd003d 100644 --- a/drama/classes/user.py +++ b/drama/classes/user.py @@ -33,6 +33,8 @@ class User(Base, Stndrd, Age_times): css = deferred(Column(String)) profilecss = deferred(Column(String)) passhash = deferred(Column(String)) + post_count = Column(Integer, default=0) + comment_count = Column(Integer, default=0) banawards = Column(Integer, default=0) created_utc = Column(Integer, default=0) suicide_utc = Column(Integer, default=0) @@ -331,16 +333,6 @@ class User(Base, Stndrd, Age_times): Comment.is_banned == False, Comment.deleted_utc == 0).count() - @property - @lazy - def post_count(self): - return self.submissions.filter_by(is_banned=False, deleted_utc=0).count() - - @property - @lazy - def comment_count(self): - return self.comments.filter(Comment.parent_submission != None).filter_by(is_banned=False, deleted_utc=0).count() - @property @lazy def alts(self): diff --git a/drama/routes/comments.py b/drama/routes/comments.py index af4275e54..52358cf87 100644 --- a/drama/routes/comments.py +++ b/drama/routes/comments.py @@ -60,7 +60,7 @@ def comment_cid_api_redirect(cid=None, pid=None): @app.get("/post_short///") @app.get("/api/v1/comment/") @app.get("/post///") -@app.route("/api/vue/comment/") +@app.get("/api/vue/comment/") @auth_desired @api("read") def post_pid_comment_cid(cid, pid=None, anything=None, v=None): @@ -625,6 +625,9 @@ def api_comment(v): cache.delete_memoized(comment_idlist) cache.delete_memoized(User.commentlisting, v) + v.comment_count = v.comments.filter(Comment.parent_submission != None).filter_by(is_banned=False, deleted_utc=0).count() + g.db.add(v) + return {"html": lambda: jsonify({"html": render_template("comments.html", v=v, comments=[c], @@ -843,8 +846,8 @@ def edit_comment(cid, v): return jsonify({"html": c.body_html}) -@app.route("/delete/comment/", methods=["GET", "POST"]) -@app.route("/api/v1/delete/comment/", methods=["GET", "POST"]) +@app.post("/delete/comment/") +@app.post("/api/v1/delete/comment/") @auth_required @validate_formkey @api("delete") diff --git a/drama/routes/posts.py b/drama/routes/posts.py index 1fe3425d8..b3d6e19ef 100644 --- a/drama/routes/posts.py +++ b/drama/routes/posts.py @@ -373,7 +373,7 @@ def edit_post(pid, v): return redirect(p.permalink) -@app.route("/submit/title", methods=['GET']) +@app.get("/submit/title") @limiter.limit("6/minute") @is_not_banned def get_post_title(v): @@ -549,7 +549,7 @@ def archiveorg(url): except Exception as e: print(e) -@app.route("/submit", methods=['POST']) +@app.post("/submit") @app.post("/api/v1/submit") @app.post("/api/vue/submit") @limiter.limit("6/minute") @@ -1037,6 +1037,8 @@ def submit_post(v): g.db.commit() send_message(f"https://rdrama.net{new_post.permalink}") + v.post_count = v.submissions.filter_by(is_banned=False, deleted_utc=0).count() + g.db.add(v) return {"html": lambda: redirect(new_post.permalink), "api": lambda: jsonify(new_post.json) } diff --git a/drama/routes/users.py b/drama/routes/users.py index 10a71eca6..73e2af129 100644 --- a/drama/routes/users.py +++ b/drama/routes/users.py @@ -45,15 +45,10 @@ def leaderboard(v): users = g.db.query(User).options(lazyload('*')) users1 = users.order_by(User.dramacoins.desc()).limit(25).all() users2 = users.order_by(User.stored_subscriber_count.desc()).limit(10).all() - users3, users4 = counts() + users3 = users.order_by(User.post_count.desc()).limit(10).all() + users4 = users.order_by(User.comment_count.desc()).limit(10).all() return render_template("leaderboard.html", v=v, users1=users1, users2=users2, users3=users3, users4=users4) -@cache.memoize(timeout=86400) -def counts(): - users3 = sorted(g.db.query(User).options(lazyload('*')).all(), key=lambda x: x.post_count, reverse=True)[:10] - users4 = sorted(g.db.query(User).options(lazyload('*')).all(), key=lambda x: x.comment_count, reverse=True)[:10] - return users3, users4 - @app.get("/@/css") def get_css(username): user = get_user(username)