176 lines
6.7 KiB
Python
176 lines
6.7 KiB
Python
from dataclasses import dataclass
|
|
from typing import List, Set
|
|
|
|
from rest_framework.decorators import api_view
|
|
from rest_framework.exceptions import PermissionDenied
|
|
from rest_framework.response import Response
|
|
|
|
from vbv_lernwelt.core.models import User
|
|
from vbv_lernwelt.course.models import CourseSession, CourseSessionUser
|
|
from vbv_lernwelt.course.views import logger
|
|
from vbv_lernwelt.course_session_group.models import CourseSessionGroup
|
|
from vbv_lernwelt.learning_mentor.models import LearningMentor
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
class CourseSessionWithRoles:
|
|
_original: CourseSession
|
|
roles: Set[str]
|
|
|
|
def __getattr__(self, name: str):
|
|
# Delegate attribute access to the _original CourseSession object
|
|
return getattr(self._original, name)
|
|
|
|
def save(self, *args, **kwargs):
|
|
raise NotImplementedError("This proxy object cannot be saved.")
|
|
|
|
|
|
def get_course_sessions_with_roles_for_user(user: User) -> List[CourseSessionWithRoles]:
|
|
result_course_sessions = {}
|
|
|
|
# participant/member/expert course sessions
|
|
csu_qs = CourseSessionUser.objects.filter(user=user).prefetch_related(
|
|
"course_session", "course_session__course"
|
|
)
|
|
for csu in csu_qs:
|
|
cs = csu.course_session
|
|
# member/expert is mutually exclusive...
|
|
cs.roles = {csu.role}
|
|
result_course_sessions[cs.id] = cs
|
|
|
|
# enrich with supervisor course sessions
|
|
csg_qs = CourseSessionGroup.objects.filter(supervisor=user).prefetch_related(
|
|
"course_session", "course_session__course"
|
|
)
|
|
for csg in csg_qs:
|
|
for cs in csg.course_session.all():
|
|
cs.roles = set()
|
|
cs = result_course_sessions.get(cs.id, cs)
|
|
|
|
cs.roles.add("SUPERVISOR")
|
|
result_course_sessions[cs.id] = cs
|
|
|
|
# enrich with mentor course sessions
|
|
lm_qs = LearningMentor.objects.filter(mentor=user).prefetch_related(
|
|
"course_session", "course_session__course"
|
|
)
|
|
for lm in lm_qs:
|
|
cs = lm.course_session
|
|
cs.roles = set()
|
|
cs = result_course_sessions.get(cs.id, cs)
|
|
|
|
cs.roles.add("LEARNING_MENTOR")
|
|
result_course_sessions[cs.id] = cs
|
|
|
|
return [
|
|
CourseSessionWithRoles(cs, cs.roles) for cs in result_course_sessions.values()
|
|
]
|
|
|
|
|
|
@api_view(["GET"])
|
|
def get_dashboard_persons(request):
|
|
try:
|
|
course_sessions = get_course_sessions_with_roles_for_user(request.user)
|
|
|
|
result_persons = {}
|
|
for cs in course_sessions:
|
|
if {
|
|
"SUPERVISOR",
|
|
"EXPERT",
|
|
"MEMBER",
|
|
} & cs.roles and cs.course.configuration.is_uk:
|
|
course_session_users = CourseSessionUser.objects.filter(
|
|
course_session=cs.id
|
|
)
|
|
my_role = (
|
|
"SUPERVISOR"
|
|
if "SUPERVISOR" in cs.roles
|
|
else ("EXPERT" if "EXPERT" in cs.roles else "MEMBER")
|
|
)
|
|
for csu in course_session_users:
|
|
result_persons[csu.user.id] = {
|
|
"user_id": csu.user.id,
|
|
"first_name": csu.user.first_name,
|
|
"last_name": csu.user.last_name,
|
|
"email": csu.user.email,
|
|
"course_sessions": [
|
|
{
|
|
"id": cs.id,
|
|
"session_title": cs.title,
|
|
"course_id": cs.course.id,
|
|
"course_title": cs.course.title,
|
|
"user_role": csu.role,
|
|
"my_role": my_role,
|
|
}
|
|
],
|
|
}
|
|
|
|
# add persons where request.user is mentor
|
|
for cs in course_sessions:
|
|
if "LEARNING_MENTOR" in cs.roles:
|
|
lm = LearningMentor.objects.filter(
|
|
mentor=request.user, course_session=cs.id
|
|
).first()
|
|
|
|
for participant in lm.participants.all():
|
|
course_session_entry = {
|
|
"id": cs.id,
|
|
"course_id": cs.course.id,
|
|
"session_title": cs.title,
|
|
"course_title": cs.course.title,
|
|
"user_role": "LEARNING_MENTEE",
|
|
"my_role": "LEARNING_MENTOR",
|
|
}
|
|
|
|
if participant.user.id not in result_persons:
|
|
result_persons[participant.user.id] = {
|
|
"user_id": participant.user.id,
|
|
"first_name": participant.user.first_name,
|
|
"last_name": participant.user.last_name,
|
|
"email": participant.user.email,
|
|
"course_sessions": [course_session_entry],
|
|
}
|
|
else:
|
|
# user is already in result_persons
|
|
result_persons[participant.user.id]["course_sessions"].append(
|
|
course_session_entry
|
|
)
|
|
|
|
# add persons where request.user is mentee
|
|
mentor_relation_qs = LearningMentor.objects.filter(
|
|
participants__user=request.user
|
|
).prefetch_related("mentor", "course_session")
|
|
for mentor_relation in mentor_relation_qs:
|
|
course_session_entry = {
|
|
"id": mentor_relation.course_session.id,
|
|
"session_title": mentor_relation.course_session.title,
|
|
"course_id": mentor_relation.course_session.course.id,
|
|
"course_title": mentor_relation.course_session.course.title,
|
|
"user_role": "LEARNING_MENTOR",
|
|
"my_role": "LEARNING_MENTEE",
|
|
}
|
|
|
|
if mentor_relation.mentor.id not in result_persons:
|
|
result_persons[mentor_relation.mentor.id] = {
|
|
"user_id": mentor_relation.mentor.id,
|
|
"first_name": mentor_relation.mentor.first_name,
|
|
"last_name": mentor_relation.mentor.last_name,
|
|
"email": mentor_relation.mentor.email,
|
|
"course_sessions": [course_session_entry],
|
|
}
|
|
else:
|
|
# user is already in result_persons
|
|
result_persons[mentor_relation.mentor.id]["course_sessions"].append(
|
|
course_session_entry
|
|
)
|
|
|
|
return Response(
|
|
status=200,
|
|
data=list(result_persons.values()),
|
|
)
|
|
except PermissionDenied as e:
|
|
raise e
|
|
except Exception as e:
|
|
logger.error(e, exc_info=True)
|
|
return Response({"error": str(e)}, status=404)
|