Skip to content

Commit

Permalink
Select teacher in INBUS classes that lack one
Browse files Browse the repository at this point in the history
This solves a longtime standing issue of importing classes
that have no assigned teacher in INBUS. Importing user
selects a teacher from the provided list.
  • Loading branch information
geordi committed Oct 19, 2024
1 parent 8b5af34 commit f3a6a47
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 9 deletions.
19 changes: 17 additions & 2 deletions api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,10 @@ def subjects_all(request) -> JsonResponse:
@require_GET
def teachers_all(request) -> JsonResponse:
teachers = User.objects.filter(groups__name="teachers")
items = tuple({"username": t.username, "full_name": t.get_full_name(), "last_name": t.last_name} for t in teachers)
items = tuple(
{"username": t.username, "full_name": t.get_full_name(), "last_name": t.last_name}
for t in teachers
)

resp = {"teachers": items}
return JsonResponse(resp)
Expand Down Expand Up @@ -757,6 +760,14 @@ def import_activities(request):
semester_id = post["semester_id"]
subject_abbr = post["subject_abbr"]
activities_id = post["activities"]
activities_to_teacher = post[
"activities_to_teacher"
] # activities_to_teacher: when INBUS doesn't provide one, UI user selects one

activities_to_teacher = {
int(activity_id): teacher_username
for activity_id, teacher_username in activities_to_teacher.items()
}

activities = [inbus.concrete_activity(activity_id) for activity_id in activities_id]
activities = [
Expand All @@ -765,7 +776,11 @@ def import_activities(request):
semester = Semester.objects.get(pk=semester_id)

try:
res["users"] = list(common.bulk_import.run(activities, subject_abbr, semester, request.user))
res["users"] = list(
common.bulk_import.run(
activities, subject_abbr, semester, request.user, activities_to_teacher
)
)
res["count"] = len(res["users"])
except (ImportException, UnicodeDecodeError) as e:
res["error"] = "".join(traceback.TracebackException.from_exception(e).format())
Expand Down
8 changes: 6 additions & 2 deletions common/bulk_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,12 @@ def run(
subject_abbr: str,
semester: Semester,
user: User,
activities_to_teacher: Dict[int, str],
) -> Generator[ImportResult, None, None]:
"""
`subject_addr`: subject abbreviation from selected subject in UI
`user`: importing user (the that uses import UI)
`activities_to_teacher`: dictionary of activities and manually assigned teachers (username) in the UI
"""

try:
Expand Down Expand Up @@ -74,8 +77,9 @@ def run(
f"Cannot create user {ca.teacherLogins.upper()}.\n\nTraceback\n\n{traceback.format_exc()}"
)
else:
# TODO: We assign all activities without teacher to one special user :-)
teacher = User.objects.get(username="GAU01")
# We assign all activities without teacher in INBUS to the one selected by importing user
teacher_username = activities_to_teacher[ca.concreteActivityId]
teacher = User.objects.get(username=teacher_username)

if not is_teacher(teacher):
teachers_group = Group.objects.get_by_natural_key("teachers")
Expand Down
35 changes: 30 additions & 5 deletions frontend/src/Teacher/InbusImport.vue
Original file line number Diff line number Diff line change
Expand Up @@ -45,17 +45,22 @@ const busy = ref<boolean>(false);
const semesters = await loadSemesters();
// Semester ID
const semester = ref(semesters.length > 0 ? semesters[semesters.length - 1].pk : null);
const semester = ref<number | null>(
semesters.length > 0 ? semesters[semesters.length - 1].pk : null
);
const subjects_kelvin = await loadKelvinSubjects();
// Subject abbreviation
const subject_kelvin_selected = ref(subjects_kelvin.length > 0 ? subjects_kelvin[0].abbr : null);
const subject_kelvin_selected = ref<string | null>(
subjects_kelvin.length > 0 ? subjects_kelvin[0].abbr : null
);
const subjects_inbus_filtered = await loadInbusSubjects(subjects_kelvin);
const subject_inbus_schedule = ref<ConcreteActivity[] | null>(null);
const teachers = await loadTeachers();
const activities_to_teacher_selected = ref({});
const classes_to_import = ref([]);
const result = ref<Result | null>(null);
Expand Down Expand Up @@ -151,6 +156,7 @@ async function loadSemesters() {
async function loadScheduleForSubjectVersionId(subject_index: number) {
subject_inbus_schedule.value = null;
activities_to_teacher_selected.value = {};
const versionId = subjects_inbus_filtered[subject_index].subjectVersionId;
const request = assembleRequest(`/api/inbus/schedule/subject/version/${versionId}`);
Expand All @@ -174,7 +180,8 @@ async function importActivities() {
const req = {
semester_id: semester.value,
subject_abbr: subject_kelvin_selected.value,
activities: classes_to_import.value
activities: classes_to_import.value,
activities_to_teacher: activities_to_teacher_selected.value
};
const res = await fetch('/api/import/activities', {
Expand All @@ -191,14 +198,21 @@ async function importActivities() {
busy.value = false;
}
function onInbusSubjectSelected(event: Event) {
function onInbusSubjectSelected(event) {
const value: string = event.target.value;
if (value !== '') {
loadScheduleForSubjectVersionId(Number.parseInt(value));
} else {
subject_inbus_schedule.value = null;
}
}
function onTeacherSelected(event) {
const value: string = event.target.value;
const [activity_id, teacher_username] = value.split(',');
activities_to_teacher_selected.value[parseInt(activity_id)] = teacher_username;
}
</script>

<template>
Expand Down Expand Up @@ -237,7 +251,18 @@ function onInbusSubjectSelected(event: Event) {
<td>{{ ca.educationTypeAbbrev }}/{{ ca.order }}, {{ ca.subjectVersionCompleteCode }}</td>

<td>
{{ ca.teacherFullNames }}
<span v-if="ca.teacherFullNames">{{ ca.teacherFullNames }}</span>
<span v-else>
<select @change="onTeacherSelected">
<option
v-for="teacher in teachers"
:key="teacher.username"
:value="`${ca.concreteActivityId},${teacher.username}`"
>
{{ teacher.username }} - {{ teacher.full_name }}
</option>
</select>
</span>
</td>

<td>
Expand Down

0 comments on commit f3a6a47

Please sign in to comment.