def destroy(self, request, pk=None): """ Destroy Marker """ marker = get_object_or_404(Marker, pk=pk) video = marker.video marker.delete() markers = [] for m in video.markers.all(): sub_m = Marker.objects.filter(pk=m.id).select_subclasses()[0] if m.type == 'S': markers.append(SectionMarkerSerializer(sub_m).data) else: markers.append(QuizMarkerSerializer(sub_m).data) response = {'markers': markers} return Response(response, status.HTTP_200_OK)
def partial_update(self, request, pk=None): """ Update a Marker based on instance of Marker class """ marker = get_object_or_404(Marker, pk=pk) sub_marker = Marker.objects.filter(pk=pk).select_subclasses()[0] if (marker.type == Marker.SECTION_MARKER): serializer = SectionMarkerSerializer(sub_marker, data=request.DATA, partial=True) if serializer.is_valid(): serializer.save() markers = [] for m in sub_marker.video.markers.all(): sub_m = Marker.objects.filter(pk=m.id).select_subclasses()[0] if m.type == 'S': markers.append(SectionMarkerSerializer(sub_m).data) else: markers.append(QuizMarkerSerializer(sub_m).data) response = {'markers': markers} return Response(response, status.HTTP_200_OK) else: return Response(serializer.errors, status.HTTP_400_BAD_REQUEST) elif marker.type == Marker.QUIZ_MARKER: serializer = QuizMarkerSerializer(sub_marker, data=request.DATA, partial=True) if serializer.is_valid(): serializer.save() markers = [] for m in sub_marker.video.markers.all(): sub_m = Marker.objects.filter(pk=m.id).select_subclasses()[0] if m.type == 'S': markers.append(SectionMarkerSerializer(sub_m).data) else: markers.append(QuizMarkerSerializer(sub_m).data) response = {'markers': markers} return Response(response, status.HTTP_200_OK) else: return Response(serializer.errors, status.HTTP_400_BAD_REQUEST) else: return("Unknown Marker type", status.HTTP_400_BAD_REQUEST)
def create(self, request): """ Create a Marker based on type in request.DATA """ print "Create Marker" if request.DATA['type'] == Marker.SECTION_MARKER: serializer = SectionMarkerSerializer(data=request.DATA) if serializer.is_valid(): serializer.save() markers = [] for m in serializer.object.video.markers.all(): sub_m = Marker.objects.filter(pk=m.id).select_subclasses()[0] if m.type == 'S': markers.append(SectionMarkerSerializer(sub_m).data) else: markers.append(QuizMarkerSerializer(sub_m).data) response = {'markers': markers} return Response(response, status.HTTP_201_CREATED) else: return Response(serializer.errors, status.HTTP_400_BAD_REQUEST) elif request.DATA['type'] == Marker.QUIZ_MARKER: serializer = QuizMarkerSerializer(data=request.DATA) if serializer.is_valid(): serializer.save() markers = [] for m in serializer.object.video.markers.all(): sub_m = Marker.objects.filter(pk=m.id).select_subclasses()[0] if m.type == 'S': markers.append(SectionMarkerSerializer(sub_m).data) else: markers.append(QuizMarkerSerializer(sub_m).data) response = {'markers': markers} return Response(response, status.HTTP_201_CREATED) else: return Response(serializer.errors, status.HTTP_400_BAD_REQUEST) else: return Response("Bad Type for Marker", status.HTTP_400_BAD_REQUEST)
def partial_update(self, request, pk=None): """ Update a Marker based on instance of Marker class """ marker = get_object_or_404(Marker, pk=pk) sub_marker = Marker.objects.filter(pk=pk).select_subclasses()[0] if (marker.type == Marker.SECTION_MARKER): serializer = SectionMarkerSerializer(sub_marker, data=request.DATA, partial=True) if serializer.is_valid(): serializer.save() markers = [] for m in sub_marker.video.markers.all(): sub_m = Marker.objects.filter( pk=m.id).select_subclasses()[0] if m.type == 'S': markers.append(SectionMarkerSerializer(sub_m).data) else: markers.append(QuizMarkerSerializer(sub_m).data) response = {'markers': markers} return Response(response, status.HTTP_200_OK) else: return Response(serializer.errors, status.HTTP_400_BAD_REQUEST) elif marker.type == Marker.QUIZ_MARKER: serializer = QuizMarkerSerializer(sub_marker, data=request.DATA, partial=True) if serializer.is_valid(): serializer.save() markers = [] for m in sub_marker.video.markers.all(): sub_m = Marker.objects.filter( pk=m.id).select_subclasses()[0] if m.type == 'S': markers.append(SectionMarkerSerializer(sub_m).data) else: markers.append(QuizMarkerSerializer(sub_m).data) response = {'markers': markers} return Response(response, status.HTTP_200_OK) else: return Response(serializer.errors, status.HTTP_400_BAD_REQUEST) else: return ("Unknown Marker type", status.HTTP_400_BAD_REQUEST)
def create(self, request): """ Create a Marker based on type in request.DATA """ print "Create Marker" if request.DATA['type'] == Marker.SECTION_MARKER: serializer = SectionMarkerSerializer(data=request.DATA) if serializer.is_valid(): serializer.save() markers = [] for m in serializer.object.video.markers.all(): sub_m = Marker.objects.filter( pk=m.id).select_subclasses()[0] if m.type == 'S': markers.append(SectionMarkerSerializer(sub_m).data) else: markers.append(QuizMarkerSerializer(sub_m).data) response = {'markers': markers} return Response(response, status.HTTP_201_CREATED) else: return Response(serializer.errors, status.HTTP_400_BAD_REQUEST) elif request.DATA['type'] == Marker.QUIZ_MARKER: serializer = QuizMarkerSerializer(data=request.DATA) if serializer.is_valid(): serializer.save() markers = [] for m in serializer.object.video.markers.all(): sub_m = Marker.objects.filter( pk=m.id).select_subclasses()[0] if m.type == 'S': markers.append(SectionMarkerSerializer(sub_m).data) else: markers.append(QuizMarkerSerializer(sub_m).data) response = {'markers': markers} return Response(response, status.HTTP_201_CREATED) else: return Response(serializer.errors, status.HTTP_400_BAD_REQUEST) else: return Response("Bad Type for Marker", status.HTTP_400_BAD_REQUEST)