From ec3b4357cf639fe2efddcfaeff16e7188d4189bf Mon Sep 17 00:00:00 2001 From: Aevann1 Date: Sat, 9 Jul 2022 12:32:49 +0200 Subject: [PATCH] refactor sorting and time filter --- files/classes/comment.py | 14 --------- files/classes/submission.py | 14 --------- files/classes/user.py | 17 ++--------- files/helpers/sorting_and_time.py | 47 +++++++++++++++++++++++++++++++ files/routes/front.py | 41 +++------------------------ files/routes/posts.py | 1 + files/routes/search.py | 34 ++-------------------- files/routes/users.py | 16 ++--------- 8 files changed, 59 insertions(+), 125 deletions(-) create mode 100644 files/helpers/sorting_and_time.py diff --git a/files/classes/comment.py b/files/classes/comment.py index 0deb50740..66f246835 100644 --- a/files/classes/comment.py +++ b/files/classes/comment.py @@ -26,20 +26,6 @@ def normalize_urls_runtime(body, v): return body -def sort_comments(sort, comments): - - if sort == 'new': - return comments.order_by(Comment.id.desc()) - elif sort == 'old': - return comments.order_by(Comment.id) - elif sort == 'controversial': - return comments.order_by((Comment.upvotes+1)/(Comment.downvotes+1) + (Comment.downvotes+1)/(Comment.upvotes+1), Comment.downvotes.desc(), Comment.id.desc()) - elif sort == "bottom": - return comments.order_by(Comment.upvotes - Comment.downvotes) - else: - return comments.order_by(Comment.realupvotes.desc(), Comment.id.desc()) - - class Comment(Base): __tablename__ = "comments" diff --git a/files/classes/submission.py b/files/classes/submission.py index d97125811..59ed0a668 100644 --- a/files/classes/submission.py +++ b/files/classes/submission.py @@ -18,20 +18,6 @@ from .votes import CommentVote from .polls import * from flask import g -def sort_posts(sort, posts): - if sort == "new": - return posts.order_by(Submission.created_utc.desc()) - elif sort == "old": - return posts.order_by(Submission.created_utc) - elif sort == "controversial": - return posts.order_by((Submission.upvotes+1)/(Submission.downvotes+1) + (Submission.downvotes+1)/(Submission.upvotes+1), Submission.downvotes.desc(), Submission.created_utc.desc()) - elif sort == "bottom": - return posts.order_by(Submission.upvotes - Submission.downvotes, Submission.created_utc.desc()) - elif sort == "comments": - return posts.order_by(Submission.comment_count.desc(), Submission.created_utc.desc()) - else: - return posts.order_by(Submission.downvotes - Submission.upvotes, Submission.created_utc.desc()) - class Submission(Base): __tablename__ = "submissions" diff --git a/files/classes/user.py b/files/classes/user.py index 398aa51c0..f86d7131b 100644 --- a/files/classes/user.py +++ b/files/classes/user.py @@ -4,6 +4,7 @@ import pyotp from files.helpers.discord import remove_user from files.helpers.media import * from files.helpers.const import * +from files.helpers.sorting_and_time import * from .alts import Alt from .saves import * from .notifications import Notification @@ -18,7 +19,6 @@ from .mod import * from .exiles import * from .sub_block import * from .sub_subscription import * -from .submission import sort_posts from files.__main__ import Base, cache from files.helpers.security import * from copy import deepcopy @@ -322,20 +322,7 @@ class User(Base): if not (v and v.admin_level > 1): posts = posts.filter_by(deleted_utc=0) - now = int(time.time()) - if t == 'hour': - cutoff = now - 3600 - elif t == 'day': - cutoff = now - 86400 - elif t == 'week': - cutoff = now - 604800 - elif t == 'month': - cutoff = now - 2592000 - elif t == 'year': - cutoff = now - 31536000 - else: - cutoff = 0 - posts = posts.filter(Submission.created_utc >= cutoff) + posts = apply_time_filter(t, posts, Submission) posts = sort_posts(sort, posts) diff --git a/files/helpers/sorting_and_time.py b/files/helpers/sorting_and_time.py new file mode 100644 index 000000000..1e231ac30 --- /dev/null +++ b/files/helpers/sorting_and_time.py @@ -0,0 +1,47 @@ +import time +from files.classes.comment import Comment +from files.classes.submission import Submission + +def apply_time_filter(t, objects, Class): + now = int(time.time()) + if t == 'hour': + cutoff = now - 3600 + elif t == 'day': + cutoff = now - 86400 + elif t == 'week': + cutoff = now - 604800 + elif t == 'month': + cutoff = now - 2592000 + elif t == 'year': + cutoff = now - 31536000 + else: + cutoff = 0 + + return objects.filter(Class.created_utc >= cutoff) + +def sort_comments(sort, comments): + + if sort == 'new': + return comments.order_by(Comment.id.desc()) + elif sort == 'old': + return comments.order_by(Comment.id) + elif sort == 'controversial': + return comments.order_by((Comment.upvotes+1)/(Comment.downvotes+1) + (Comment.downvotes+1)/(Comment.upvotes+1), Comment.downvotes.desc(), Comment.id.desc()) + elif sort == "bottom": + return comments.order_by(Comment.upvotes - Comment.downvotes) + else: + return comments.order_by(Comment.realupvotes.desc(), Comment.id.desc()) + +def sort_posts(sort, posts): + if sort == "new": + return posts.order_by(Submission.created_utc.desc()) + elif sort == "old": + return posts.order_by(Submission.created_utc) + elif sort == "controversial": + return posts.order_by((Submission.upvotes+1)/(Submission.downvotes+1) + (Submission.downvotes+1)/(Submission.upvotes+1), Submission.downvotes.desc(), Submission.created_utc.desc()) + elif sort == "bottom": + return posts.order_by(Submission.upvotes - Submission.downvotes, Submission.created_utc.desc()) + elif sort == "comments": + return posts.order_by(Submission.comment_count.desc(), Submission.created_utc.desc()) + else: + return posts.order_by(Submission.downvotes - Submission.upvotes, Submission.created_utc.desc()) \ No newline at end of file diff --git a/files/routes/front.py b/files/routes/front.py index 614775422..957dc8eb0 100644 --- a/files/routes/front.py +++ b/files/routes/front.py @@ -2,6 +2,7 @@ from files.helpers.wrappers import * from files.helpers.get import * from files.helpers.discord import * from files.helpers.const import * +from files.helpers.sorting_and_time import * from files.__main__ import app, cache, limiter from files.classes.submission import Submission from files.helpers.awards import award_timers @@ -89,15 +90,7 @@ def frontlist(v=None, sort="hot", page=1, t="all", ids_only=True, ccmode="false" if lt: posts = posts.filter(Submission.created_utc < lt) if not gt and not lt: - if t == 'all': cutoff = 0 - else: - now = int(time.time()) - if t == 'hour': cutoff = now - 3600 - elif t == 'week': cutoff = now - 604800 - elif t == 'month': cutoff = now - 2592000 - elif t == 'year': cutoff = now - 31536000 - else: cutoff = now - 86400 - posts = posts.filter(Submission.created_utc >= cutoff) + posts = apply_time_filter(t, posts, Submission) if (ccmode == "true"): posts = posts.filter(Submission.club == True) @@ -205,20 +198,7 @@ def changeloglist(v=None, sort="new", page=1, t="all", site=None): posts = posts.filter(Submission.title.ilike('_changelog%'), Submission.author_id.in_(allowed)) - if t != 'all': - cutoff = 0 - now = int(time.time()) - if t == 'hour': - cutoff = now - 3600 - elif t == 'day': - cutoff = now - 86400 - elif t == 'week': - cutoff = now - 604800 - elif t == 'month': - cutoff = now - 2592000 - elif t == 'year': - cutoff = now - 31536000 - posts = posts.filter(Submission.created_utc >= cutoff) + posts = apply_time_filter(t, posts, Submission) posts = sort_posts(sort, posts) @@ -308,20 +288,7 @@ def comment_idlist(page=1, v=None, nsfw=False, sort="new", t="all", gt=0, lt=0, if lt: comments = comments.filter(Comment.created_utc < lt) if not gt and not lt: - now = int(time.time()) - if t == 'hour': - cutoff = now - 3600 - elif t == 'day': - cutoff = now - 86400 - elif t == 'week': - cutoff = now - 604800 - elif t == 'month': - cutoff = now - 2592000 - elif t == 'year': - cutoff = now - 31536000 - else: - cutoff = 0 - comments = comments.filter(Comment.created_utc >= cutoff) + comments = apply_time_filter(t, comments, Comment) comments = sort_comments(sort, comments) diff --git a/files/routes/posts.py b/files/routes/posts.py index 9e31bab07..2d6b265f5 100644 --- a/files/routes/posts.py +++ b/files/routes/posts.py @@ -10,6 +10,7 @@ from files.helpers.regex import * from files.helpers.slots import * from files.helpers.get import * from files.helpers.actions import * +from files.helpers.sorting_and_time import * from files.classes import * from flask import * from io import BytesIO diff --git a/files/routes/search.py b/files/routes/search.py index cbbdb47b8..ca54b7827 100644 --- a/files/routes/search.py +++ b/files/routes/search.py @@ -4,6 +4,7 @@ from sqlalchemy import * from flask import * from files.__main__ import app from files.helpers.regex import * +from files.helpers.sorting_and_time import * search_operator_hole = HOLE_NAME @@ -112,21 +113,7 @@ def searchposts(v): if search_operator_hole in criteria: posts = posts.filter(Submission.sub == criteria[search_operator_hole]) - if t: - now = int(time.time()) - if t == 'hour': - cutoff = now - 3600 - elif t == 'day': - cutoff = now - 86400 - elif t == 'week': - cutoff = now - 604800 - elif t == 'month': - cutoff = now - 2592000 - elif t == 'year': - cutoff = now - 31536000 - else: - cutoff = 0 - posts = posts.filter(Submission.created_utc >= cutoff) + posts = apply_time_filter(t, posts, Submission) posts = sort_posts(sort, posts) @@ -203,22 +190,7 @@ def searchcomments(v): if search_operator_hole in criteria: comments = comments.filter(Submission.sub == criteria[search_operator_hole]) - if t: - now = int(time.time()) - if t == 'hour': - cutoff = now - 3600 - elif t == 'day': - cutoff = now - 86400 - elif t == 'week': - cutoff = now - 604800 - elif t == 'month': - cutoff = now - 2592000 - elif t == 'year': - cutoff = now - 31536000 - else: - cutoff = 0 - comments = comments.filter(Comment.created_utc >= cutoff) - + comments = apply_time_filter(t, comments, Comment) if v.admin_level < 2: private = [x[0] for x in g.db.query(Submission.id).filter(Submission.private == True).all()] diff --git a/files/routes/users.py b/files/routes/users.py index 65cc0bee3..4fd87b4bb 100644 --- a/files/routes/users.py +++ b/files/routes/users.py @@ -6,6 +6,7 @@ from files.classes.views import ViewerRelationship from files.helpers.alerts import * from files.helpers.sanitize import * from files.helpers.const import * +from files.helpers.sorting_and_time import * from files.mail import * from flask import * from files.__main__ import app, limiter, db_session @@ -1015,20 +1016,7 @@ def u_username_comments(username, v=None): if not (v and v.admin_level > 1): comments = comments.filter_by(deleted_utc=0) - now = int(time.time()) - if t == 'hour': - cutoff = now - 3600 - elif t == 'day': - cutoff = now - 86400 - elif t == 'week': - cutoff = now - 604800 - elif t == 'month': - cutoff = now - 2592000 - elif t == 'year': - cutoff = now - 31536000 - else: - cutoff = 0 - comments = comments.filter(Comment.created_utc >= cutoff) + comments = apply_time_filter(t, comments, Comment) comments = sort_comments(sort, comments)