|
6 | 6 | from django.contrib.postgres.indexes import GinIndex |
7 | 7 | from util.models import CommentMixin |
8 | 8 | from django_prometheus.models import ExportModelOperationsMixin |
| 9 | +import datetime |
9 | 10 |
|
10 | 11 | import random |
11 | 12 |
|
@@ -52,15 +53,50 @@ def attachment_name(self): |
52 | 53 | " ", "_" |
53 | 54 | ) |
54 | 55 |
|
55 | | - def sort_key(self): |
| 56 | + def sort_key_number(self): |
| 57 | + # original sort key function |
56 | 58 | end = 0 |
57 | 59 | while ( |
58 | 60 | end + 1 < len(self.displayname) and self.displayname[-end - 1 :].isdigit() |
59 | 61 | ): |
60 | 62 | end += 1 |
61 | 63 | if end == 0: |
62 | 64 | 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 |
64 | 100 |
|
65 | 101 | def count_answered(self): |
66 | 102 | return self.answersection_set.filter( |
|
0 commit comments