Skip to content

Commit

Permalink
chore: Support partial update for project
Browse files Browse the repository at this point in the history
  • Loading branch information
Topvennie committed Apr 9, 2024
1 parent 4ef4d9f commit beae0e4
Showing 1 changed file with 16 additions and 12 deletions.
28 changes: 16 additions & 12 deletions backend/api/serializers/project_serializer.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
from api.models.checks import FileExtension
from api.models.group import Group
from api.models.project import Project
from api.serializers.checks_serializer import StructureCheckSerializer
from api.serializers.submission_serializer import SubmissionSerializer
from django.utils import timezone
from django.utils.translation import gettext
from rest_framework import serializers
from api.models.project import Project
from api.models.group import Group
from rest_framework.exceptions import ValidationError
from django.utils import timezone
from api.models.checks import FileExtension
from api.serializers.submission_serializer import SubmissionSerializer
from api.serializers.checks_serializer import StructureCheckSerializer


class ProjectSerializer(serializers.ModelSerializer):
Expand Down Expand Up @@ -41,17 +41,21 @@ class Meta:
fields = "__all__"

def validate(self, data):
if "course" in self.context:
data["course_id"] = self.context["course"].id
else:
raise ValidationError(gettext("project.errors.context"))
if not self.partial:
# Only reauire course if it is not a partial update
if "course" in self.context:
data["course_id"] = self.context["course"].id
else:
raise ValidationError(gettext("project.errors.context"))

# Check if start date of the project is not in the past
if data["start_date"] < timezone.now().replace(hour=0, minute=0, second=0):
if "start_date" in data and data["start_date"] < timezone.now().replace(hour=0, minute=0, second=0):
raise ValidationError(gettext("project.errors.start_date_in_past"))

# Check if deadline of the project is before the start date
if data["deadline"] < data["start_date"]:
# Data will always contain start_date if it's not a partial update. Same goes for deadline
start_date = data["start_date"] if "start_date" in data else self.instance.start_date
if "deadline" in data and data["deadline"] < start_date:
raise ValidationError(gettext("project.errors.deadline_before_start_date"))

return data
Expand Down

0 comments on commit beae0e4

Please sign in to comment.