Example #1
0
    def test_send_video_to_simple_user(self):
        """A message containing serialized video is dispatched to the regular group."""
        video = VideoFactory()

        channel_layer = get_channel_layer()
        async_to_sync(channel_layer.group_add)(
            VIDEO_ROOM_NAME.format(video_id=str(video.id)), "test_channel")

        channel_layers_utils.dispatch_video(video, to_admin=False)

        message = async_to_sync(channel_layer.receive)("test_channel")
        self.assertEqual(message["type"], "video_updated")
        self.assertEqual(
            message["video"],
            VideoSerializer(video, context={
                "is_admin": False
            }).data)
Example #2
0
    def initiate_live(
        self,
        request,
        pk=None,
    ):
        """Create a live stack on AWS ready to stream.

        Parameters
        ----------
        request : Type[django.http.request.HttpRequest]
            The request on the API endpoint
        pk: string
            The primary key of the video

        Returns
        -------
        Type[rest_framework.response.Response]
            HttpResponse with the serialized video.
        """
        serializer = serializers.InitLiveStateSerializer(data=request.data)

        if not serializer.is_valid():
            return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

        video = self.get_object()

        video.live_type = serializer.validated_data["type"]
        video.upload_state = defaults.PENDING
        video.live_state = defaults.IDLE
        video.uploaded_on = None

        video.save()
        # Dispatch only for admin users. No student should be connected on the video at this point.
        channel_layers_utils.dispatch_video(video, to_admin=True)
        serializer = self.get_serializer(video)

        return Response(serializer.data)