Skip to content

Commit

Permalink
update validation logic and address comments
Browse files Browse the repository at this point in the history
  • Loading branch information
rm03 committed Nov 26, 2023
1 parent dd74e70 commit 1b7cef0
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 15 deletions.
3 changes: 2 additions & 1 deletion backend/clubs/migrations/0091_applicationextension.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Generated by Django 3.2.18 on 2023-11-23 00:07
# Generated by Django 3.2.18 on 2023-11-25 03:58

import django.db.models.deletion
from django.conf import settings
Expand Down Expand Up @@ -42,5 +42,6 @@ class Migration(migrations.Migration):
),
),
],
options={"unique_together": {("user", "application")}},
),
]
3 changes: 3 additions & 0 deletions backend/clubs/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -1603,6 +1603,9 @@ class ApplicationExtension(models.Model):
)
end_time = models.DateTimeField()

class Meta:
unique_together = (("user", "application"),)


class ApplicationCommittee(models.Model):
"""
Expand Down
47 changes: 39 additions & 8 deletions backend/clubs/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -2444,12 +2444,6 @@ class Meta:
"end_time",
)

def validate_username(self, value):
user = get_user_model().objects.filter(username=value).first()
if not user:
raise serializers.ValidationError("Please provide a valid username!")
return value

def create(self, validated_data):
username = validated_data.get("user").pop("username")
validated_data["user"] = get_user_model().objects.get(username=username)
Expand All @@ -2462,13 +2456,50 @@ def create(self, validated_data):
return super().create(validated_data)

def update(self, instance, validated_data):
user_field = validated_data.pop("user", None)
if user_field:
if user_field := validated_data.pop("user", None):
username = user_field.pop("username")
user = get_user_model().objects.get(username=username)
instance.user = user
return super().update(instance, validated_data)

def validate(self, data):
username = None
if user_field := data.get("user") or not self.instance:
username = user_field.get("username")
user = get_user_model().objects.filter(username=username).first()
if not user:
raise serializers.ValidationError("Please provide a valid username!")

application_pk = self.context["view"].kwargs.get("application_pk")
application = ClubApplication.objects.filter(pk=application_pk).first()

if not application:
raise serializers.ValidationError("Invalid application id!")

if (
(
not self.instance
or (username and self.instance.user.username != username)
)
and ApplicationExtension.objects.filter(
user=user, application=application
).exists()
):
raise serializers.ValidationError(
"An extension for this user and application already exists!"
)

extension_end_time = data.get("end_time")
if (
extension_end_time
and extension_end_time <= application.application_end_time
):
raise serializers.ValidationError(
"Extension end time must be greater than the application end time!"
)

return data


class ApplicationSubmissionSerializer(serializers.ModelSerializer):
committee = ApplicationCommitteeSerializer(required=False, read_only=True)
Expand Down
8 changes: 2 additions & 6 deletions backend/clubs/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -4458,11 +4458,7 @@ def question_response(self, *args, **kwargs):

# prevent submissions outside of the open duration
now = timezone.now()
extension = (
application.extensions.filter(user=self.request.user)
.order_by("-end_time")
.first()
)
extension = application.extensions.filter(user=self.request.user).first()
end_time = (
max(extension.end_time, application.application_end_time)
if extension
Expand Down Expand Up @@ -4814,7 +4810,7 @@ def current(self, *args, **kwargs):
- $ref: "#/components/schemas/ClubApplication"
---
"""
qs = self.get_queryset()
qs = self.get_queryset().prefetch_related("extensions")
now = timezone.now()
user = self.request.user
q = Q(application_end_time__gte=now)
Expand Down

0 comments on commit 1b7cef0

Please sign in to comment.