From b18728eef6b4f65e479bef5d69f915cbafbdac69 Mon Sep 17 00:00:00 2001 From: Aroy-Art Date: Wed, 19 Jun 2024 18:21:42 +0200 Subject: [PATCH] Add: API UserProfileAPIView.seen_post function --- archivist/apps/api/urls.py | 7 +++++++ archivist/apps/api/views.py | 42 +++++++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+) create mode 100644 archivist/apps/api/urls.py create mode 100644 archivist/apps/api/views.py diff --git a/archivist/apps/api/urls.py b/archivist/apps/api/urls.py new file mode 100644 index 0000000..8f3ab82 --- /dev/null +++ b/archivist/apps/api/urls.py @@ -0,0 +1,7 @@ +from django.urls import path + +app_name = 'api' + +urlpatterns = [ + path('profile/seen_posts/', UserProfileAPIView.seen_post, name='user_seen_post'), +] \ No newline at end of file diff --git a/archivist/apps/api/views.py b/archivist/apps/api/views.py new file mode 100644 index 0000000..c19fbe5 --- /dev/null +++ b/archivist/apps/api/views.py @@ -0,0 +1,42 @@ +from django.http import JsonResponse + +# Create your views here. + +from django.contrib.auth.models import User, Group + +from apps.user.models import UserProfile, SeenPost + +from apps.sites.models import Submissions + +class UserProfileAPIView(APIView): + + def seen_post(request, submission_hash): + + user = UserProfile.objects.get(user=request.user) + + if request.method == 'GET': + try: + submission = Submissions.objects.get(submission_hash=submission_hash) + + try: + SeenPost.objects.get(user=user, post_id=submission.pk) + + return JsonResponse({'seen': True}, status=status.HTTP_200_OK) + + except SeenPost.DoesNotExist: + return JsonResponse({'seen': False}, status=status.HTTP_200_OK) + + except Submissions.DoesNotExist: + return JsonResponse({'message': 'Submission not found.'}, status=status.HTTP_404_NOT_FOUND) + + + if request.method == 'PUT': + submission = Submissions.objects.get(submission_hash=submission_hash) + + SeenPost.objects.get_or_create(user=user, post_id=submission.pk) + + return JsonResponse({'seen': True}, status=status.HTTP_200_OK) + + else: + return JsonResponse({'message': 'Only GET and PUT requests are allowed.'}, status=status.HTTP_405_METHOD_NOT_ALLOWED) +