-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #37 from SELab-2/views-serializers
Views serializers
- Loading branch information
Showing
6 changed files
with
77 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,34 @@ | ||
from rest_framework import serializers | ||
from api.models.project import Project | ||
|
||
from datetime import datetime | ||
|
||
|
||
class ProjectSerializer(serializers.ModelSerializer): | ||
class Meta: | ||
model = Project | ||
fields = '__all__' | ||
fields = '__all__' | ||
|
||
def create(self, validated_data): | ||
deadline = validated_data.pop('deadline') | ||
|
||
validate_deadline(deadline) | ||
|
||
project = Project.objects.create(**validated_data) | ||
project.deadline.set(deadline) | ||
return project | ||
|
||
def update(self, instance, validated_data): | ||
deadline = validated_data.pop('deadline') | ||
|
||
validate_deadline(deadline) | ||
|
||
instance = Project.objects.create(**validated_data) | ||
instance.deadline.set(deadline) | ||
instance.save() | ||
return instance | ||
|
||
|
||
def validate_deadline(deadline): | ||
if deadline <= datetime.now(): | ||
raise serializers.ValidationError("Deadline moet in de toekomst liggen") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters