From 04a7ba51e4dd0b9e9198059dc62c1a8d02902ead Mon Sep 17 00:00:00 2001 From: Colin Powell Date: Tue, 15 Oct 2024 14:49:43 -0400 Subject: [PATCH] [tasks] Add user id to Profile --- .../0018_userprofile_todoist_user_id.py | 18 +++++++++++++ vrobbler/apps/profiles/models.py | 1 + .../migrations/0065_alter_scrobble_log.py | 25 +++++++++++++++++++ vrobbler/apps/scrobbles/models.py | 3 +-- ...k_source_remove_task_source_url_pattern.py | 21 ++++++++++++++++ vrobbler/apps/tasks/webhooks.py | 14 +++++++++-- 6 files changed, 78 insertions(+), 4 deletions(-) create mode 100644 vrobbler/apps/profiles/migrations/0018_userprofile_todoist_user_id.py create mode 100644 vrobbler/apps/scrobbles/migrations/0065_alter_scrobble_log.py create mode 100644 vrobbler/apps/tasks/migrations/0003_remove_task_source_remove_task_source_url_pattern.py diff --git a/vrobbler/apps/profiles/migrations/0018_userprofile_todoist_user_id.py b/vrobbler/apps/profiles/migrations/0018_userprofile_todoist_user_id.py new file mode 100644 index 0000000..25586b1 --- /dev/null +++ b/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), + ), + ] diff --git a/vrobbler/apps/profiles/models.py b/vrobbler/apps/profiles/models.py index 9ba8554..20ff783 100644 --- a/vrobbler/apps/profiles/models.py +++ b/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) diff --git a/vrobbler/apps/scrobbles/migrations/0065_alter_scrobble_log.py b/vrobbler/apps/scrobbles/migrations/0065_alter_scrobble_log.py new file mode 100644 index 0000000..06def70 --- /dev/null +++ b/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, + ), + ), + ] diff --git a/vrobbler/apps/scrobbles/models.py b/vrobbler/apps/scrobbles/models.py index c438b64..cef2f81 100644 --- a/vrobbler/apps/scrobbles/models.py +++ b/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 diff --git a/vrobbler/apps/tasks/migrations/0003_remove_task_source_remove_task_source_url_pattern.py b/vrobbler/apps/tasks/migrations/0003_remove_task_source_remove_task_source_url_pattern.py new file mode 100644 index 0000000..1843ae9 --- /dev/null +++ b/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", + ), + ] diff --git a/vrobbler/apps/tasks/webhooks.py b/vrobbler/apps/tasks/webhooks.py index f400375..41131bd 100644 --- a/vrobbler/apps/tasks/webhooks.py +++ b/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)