Make shared top list position ordering consistent
This commit is contained in:
parent
c74257008e
commit
693bff78c2
1 changed files with 14 additions and 14 deletions
28
karma/db.py
28
karma/db.py
|
@ -62,7 +62,7 @@ class Karma:
|
||||||
func.sum(case([(c.value > 0, c.value)], else_=0)).label("positive"),
|
func.sum(case([(c.value > 0, c.value)], else_=0)).label("positive"),
|
||||||
func.abs(func.sum(case([(c.value < 0, c.value)], else_=0))).label("negative")])
|
func.abs(func.sum(case([(c.value < 0, c.value)], else_=0))).label("negative")])
|
||||||
.group_by(c.given_for)
|
.group_by(c.given_for)
|
||||||
.order_by(direction("total"))
|
.order_by(direction("total"), asc(c.given_for))
|
||||||
.limit(limit)))
|
.limit(limit)))
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
|
@ -73,6 +73,18 @@ class Karma:
|
||||||
def get_bottom_users(cls, limit: int = 10) -> Iterable['UserKarmaStats']:
|
def get_bottom_users(cls, limit: int = 10) -> Iterable['UserKarmaStats']:
|
||||||
return cls.get_user_stats(direction=asc, limit=limit)
|
return cls.get_user_stats(direction=asc, limit=limit)
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def get_user_stats(cls, direction, limit: int = 10) -> Iterable['UserKarmaStats']:
|
||||||
|
c = cls.c
|
||||||
|
return (UserKarmaStats(*row) for row in cls.db.execute(
|
||||||
|
select([c.given_to,
|
||||||
|
func.sum(c.value).label("total"),
|
||||||
|
func.sum(case([(c.value > 0, c.value)], else_=0)).label("positive"),
|
||||||
|
func.abs(func.sum(case([(c.value < 0, c.value)], else_=0))).label("negative")])
|
||||||
|
.group_by(c.given_to)
|
||||||
|
.order_by(direction("total"), asc(c.given_to))
|
||||||
|
.limit(limit)))
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def get_karma(cls, user_id: UserID) -> Optional['UserKarmaStats']:
|
def get_karma(cls, user_id: UserID) -> Optional['UserKarmaStats']:
|
||||||
c = cls.c
|
c = cls.c
|
||||||
|
@ -92,24 +104,12 @@ class Karma:
|
||||||
c = cls.c
|
c = cls.c
|
||||||
rows = cls.db.execute(select([c.given_to])
|
rows = cls.db.execute(select([c.given_to])
|
||||||
.group_by(c.given_to)
|
.group_by(c.given_to)
|
||||||
.order_by(desc(func.sum(c.value))))
|
.order_by(desc(func.sum(c.value)), asc(c.given_to)))
|
||||||
for i, row in enumerate(rows):
|
for i, row in enumerate(rows):
|
||||||
if row[0] == user_id:
|
if row[0] == user_id:
|
||||||
return i
|
return i
|
||||||
return -1
|
return -1
|
||||||
|
|
||||||
@classmethod
|
|
||||||
def get_user_stats(cls, direction, limit: int = 10) -> Iterable['UserKarmaStats']:
|
|
||||||
c = cls.c
|
|
||||||
return (UserKarmaStats(*row) for row in cls.db.execute(
|
|
||||||
select([c.given_to,
|
|
||||||
func.sum(c.value).label("total"),
|
|
||||||
func.sum(case([(c.value > 0, c.value)], else_=0)).label("positive"),
|
|
||||||
func.abs(func.sum(case([(c.value < 0, c.value)], else_=0))).label("negative")])
|
|
||||||
.group_by(c.given_to)
|
|
||||||
.order_by(direction("total"))
|
|
||||||
.limit(limit)))
|
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def all(cls, user_id: UserID) -> Iterable['Karma']:
|
def all(cls, user_id: UserID) -> Iterable['Karma']:
|
||||||
return (cls(given_to=given_to, given_by=given_by, given_in=given_in, given_for=given_for,
|
return (cls(given_to=given_to, given_by=given_by, given_in=given_in, given_for=given_for,
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue