forked from rDrama/rDrama
1
0
Fork 0

cut down on the number of queries

master
Aevann1 2022-06-29 09:22:18 +02:00
parent 5661cdb271
commit 070945d98a
7 changed files with 23 additions and 39 deletions

View File

@ -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):

View File

@ -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))
def active_flags(self):
return len(self.flags)

View File

@ -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)):

View File

@ -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

View File

@ -186,7 +186,7 @@
{% if c.bannedfor %}
<i class="fas fa-hammer-crash text-danger" data-bs-toggle="tooltip" data-bs-placement="bottom" title="User was banned for this comment for {{c.bannedfor}}"></i>
{% endif %}
{% if c.active_flags(v) %}<a class="btn btn-primary" style="padding:1px 5px; font-size:10px"role="button" onclick="document.getElementById('flaggers-{{c.id}}').classList.toggle('d-none')">{{c.active_flags(v)}} Report{{ help.plural(c.active_flags(v)) }}</a>{% endif %}
{% if c.active_flags %}<a class="btn btn-primary" style="padding:1px 5px; font-size:10px"role="button" onclick="document.getElementById('flaggers-{{c.id}}').classList.toggle('d-none')">{{c.active_flags}} Report{{ help.plural(c.active_flags) }}</a>{% endif %}
{% if c.over_18 %}<span class="badge badge-danger text-small-extra mr-1">+18</span>{% endif %}
{% if v and v.admin_level > 1 and c.author.shadowbanned %}<i class="fas fa-user-times text-admin" data-bs-toggle="tooltip" data-bs-placement="bottom" title="Shadowbanned by @{{c.author.shadowbanned}}"></i>{% endif %}
{% if c.stickied %}
@ -257,12 +257,12 @@
{{c.wordle_html(v) | safe}}
{% endif %}
</div>
{% if c.active_flags(v) %}
{% if c.active_flags %}
<div id="flaggers-{{c.id}}" class="flaggers d-none">
<strong><i class="far fa-fw fa-flag"></i> Reported by:</strong>
<pre></pre>
<ul style="padding-left:20px; margin-bottom: 0;word-wrap:break-word">
{% for f in c.flags(v) %}
{% for f in c.flags %}
<li>{% if not c.ghost %}<a style="font-weight:bold" href="{{f.user.url}}">{{f.user.username}}</a>{% else %}👻{% endif %}{% if f.reason %}: {{f.realreason(v) | safe}}{% endif %} {% if v and v.admin_level > 1 %}<a role="button" onclick="post_toast(this,'/del_report/comment/{{f.comment_id}}/{{f.user_id}}')">[remove]</a>{% endif %}</li>
{% endfor %}
</ul>

View File

@ -735,7 +735,7 @@
{% if p.is_bot %} <i class="fas fa-robot text-info" data-bs-toggle="tooltip" data-bs-placement="bottom" title="Bot"></i>{% endif %}
{% if p.over_18 %}<span class="badge badge-danger text-small-extra mr-1">+18</span>{% endif %}
{% if p.private %}<span class="badge border-warning border-1 text-small-extra">Draft</span>{% endif %}
{% if p.active_flags(v) %}<a class="btn btn-primary" role="button" style="padding:1px 5px; font-size:10px"onclick="document.getElementById('flaggers').classList.toggle('d-none')">{{p.active_flags(v)}} Report{{ help.plural(p.active_flags(v)) }}</a>{% endif %}
{% if p.active_flags %}<a class="btn btn-primary" role="button" style="padding:1px 5px; font-size:10px"onclick="document.getElementById('flaggers').classList.toggle('d-none')">{{p.active_flags}} Report{{ help.plural(p.active_flags) }}</a>{% endif %}
{% if p.ghost %}
<span {% if p.distinguish_level %}class="mod"{% endif %}>👻</span>
@ -765,12 +765,12 @@
{% endif %}
&nbsp;&nbsp;{{p.views}} thread views
</div>
{% if p.active_flags(v) %}
{% if p.active_flags %}
<div id="flaggers" class="flaggers d-none">
<strong><i class="far fa-fw fa-flag"></i> Reported by:</strong>
<pre></pre>
<ul style="padding-left:20px; margin-bottom: 0;word-wrap:break-word">
{% for f in p.flags(v) %}
{% for f in p.flags %}
<li><a style="font-weight:bold" href="{{f.user.url}}">{{f.user.username}}</a>{% if f.reason %}: {{f.realreason(v) | safe}}{% endif %} {% if v and v.admin_level > 1 %}<a role="button" onclick="post_toast(this,'/del_report/post/{{f.post_id}}/{{f.user_id}}')">[remove]</a>{% endif %}</li>
{% endfor %}
</ul>

View File

@ -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 %}
<div id="flaggers-{{p.id}}" class="flaggers d-none">
<strong><i class="far fa-fw fa-flag"></i> Reported by:</strong>
<pre></pre>
<ul style="padding-left:20px; margin-bottom: 0;word-wrap:break-word">
{% for f in p.flags(v) %}
{% for f in p.flags %}
<li><a style="font-weight:bold" href="{{f.user.url}}">{{f.user.username}}</a>{% if f.reason %}: {{f.realreason(v) | safe}}{% endif %} {% if v and v.admin_level > 1 %}<a role="button" onclick="post_toast(this,'/del_report/post/{{f.post_id}}/{{f.user_id}}')">[remove]</a>{% endif %}</li>
{% endfor %}
</ul>
@ -184,7 +184,7 @@
{% if p.is_blocking %}<i class="fas fa-user-minus text-warning" data-bs-toggle="tooltip" data-bs-placement="bottom" title="You're blocking this user, but you can see this post because you're an admin."></i>{% endif %}
{% if p.is_blocked %}<i class="fas fa-user-minus text-danger" data-bs-toggle="tooltip" data-bs-placement="bottom" title="This user is blocking you."></i>{% endif %}
{% if p.private %}<span class="badge border-warning border-1 text-small-extra">Draft</span>{% endif %}
{% if p.active_flags(v) %}<a class="btn btn-primary" role="button" style="padding:1px 5px; font-size:10px"onclick="document.getElementById('flaggers-{{p.id}}').classList.toggle('d-none')">{{p.active_flags(v)}} Report{{ help.plural(p.active_flags(v)) }}</a>{% endif %}
{% if p.active_flags %}<a class="btn btn-primary" role="button" style="padding:1px 5px; font-size:10px"onclick="document.getElementById('flaggers-{{p.id}}').classList.toggle('d-none')">{{p.active_flags}} Report{{ help.plural(p.active_flags) }}</a>{% endif %}
{% if p.ghost %}
<span {% if p.distinguish_level %}class="mod"{% endif %}>👻</span>