Skip to content

Commit

Permalink
Sort classes in class filter by date/time (#355)
Browse files Browse the repository at this point in the history
  • Loading branch information
geordi committed Oct 4, 2023
1 parent 2fd2b07 commit 9efee23
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
12 changes: 10 additions & 2 deletions api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
from shutil import copytree, ignore_patterns
from django.utils.dateparse import parse_datetime


logger = logging.getLogger(__name__)


Expand All @@ -51,20 +50,29 @@ def tasks_list(request):
})
return JsonResponse({'tasks': result})


@user_passes_test(is_teacher)
def all_classes(request):
# https://stackoverflow.com/questions/13844158/sort-week-day-texts
# {'MO': 0, 'TU': 1, ...}
day_mapper = {name:val for val, name in enumerate(Class.Day)}
conds = {}

if not request.user.is_superuser:
conds['teacher_id'] = request.user.id

semesters = defaultdict(lambda: defaultdict(lambda: defaultdict(list)))
for cl in Class.objects.filter(**conds).select_related('teacher', 'subject', 'semester'):
classes = Class.objects.filter(**conds).select_related('teacher', 'subject', 'semester')

classes = sorted(classes, key=lambda klass: (day_mapper[klass.day], klass.time))

for cl in classes:
sem = str(cl.semester)
semesters[sem][cl.subject.abbr][cl.teacher.username].append(cl.code)

return JsonResponse({'semesters': semesters})


@user_passes_test(is_teacher)
def class_detail_list(request):
class_conditions = {}
Expand Down
3 changes: 2 additions & 1 deletion frontend/src/ClassFilter.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,8 @@
{/if}
<select class="form-select form-select-sm" bind:value={clazz} disabled={!(semester && subject)}>
<option value="">Class</option>
{#each sorted(classes) as clazz (clazz)}
<!-- `classes` are sorted serverside -->
{#each classes as clazz (clazz)}
<option>{clazz}</option>
{/each}
</select>
Expand Down

0 comments on commit 9efee23

Please sign in to comment.