فهرست منبع

Finish func: `save_voice_action`

Micrufun 2 سال پیش
والد
کامیت
21f6ec7795
2فایلهای تغییر یافته به همراه22 افزوده شده و 1 حذف شده
  1. 2 0
      files/models.py
  2. 20 1
      files/tasks.py

+ 2 - 0
files/models.py

@@ -1609,6 +1609,8 @@ class Voice(models.Model):
         help_text="voice file",
     )
 
+    reported_times = models.IntegerField(default=0, help_text="how many times a voice is reported")
+
     start = models.FloatField(blank=True, null=True, help_text="Time on video that a voice will start playing")
 
     title = models.CharField(max_length=100, help_text="voice title", blank=True, db_index=True)

+ 20 - 1
files/tasks.py

@@ -710,6 +710,7 @@ def save_voice_action(user_or_session, friendly_token=None, action="watch", extr
         ):
             return False
 
+    # TODO: Exactly why the previous `watch` actions are deleted?
     if action == "watch":
         if user:
             VoiceAction.objects.filter(user=user, voice=voice, action="watch").delete()
@@ -729,7 +730,25 @@ def save_voice_action(user_or_session, friendly_token=None, action="watch", extr
     )
     ma.save()
 
-    # TODO.
+    if action == "watch":
+        voice.views += 1
+        voice.save(update_fields=["views"])
+    elif action == "report":
+        voice.reported_times += 1
+        if voice.reported_times >= settings.REPORTED_TIMES_THRESHOLD:
+            # Delete voice?
+            voice.delete()
+        voice.save(update_fields=["reported_times"])
+        # TODO: notify_users
+        # There is a code for notification. That might be helpful.
+    elif action == "like":
+        voice.likes += 1
+        voice.save(update_fields=["likes"])
+    elif action == "likeundo":
+        voice.likes -= 1
+        voice.save(update_fields=["likes"])
+
+    return True
 
 @task(name="get_list_of_popular_media", queue="long_tasks")
 def get_list_of_popular_media():