Skip to content

Commit 14597f8

Browse files
authored
Merge pull request #65 from mostafam99/feature/exam-ordering
exam chronological ordering
2 parents 9cc8490 + 3008885 commit 14597f8

File tree

1 file changed

+38
-2
lines changed

1 file changed

+38
-2
lines changed

backend/answers/models.py

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from django.contrib.postgres.indexes import GinIndex
77
from util.models import CommentMixin
88
from django_prometheus.models import ExportModelOperationsMixin
9+
import datetime
910

1011
import random
1112

@@ -52,15 +53,50 @@ def attachment_name(self):
5253
" ", "_"
5354
)
5455

55-
def sort_key(self):
56+
def sort_key_number(self):
57+
# original sort key function
5658
end = 0
5759
while (
5860
end + 1 < len(self.displayname) and self.displayname[-end - 1 :].isdigit()
5961
):
6062
end += 1
6163
if end == 0:
6264
return 0, self.displayname
63-
return int(self.displayname[-end:]), self.displayname
65+
return int(self.displayname[-end:])
66+
67+
def try_parse_exam_date(self):
68+
exam_name = self.displayname
69+
parts_of_name = exam_name.strip().split()
70+
month = None
71+
year = None
72+
for part in parts_of_name:
73+
try:
74+
month = datetime.datetime.strptime(part, "%B")
75+
except ValueError:
76+
pass
77+
try:
78+
year = datetime.datetime.strptime(part, "%Y")
79+
except ValueError:
80+
pass
81+
if month and year:
82+
break
83+
if not(year):
84+
return datetime.datetime(1984, 1, 1)
85+
# in a one or two courses, there are 'Exams' with no year and no month. this puts them at the end
86+
# i haven't seen an exam with just a month.
87+
if year and month: return datetime.datetime(year.year, month.month, 1)
88+
if year and not(month): return year
89+
90+
def sort_key(self):
91+
if self.exam_type.displayname in ["Exams", "Mock Exams"]:
92+
try:
93+
val = datetime.datetime.strptime(self.displayname.strip(), "%B %Y")
94+
except ValueError:
95+
val = self.try_parse_exam_date()
96+
else:
97+
val = self.sort_key_number()
98+
99+
return val, self.displayname
64100

65101
def count_answered(self):
66102
return self.answersection_set.filter(

0 commit comments

Comments
 (0)