Micrufun пре 2 година
родитељ
комит
11cba29ede
2 измењених фајлова са 58 додато и 36 уклоњено
  1. 39 0
      files/tasks.py
  2. 19 36
      files/views.py

+ 39 - 0
files/tasks.py

@@ -775,6 +775,45 @@ def save_voice_action(user_or_session, friendly_token=None, action="watch", extr
 
     return True
 
+@task(name="video_with_voices", queue="short_tasks")
+def video_with_voices(user_or_session, friendly_token=None, voicesUid=None):
+    """Short task that combines a video with some voices"""
+
+    try:
+        media = Media.objects.get(friendly_token=friendly_token)
+    except BaseException:
+        return False
+
+    voices = []
+
+    for uid in voicesUid:
+        # Double-check voice existence.
+        try:
+            voice = Voice.objects.get(uid=uid)
+        except BaseException:
+            return False
+        voices.append(voice)
+
+    # To download a video combined with some voices,
+    # we require a valid user or session.
+    # We have to check remote_ip to avoid spam.
+    user = user_or_session.get("user_id")
+    session_key = user_or_session.get("user_session")
+    remote_ip = user_or_session.get("remote_ip_addr")
+
+    if user:
+        try:
+            user = User.objects.get(id=user)
+        except BaseException:
+            return False
+
+    if not (user or session_key):
+        return False
+
+    # TODO: Check if media is of video type.
+    # TODO: Combine video with the voices.
+    # TODO: How to get this short task result?
+
 @task(name="get_list_of_popular_media", queue="long_tasks")
 def get_list_of_popular_media():
     """Experimental task for preparing media listing

+ 19 - 36
files/views.py

@@ -70,6 +70,7 @@ from .serializers import (
 from .stop_words import STOP_WORDS
 from .tasks import save_user_action
 from .tasks import save_voice_action
+from .tasks import video_with_voices
 
 # TODO: Should we consider USER_VOICE_ACTIONS too?
 VALID_USER_ACTIONS = [action for action, name in USER_MEDIA_ACTIONS]
@@ -861,51 +862,33 @@ class VideoWithVoices(APIView):
 
     @swagger_auto_schema(
         manual_parameters=[],
-        tags=['Voice Actions'],
+        tags=['Combine a video media with some voices'],
         operation_summary='to_be_written',
         operation_description='to_be_written',
     )
-    def post(self, request, friendly_token, uid=None):
-        # perform like/dislike/report actions
-        #
-        # Test command:
-        # curl -X POST http://127.0.0.1:80/api/v1/media/dd9TrZxDe/voices/f07f84a8-cf0a-445f-8ae7-568b16ddce55/actions
-        # Response:
-        # {"detail":"action allowed on logged in users only"}
-
+    def post(self, request, friendly_token):
         media = self.get_object(friendly_token)
         if isinstance(media, Response):
             return media
 
-        # Double-check voice existence.
-        try:
-            voice = Voice.objects.get(uid=uid)
-        except BaseException:
-            return Response({"detail": "voice does not exist"}, status=status.HTTP_400_BAD_REQUEST,)
+        voicesUid = request.data.get("voicesUid")
+        voicesSrc = request.data.get("voicesSrc")
 
-        action = request.data.get("type")
-        extra = request.data.get("extra_info")
-        if request.user.is_anonymous:
-            # there is a list of allowed actions for
-            # anonymous users, specified in settings
-            if action not in settings.ALLOW_ANONYMOUS_ACTIONS:
-                return Response(
-                    {"detail": "action allowed on logged in users only"},
-                    status=status.HTTP_400_BAD_REQUEST,
-                )
-        if action:
-            user_or_session = get_user_or_session(request)
-            save_voice_action.delay(
-                user_or_session,
-                friendly_token=media.friendly_token,
-                action=action,
-                extra_info=extra,
-                uid=voice.uid,
-            )
+        for uid in voicesUid:
+            # Double-check voice existence.
+            try:
+                voice = Voice.objects.get(uid=uid)
+            except BaseException:
+                return Response({"detail": "voice does not exist"}, status=status.HTTP_400_BAD_REQUEST,)
 
-            return Response({"detail": "action received"}, status=status.HTTP_201_CREATED)
-        else:
-            return Response({"detail": "no action specified"}, status=status.HTTP_400_BAD_REQUEST)
+        user_or_session = get_user_or_session(request)
+        video_with_voices.delay(
+            user_or_session,
+            friendly_token=media.friendly_token,
+            voicesUid=voicesUid,
+        )
+
+        return Response({"detail": "video is combined with voices, it's ready for download"}, status=status.HTTP_201_CREATED)
 
 class MediaSearch(APIView):
     """