Micrufun 2 éve
szülő
commit
bb01471004
2 módosított fájl, 66 hozzáadás és 1 törlés
  1. 46 0
      files/methods.py
  2. 20 1
      files/tasks.py

+ 46 - 0
files/methods.py

@@ -140,6 +140,52 @@ def pre_save_action__voice(media, user, session_key, action, remote_ip, voice):
 
     return False
 
+def pre_save_action__videowithvoices(media, user, session_key, action, remote_ip):
+    """This will perform some checkes
+    example threshold checks, before performing an action of combining a video with some voices
+    """
+
+    # This method is only for a specific action type.
+    if action != "getvideowithvoices":
+        return False
+
+    from actions.models import VoiceAction
+
+    if user:
+        query = VoiceAction.objects.filter(media=media, action=action, user=user)
+    else:
+        query = VoiceAction.objects.filter(media=media, action=action, session_key=session_key)
+    query = query.order_by("-action_date")
+
+    if query:
+        query = query.first()
+        if user:
+            # The user has done the action before.
+            now = datetime.now(query.action_date.tzinfo)
+            # User has to wait for a few seconds, before repeating the same action again.
+            # Force a time gap between two consecuitive actions.
+            if (now - query.action_date).seconds > settings.TIME_TO_ACTION_ANONYMOUS:
+                return True
+    else:
+        if user: # first time action
+            # The user is doing this action for the first time.
+            return True
+
+    if not user:
+        # perform some checking for requests where no session
+        # id is specified (and user is anonymous) to avoid spam
+        # eg allow for the same remote_ip for a specific number of actions
+        query = VoiceAction.objects.filter(media=media, action=action, remote_ip=remote_ip).filter(user=None).order_by("-action_date")
+        if query:
+            query = query.first()
+            now = datetime.now(query.action_date.tzinfo)
+            if (now - query.action_date).seconds > settings.TIME_TO_ACTION_ANONYMOUS:
+                return True
+        else:
+            return True
+
+    return False
+
 def is_mediacms_editor(user):
     """Whether user is MediaCMS editor"""
 

+ 20 - 1
files/tasks.py

@@ -36,6 +36,7 @@ from .helpers import (
 )
 from .methods import list_tasks, notify_users, pre_save_action
 from .methods import pre_save_action__voice
+from .methods import pre_save_action__videowithvoices
 from .models import Category, EncodeProfile, Encoding, Media, Rating, Tag
 from .models import Voice
 
@@ -810,7 +811,25 @@ def video_with_voices(user_or_session, friendly_token=None, voicesUid=None):
     if not (user or session_key):
         return False
 
-    # TODO: Check spam and more.
+    # Avoid spam and more.
+    if not pre_save_action__videowithvoices(
+        media=media,
+        user=user,
+        session_key=session_key,
+        action="getvideowithvoices",
+        remote_ip=remote_ip,
+    ):
+        return False
+
+    va = VoiceAction(
+        user=user,
+        session_key=session_key,
+        media=media,
+        action="getvideowithvoices",
+        remote_ip=remote_ip,
+    )
+    va.save()
+
     # TODO: Check if media is of video type.
     # TODO: Combine video with the voices.
     # TODO: How to get this short task result?