|
38 | 38 |
|
39 | 39 | from .utils import generate_timeslots, get_occupied_timeslots_from_request |
40 | 40 | from .serializers import ( |
| 41 | + ApplicationFileAttachmentSerializer, |
41 | 42 | InterviewSerializer, |
42 | 43 | RecruitmentSerializer, |
43 | 44 | InterviewRoomSerializer, |
@@ -121,13 +122,38 @@ def verify_signature(*, payload_body: Any, secret_token: str, signature_header: |
121 | 122 |
|
122 | 123 |
|
123 | 124 | class ApplicationFileAttachmentViewSet(ModelViewSet): |
124 | | - serializer_class = ApplicationFileAttachmentSerializer |
125 | 125 | queryset = ApplicationFileAttachment.objects.all() |
| 126 | + serializer_class = ApplicationFileAttachmentSerializer |
| 127 | + permission_classes = [IsAuthenticated] |
| 128 | + parser_classes = (MultiPartParser, FormParser) |
126 | 129 |
|
127 | | - # Typically, you might want extra permission logic here: |
128 | | - # - Only the applicant or a recruiter with certain perms can read attachments |
129 | | - # - Only the applicant can create attachments for *their own* application |
130 | | - # etc. |
| 130 | + def get_queryset(self) -> Response | None: |
| 131 | + # Restrict to the user's applications or recruiter permissions |
| 132 | + # FIX: Consider permissions |
| 133 | + user = self.request.user |
| 134 | + if user.is_staff: |
| 135 | + return self.queryset |
| 136 | + return self.queryset.filter(application__user=user) |
| 137 | + |
| 138 | + def create(self, request: Request) -> Response | None: |
| 139 | + application_id = request.data.get('application_id') |
| 140 | + try: |
| 141 | + application = RecruitmentApplication.objects.get(id=application_id, user=request.user) |
| 142 | + except RecruitmentApplication.DoesNotExist: |
| 143 | + return Response({'error': 'Application not found or not authorized'}, status=status.HTTP_404_NOT_FOUND) |
| 144 | + |
| 145 | + serializer = self.get_serializer(data=request.data, context={'application': application}) |
| 146 | + serializer.is_valid(raise_exception=True) |
| 147 | + self.perform_create(serializer) |
| 148 | + return Response(serializer.data, status=status.HTTP_201_CREATED) |
| 149 | + |
| 150 | + def destroy(self, request: Request) -> Response | None: |
| 151 | + instance = self.get_object() |
| 152 | + # FIX: Consider permissions |
| 153 | + if instance.application.user != request.user and not request.user.is_staff: |
| 154 | + return Response({'error': 'Not authorized'}, status=status.HTTP_403_FORBIDDEN) |
| 155 | + self.perform_destroy(instance) |
| 156 | + return Response(status=status.HTTP_204_NO_CONTENT) |
131 | 157 |
|
132 | 158 |
|
133 | 159 | @method_decorator(ensure_csrf_cookie, 'dispatch') |
|
0 commit comments