Kaynağa Gözat

[tasks] Add user id to Profile

Colin Powell 8 ay önce
ebeveyn
işleme
04a7ba51e4

+ 18 - 0
vrobbler/apps/profiles/migrations/0018_userprofile_todoist_user_id.py

@@ -0,0 +1,18 @@
+# Generated by Django 4.2.16 on 2024-10-15 18:47
+
+from django.db import migrations, models
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ("profiles", "0017_userprofile_todoist_auth_key_and_more"),
+    ]
+
+    operations = [
+        migrations.AddField(
+            model_name="userprofile",
+            name="todoist_user_id",
+            field=models.CharField(blank=True, max_length=100, null=True),
+        ),
+    ]

+ 1 - 0
vrobbler/apps/profiles/models.py

@@ -33,6 +33,7 @@ class UserProfile(TimeStampedModel):
 
     todoist_auth_key = EncryptedField(**BNULL)
     todoist_state = EncryptedField(**BNULL)
+    todoist_user_id = models.CharField(max_length=100, **BNULL)
 
     redirect_to_webpage = models.BooleanField(default=True)
 

+ 25 - 0
vrobbler/apps/scrobbles/migrations/0065_alter_scrobble_log.py

@@ -0,0 +1,25 @@
+# Generated by Django 4.2.16 on 2024-10-15 18:47
+
+from django.db import migrations, models
+import scrobbles.dataclasses
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ("scrobbles", "0064_scrobble_task_alter_scrobble_media_type"),
+    ]
+
+    operations = [
+        migrations.AlterField(
+            model_name="scrobble",
+            name="log",
+            field=models.JSONField(
+                blank=True,
+                decoder=scrobbles.dataclasses.ScrobbleLogDataDecoder,
+                default=dict,
+                encoder=scrobbles.dataclasses.ScrobbleLogDataEncoder,
+                null=True,
+            ),
+        ),
+    ]

+ 1 - 2
vrobbler/apps/scrobbles/models.py

@@ -1246,9 +1246,8 @@ class Scrobble(TimeStampedModel):
 
         # Set our playback seconds, and calc long play seconds
         self.playback_position_seconds = seconds_elapsed
-        past_seconds = 0
         if self.previous:
-            past_seconds = self.previous.long_play_seconds
+            past_seconds = self.previous.long_play_seconds or 0
 
         self.long_play_seconds = past_seconds + seconds_elapsed
 

+ 21 - 0
vrobbler/apps/tasks/migrations/0003_remove_task_source_remove_task_source_url_pattern.py

@@ -0,0 +1,21 @@
+# Generated by Django 4.2.16 on 2024-10-15 18:47
+
+from django.db import migrations
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ("tasks", "0002_task_source_task_source_url_pattern"),
+    ]
+
+    operations = [
+        migrations.RemoveField(
+            model_name="task",
+            name="source",
+        ),
+        migrations.RemoveField(
+            model_name="task",
+            name="source_url_pattern",
+        ),
+    ]

+ 12 - 2
vrobbler/apps/tasks/webhooks.py

@@ -10,6 +10,7 @@ from scrobbles.scrobblers import (
     todoist_scrobble_task,
     todoist_scrobble_task_finish,
 )
+from profiles.models import UserProfile
 
 logger = logging.getLogger(__name__)
 
@@ -51,11 +52,20 @@ def todoist_webhook(request):
         )
         return Response({}, status=status.HTTP_304_NOT_MODIFIED)
 
+    user_id = (
+        UserProfile.objects.filter(todoist_user_id=post_data.get("user_id"))
+        .first()
+        .user_id
+    )
+    # TODO huge hack, find a way to populate user id from Todoist
+    if not user_id:
+        user_id = 1
+
     scrobble = None
     if "completed" in todoist_task["todoist_event"]:
-        scrobble = todoist_scrobble_task_finish(todoist_task, request.user.id)
+        scrobble = todoist_scrobble_task_finish(todoist_task, user_id)
     elif "inprogress" in todoist_task["todoist_label_list"]:
-        scrobble = todoist_scrobble_task(todoist_task, request.user.id)
+        scrobble = todoist_scrobble_task(todoist_task, user_id)
 
     if not scrobble:
         return Response({}, status=status.HTTP_400_BAD_REQUEST)