From 070945d98acd8122c6605f18c63cddd7d5f2c218 Mon Sep 17 00:00:00 2001 From: Aevann1 Date: Wed, 29 Jun 2022 09:22:18 +0200 Subject: [PATCH] cut down on the number of queries --- files/classes/comment.py | 18 +++++------------- files/classes/submission.py | 18 +++++------------- files/helpers/get.py | 4 ++-- files/routes/admin.py | 4 ++-- files/templates/comments.html | 6 +++--- files/templates/submission.html | 6 +++--- files/templates/submission_listing.html | 6 +++--- 7 files changed, 23 insertions(+), 39 deletions(-) diff --git a/files/classes/comment.py b/files/classes/comment.py index 85577933a..765b9934c 100644 --- a/files/classes/comment.py +++ b/files/classes/comment.py @@ -83,8 +83,8 @@ class Comment(Base): parent_comment = relationship("Comment", remote_side=[id], viewonly=True) child_comments = relationship("Comment", lazy="dynamic", remote_side=[parent_comment_id], viewonly=True) awards = relationship("AwardRelationship", order_by="AwardRelationship.awarded_utc.desc()", viewonly=True) - reports = relationship("CommentFlag", viewonly=True) - + flags = relationship("CommentFlag", order_by="CommentFlag.created_utc", viewonly=True) + def __init__(self, *args, **kwargs): if "created_utc" not in kwargs: kwargs["created_utc"] = int(time.time()) @@ -99,15 +99,6 @@ class Comment(Base): def top_comment(self): return g.db.get(Comment, self.top_comment_id) - @lazy - def flags(self, v): - flags = g.db.query(CommentFlag).filter_by(comment_id=self.id).order_by(CommentFlag.created_utc).all() - if not (v and (v.shadowbanned or v.admin_level >= 2)): - for flag in flags: - if flag.user.shadowbanned: - flags.remove(flag) - return flags - @lazy def poll_voted(self, v): if v: @@ -296,7 +287,7 @@ class Comment(Base): @lazy def json_raw(self): flags = {} - for f in self.flags(None): flags[f.user.username] = f.reason + for f in self.flags: flags[f.user.username] = f.reason data= { 'id': self.id, @@ -473,8 +464,9 @@ class Comment(Base): @lazy def is_op(self): return self.author_id==self.post.author_id + @property @lazy - def active_flags(self, v): return len(self.flags(v)) + def active_flags(self): return len(self.flags) @lazy def wordle_html(self, v): diff --git a/files/classes/submission.py b/files/classes/submission.py index a33aad78d..77e0b969d 100644 --- a/files/classes/submission.py +++ b/files/classes/submission.py @@ -74,7 +74,7 @@ class Submission(Base): oauth_app = relationship("OauthApp", viewonly=True) approved_by = relationship("User", uselist=False, primaryjoin="Submission.is_approved==User.id", viewonly=True) awards = relationship("AwardRelationship", order_by="AwardRelationship.awarded_utc.desc()", viewonly=True) - reports = relationship("Flag", viewonly=True) + flags = relationship("Flag", order_by="Flag.created_utc", viewonly=True) comments = relationship("Comment", primaryjoin="Comment.parent_submission==Submission.id") subr = relationship("Sub", primaryjoin="foreign(Submission.sub)==remote(Sub.name)", viewonly=True) @@ -93,15 +93,6 @@ class Submission(Base): if self.downvotes > 5 and 0.25 < self.upvotes / self.downvotes < 4: return True return False - @lazy - def flags(self, v): - flags = g.db.query(Flag).filter_by(post_id=self.id).order_by(Flag.created_utc).all() - if not (v and (v.shadowbanned or v.admin_level >= 2)): - for flag in flags: - if flag.user.shadowbanned: - flags.remove(flag) - return flags - @property @lazy def options(self): @@ -287,7 +278,7 @@ class Submission(Base): @lazy def json_raw(self): flags = {} - for f in self.flags(None): flags[f.user.username] = f.reason + for f in self.flags: flags[f.user.username] = f.reason data = {'author_name': self.author_name if self.author else '', 'permalink': self.permalink, @@ -504,6 +495,7 @@ class Submission(Base): return True return False + @property @lazy - def active_flags(self, v): - return len(self.flags(v)) \ No newline at end of file + def active_flags(self): + return len(self.flags) \ No newline at end of file diff --git a/files/helpers/get.py b/files/helpers/get.py index 919a37484..644a522e4 100644 --- a/files/helpers/get.py +++ b/files/helpers/get.py @@ -1,6 +1,6 @@ from files.classes import * from flask import g - +from sqlalchemy.orm import joinedload def get_id(username, v=None, graceful=False): @@ -190,7 +190,7 @@ def get_posts(pids, v=None): blocked, blocked.c.user_id == Submission.author_id, isouter=True - ).all() + ).options(joinedload(Submission.flags), joinedload(Submission.awards)).all() output = [p[0] for p in query] for i in range(len(output)): diff --git a/files/routes/admin.py b/files/routes/admin.py index 71d6267b0..5536c30cb 100644 --- a/files/routes/admin.py +++ b/files/routes/admin.py @@ -430,7 +430,7 @@ def reported_posts(v): listing = g.db.query(Submission).filter_by( is_approved=None, is_banned=False - ).join(Submission.reports).order_by(Submission.id.desc()).offset(25 * (page - 1)).limit(26) + ).join(Submission.flags).order_by(Submission.id.desc()).offset(25 * (page - 1)).limit(26) listing = [p.id for p in listing] next_exists = len(listing) > 25 @@ -452,7 +452,7 @@ def reported_comments(v): ).filter_by( is_approved=None, is_banned=False - ).join(Comment.reports).order_by(Comment.id.desc()).offset(25 * (page - 1)).limit(26).all() + ).join(Comment.flags).order_by(Comment.id.desc()).offset(25 * (page - 1)).limit(26).all() listing = [c.id for c in listing] next_exists = len(listing) > 25 diff --git a/files/templates/comments.html b/files/templates/comments.html index ab43d8db6..33c7e70a0 100644 --- a/files/templates/comments.html +++ b/files/templates/comments.html @@ -186,7 +186,7 @@ {% if c.bannedfor %} {% endif %} - {% if c.active_flags(v) %}{{c.active_flags(v)}} Report{{ help.plural(c.active_flags(v)) }}{% endif %} + {% if c.active_flags %}{{c.active_flags}} Report{{ help.plural(c.active_flags) }}{% endif %} {% if c.over_18 %}+18{% endif %} {% if v and v.admin_level > 1 and c.author.shadowbanned %}{% endif %} {% if c.stickied %} @@ -257,12 +257,12 @@ {{c.wordle_html(v) | safe}} {% endif %} - {% if c.active_flags(v) %} + {% if c.active_flags %}
Reported by:

 				
diff --git a/files/templates/submission.html b/files/templates/submission.html
index 3574ce0f6..fc4fb05fd 100644
--- a/files/templates/submission.html
+++ b/files/templates/submission.html
@@ -735,7 +735,7 @@
 						{% if p.is_bot %} {% endif %}
 						{% if p.over_18 %}+18{% endif %}
 						{% if p.private %}Draft{% endif %}
-						{% if p.active_flags(v) %}{{p.active_flags(v)}} Report{{ help.plural(p.active_flags(v)) }}{% endif %}
+						{% if p.active_flags %}{{p.active_flags}} Report{{ help.plural(p.active_flags) }}{% endif %}
 
 						{% if p.ghost %}
 							👻
@@ -765,12 +765,12 @@
 						{% endif %}
 						  {{p.views}} thread views
 					
- {% if p.active_flags(v) %} + {% if p.active_flags %}
Reported by:

 						
diff --git a/files/templates/submission_listing.html b/files/templates/submission_listing.html
index bc839040c..61c97fa2c 100644
--- a/files/templates/submission_listing.html
+++ b/files/templates/submission_listing.html
@@ -67,12 +67,12 @@
 
 {% set v_forbid_deleted = (p.deleted_utc != 0 or p.is_banned) and not (v and v.admin_level >= 2) and not (v and v.id == p.author_id) %}
 
-{% if p.active_flags(v) %}
+{% if p.active_flags %}
 	
Reported by:

 		
    - {% for f in p.flags(v) %} + {% for f in p.flags %}
  • {{f.user.username}}{% if f.reason %}: {{f.realreason(v) | safe}}{% endif %} {% if v and v.admin_level > 1 %}[remove]{% endif %}
  • {% endfor %}
@@ -184,7 +184,7 @@ {% if p.is_blocking %}{% endif %} {% if p.is_blocked %}{% endif %} {% if p.private %}Draft{% endif %} - {% if p.active_flags(v) %}{{p.active_flags(v)}} Report{{ help.plural(p.active_flags(v)) }}{% endif %} + {% if p.active_flags %}{{p.active_flags}} Report{{ help.plural(p.active_flags) }}{% endif %} {% if p.ghost %} 👻