refactor sorting and time filter

master
Aevann1 2022-07-09 12:32:49 +02:00
parent 3238a78eb1
commit ec3b4357cf
8 changed files with 59 additions and 125 deletions

View File

@ -26,20 +26,6 @@ def normalize_urls_runtime(body, v):
return body 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): class Comment(Base):
__tablename__ = "comments" __tablename__ = "comments"

View File

@ -18,20 +18,6 @@ from .votes import CommentVote
from .polls import * from .polls import *
from flask import g 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): class Submission(Base):
__tablename__ = "submissions" __tablename__ = "submissions"

View File

@ -4,6 +4,7 @@ import pyotp
from files.helpers.discord import remove_user from files.helpers.discord import remove_user
from files.helpers.media import * from files.helpers.media import *
from files.helpers.const import * from files.helpers.const import *
from files.helpers.sorting_and_time import *
from .alts import Alt from .alts import Alt
from .saves import * from .saves import *
from .notifications import Notification from .notifications import Notification
@ -18,7 +19,6 @@ from .mod import *
from .exiles import * from .exiles import *
from .sub_block import * from .sub_block import *
from .sub_subscription import * from .sub_subscription import *
from .submission import sort_posts
from files.__main__ import Base, cache from files.__main__ import Base, cache
from files.helpers.security import * from files.helpers.security import *
from copy import deepcopy from copy import deepcopy
@ -322,20 +322,7 @@ class User(Base):
if not (v and v.admin_level > 1): if not (v and v.admin_level > 1):
posts = posts.filter_by(deleted_utc=0) posts = posts.filter_by(deleted_utc=0)
now = int(time.time()) posts = apply_time_filter(t, posts, Submission)
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 = sort_posts(sort, posts) posts = sort_posts(sort, posts)

View File

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

View File

@ -2,6 +2,7 @@ from files.helpers.wrappers import *
from files.helpers.get import * from files.helpers.get import *
from files.helpers.discord import * from files.helpers.discord import *
from files.helpers.const import * from files.helpers.const import *
from files.helpers.sorting_and_time import *
from files.__main__ import app, cache, limiter from files.__main__ import app, cache, limiter
from files.classes.submission import Submission from files.classes.submission import Submission
from files.helpers.awards import award_timers 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 lt: posts = posts.filter(Submission.created_utc < lt)
if not gt and not lt: if not gt and not lt:
if t == 'all': cutoff = 0 posts = apply_time_filter(t, posts, Submission)
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)
if (ccmode == "true"): if (ccmode == "true"):
posts = posts.filter(Submission.club == 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)) posts = posts.filter(Submission.title.ilike('_changelog%'), Submission.author_id.in_(allowed))
if t != 'all': posts = apply_time_filter(t, posts, Submission)
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 = sort_posts(sort, posts) 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 lt: comments = comments.filter(Comment.created_utc < lt)
if not gt and not lt: if not gt and not lt:
now = int(time.time()) comments = apply_time_filter(t, comments, Comment)
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 = sort_comments(sort, comments) comments = sort_comments(sort, comments)

View File

@ -10,6 +10,7 @@ from files.helpers.regex import *
from files.helpers.slots import * from files.helpers.slots import *
from files.helpers.get import * from files.helpers.get import *
from files.helpers.actions import * from files.helpers.actions import *
from files.helpers.sorting_and_time import *
from files.classes import * from files.classes import *
from flask import * from flask import *
from io import BytesIO from io import BytesIO

View File

@ -4,6 +4,7 @@ from sqlalchemy import *
from flask import * from flask import *
from files.__main__ import app from files.__main__ import app
from files.helpers.regex import * from files.helpers.regex import *
from files.helpers.sorting_and_time import *
search_operator_hole = HOLE_NAME search_operator_hole = HOLE_NAME
@ -112,21 +113,7 @@ def searchposts(v):
if search_operator_hole in criteria: if search_operator_hole in criteria:
posts = posts.filter(Submission.sub == criteria[search_operator_hole]) posts = posts.filter(Submission.sub == criteria[search_operator_hole])
if t: posts = apply_time_filter(t, posts, Submission)
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 = sort_posts(sort, posts) posts = sort_posts(sort, posts)
@ -203,22 +190,7 @@ def searchcomments(v):
if search_operator_hole in criteria: if search_operator_hole in criteria:
comments = comments.filter(Submission.sub == criteria[search_operator_hole]) comments = comments.filter(Submission.sub == criteria[search_operator_hole])
if t: comments = apply_time_filter(t, comments, Comment)
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)
if v.admin_level < 2: if v.admin_level < 2:
private = [x[0] for x in g.db.query(Submission.id).filter(Submission.private == True).all()] private = [x[0] for x in g.db.query(Submission.id).filter(Submission.private == True).all()]

View File

@ -6,6 +6,7 @@ from files.classes.views import ViewerRelationship
from files.helpers.alerts import * from files.helpers.alerts import *
from files.helpers.sanitize import * from files.helpers.sanitize import *
from files.helpers.const import * from files.helpers.const import *
from files.helpers.sorting_and_time import *
from files.mail import * from files.mail import *
from flask import * from flask import *
from files.__main__ import app, limiter, db_session 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): if not (v and v.admin_level > 1):
comments = comments.filter_by(deleted_utc=0) comments = comments.filter_by(deleted_utc=0)
now = int(time.time()) comments = apply_time_filter(t, comments, Comment)
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 = sort_comments(sort, comments) comments = sort_comments(sort, comments)