try to cut down on number of db queries

master
Aevann1 2022-06-29 08:14:25 +02:00
parent c341be2866
commit c674c6a056
5 changed files with 10 additions and 19 deletions

View File

@ -70,11 +70,11 @@ class Submission(Base):
embed_url = Column(String)
new = Column(Boolean)
author = relationship("User", primaryjoin="Submission.author_id==User.id")
author = relationship("User", primaryjoin="Submission.author_id==User.id", lazy="joined")
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)
awards = relationship("AwardRelationship", order_by="AwardRelationship.awarded_utc.desc()", lazy="joined", viewonly=True)
flags = relationship("Flag", order_by="Flag.created_utc", lazy="joined", 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,
@ -506,4 +497,4 @@ class Submission(Base):
@lazy
def active_flags(self, v):
return len(self.flags(v))
return len(self.flags)

View File

@ -135,7 +135,7 @@ class User(Base):
total_held_lottery_tickets = Column(Integer, default=0)
total_lottery_winnings = Column(Integer, default=0)
badges = relationship("Badge", order_by="Badge.created_utc", viewonly=True)
badges = relationship("Badge", order_by="Badge.created_utc", lazy="joined", viewonly=True)
subscriptions = relationship("Subscription", viewonly=True)
following = relationship("Follow", primaryjoin="Follow.user_id==User.id", viewonly=True)
followers = relationship("Follow", primaryjoin="Follow.target_id==User.id", viewonly=True)

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

@ -770,7 +770,7 @@
<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

@ -72,7 +72,7 @@
<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>