Skip to content

Commit aedc5da

Browse files
committed
add views for unsubscribing
1 parent 9005e2f commit aedc5da

2 files changed

Lines changed: 37 additions & 2 deletions

File tree

projects/urls.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
ProjectRecommendedUsers,
1414
SetLikeOnProject,
1515
ProjectSubscribe,
16+
ProjectUnsubscribe,
1617
ProjectSubscribers,
1718
)
1819

@@ -23,6 +24,7 @@
2324
path("<int:pk>/like/", SetLikeOnProject.as_view()),
2425
path("<int:project_pk>/news/", NewsList.as_view()),
2526
path("<int:project_pk>/subscribe/", ProjectSubscribe.as_view()),
27+
path("<int:project_pk>/unsubscribe/", ProjectUnsubscribe.as_view()),
2628
path("<int:project_pk>/subscribers/", ProjectSubscribers.as_view()),
2729
path("<int:project_pk>/news/<int:pk>/", NewsDetail.as_view()),
2830
path("<int:project_pk>/news/<int:pk>/set_viewed/", NewsDetailSetViewed.as_view()),

projects/views.py

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -420,11 +420,44 @@ def post(self, request, project_pk):
420420
project = Project.objects.get(pk=project_pk)
421421
except Project.DoesNotExist:
422422
raise NotFound
423-
424-
project.subscribers.add(request.user)
423+
try:
424+
project.subscribers.add(request.user)
425+
except Exception:
426+
return Response(
427+
{
428+
"detail": f"User {request.user.id} is not part of project {project.pk}."
429+
},
430+
status=status.HTTP_400_BAD_REQUEST,
431+
)
425432

426433
logger.info(f"User {request.user.id} subscribed to project {project_pk}")
427434

428435
return Response(
429436
{"detail": "Subscriber was successfully added"}, status=status.HTTP_200_OK
430437
)
438+
439+
440+
class ProjectUnsubscribe(APIView):
441+
permission_classes = [IsAuthenticated]
442+
443+
def post(self, request, project_pk):
444+
try:
445+
project = Project.objects.get(pk=project_pk)
446+
except Project.DoesNotExist:
447+
raise NotFound
448+
try:
449+
project.subscribers.remove(request.user)
450+
# todo: add more specific error here
451+
except Exception:
452+
return Response(
453+
{
454+
"detail": f"User {request.user.id} is not part of project {project.pk}."
455+
},
456+
status=status.HTTP_400_BAD_REQUEST,
457+
)
458+
459+
logger.info(f"User {request.user.id} unsubscribed to project {project_pk}")
460+
461+
return Response(
462+
{"detail": "Subscriber was successfully removed"}, status=status.HTTP_200_OK
463+
)

0 commit comments

Comments
 (0)