|
| 1 | +import logging |
| 2 | + |
1 | 3 | from django.contrib.auth import get_user_model |
2 | 4 | from django.db.models import Q |
3 | 5 | from django_filters import rest_framework as filters |
| 6 | +from drf_yasg import openapi |
| 7 | +from drf_yasg.utils import swagger_auto_schema |
4 | 8 | from rest_framework import generics, permissions, status |
| 9 | +from rest_framework.exceptions import NotFound |
5 | 10 | from rest_framework.permissions import IsAuthenticated |
6 | 11 | from rest_framework.response import Response |
7 | 12 | from rest_framework.views import APIView |
|
33 | 38 | ProjectCollaboratorSerializer, |
34 | 39 | ProjectNewsListSerializer, |
35 | 40 | ProjectNewsDetailSerializer, |
| 41 | + ProjectSubscribersListSerializer, |
36 | 42 | ) |
37 | 43 | from users.models import LikesOnProject |
38 | 44 | from users.serializers import UserListSerializer |
39 | 45 | from vacancy.models import VacancyResponse |
40 | 46 | from vacancy.serializers import VacancyResponseListSerializer |
41 | 47 |
|
| 48 | +logger = logging.getLogger() |
| 49 | + |
42 | 50 | User = get_user_model() |
43 | 51 |
|
44 | 52 |
|
@@ -381,3 +389,75 @@ def post(self, request, *args, **kwargs): |
381 | 389 | return Response(status=status.HTTP_200_OK) |
382 | 390 | except ProjectNews.DoesNotExist: |
383 | 391 | return Response(status=status.HTTP_404_NOT_FOUND) |
| 392 | + |
| 393 | + |
| 394 | +class ProjectSubscribers(APIView): |
| 395 | + permission_classes = [IsAuthenticated] |
| 396 | + |
| 397 | + @swagger_auto_schema( |
| 398 | + responses={ |
| 399 | + 200: openapi.Response( |
| 400 | + "List of project subscribers", ProjectSubscribersListSerializer(many=True) |
| 401 | + ) |
| 402 | + } |
| 403 | + ) |
| 404 | + def get(self, request, *args, **kwargs): |
| 405 | + try: |
| 406 | + project = Project.objects.get(pk=self.kwargs["project_pk"]) |
| 407 | + except Project.DoesNotExist: |
| 408 | + raise NotFound |
| 409 | + subscribers = ProjectSubscribersListSerializer( |
| 410 | + project.subscribers.all(), many=True |
| 411 | + ).data |
| 412 | + return Response(subscribers, status=status.HTTP_200_OK) |
| 413 | + |
| 414 | + |
| 415 | +class ProjectSubscribe(APIView): |
| 416 | + permission_classes = [IsAuthenticated] |
| 417 | + |
| 418 | + def post(self, request, project_pk): |
| 419 | + try: |
| 420 | + project = Project.objects.get(pk=project_pk) |
| 421 | + except Project.DoesNotExist: |
| 422 | + raise NotFound |
| 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 | + ) |
| 432 | + |
| 433 | + logger.info(f"User {request.user.id} subscribed to project {project_pk}") |
| 434 | + |
| 435 | + return Response( |
| 436 | + {"detail": "Subscriber was successfully added"}, status=status.HTTP_200_OK |
| 437 | + ) |
| 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