Skip to content

Commit 1b7cef0

Browse files
committed
update validation logic and address comments
1 parent dd74e70 commit 1b7cef0

File tree

4 files changed

+46
-15
lines changed

4 files changed

+46
-15
lines changed

backend/clubs/migrations/0091_applicationextension.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Generated by Django 3.2.18 on 2023-11-23 00:07
1+
# Generated by Django 3.2.18 on 2023-11-25 03:58
22

33
import django.db.models.deletion
44
from django.conf import settings
@@ -42,5 +42,6 @@ class Migration(migrations.Migration):
4242
),
4343
),
4444
],
45+
options={"unique_together": {("user", "application")}},
4546
),
4647
]

backend/clubs/models.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1603,6 +1603,9 @@ class ApplicationExtension(models.Model):
16031603
)
16041604
end_time = models.DateTimeField()
16051605

1606+
class Meta:
1607+
unique_together = (("user", "application"),)
1608+
16061609

16071610
class ApplicationCommittee(models.Model):
16081611
"""

backend/clubs/serializers.py

Lines changed: 39 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2444,12 +2444,6 @@ class Meta:
24442444
"end_time",
24452445
)
24462446

2447-
def validate_username(self, value):
2448-
user = get_user_model().objects.filter(username=value).first()
2449-
if not user:
2450-
raise serializers.ValidationError("Please provide a valid username!")
2451-
return value
2452-
24532447
def create(self, validated_data):
24542448
username = validated_data.get("user").pop("username")
24552449
validated_data["user"] = get_user_model().objects.get(username=username)
@@ -2462,13 +2456,50 @@ def create(self, validated_data):
24622456
return super().create(validated_data)
24632457

24642458
def update(self, instance, validated_data):
2465-
user_field = validated_data.pop("user", None)
2466-
if user_field:
2459+
if user_field := validated_data.pop("user", None):
24672460
username = user_field.pop("username")
24682461
user = get_user_model().objects.get(username=username)
24692462
instance.user = user
24702463
return super().update(instance, validated_data)
24712464

2465+
def validate(self, data):
2466+
username = None
2467+
if user_field := data.get("user") or not self.instance:
2468+
username = user_field.get("username")
2469+
user = get_user_model().objects.filter(username=username).first()
2470+
if not user:
2471+
raise serializers.ValidationError("Please provide a valid username!")
2472+
2473+
application_pk = self.context["view"].kwargs.get("application_pk")
2474+
application = ClubApplication.objects.filter(pk=application_pk).first()
2475+
2476+
if not application:
2477+
raise serializers.ValidationError("Invalid application id!")
2478+
2479+
if (
2480+
(
2481+
not self.instance
2482+
or (username and self.instance.user.username != username)
2483+
)
2484+
and ApplicationExtension.objects.filter(
2485+
user=user, application=application
2486+
).exists()
2487+
):
2488+
raise serializers.ValidationError(
2489+
"An extension for this user and application already exists!"
2490+
)
2491+
2492+
extension_end_time = data.get("end_time")
2493+
if (
2494+
extension_end_time
2495+
and extension_end_time <= application.application_end_time
2496+
):
2497+
raise serializers.ValidationError(
2498+
"Extension end time must be greater than the application end time!"
2499+
)
2500+
2501+
return data
2502+
24722503

24732504
class ApplicationSubmissionSerializer(serializers.ModelSerializer):
24742505
committee = ApplicationCommitteeSerializer(required=False, read_only=True)

backend/clubs/views.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4458,11 +4458,7 @@ def question_response(self, *args, **kwargs):
44584458

44594459
# prevent submissions outside of the open duration
44604460
now = timezone.now()
4461-
extension = (
4462-
application.extensions.filter(user=self.request.user)
4463-
.order_by("-end_time")
4464-
.first()
4465-
)
4461+
extension = application.extensions.filter(user=self.request.user).first()
44664462
end_time = (
44674463
max(extension.end_time, application.application_end_time)
44684464
if extension
@@ -4814,7 +4810,7 @@ def current(self, *args, **kwargs):
48144810
- $ref: "#/components/schemas/ClubApplication"
48154811
---
48164812
"""
4817-
qs = self.get_queryset()
4813+
qs = self.get_queryset().prefetch_related("extensions")
48184814
now = timezone.now()
48194815
user = self.request.user
48204816
q = Q(application_end_time__gte=now)

0 commit comments

Comments
 (0)