Sorting: add comments sort for comments and constantify sorts
This commit is contained in:
parent
0af172d173
commit
5f73302b28
12 changed files with 79 additions and 247 deletions
|
@ -1,10 +1,15 @@
|
|||
import time
|
||||
from typing import Union
|
||||
|
||||
from sqlalchemy.sql import func
|
||||
from sqlalchemy.orm import Query
|
||||
|
||||
from files.classes.comment import Comment
|
||||
from files.classes.submission import Submission
|
||||
from files.helpers.const import *
|
||||
|
||||
|
||||
|
||||
def apply_time_filter(objects, t, cls):
|
||||
now = int(time.time())
|
||||
if t == 'hour':
|
||||
|
@ -22,35 +27,37 @@ def apply_time_filter(objects, t, cls):
|
|||
return objects.filter(cls.created_utc >= cutoff)
|
||||
|
||||
|
||||
def sort_objects(objects, sort, cls):
|
||||
def sort_objects(objects: Query, sort: str, cls: type[Union[Comment, Submission]]):
|
||||
if sort == 'hot':
|
||||
ti = int(time.time()) + 3600
|
||||
return objects.order_by(
|
||||
ordered = objects.order_by(
|
||||
-100000
|
||||
* (cls.upvotes + 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())
|
||||
/ (func.power((ti - cls.created_utc) / 1000, 1.23)))
|
||||
elif sort == 'bump' and cls is Submission:
|
||||
ordered = objects.filter(cls.comment_count > 1).order_by(cls.bump_utc.desc())
|
||||
elif sort == 'comments':
|
||||
if cls is Submission:
|
||||
ordered = objects.order_by(cls.comment_count.desc())
|
||||
elif cls is Comment:
|
||||
ordered = objects.order_by(cls.descendant_count.desc())
|
||||
else:
|
||||
ordered = objects
|
||||
elif sort == 'controversial':
|
||||
return objects.order_by(
|
||||
ordered = objects.order_by(
|
||||
(cls.upvotes + 1) / (cls.downvotes + 1)
|
||||
+ (cls.downvotes + 1) / (cls.upvotes + 1),
|
||||
cls.downvotes.desc(), cls.created_utc.desc())
|
||||
cls.downvotes.desc())
|
||||
elif sort == 'top':
|
||||
return objects.order_by(
|
||||
cls.downvotes - cls.upvotes, cls.created_utc.desc())
|
||||
ordered = objects.order_by(cls.downvotes - cls.upvotes)
|
||||
elif sort == 'bottom':
|
||||
return objects.order_by(
|
||||
cls.upvotes - cls.downvotes, cls.created_utc.desc())
|
||||
ordered = objects.order_by(cls.upvotes - cls.downvotes)
|
||||
elif sort == 'old':
|
||||
return objects.order_by(cls.created_utc)
|
||||
else: # default, or sort == 'new'
|
||||
return objects.order_by(cls.created_utc.desc())
|
||||
else:
|
||||
ordered = objects
|
||||
ordered = ordered.order_by(cls.created_utc.desc())
|
||||
return ordered
|
||||
|
||||
|
||||
# Presently designed around files.helpers.get.get_comment_trees_eager
|
||||
|
@ -65,6 +72,8 @@ def sort_comment_results(comments, sort):
|
|||
/ (pow(((ti - c.created_utc) / 1000), 1.23)),
|
||||
DESC - c.created_utc
|
||||
)
|
||||
elif sort == 'comments':
|
||||
key_func = lambda c: DESC - c.descendant_count
|
||||
elif sort == 'controversial':
|
||||
key_func = lambda c: (
|
||||
(c.upvotes + 1) / (c.downvotes + 1)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue