Skip to content

Commit

Permalink
Merge pull request #531 from mrlvsb/inbus-import-vue
Browse files Browse the repository at this point in the history
Import from INBUS rewrite in Vue
  • Loading branch information
geordi authored Oct 22, 2024
2 parents 682d07b + 009839d commit 7c2474d
Show file tree
Hide file tree
Showing 12 changed files with 481 additions and 224 deletions.
1 change: 1 addition & 0 deletions api/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
path("classes/<int:class_id>/add_students", views.add_student_to_class),
path("subject/<subject_abbr>", views.subject_list),
path("subjects/all", views.subjects_all),
path("teachers/all", views.teachers_all),
path("reevaluate_task/<int:task_id>", views.reevaluate_task),
path("search", views.search),
path("transfer_students", views.transfer_students),
Expand Down
35 changes: 31 additions & 4 deletions api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import django.http
from django.shortcuts import get_object_or_404, resolve_url
from django.http import HttpRequest, HttpResponseBadRequest
from django.views.decorators.http import require_POST
from django.views.decorators.http import require_GET, require_POST
from django.contrib.auth.models import User
from django.urls import reverse
from common.models import (
Expand Down Expand Up @@ -284,6 +284,19 @@ def subjects_all(request) -> JsonResponse:
return JsonResponse(resp)


@user_passes_test(is_teacher)
@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
)

resp = {"teachers": items}
return JsonResponse(resp)


@login_required
def info(request):
res = {}
Expand Down Expand Up @@ -745,8 +758,16 @@ def import_activities(request):
post = json.loads(request.body.decode("utf-8"))

semester_id = post["semester_id"]
subject = post["subject"]
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 @@ -755,10 +776,16 @@ def import_activities(request):
semester = Semester.objects.get(pk=semester_id)

try:
res["users"] = list(common.bulk_import.run(activities, subject, 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())
# msg = traceback.TracebackException.from_exception(e).format()
msg = e.args[0]
res["error"] = msg
except BaseException:
res["error"] = traceback.format_exc()

Expand Down
23 changes: 16 additions & 7 deletions common/bulk_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from typing import List, Dict, Generator
import traceback

from .inbus.dto import ConcreteActivity
from .inbus.dto import ConcreteActivity, ConcreteActivityId


class ImportException(Exception):
Expand All @@ -26,15 +26,17 @@ class ImportResult:

def run(
concrete_activities: List[ConcreteActivity],
subj: Dict[str, str],
subject_abbr: str,
semester: Semester,
user: User,
activities_to_teacher: Dict[int, str],
) -> Generator[ImportResult, None, None]:
"""
`subj`: subject from selected subject in UI as dictionary with k:abbr, v: name
`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
"""

subject_abbr = subj["abbr"]
try:
subject = Subject.objects.get(abbr=subject_abbr)
except Subject.DoesNotExist:
Expand Down Expand Up @@ -75,8 +77,13 @@ 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
try:
teacher_username = activities_to_teacher[ca.concreteActivityId]
teacher = User.objects.get(username=teacher_username)
except KeyError:
msg = f"There's no assigned teacher to activity {ca.code()}. Please, make sure you selected one."
raise ImportException(msg)

if not is_teacher(teacher):
teachers_group = Group.objects.get_by_natural_key("teachers")
Expand All @@ -87,7 +94,9 @@ def run(
class_in_db[c].save()

# Students
students_in_class = inbus.students_in_concrete_activity(ca.concreteActivityId)
students_in_class = inbus.students_in_concrete_activity(
ConcreteActivityId(ca.concreteActivityId)
)

for student in students_in_class:
login = student.login.upper()
Expand Down
2 changes: 0 additions & 2 deletions frontend/src/App.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,12 @@ import EditTask from './EditTask.svelte';
import ClassList from './ClassList.svelte';
import Router from 'svelte-spa-router';
import StudentTransfer from './StudentTransfer.svelte';
import ImportFromInbus from './ImportFromInbus.svelte';
import { user } from './global.js';
const routes = {
'/task/edit/:id': EditTask,
'/task/add/:subject': EditTask,
'/student_transfer': StudentTransfer,
'/import': ImportFromInbus,
'/': ClassList
};
</script>
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/ClassList.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ $: {
teacher={filter.teacher}
clazz={filter.class} />

<a class="btn btn-sm p-1" href="/#/import" title="Bulk import students from EDISON">
<a class="btn btn-sm p-1" href="/import/inbus" title="Bulk import students from EDISON">
<span class="iconify" data-icon="mdi:calendar-import"></span>
</a>

Expand Down
209 changes: 0 additions & 209 deletions frontend/src/ImportFromInbus.svelte

This file was deleted.

Loading

0 comments on commit 7c2474d

Please sign in to comment.