[tasks] Revert webhook for Todoist
This commit is contained in:
@ -1,10 +1,13 @@
|
|||||||
import json
|
import json
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
|
from django.views.decorators.csrf import csrf_exempt
|
||||||
from profiles.models import UserProfile
|
from profiles.models import UserProfile
|
||||||
|
from rest_framework.decorators import permission_classes
|
||||||
|
from rest_framework.permissions import IsAuthenticated
|
||||||
from rest_framework import status
|
from rest_framework import status
|
||||||
from rest_framework.response import Response
|
from rest_framework.response import Response
|
||||||
from scrobbles.views import WebhookView
|
from rest_framework.views import APIView
|
||||||
from scrobbles.scrobblers import (
|
from scrobbles.scrobblers import (
|
||||||
emacs_scrobble_task,
|
emacs_scrobble_task,
|
||||||
emacs_scrobble_update_task,
|
emacs_scrobble_update_task,
|
||||||
@ -15,10 +18,11 @@ from scrobbles.scrobblers import (
|
|||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
class TodoistWebhookView(WebhookView):
|
@csrf_exempt
|
||||||
def post(self, request):
|
@permission_classes([IsAuthenticated])
|
||||||
|
def todoist_webhook(request):
|
||||||
post_data = request.data
|
post_data = request.data
|
||||||
self.logger.info(
|
logger.info(
|
||||||
"[todoist_webhook] called",
|
"[todoist_webhook] called",
|
||||||
extra={"post_data": post_data},
|
extra={"post_data": post_data},
|
||||||
)
|
)
|
||||||
@ -27,11 +31,12 @@ class TodoistWebhookView(WebhookView):
|
|||||||
todoist_type, todoist_event = post_data.get("event_name").split(":")
|
todoist_type, todoist_event = post_data.get("event_name").split(":")
|
||||||
event_data = post_data.get("event_data", {})
|
event_data = post_data.get("event_data", {})
|
||||||
is_item_type = todoist_type == "item"
|
is_item_type = todoist_type == "item"
|
||||||
is_note_type = todoist_type == "note"
|
is_note_type = todoist_tyllll = "note"
|
||||||
new_labels = event_data.get("labels", [])
|
new_labels = event_data.get("labels", [])
|
||||||
old_labels = (
|
old_labels = (
|
||||||
post_data.get("event_data_extra", {}).get("old_item", {}).get("labels", [])
|
post_data.get("event_data_extra", {}).get("old_item", {}).get("labels", [])
|
||||||
)
|
)
|
||||||
|
# TODO Don't hard code status strings in here
|
||||||
is_updated = todoist_event in ["updated"]
|
is_updated = todoist_event in ["updated"]
|
||||||
is_added = todoist_event in ["added"]
|
is_added = todoist_event in ["added"]
|
||||||
|
|
||||||
@ -60,13 +65,11 @@ class TodoistWebhookView(WebhookView):
|
|||||||
"updated_at": task_data.get("updated_at"),
|
"updated_at": task_data.get("updated_at"),
|
||||||
"details": task_data.get("description"),
|
"details": task_data.get("description"),
|
||||||
"notes": event_data.get("content"),
|
"notes": event_data.get("content"),
|
||||||
"is_deleted": (
|
"is_deleted": (True if event_data.get("is_deleted") == "true" else False),
|
||||||
True if event_data.get("is_deleted") == "true" else False
|
|
||||||
),
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (is_added and not todoist_note) or (is_updated and not todoist_task):
|
if (is_added and not todoist_note) or (is_updated and not todoist_task):
|
||||||
self.logger.info(
|
logger.info(
|
||||||
"[todoist_webhook] ignoring wrong todoist type, event or labels",
|
"[todoist_webhook] ignoring wrong todoist type, event or labels",
|
||||||
extra={
|
extra={
|
||||||
"todoist_type": todoist_type,
|
"todoist_type": todoist_type,
|
||||||
@ -96,7 +99,7 @@ class TodoistWebhookView(WebhookView):
|
|||||||
scrobble = todoist_scrobble_update_task(todoist_note, user_profile.user_id)
|
scrobble = todoist_scrobble_update_task(todoist_note, user_profile.user_id)
|
||||||
|
|
||||||
if not scrobble:
|
if not scrobble:
|
||||||
self.logger.info(
|
logger.info(
|
||||||
"[todoist_webhook] finished with no note or task found",
|
"[todoist_webhook] finished with no note or task found",
|
||||||
extra={"scrobble_id": None},
|
extra={"scrobble_id": None},
|
||||||
)
|
)
|
||||||
@ -105,14 +108,18 @@ class TodoistWebhookView(WebhookView):
|
|||||||
status=status.HTTP_304_NOT_MODIFIED,
|
status=status.HTTP_304_NOT_MODIFIED,
|
||||||
)
|
)
|
||||||
|
|
||||||
self.logger.info(
|
logger.info(
|
||||||
"[todoist_webhook] finished",
|
"[todoist_webhook] finished",
|
||||||
extra={"scrobble_id": scrobble.id},
|
extra={"scrobble_id": scrobble.id},
|
||||||
)
|
)
|
||||||
return Response({"scrobble_id": scrobble.id}, status=status.HTTP_200_OK)
|
return Response({"scrobble_id": scrobble.id}, status=status.HTTP_200_OK)
|
||||||
|
|
||||||
|
|
||||||
class EmacsWebhookView(WebhookView):
|
class EmacsWebhookView(APIView):
|
||||||
|
@property
|
||||||
|
def logger(self):
|
||||||
|
return logger
|
||||||
|
|
||||||
def post(self, request):
|
def post(self, request):
|
||||||
try:
|
try:
|
||||||
post_data = json.loads(request.data)
|
post_data = json.loads(request.data)
|
||||||
|
|||||||
Reference in New Issue
Block a user