mirror of https://github.com/JohnHammond/CTFd.git
Fix place ordinal calculation (#1067)
* Fix scoreboard place ordinalization in Python 3 * Extract ordinalization code to `CTFd.utils.humanize.numbers.ordinalize`.selenium-screenshot-testing
parent
f2e0b9e8b5
commit
2bdf7b64d6
|
@ -3,6 +3,7 @@ from flask_marshmallow import Marshmallow
|
|||
from sqlalchemy.orm import validates, column_property
|
||||
from sqlalchemy.ext.hybrid import hybrid_property
|
||||
from CTFd.utils.crypto import hash_password
|
||||
from CTFd.utils.humanize.numbers import ordinalize
|
||||
from CTFd.cache import cache
|
||||
import datetime
|
||||
import six
|
||||
|
@ -349,14 +350,11 @@ class Users(db.Model):
|
|||
|
||||
standings = get_user_standings(admin=admin)
|
||||
|
||||
# http://codegolf.stackexchange.com/a/4712
|
||||
try:
|
||||
i = standings.index((self.id,)) + 1
|
||||
n = standings.index((self.id,)) + 1
|
||||
if numeric:
|
||||
return i
|
||||
else:
|
||||
k = i % 10
|
||||
return "%d%s" % (i, "tsnrhtdd"[(i / 10 % 10 != 1) * (k < 4) * k :: 4])
|
||||
return n
|
||||
return ordinalize(n)
|
||||
except ValueError:
|
||||
return None
|
||||
|
||||
|
@ -469,7 +467,7 @@ class Teams(db.Model):
|
|||
score += member.get_score(admin=admin)
|
||||
return score
|
||||
|
||||
def get_place(self, admin=False):
|
||||
def get_place(self, admin=False, numeric=False):
|
||||
"""
|
||||
This method is generally a clone of CTFd.scoreboard.get_standings.
|
||||
The point being that models.py must be self-reliant and have little
|
||||
|
@ -480,11 +478,11 @@ class Teams(db.Model):
|
|||
|
||||
standings = get_team_standings(admin=admin)
|
||||
|
||||
# http://codegolf.stackexchange.com/a/4712
|
||||
try:
|
||||
i = standings.index((self.id,)) + 1
|
||||
k = i % 10
|
||||
return "%d%s" % (i, "tsnrhtdd"[(i / 10 % 10 != 1) * (k < 4) * k :: 4])
|
||||
n = standings.index((self.id,)) + 1
|
||||
if numeric:
|
||||
return n
|
||||
return ordinalize(n)
|
||||
except ValueError:
|
||||
return None
|
||||
|
||||
|
|
|
@ -0,0 +1,6 @@
|
|||
def ordinalize(n):
|
||||
"""
|
||||
http://codegolf.stackexchange.com/a/4712
|
||||
"""
|
||||
k = n % 10
|
||||
return "%d%s" % (n, "tsnrhtdd"[(n // 10 % 10 != 1) * (k < 4) * k :: 4])
|
|
@ -0,0 +1,19 @@
|
|||
from CTFd.utils.humanize.numbers import ordinalize
|
||||
|
||||
|
||||
def test_ordinalize():
|
||||
tests = {
|
||||
1: "1st",
|
||||
2: "2nd",
|
||||
3: "3rd",
|
||||
4: "4th",
|
||||
11: "11th",
|
||||
12: "12th",
|
||||
13: "13th",
|
||||
101: "101st",
|
||||
102: "102nd",
|
||||
103: "103rd",
|
||||
111: "111th",
|
||||
}
|
||||
for t, v in tests.items():
|
||||
assert ordinalize(t) == v
|
Loading…
Reference in New Issue