Skip to content
This repository has been archived by the owner on Apr 2, 2024. It is now read-only.

Commit

Permalink
Serializing course contents
Browse files Browse the repository at this point in the history
  • Loading branch information
EssaAlshammri committed May 8, 2017
1 parent 04905fc commit d1a1cba
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 3 deletions.
2 changes: 1 addition & 1 deletion e-learning/educa/courses/api/permissions.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from rest_framework.permissions import BasePermission


class isEnrolled(BasePermission):
class IsEnrolled(BasePermission):

def has_object_permission(self, request, view, obj):
return obj.students.filter(id=request.user.id).exists()
33 changes: 32 additions & 1 deletion e-learning/educa/courses/api/serializers.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from rest_framework import serializers

from ..models import Course, Module, Subject
from ..models import Content, Course, Module, Subject


class SubjectSerializer(serializers.ModelSerializer):
Expand All @@ -24,3 +24,34 @@ class Meta:
model = Course
fields = ['id', 'subject', 'title', 'slug',
'overview', 'created', 'owner', 'modules']


class ItemRelatedField(serializers.RelatedField):

def to_representation(self, value):
return value.render()


class ContentSerializer(serializers.ModelSerializer):
item = ItemRelatedField(read_only=True)

class Meta:
model = Content
fields = ['order', 'item']


class ModuleWithContentsSerializer(serializers.ModelSerializer):
contents = ContentSerializer(many=True)

class Meta:
model = Module
fields = ('order', 'title', 'description', 'contents')


class CourseWithContentsSerializer(serializers.ModelSerializer):
modules = ModuleWithContentsSerializer(many=True)

class Meta:
model = Course
fields = ('id', 'subject', 'title', 'slug',
'overview', 'created', 'owner', 'modules')
11 changes: 10 additions & 1 deletion e-learning/educa/courses/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
from rest_framework.views import APIView

from ..models import Course, Subject
from .serializers import CourseSerializer, SubjectSerializer
from .permissions import IsEnrolled
from .serializers import (CourseSerializer, CourseWithContentsSerializer,
SubjectSerializer)


class SubjectListView(generics.ListAPIView):
Expand Down Expand Up @@ -41,3 +43,10 @@ def enroll(self, request, pk, format=None):
course = self.get_object()
course.students.add(request.user)
return Response({'enrolled': True})

@detail_route(methods=['get'],
serializer_class=CourseWithContentsSerializer,
authentication_classes=[BasicAuthentication],
permission_classes=[IsAuthenticated,IsEnrolled])
def content(self, request, *args, **kwargs):
return self.retrieve(request, *args, **kwargs)

0 comments on commit d1a1cba

Please sign in to comment.