Add learning mentor relations to persons view
This commit is contained in:
parent
d6885f4e7f
commit
de91814c6a
|
|
@ -10,9 +10,6 @@ from django.views import defaults as default_views
|
||||||
from django.views.decorators.csrf import csrf_exempt
|
from django.views.decorators.csrf import csrf_exempt
|
||||||
from django_ratelimit.exceptions import Ratelimited
|
from django_ratelimit.exceptions import Ratelimited
|
||||||
from graphene_django.views import GraphQLView
|
from graphene_django.views import GraphQLView
|
||||||
from wagtail import urls as wagtail_urls
|
|
||||||
from wagtail.admin import urls as wagtailadmin_urls
|
|
||||||
from wagtail.documents import urls as media_library_urls
|
|
||||||
|
|
||||||
from vbv_lernwelt.api.directory import list_entities
|
from vbv_lernwelt.api.directory import list_entities
|
||||||
from vbv_lernwelt.api.user import get_profile, me_user_view, post_avatar
|
from vbv_lernwelt.api.user import get_profile, me_user_view, post_avatar
|
||||||
|
|
@ -61,6 +58,9 @@ from vbv_lernwelt.importer.views import (
|
||||||
)
|
)
|
||||||
from vbv_lernwelt.media_files.views import user_image
|
from vbv_lernwelt.media_files.views import user_image
|
||||||
from vbv_lernwelt.notify.views import email_notification_settings
|
from vbv_lernwelt.notify.views import email_notification_settings
|
||||||
|
from wagtail import urls as wagtail_urls
|
||||||
|
from wagtail.admin import urls as wagtailadmin_urls
|
||||||
|
from wagtail.documents import urls as media_library_urls
|
||||||
|
|
||||||
|
|
||||||
class SignedIntConverter(IntConverter):
|
class SignedIntConverter(IntConverter):
|
||||||
|
|
|
||||||
|
|
@ -1,12 +1,12 @@
|
||||||
from django.test import TestCase
|
from django.test import TestCase
|
||||||
|
|
||||||
from vbv_lernwelt.course.creators.test_utils import (
|
from vbv_lernwelt.course.creators.test_utils import (
|
||||||
|
add_course_session_group_supervisor,
|
||||||
|
add_course_session_user,
|
||||||
create_course,
|
create_course,
|
||||||
create_course_session,
|
create_course_session,
|
||||||
create_user,
|
|
||||||
add_course_session_user,
|
|
||||||
create_course_session_group,
|
create_course_session_group,
|
||||||
add_course_session_group_supervisor,
|
create_user,
|
||||||
)
|
)
|
||||||
from vbv_lernwelt.course.models import CourseSessionUser
|
from vbv_lernwelt.course.models import CourseSessionUser
|
||||||
from vbv_lernwelt.dashboard.views import get_course_sessions_with_roles_for_user
|
from vbv_lernwelt.dashboard.views import get_course_sessions_with_roles_for_user
|
||||||
|
|
|
||||||
|
|
@ -7,7 +7,6 @@ from rest_framework.response import Response
|
||||||
|
|
||||||
from vbv_lernwelt.core.models import User
|
from vbv_lernwelt.core.models import User
|
||||||
from vbv_lernwelt.course.models import CourseSession, CourseSessionUser
|
from vbv_lernwelt.course.models import CourseSession, CourseSessionUser
|
||||||
from vbv_lernwelt.course.serializers import CourseSessionSerializer
|
|
||||||
from vbv_lernwelt.course.views import logger
|
from vbv_lernwelt.course.views import logger
|
||||||
from vbv_lernwelt.course_session_group.models import CourseSessionGroup
|
from vbv_lernwelt.course_session_group.models import CourseSessionGroup
|
||||||
from vbv_lernwelt.learning_mentor.models import LearningMentor
|
from vbv_lernwelt.learning_mentor.models import LearningMentor
|
||||||
|
|
@ -73,22 +72,101 @@ def get_dashboard_persons(request):
|
||||||
try:
|
try:
|
||||||
course_sessions = get_course_sessions_with_roles_for_user(request.user)
|
course_sessions = get_course_sessions_with_roles_for_user(request.user)
|
||||||
|
|
||||||
persons = []
|
result_persons = {}
|
||||||
for cs in course_sessions:
|
for cs in course_sessions:
|
||||||
if {"SUPERVISOR", "EXPERT", "MEMBER"} & cs.roles:
|
if {
|
||||||
persons.extend(
|
"SUPERVISOR",
|
||||||
CourseSessionUser.objects.filter(course_session=cs.id).values(
|
"EXPERT",
|
||||||
"user"
|
"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,
|
||||||
|
}
|
||||||
|
],
|
||||||
|
}
|
||||||
|
|
||||||
all_to_serialize = course_sessions
|
# 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(
|
return Response(
|
||||||
status=200,
|
status=200,
|
||||||
data=CourseSessionSerializer(
|
data=list(result_persons.values()),
|
||||||
all_to_serialize, many=True, context={"user": request.user}
|
|
||||||
).data,
|
|
||||||
)
|
)
|
||||||
except PermissionDenied as e:
|
except PermissionDenied as e:
|
||||||
raise e
|
raise e
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue