diff --git a/files/classes/comment.py b/files/classes/comment.py index 9e6bdd2f6..ea420713b 100644 --- a/files/classes/comment.py +++ b/files/classes/comment.py @@ -142,21 +142,11 @@ class Comment(Base): years = now.tm_year - ctd.tm_year return f"{years}yr ago" - @property - @lazy - def score(self): - return self.upvotes - self.downvotes - @property @lazy def fullname(self): return f"t3_{self.id}" - @property - @lazy - def score_disputed(self): - return (self.upvotes+1) * (self.downvotes+1) - @property @lazy def parent(self): diff --git a/files/classes/submission.py b/files/classes/submission.py index 7634c6499..a6959268b 100644 --- a/files/classes/submission.py +++ b/files/classes/submission.py @@ -165,21 +165,11 @@ class Submission(Base): return str(time.strftime("%d/%B/%Y %H:%M:%S UTC", time.gmtime(self.edited_utc))) - @property - @lazy - def score(self): - return self.upvotes - self.downvotes - @property @lazy def hotscore(self): return 10000000*(self.upvotes - self.downvotes + 1)/(((self.age+3600)/1000)**(1.35)) - @property - @lazy - def score_disputed(self): - return (self.upvotes+1) * (self.downvotes+1) - @property @lazy diff --git a/files/classes/user.py b/files/classes/user.py index 3fffddfa8..7f60e703a 100644 --- a/files/classes/user.py +++ b/files/classes/user.py @@ -241,10 +241,10 @@ class User(Base): if self.shadowbanned and not (v and (v.admin_level >= 3 or v.id == self.id)): return [] - submissions = g.db.query(Submission).options(lazyload('*')).filter_by(author_id=self.id, is_pinned=False) + posts = g.db.query(Submission.id).options(lazyload('*')).filter_by(author_id=self.id, is_pinned=False) if not (v and (v.admin_level >= 3 or v.id == self.id)): - submissions = submissions.filter_by(deleted_utc=0, is_banned=False, private=False) + posts = posts.filter_by(deleted_utc=0, is_banned=False, private=False) now = int(time.time()) if t == 'hour': @@ -259,25 +259,24 @@ class User(Base): cutoff = now - 31536000 else: cutoff = 0 - submissions = submissions.filter(Submission.created_utc >= cutoff) + posts = posts.filter(Submission.created_utc >= cutoff) if sort == "new": - submissions = submissions.order_by(Submission.created_utc.desc()).all() + posts = posts.order_by(Submission.created_utc.desc()) elif sort == "old": - submissions = submissions.order_by(Submission.created_utc.asc()).all() + posts = posts.order_by(Submission.created_utc.asc()) elif sort == "controversial": - submissions = sorted(submissions.all(), key=lambda x: x.score_disputed, reverse=True) + posts = posts.order_by(Submission.upvotes * Submission.downvotes) elif sort == "top": - submissions = sorted(submissions.all(), key=lambda x: x.score, reverse=True) + posts = posts.order_by(Submission.downvotes - Submission.upvotes) elif sort == "bottom": - submissions = sorted(submissions.all(), key=lambda x: x.score) + posts = posts.order_by(Submission.upvotes - Submission.downvotes) elif sort == "comments": - submissions = submissions.order_by(Submission.comment_count.desc()).all() + posts = posts.order_by(Submission.comment_count.desc()) - firstrange = 25 * (page - 1) - secondrange = firstrange + 26 - listing = [x.id for x in submissions[firstrange:secondrange]] - return listing + posts = posts.offset(25 * (page - 1)).limit(26).all() + + return [x[0] for x in posts] @property @lazy diff --git a/files/routes/front.py b/files/routes/front.py index 9c6f9530e..e2a41f3a3 100644 --- a/files/routes/front.py +++ b/files/routes/front.py @@ -109,10 +109,10 @@ def notifications(v): -# @cache.memoize(timeout=86400) +@cache.memoize(timeout=86400) def frontlist(v=None, sort="hot", page=1, t="all", ids_only=True, filter_words='', **kwargs): - posts = g.db.query(Submission).options(lazyload('*')) + posts = g.db.query(Submission.id).options(lazyload('*')) if 'rdrama' in request.host and t != 'day' and sort in ["hot","controversial"]: cutoff = int(time.time()) - 86400 @@ -172,35 +172,32 @@ def frontlist(v=None, sort="hot", page=1, t="all", ids_only=True, filter_words=' if sort == "hot": posts = sorted(posts.all(), key=lambda x: x.hotscore, reverse=True) + firstrange = 25 * (page - 1) + secondrange = firstrange+100 + posts = posts[firstrange:secondrange] elif sort == "new": - posts = posts.order_by(Submission.created_utc.desc()).all() + posts = posts.order_by(Submission.created_utc.desc()) elif sort == "old": - posts = posts.order_by(Submission.created_utc.asc()).all() + posts = posts.order_by(Submission.created_utc.asc()) elif sort == "controversial": - posts = sorted(posts.all(), key=lambda x: x.score_disputed, reverse=True) + posts = posts.order_by(Submission.upvotes * Submission.downvotes) elif sort == "top": - posts = posts.order_by(Submission.downvotes - Submission.upvotes).all() + posts = posts.order_by(Submission.downvotes - Submission.upvotes) elif sort == "bottom": - posts = sorted(posts.all(), key=lambda x: x.score) + posts = posts.order_by(Submission.upvotes - Submission.downvotes) elif sort == "comments": - posts = posts.order_by(Submission.comment_count.desc()).all() - elif sort == "random": - posts = posts.all() - posts = random.sample(posts, k=len(posts)) - else: - abort(400) + posts = posts.order_by(Submission.comment_count.desc()) + + if sort != "hot": posts = posts.offset(25 * (page - 1)).limit(26).all() - firstrange = 25 * (page - 1) - secondrange = firstrange+100 - posts = posts[firstrange:secondrange] next_exists = (len(posts) > 25) posts = posts[:25] - if page == 1: posts = g.db.query(Submission).options(lazyload('*')).filter(Submission.stickied != None).all() + posts + if page == 1: posts = g.db.query(Submission.id).options(lazyload('*')).filter(Submission.stickied != None).all() + posts - if ids_only: posts = [x.id for x in posts] + if ids_only: posts = [x[0] for x in posts] return posts, next_exists @@ -249,7 +246,7 @@ def front_all(v): @cache.memoize(timeout=86400) def changeloglist(v=None, sort="new", page=1 ,t="all", **kwargs): - posts = g.db.query(Submission).options(lazyload('*')).filter_by(is_banned=False, private=False,).filter(Submission.deleted_utc == 0) + posts = g.db.query(Submission.id).options(lazyload('*')).filter_by(is_banned=False, private=False,).filter(Submission.deleted_utc == 0) if v and v.admin_level == 0: blocking = [x[0] for x in g.db.query( @@ -290,32 +287,22 @@ def changeloglist(v=None, sort="new", page=1 ,t="all", **kwargs): if lt: posts = posts.filter(Submission.created_utc < lt) - if sort == "hot": - posts = sorted(posts.all(), key=lambda x: x.hotscore, reverse=True) - elif sort == "new": - posts = posts.order_by(Submission.created_utc.desc()).all() + if sort == "new": + posts = posts.order_by(Submission.created_utc.desc()) elif sort == "old": - posts = posts.order_by(Submission.created_utc.asc()).all() + posts = posts.order_by(Submission.created_utc.asc()) elif sort == "controversial": - posts = sorted(posts.all(), key=lambda x: x.score_disputed, reverse=True) + posts = posts.order_by(Submission.upvotes * Submission.downvotes) elif sort == "top": - posts = sorted(posts.all(), key=lambda x: x.score, reverse=True) + posts = posts.order_by(Submission.downvotes - Submission.upvotes) elif sort == "bottom": - posts = sorted(posts.all(), key=lambda x: x.score) + posts = posts.order_by(Submission.upvotes - Submission.downvotes) elif sort == "comments": - posts = posts.order_by(Submission.comment_count.desc()).all() - elif sort == "random": - posts = posts.all() - posts = random.sample(posts, k=len(posts)) - else: - abort(400) + posts = posts.order_by(Submission.comment_count.desc()) - firstrange = 25 * (page - 1) - secondrange = firstrange+26 - posts = posts[firstrange:secondrange] + posts = posts.offset(25 * (page - 1)).limit(26).all() - posts = [x.id for x in posts] - return posts + return [x[0] for x in posts] @app.get("/changelog") @auth_desired @@ -366,7 +353,7 @@ def comment_idlist(page=1, v=None, nsfw=False, sort="new", t="all", **kwargs): posts = posts.subquery() - comments = g.db.query(Comment).options(lazyload('*')).filter(Comment.parent_submission.notin_(cc_idlist)) + comments = g.db.query(Comment.id).options(lazyload('*')).filter(Comment.parent_submission.notin_(cc_idlist)) if v and v.admin_level <= 3: blocking = [x[0] for x in g.db.query( @@ -402,21 +389,18 @@ def comment_idlist(page=1, v=None, nsfw=False, sort="new", t="all", **kwargs): comments = comments.filter(Comment.created_utc >= cutoff) if sort == "new": - comments = comments.order_by(Comment.created_utc.desc()).all() + comments = comments.order_by(Comment.created_utc.desc()) elif sort == "old": - comments = comments.order_by(Comment.created_utc.asc()).all() + comments = comments.order_by(Comment.created_utc.asc()) elif sort == "controversial": - comments = sorted(comments.all(), key=lambda x: x.score_disputed, reverse=True) + comments = comments.order_by(Comment.upvotes * Comment.downvotes) elif sort == "top": - comments = sorted(comments.all(), key=lambda x: x.score, reverse=True) + comments = comments.order_by(Comment.downvotes - Comment.upvotes) elif sort == "bottom": - comments = sorted(comments.all(), key=lambda x: x.score) + comments = comments.order_by(Comment.upvotes - Comment.downvotes) - firstrange = 25 * (page - 1) - secondrange = firstrange+26 - comments = comments[firstrange:secondrange] - - return [x.id for x in comments] + comments = comments.offset(25 * (page - 1)).limit(26).all() + return [x[0] for x in comments] @app.get("/comments") @auth_desired diff --git a/files/routes/posts.py b/files/routes/posts.py index 5d77ab788..36b344e50 100644 --- a/files/routes/posts.py +++ b/files/routes/posts.py @@ -147,24 +147,19 @@ def post_id(pid, anything=None, v=None): isouter=True ) - if sort == "top": - comments = sorted(comments.all(), key=lambda x: x[0].score, reverse=True) - elif sort == "bottom": - comments = sorted(comments.all(), key=lambda x: x[0].score) - elif sort == "new": - comments = comments.order_by(Comment.created_utc.desc()).all() + if sort == "new": + comments = comments.order_by(Comment.created_utc.desc()) elif sort == "old": - comments = comments.order_by(Comment.created_utc.asc()).all() + comments = comments.order_by(Comment.created_utc.asc()) elif sort == "controversial": - comments = sorted(comments.all(), key=lambda x: x[0].score_disputed, reverse=True) - elif sort == "random": - c = comments.all() - comments = random.sample(c, k=len(c)) - else: - abort(422) + comments = comments.order_by(Comment.upvotes * Comment.downvotes) + elif sort == "top": + comments = comments.order_by(Comment.downvotes - Comment.upvotes) + elif sort == "bottom": + comments = comments.order_by(Comment.upvotes - Comment.downvotes) output = [] - for c in comments: + for c in comments.all(): comment = c[0] comment.voted = c[1] or 0 comment.is_blocking = c[2] or 0 @@ -177,23 +172,18 @@ def post_id(pid, anything=None, v=None): shadowbanned = [x[0] for x in g.db.query(User.id).options(lazyload('*')).filter(User.shadowbanned == True).all()] comments = g.db.query(Comment).filter(Comment.parent_submission == post.id, Comment.author_id.notin_(shadowbanned)) - if sort == "top": - comments = sorted(comments.all(), key=lambda x: x.score, reverse=True) - elif sort == "bottom": - comments = sorted(comments.all(), key=lambda x: x.score) - elif sort == "new": - comments = comments.order_by(Comment.created_utc.desc()).all() + if sort == "new": + comments = comments.order_by(Comment.created_utc.desc()) elif sort == "old": - comments = comments.order_by(Comment.created_utc.asc()).all() + comments = comments.order_by(Comment.created_utc.asc()) elif sort == "controversial": - comments = sorted(comments.all(), key=lambda x: x.score_disputed, reverse=True) - elif sort == "random": - c = comments.all() - comments = random.sample(c, k=len(c)) - else: - abort(422) + comments = comments.order_by(Comment.upvotes * Comment.downvotes) + elif sort == "top": + comments = comments.order_by(Comment.downvotes - Comment.upvotes) + elif sort == "bottom": + comments = comments.order_by(Comment.upvotes - Comment.downvotes) - post.preloaded_comments = comments + post.preloaded_comments = comments.all() if not v or v.highlightcomments: last_view_utc = session.get(str(post.id)) diff --git a/files/routes/search.py b/files/routes/search.py index 505d0606d..09489c05c 100644 --- a/files/routes/search.py +++ b/files/routes/search.py @@ -59,7 +59,7 @@ def searchposts(v): - posts = g.db.query(Submission).options( + posts = g.db.query(Submission.id).options( lazyload('*') ).join( Submission.submission_aux, @@ -149,30 +149,23 @@ def searchposts(v): ) if sort == "new": - posts = posts.order_by(Submission.created_utc.desc()).all() + posts = posts.order_by(Submission.created_utc.desc()) elif sort == "old": - posts = posts.order_by(Submission.created_utc.asc()).all() + posts = posts.order_by(Submission.created_utc.asc()) elif sort == "controversial": - posts = sorted(posts.all(), key=lambda x: x.score_disputed, reverse=True) + posts = posts.order_by(Submission.upvotes * Submission.downvotes) elif sort == "top": - posts = sorted(posts.all(), key=lambda x: x.score, reverse=True) + posts = posts.order_by(Submission.downvotes - Submission.upvotes) elif sort == "bottom": - posts = sorted(posts.all(), key=lambda x: x.score) + posts = posts.order_by(Submission.upvotes - Submission.downvotes) elif sort == "comments": - posts = posts.order_by(Submission.comment_count.desc()).all() - elif sort == "random": - posts = posts.all() - posts = random.sample(posts, k=len(posts)) - else: - abort(400) + posts = posts.order_by(Submission.comment_count.desc()) + + posts = posts.offset(25 * (page - 1)).limit(26).all() total = len(posts) - firstrange = 25 * (page - 1) - secondrange = firstrange+26 - posts = posts[firstrange:secondrange] - - ids = [x.id for x in posts] + ids = [x[0] for x in posts] diff --git a/files/routes/settings.py b/files/routes/settings.py index df45b32d0..ff8880353 100644 --- a/files/routes/settings.py +++ b/files/routes/settings.py @@ -169,7 +169,7 @@ def settings_profile_post(v): defaultsortingcomments = request.values.get("defaultsortingcomments") if defaultsortingcomments: - if defaultsortingcomments in ["new", "old", "controversial", "top", "bottom", "random"]: + if defaultsortingcomments in ["new", "old", "controversial", "top", "bottom"]: v.defaultsortingcomments = defaultsortingcomments updated = True else: @@ -177,7 +177,7 @@ def settings_profile_post(v): defaultsorting = request.values.get("defaultsorting") if defaultsorting: - if defaultsorting in ["hot", "new", "old", "comments", "controversial", "top", "bottom", "random"]: + if defaultsorting in ["hot", "new", "old", "comments", "controversial", "top", "bottom"]: v.defaultsorting = defaultsorting updated = True else: diff --git a/files/routes/users.py b/files/routes/users.py index 379f83fd1..d97e3ae41 100644 --- a/files/routes/users.py +++ b/files/routes/users.py @@ -576,19 +576,18 @@ def u_username_comments(username, v=None): comments = comments.filter(Comment.created_utc >= cutoff) if sort == "new": - comments = comments.order_by(Comment.created_utc.desc()).all() + comments = comments.order_by(Comment.created_utc.desc()) elif sort == "old": - comments = comments.order_by(Comment.created_utc.asc()).all() + comments = comments.order_by(Comment.created_utc.asc()) elif sort == "controversial": - comments = sorted(comments.all(), key=lambda x: x.score_disputed, reverse=True) + comments = comments.order_by(Comment.upvotes * Comment.downvotes) elif sort == "top": - comments = sorted(comments.all(), key=lambda x: x.score, reverse=True) + comments = comments.order_by(Comment.downvotes - Comment.upvotes) elif sort == "bottom": - comments = sorted(comments.all(), key=lambda x: x.score) + comments = comments.order_by(Comment.upvotes - Comment.downvotes) - firstrange = 25 * (page - 1) - secondrange = firstrange + 26 - ids = [x.id for x in comments[firstrange:secondrange]] + comments = comments.offset(25 * (page - 1)).limit(26).all() + ids = [x.id for x in comments] # we got 26 items just to see if a next page exists next_exists = (len(ids) > 25) diff --git a/files/templates/changelog.html b/files/templates/changelog.html index cdc9ede94..017e338e3 100644 --- a/files/templates/changelog.html +++ b/files/templates/changelog.html @@ -52,7 +52,6 @@ {% if sort != "old" %}Old{% endif %} {% if sort != "controversial" %}Controversial{% endif %} {% if sort != "comments" %}Comments{% endif %} - {% if sort != "random" %}Random{% endif %} diff --git a/files/templates/home.html b/files/templates/home.html index 25425b6fd..47e8adbdc 100644 --- a/files/templates/home.html +++ b/files/templates/home.html @@ -50,7 +50,6 @@ {% if sort != "old" %}Old{% endif %} {% if sort != "controversial" %}Controversial{% endif %} {% if sort != "comments" %}Comments{% endif %} - {% if sort != "random" %}Random{% endif %} diff --git a/files/templates/search.html b/files/templates/search.html index ef25e0874..2620d9fc1 100644 --- a/files/templates/search.html +++ b/files/templates/search.html @@ -39,7 +39,6 @@ {% if sort=="old" %}{% endif %} {% if sort=="controversial" %}{% endif %} {% if sort=="comments" %}{% endif %} - {% if sort=="random" %}{% endif %} {{sort | capitalize}} diff --git a/files/templates/settings_filters.html b/files/templates/settings_filters.html index 90f1a0f02..2aa47c68d 100644 --- a/files/templates/settings_filters.html +++ b/files/templates/settings_filters.html @@ -24,7 +24,7 @@

Change the default sorting for comments.

@@ -43,7 +43,7 @@

Change the default sorting for posts.

diff --git a/files/templates/submission.html b/files/templates/submission.html index b964e7562..0ea060db0 100644 --- a/files/templates/submission.html +++ b/files/templates/submission.html @@ -595,7 +595,6 @@ {% if sort=="new" %}{% endif %} {% if sort=="old" %}{% endif %} {% if sort=="controversial" %}{% endif %} - {% if sort=="random" %}{% endif %} {{sort | capitalize}} {% if linked_comment and p.comment_count >= 2%}