Clarify architecture of sort_objects.
parent
d18e247513
commit
166e33dc38
|
@ -10,6 +10,7 @@ from files.classes.votes import CommentVote
|
|||
from files.helpers.const import *
|
||||
from files.helpers.regex import *
|
||||
from files.helpers.lazy import lazy
|
||||
from files.helpers.sorting_and_time import *
|
||||
from .flags import CommentFlag
|
||||
from .votes import CommentVote
|
||||
from .saves import CommentSaveRelationship
|
||||
|
@ -17,31 +18,6 @@ from random import randint
|
|||
from math import floor
|
||||
|
||||
|
||||
def sort_objects(sort, objects, Class, v):
|
||||
if not (v and v.can_see_shadowbanned):
|
||||
objects = objects.join(Class.author).filter(User.shadowbanned == None)
|
||||
|
||||
if sort == 'hot':
|
||||
ti = int(time.time()) + 3600
|
||||
if SITE_NAME == 'rDrama': metric = Class.realupvotes
|
||||
else: metric = Class.upvotes - Class.downvotes
|
||||
if Class.__name__ == "Submission": metric += Class.comment_count/5
|
||||
return objects.order_by(-1000000*(metric + 1)/(func.power(((ti - Class.created_utc)/1000), 1.23)), Class.created_utc.desc())
|
||||
elif sort == "bump" and Class.__name__ == "Submission":
|
||||
return objects.filter(Class.comment_count > 1).order_by(Class.bump_utc.desc(), Class.created_utc.desc())
|
||||
elif sort == "comments" and Class.__name__ == "Submission":
|
||||
return objects.order_by(Class.comment_count.desc(), Class.created_utc.desc())
|
||||
elif sort == "new":
|
||||
return objects.order_by(Class.created_utc.desc())
|
||||
elif sort == "old":
|
||||
return objects.order_by(Class.created_utc)
|
||||
elif sort == "controversial":
|
||||
return objects.order_by((Class.upvotes+1)/(Class.downvotes+1) + (Class.downvotes+1)/(Class.upvotes+1), Class.downvotes.desc(), Class.created_utc.desc())
|
||||
elif sort == "bottom":
|
||||
return objects.order_by(Class.upvotes - Class.downvotes, Class.created_utc.desc())
|
||||
else:
|
||||
return objects.order_by(Class.downvotes - Class.upvotes, Class.created_utc.desc())
|
||||
|
||||
def normalize_urls_runtime(body, v):
|
||||
if not v: return body
|
||||
|
||||
|
@ -238,7 +214,8 @@ class Comment(Base):
|
|||
|
||||
if not self.parent_submission: sort='old'
|
||||
|
||||
return sort_objects(sort, replies, Comment, v)
|
||||
return sort_objects(sort, replies, Comment,
|
||||
include_shadowbanned=(not (v and v.can_see_shadowbanned)))
|
||||
|
||||
|
||||
@property
|
||||
|
|
|
@ -7,7 +7,6 @@ from files.helpers.media import *
|
|||
from files.helpers.const import *
|
||||
from files.classes.casino_game import Casino_Game
|
||||
from files.helpers.sorting_and_time import *
|
||||
from files.classes.comment import sort_objects
|
||||
from .alts import Alt
|
||||
from .saves import *
|
||||
from .notifications import Notification
|
||||
|
@ -479,7 +478,8 @@ class User(Base):
|
|||
|
||||
posts = apply_time_filter(t, posts, Submission)
|
||||
|
||||
posts = sort_objects(sort, posts, Submission, v)
|
||||
posts = sort_objects(sort, posts, Submission,
|
||||
include_shadowbanned=(not (v and v.can_see_shadowbanned)))
|
||||
|
||||
posts = posts.offset(25 * (page - 1)).limit(26).all()
|
||||
|
||||
|
|
|
@ -1,10 +1,8 @@
|
|||
import time
|
||||
from files.classes.comment import Comment
|
||||
from files.classes.submission import Submission
|
||||
from files.helpers.const import *
|
||||
from sqlalchemy.sql import func
|
||||
|
||||
def apply_time_filter(t, objects, Class):
|
||||
def apply_time_filter(t, objects, cls):
|
||||
now = int(time.time())
|
||||
if t == 'hour':
|
||||
cutoff = now - 3600
|
||||
|
@ -19,4 +17,30 @@ def apply_time_filter(t, objects, Class):
|
|||
else:
|
||||
cutoff = 0
|
||||
|
||||
return objects.filter(Class.created_utc >= cutoff)
|
||||
return objects.filter(cls.created_utc >= cutoff)
|
||||
|
||||
def sort_objects(sort, objects, cls, include_shadowbanned=False):
|
||||
if not include_shadowbanned:
|
||||
cls_user = cls.__mapper__.relationships['author'].entity.entity
|
||||
objects = objects.join(cls.author).filter(cls_user.shadowbanned == None)
|
||||
|
||||
if sort == 'hot':
|
||||
ti = int(time.time()) + 3600
|
||||
if SITE_NAME == 'rDrama': metric = cls.realupvotes
|
||||
else: metric = cls.upvotes - cls.downvotes
|
||||
if cls.__name__ == "Submission": metric += cls.comment_count/5
|
||||
return objects.order_by(-1000000*(metric + 1)/(func.power(((ti - cls.created_utc)/1000), 1.23)), cls.created_utc.desc())
|
||||
elif sort == "bump" and cls.__name__ == "Submission":
|
||||
return objects.filter(cls.comment_count > 1).order_by(cls.bump_utc.desc(), cls.created_utc.desc())
|
||||
elif sort == "comments" and cls.__name__ == "Submission":
|
||||
return objects.order_by(cls.comment_count.desc(), cls.created_utc.desc())
|
||||
elif sort == "new":
|
||||
return objects.order_by(cls.created_utc.desc())
|
||||
elif sort == "old":
|
||||
return objects.order_by(cls.created_utc)
|
||||
elif sort == "controversial":
|
||||
return objects.order_by((cls.upvotes+1)/(cls.downvotes+1) + (cls.downvotes+1)/(cls.upvotes+1), cls.downvotes.desc(), cls.created_utc.desc())
|
||||
elif sort == "bottom":
|
||||
return objects.order_by(cls.upvotes - cls.downvotes, cls.created_utc.desc())
|
||||
else:
|
||||
return objects.order_by(cls.downvotes - cls.upvotes, cls.created_utc.desc())
|
||||
|
|
|
@ -3,7 +3,6 @@ from files.helpers.get import *
|
|||
from files.helpers.discord import *
|
||||
from files.helpers.const import *
|
||||
from files.helpers.sorting_and_time import *
|
||||
from files.classes.comment import sort_objects
|
||||
from files.__main__ import app, cache, limiter
|
||||
from files.classes.submission import Submission
|
||||
from files.helpers.awards import award_timers
|
||||
|
@ -116,7 +115,8 @@ def frontlist(v=None, sort="hot", page=1, t="all", ids_only=True, ccmode="false"
|
|||
if not (v and v.shadowbanned):
|
||||
posts = posts.join(Submission.author).filter(User.shadowbanned == None)
|
||||
|
||||
posts = sort_objects(sort, posts, Submission, v)
|
||||
posts = sort_objects(sort, posts, Submission,
|
||||
include_shadowbanned=(not (v and v.can_see_shadowbanned)))
|
||||
|
||||
if v: size = v.frontsize or 0
|
||||
else: size = 25
|
||||
|
@ -236,7 +236,8 @@ def comment_idlist(page=1, v=None, nsfw=False, sort="new", t="all", gt=0, lt=0,
|
|||
if not gt and not lt:
|
||||
comments = apply_time_filter(t, comments, Comment)
|
||||
|
||||
comments = sort_objects(sort, comments, Comment, v)
|
||||
comments = sort_objects(sort, comments, Comment,
|
||||
include_shadowbanned=(not (v and v.can_see_shadowbanned)))
|
||||
|
||||
comments = comments.offset(25 * (page - 1)).limit(26).all()
|
||||
return [x[0] for x in comments]
|
||||
|
|
|
@ -11,7 +11,6 @@ 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.comment import sort_objects
|
||||
from files.classes import *
|
||||
from flask import *
|
||||
from io import BytesIO
|
||||
|
@ -197,7 +196,8 @@ def post_id(pid, anything=None, v=None, sub=None):
|
|||
|
||||
comments = comments.filter(Comment.level == 1, Comment.stickied == None)
|
||||
|
||||
comments = sort_objects(sort, comments, Comment, v)
|
||||
comments = sort_objects(sort, comments, Comment,
|
||||
include_shadowbanned=(not (v and v.can_see_shadowbanned)))
|
||||
|
||||
comments = [c[0] for c in comments.all()]
|
||||
else:
|
||||
|
@ -205,7 +205,8 @@ def post_id(pid, anything=None, v=None, sub=None):
|
|||
|
||||
comments = g.db.query(Comment).join(Comment.author).filter(User.shadowbanned == None, Comment.parent_submission == post.id, Comment.level == 1, Comment.stickied == None)
|
||||
|
||||
comments = sort_objects(sort, comments, Comment, v)
|
||||
comments = sort_objects(sort, comments, Comment,
|
||||
include_shadowbanned=(not (v and v.can_see_shadowbanned)))
|
||||
|
||||
comments = comments.all()
|
||||
|
||||
|
@ -317,13 +318,15 @@ def viewmore(v, pid, sort, offset):
|
|||
|
||||
comments = comments.filter(Comment.level == 1)
|
||||
|
||||
comments = sort_objects(sort, comments, Comment, v)
|
||||
comments = sort_objects(sort, comments, Comment,
|
||||
include_shadowbanned=(not (v and v.can_see_shadowbanned)))
|
||||
|
||||
comments = [c[0] for c in comments.all()]
|
||||
else:
|
||||
comments = g.db.query(Comment).join(Comment.author).filter(User.shadowbanned == None, Comment.parent_submission == pid, Comment.level == 1, Comment.stickied == None, Comment.id.notin_(ids))
|
||||
|
||||
comments = sort_objects(sort, comments, Comment, v)
|
||||
comments = sort_objects(sort, comments, Comment,
|
||||
include_shadowbanned=(not (v and v.can_see_shadowbanned)))
|
||||
|
||||
comments = comments.offset(offset).all()
|
||||
|
||||
|
|
|
@ -5,7 +5,6 @@ from flask import *
|
|||
from files.__main__ import app
|
||||
from files.helpers.regex import *
|
||||
from files.helpers.sorting_and_time import *
|
||||
from files.classes.comment import sort_objects
|
||||
import time
|
||||
from calendar import timegm
|
||||
|
||||
|
@ -146,7 +145,8 @@ def searchposts(v):
|
|||
|
||||
posts = apply_time_filter(t, posts, Submission)
|
||||
|
||||
posts = sort_objects(sort, posts, Submission, v)
|
||||
posts = sort_objects(sort, posts, Submission,
|
||||
include_shadowbanned=(not (v and v.can_see_shadowbanned)))
|
||||
|
||||
total = posts.count()
|
||||
|
||||
|
@ -249,7 +249,8 @@ def searchcomments(v):
|
|||
except: abort(400)
|
||||
comments = comments.filter(Comment.created_utc < before)
|
||||
|
||||
comments = sort_objects(sort, comments, Comment, v)
|
||||
comments = sort_objects(sort, comments, Comment,
|
||||
include_shadowbanned=(not (v and v.can_see_shadowbanned)))
|
||||
|
||||
total = comments.count()
|
||||
|
||||
|
|
|
@ -8,7 +8,6 @@ from files.helpers.alerts import *
|
|||
from files.helpers.sanitize import *
|
||||
from files.helpers.const import *
|
||||
from files.helpers.sorting_and_time import *
|
||||
from files.classes.comment import sort_objects
|
||||
from files.mail import *
|
||||
from flask import *
|
||||
from files.__main__ import app, limiter, db_session
|
||||
|
@ -910,7 +909,8 @@ def u_username_comments(username, v=None):
|
|||
|
||||
comments = apply_time_filter(t, comments, Comment)
|
||||
|
||||
comments = sort_objects(sort, comments, Comment, v)
|
||||
comments = sort_objects(sort, comments, Comment,
|
||||
include_shadowbanned=(not (v and v.can_see_shadowbanned)))
|
||||
|
||||
comments = comments.offset(25 * (page - 1)).limit(26).all()
|
||||
ids = [x.id for x in comments]
|
||||
|
|
Loading…
Reference in New Issue