Skip to content

Commit e28e2ef

Browse files
committed
feat: ApplicationFileAttachmentViewSet
1 parent 0b4ee68 commit e28e2ef

File tree

3 files changed

+36
-21
lines changed

3 files changed

+36
-21
lines changed

backend/samfundet/models/recruitment.py

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ class RecruitmentPosition(CustomBaseModel):
167167
# TODO: Implement interviewer functionality
168168
interviewers = models.ManyToManyField(to=User, help_text='Interviewers for the position', blank=True, related_name='interviewers')
169169

170-
#FIX: Add functionality for setting allowed attachments for application
170+
# FIX: Add functionality for setting allowed attachments for application
171171
recruitment = models.ForeignKey(
172172
Recruitment,
173173
on_delete=models.CASCADE,
@@ -320,15 +320,16 @@ def resolve_gang(self, *, return_id: bool = False) -> Gang | int:
320320
return self.room.resolve_gang(return_id=return_id)
321321

322322

323-
324323
class RecruitmentApplication(CustomBaseModel):
325324
# UUID so that applicants cannot see recruitment info with their own id number
326325
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
327326
application_text = models.TextField(help_text='Application text')
328327
recruitment_position = models.ForeignKey(
329328
RecruitmentPosition, on_delete=models.CASCADE, help_text='The position which is recruiting', related_name='applications'
330329
)
331-
recruitment = models.ForeignKey(Recruitment, on_delete=models.CASCADE, null=True, help_text='The recruitment that is recruiting', related_name='applications')
330+
recruitment = models.ForeignKey(
331+
Recruitment, on_delete=models.CASCADE, null=True, help_text='The recruitment that is recruiting', related_name='applications'
332+
)
332333
user = models.ForeignKey(User, on_delete=models.CASCADE, help_text='The user that is applying', related_name='applications')
333334
applicant_priority = models.PositiveIntegerField(null=True, blank=True, help_text='The priority of the application')
334335

@@ -492,22 +493,24 @@ def update_applicant_state(self) -> None:
492493
application.applicant_state = RecruitmentApplicantStates.NOT_WANTED
493494
application.save()
494495

496+
495497
class ApplicationFileAttachment(CustomBaseModel):
496-
application = models.ForeignKey(RecruitmentApplication,
497-
on_delete=models.CASCADE,
498-
related_name="attachments",
499-
help_text="The recruitment application this file is attached to")
500-
application_file = models.FileField(upload_to="some/path")
498+
application = models.ForeignKey(
499+
RecruitmentApplication, on_delete=models.CASCADE, related_name='attachments', help_text='The recruitment application this file is attached to'
500+
)
501+
application_file = models.FileField(upload_to='some/path')
501502
application_file_type = models.CharField(max_length=50, blank=True)
502503

503504
def clean(self) -> None:
504505
super().clean()
505-
if self.file:
506-
file_type = self.file.content_type
507-
self.file_type = file_type
506+
if self.application_file:
507+
file_type = self.application_file.content_type
508+
self.application_file_type = file_type or ''
508509
allowed_types = [
509-
'image/jpeg', 'image/png', # Images
510-
'video/mp4' # Video
510+
'image/jpeg',
511+
'image/png', # Images
512+
'video/mp4', # Video
513+
'application/pdf',
511514
]
512515
if file_type not in allowed_types:
513516
raise ValidationError('Wrong filetype')
@@ -517,6 +520,7 @@ def clean(self) -> None:
517520
def __str__(self):
518521
return f'Attachment for {self.application} - {self.file.name}'
519522

523+
520524
class RecruitmentInterviewAvailability(CustomBaseModel):
521525
"""This models all possible times for interviews for the given recruitment.
522526

backend/samfundet/serializers.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -84,16 +84,15 @@
8484
class ApplicationFileAttachmentSerializer(CustomBaseSerializer):
8585
class Meta:
8686
model = ApplicationFileAttachment
87+
fields = (
88+
'id',
89+
'application',
90+
'application_file',
91+
'application_file_type',
92+
)
8793

8894
def validate(self, attrs: dict) -> dict:
89-
admission_image = attrs.get('admission_image')
90-
91-
# if admission_image:
92-
# is_image_valid(admission_image)
93-
# else:
94-
# msg = 'Image is invalid'
95-
# raise serializers.ValidationError(msg, code="authorization")
96-
# if
95+
return attrs
9796

9897

9998
class TagSerializer(CustomBaseSerializer):

backend/samfundet/views.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
from .homepage import homepage
5151
from .models.role import Role, UserOrgRole, UserGangRole, UserGangSectionRole
5252
from .serializers import (
53+
ApplicationFileAttachmentSerializer,
5354
TagSerializer,
5455
GangSerializer,
5556
MenuSerializer,
@@ -143,6 +144,7 @@
143144
UserFeedbackModel,
144145
)
145146
from .models.recruitment import (
147+
ApplicationFileAttachment,
146148
Interview,
147149
Recruitment,
148150
InterviewRoom,
@@ -690,6 +692,16 @@ def delete(self, request: Request) -> Response:
690692
# =============================== #
691693

692694

695+
class ApplicationFileAttachmentViewSet(ModelViewSet):
696+
serializer_class = ApplicationFileAttachmentSerializer
697+
queryset = ApplicationFileAttachment.objects.all()
698+
699+
# Typically, you might want extra permission logic here:
700+
# - Only the applicant or a recruiter with certain perms can read attachments
701+
# - Only the applicant can create attachments for *their own* application
702+
# etc.
703+
704+
693705
@method_decorator(ensure_csrf_cookie, 'dispatch')
694706
class RecruitmentView(ModelViewSet):
695707
permission_classes = (DjangoModelPermissionsOrAnonReadOnly,)

0 commit comments

Comments
 (0)