refactor sorting posts
parent
8ecb4582d0
commit
10a47ca837
|
@ -15,6 +15,23 @@ from flask import g
|
|||
from .sub import *
|
||||
from .votes import CommentVote
|
||||
|
||||
|
||||
def sort_posts(sort, posts):
|
||||
if sort == "new":
|
||||
order = Submission.created_utc.desc()
|
||||
elif sort == "old":
|
||||
order = Submission.created_utc
|
||||
elif sort == "controversial":
|
||||
order = (Submission.upvotes+1)/(Submission.downvotes+1) + (Submission.downvotes+1)/(Submission.upvotes+1), Submission.downvotes.desc()
|
||||
elif sort == "bottom":
|
||||
order = Submission.realupvotes()
|
||||
elif sort == "comments":
|
||||
order = Submission.comment_count.desc()
|
||||
else:
|
||||
order = Submission.realupvotes().desc()
|
||||
|
||||
return posts.order_by(order)
|
||||
|
||||
class Submission(Base):
|
||||
__tablename__ = "submissions"
|
||||
|
||||
|
|
|
@ -326,19 +326,8 @@ class User(Base):
|
|||
cutoff = 0
|
||||
posts = posts.filter(Submission.created_utc >= cutoff)
|
||||
|
||||
if sort == "new":
|
||||
posts = posts.order_by(Submission.created_utc.desc())
|
||||
elif sort == "old":
|
||||
posts = posts.order_by(Submission.created_utc)
|
||||
elif sort == "controversial":
|
||||
posts = posts.order_by((Submission.upvotes+1)/(Submission.downvotes+1) + (Submission.downvotes+1)/(Submission.upvotes+1), Submission.downvotes.desc())
|
||||
elif sort == "top":
|
||||
posts = posts.order_by(Submission.downvotes - Submission.upvotes)
|
||||
elif sort == "bottom":
|
||||
posts = posts.order_by(Submission.upvotes - Submission.downvotes)
|
||||
elif sort == "comments":
|
||||
posts = posts.order_by(Submission.comment_count.desc())
|
||||
|
||||
posts = sort_posts(sort, posts)
|
||||
|
||||
posts = posts.offset(25 * (page - 1)).limit(26).all()
|
||||
|
||||
return [x[0] for x in posts]
|
||||
|
|
|
@ -302,18 +302,8 @@ def frontlist(v=None, sort="hot", page=1, t="all", ids_only=True, ccmode="false"
|
|||
posts = posts.order_by(-1000000*(Submission.realupvotes + 1 + Submission.comment_count/num + func.least(50, (func.length(Submission.body_html)-func.length(func.replace(Submission.body_html,'<a href="https://','')))/5))/(func.power(((ti - Submission.created_utc)/1000), 1.23)), Submission.created_utc.desc())
|
||||
elif sort == "bump":
|
||||
posts = posts.filter(Submission.comment_count > 1).order_by(Submission.bump_utc.desc(), Submission.created_utc.desc())
|
||||
elif sort == "new":
|
||||
posts = posts.order_by(Submission.created_utc.desc())
|
||||
elif sort == "old":
|
||||
posts = posts.order_by(Submission.created_utc)
|
||||
elif sort == "controversial":
|
||||
posts = posts.order_by((Submission.upvotes+1)/(Submission.downvotes+1) + (Submission.downvotes+1)/(Submission.upvotes+1), Submission.downvotes.desc(), Submission.created_utc.desc())
|
||||
elif sort == "top":
|
||||
posts = posts.order_by(Submission.downvotes - Submission.upvotes, Submission.created_utc.desc())
|
||||
elif sort == "bottom":
|
||||
posts = posts.order_by(Submission.upvotes - Submission.downvotes, Submission.created_utc.desc())
|
||||
elif sort == "comments":
|
||||
posts = posts.order_by(Submission.comment_count.desc(), Submission.created_utc.desc())
|
||||
else:
|
||||
posts = sort_posts(sort, posts)
|
||||
|
||||
if v: size = v.frontsize or 0
|
||||
else: size = 25
|
||||
|
@ -403,18 +393,7 @@ def changeloglist(v=None, sort="new", page=1, t="all", site=None):
|
|||
cutoff = now - 31536000
|
||||
posts = posts.filter(Submission.created_utc >= cutoff)
|
||||
|
||||
if sort == "new":
|
||||
posts = posts.order_by(Submission.created_utc.desc())
|
||||
elif sort == "old":
|
||||
posts = posts.order_by(Submission.created_utc)
|
||||
elif sort == "controversial":
|
||||
posts = posts.order_by((Submission.upvotes+1)/(Submission.downvotes+1) + (Submission.downvotes+1)/(Submission.upvotes+1), Submission.downvotes.desc())
|
||||
elif sort == "top":
|
||||
posts = posts.order_by(Submission.downvotes - Submission.upvotes)
|
||||
elif sort == "bottom":
|
||||
posts = posts.order_by(Submission.upvotes - Submission.downvotes)
|
||||
elif sort == "comments":
|
||||
posts = posts.order_by(Submission.comment_count.desc())
|
||||
posts = sort_posts(sort, posts)
|
||||
|
||||
posts = posts.offset(25 * (page - 1)).limit(26).all()
|
||||
|
||||
|
|
|
@ -135,18 +135,7 @@ def searchposts(v):
|
|||
cutoff = 0
|
||||
posts = posts.filter(Submission.created_utc >= cutoff)
|
||||
|
||||
if sort == "new":
|
||||
posts = posts.order_by(Submission.created_utc.desc())
|
||||
elif sort == "old":
|
||||
posts = posts.order_by(Submission.created_utc)
|
||||
elif sort == "controversial":
|
||||
posts = posts.order_by((Submission.upvotes+1)/(Submission.downvotes+1) + (Submission.downvotes+1)/(Submission.upvotes+1), Submission.downvotes.desc())
|
||||
elif sort == "top":
|
||||
posts = posts.order_by(Submission.downvotes - Submission.upvotes)
|
||||
elif sort == "bottom":
|
||||
posts = posts.order_by(Submission.upvotes - Submission.downvotes)
|
||||
elif sort == "comments":
|
||||
posts = posts.order_by(Submission.comment_count.desc())
|
||||
posts = sort_posts(sort, posts)
|
||||
|
||||
total = posts.count()
|
||||
|
||||
|
|
Loading…
Reference in New Issue