Commit ee5b31d7 authored by Bruno Martin's avatar Bruno Martin
Browse files

refactor class views and serializers from legacy,

with performance improvemtns
parent b9e7deea
......@@ -57,5 +57,6 @@ class Class(models.Model):
@property
def get_students(self):
from courses.models import CourseStudent
return CourseStudent.objects.filter(course=self.course, user__in=self.students.all()).order_by('user__first_name')
return self.course.course_students.filter(
user__in=self.students.all(),
).select_related('user').order_by('user__first_name')
from rest_framework import serializers
from users.serializers import SimpleUserSerializer, TimtecUserAdminCertificateSerializer
from courses.models import CourseStudent
from courses.serializers import BasicCourseSerializer
from courses.workspaces.serializers import WorkspaceBaseSerializerMixin
from courses_certification.serializers import (
CourseCertificationSerializer,
CertificationProcessSerializer,
EvaluationSerializer,
)
from .models import Class
class BasicClassSerializer(serializers.ModelSerializer):
class Meta:
model = Class
fields = ('id', 'name', 'assistants')
class CourseStudentClassSerializer(serializers.ModelSerializer):
user = TimtecUserAdminCertificateSerializer(read_only=True)
class Meta:
model = CourseStudent
fields = ('id', 'user', 'course_finished',
'certificate', 'can_emmit_receipt', 'percent_progress',)
class ClassSerializer(WorkspaceBaseSerializerMixin, serializers.ModelSerializer):
students_details = CourseStudentClassSerializer(source='get_students', many=True, read_only=True)
processes = CertificationProcessSerializer(read_only=True, many=True)
evaluations = EvaluationSerializer(read_only=True, many=True)
course = BasicCourseSerializer(read_only=True)
assistants = SimpleUserSerializer(read_only=True, many=True)
class Meta:
model = Class
fields = "__all__"
def update(self, instance, validated_data, **kwargs):
assistants = self.context['request'].data.get('assistants', None)
updated_class = super(ClassSerializer, self).update(instance, validated_data)
# If there are assistans to be associated with the class, do it now
updated_class.assistants.clear()
for assistant in assistants:
updated_class.assistants.add(assistant['id'])
contract = self.context['request'].data.get('contract', None)
if contract:
current = updated_class.contract.first()
if not current or (contract['id'] != current.id):
c = Workspace.objects.get(pk=contract['id'])
updated_class.contract.clear()
updated_class.contract.add(c)
else:
updated_class.contract.clear()
updated_class.save()
return updated_class
\ No newline at end of file
from rest_framework import viewsets, mixins, status
from rest_framework.permissions import IsAuthenticated
from rest_framework.response import Response
from django.db.models import Q
from courses.models import Course
from .models import Class
from courses.classes.serializers import (
BasicClassSerializer,
ClassSerializer,
)
class BasicClassViewSet(viewsets.ReadOnlyModelViewSet):
queryset = Class.objects.all()
serializer_class = BasicClassSerializer
filter_fields = ['course']
permission_classes = [IsAuthenticated]
def get_queryset(self):
queryset = super().get_queryset()
if not (self.request.user.is_staff or self.request.user.is_superuser):
queryset = queryset.filter(
Q(course__professors=self.request.user, course__course_professors__role='coordinator') |
Q(assistants=self.request.user)
).distinct()
queryset = queryset.prefetch_related('assistants')
return queryset
class ClassViewSet(BasicClassViewSet, viewsets.ModelViewSet):
serializer_class = ClassSerializer
def get_queryset(self):
queryset = super().get_queryset()
queryset = queryset.prefetch_related(
'assistants',
'students',
'course__course_students',
)
return queryset
\ No newline at end of file
......@@ -308,6 +308,7 @@ class CourseStudent(models.Model):
Course,
models.CASCADE,
verbose_name=_('Course'),
related_name='course_students',
)
start_date = models.DateTimeField(
default=timezone.now,
......
......@@ -61,3 +61,29 @@ class CourseSerializer(serializers.ModelSerializer):
course.save()
return course
class BasicCourseSerializer(serializers.ModelSerializer):
home_thumbnail_url = serializers.SerializerMethodField()
class Meta:
model = Course
fields = (
"id",
"slug",
"name",
"status",
"home_thumbnail_url",
"start_date",
"home_published",
"has_started",
"min_percent_to_complete",
"is_public",
)
@staticmethod
def get_home_thumbnail_url(obj):
if obj.home_thumbnail:
return obj.home_thumbnail.url
return ''
......@@ -10,7 +10,10 @@ from courses.workspaces.views import (
WorkspaceViewSet,
WorkspaceGroupViewSet,
WorkspaceGroupAdminViewSet,
)
from courses.classes.views import (
ClassViewSet,
BasicClassViewSet,
)
router = routers.DefaultRouter()
......@@ -23,6 +26,7 @@ router.register(r'workspaces', WorkspaceViewSet)
router.register(r'group', WorkspaceGroupViewSet, base_name='group')
router.register(r'group_admin', WorkspaceGroupAdminViewSet, base_name='group_admin')
router.register(r'class', ClassViewSet)
router.register(r'class_basic', BasicClassViewSet)
# router.register(r'course_carousel', views.CarouselCourseView, base_name='course_carousel')
app_name = 'courses'
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment