|
1 | 1 | from django.db.models import Q |
2 | 2 | from drf_yasg.utils import swagger_auto_schema |
3 | 3 | from rest_framework import permissions, viewsets |
| 4 | +from rest_framework.response import Response |
| 5 | +from rest_framework import status |
4 | 6 |
|
5 | | -from kanban.models import Board |
6 | | -from kanban.serializers import BoardSerializer |
| 7 | +from kanban.models import Board, BoardColumn |
| 8 | +from kanban.serializers import BoardSerializer, BoardColumnSerializer |
7 | 9 | from projects.models import Collaborator |
8 | 10 |
|
9 | 11 |
|
@@ -52,3 +54,53 @@ def update(self, request, *args, **kwargs): |
52 | 54 | @swagger_auto_schema(tags=["Kanban Boards"]) |
53 | 55 | def destroy(self, request, *args, **kwargs): |
54 | 56 | return super().destroy(request, *args, **kwargs) |
| 57 | + |
| 58 | + |
| 59 | +class BoardColumnViewSet(viewsets.ModelViewSet): |
| 60 | + serializer_class = BoardColumnSerializer |
| 61 | + permission_classes = [permissions.IsAuthenticated] |
| 62 | + |
| 63 | + def get_queryset(self): |
| 64 | + user = self.request.user |
| 65 | + collaborator_projects = Collaborator.objects.filter(user=user).values( |
| 66 | + "project_id" |
| 67 | + ) |
| 68 | + return ( |
| 69 | + BoardColumn.objects.select_related("board", "board__project") |
| 70 | + .filter( |
| 71 | + Q(board__project__leader_id=user.id) |
| 72 | + | Q(board__project_id__in=collaborator_projects) |
| 73 | + ) |
| 74 | + .distinct() |
| 75 | + ) |
| 76 | + |
| 77 | + @swagger_auto_schema(tags=["Kanban Columns"]) |
| 78 | + def list(self, request, *args, **kwargs): |
| 79 | + return super().list(request, *args, **kwargs) |
| 80 | + |
| 81 | + @swagger_auto_schema(tags=["Kanban Columns"]) |
| 82 | + def create(self, request, *args, **kwargs): |
| 83 | + return super().create(request, *args, **kwargs) |
| 84 | + |
| 85 | + @swagger_auto_schema(tags=["Kanban Columns"]) |
| 86 | + def retrieve(self, request, *args, **kwargs): |
| 87 | + return super().retrieve(request, *args, **kwargs) |
| 88 | + |
| 89 | + @swagger_auto_schema(tags=["Kanban Columns"]) |
| 90 | + def partial_update(self, request, *args, **kwargs): |
| 91 | + return super().partial_update(request, *args, **kwargs) |
| 92 | + |
| 93 | + @swagger_auto_schema(tags=["Kanban Columns"]) |
| 94 | + def update(self, request, *args, **kwargs): |
| 95 | + return super().update(request, *args, **kwargs) |
| 96 | + |
| 97 | + @swagger_auto_schema(tags=["Kanban Columns"]) |
| 98 | + def destroy(self, request, *args, **kwargs): |
| 99 | + try: |
| 100 | + return super().destroy(request, *args, **kwargs) |
| 101 | + except Exception as exc: |
| 102 | + from django.core.exceptions import ValidationError |
| 103 | + |
| 104 | + if isinstance(exc, ValidationError): |
| 105 | + return Response(exc.messages, status=status.HTTP_400_BAD_REQUEST) |
| 106 | + raise |
0 commit comments