Skip to content
Snippets Groups Projects
Verified Commit 0c202527 authored by Ernesto Rico Schmidt's avatar Ernesto Rico Schmidt
Browse files

feat: raise a ValidationError if a note for this timeslot already exists

parent bce12fd6
No related branches found
No related tags found
No related merge requests found
...@@ -28,6 +28,7 @@ from rest_framework.permissions import exceptions ...@@ -28,6 +28,7 @@ from rest_framework.permissions import exceptions
from django.conf import settings from django.conf import settings
from django.contrib.auth.models import User from django.contrib.auth.models import User
from django.core.exceptions import ObjectDoesNotExist from django.core.exceptions import ObjectDoesNotExist
from django.db import IntegrityError
from django.utils import text, timezone from django.utils import text, timezone
from program.models import ( from program.models import (
Category, Category,
...@@ -1023,22 +1024,27 @@ class NoteSerializer(serializers.ModelSerializer): ...@@ -1023,22 +1024,27 @@ class NoteSerializer(serializers.ModelSerializer):
# optional foreign key # optional foreign key
validated_data["image"] = validated_data.pop("image", None) validated_data["image"] = validated_data.pop("image", None)
note = Note.objects.create( try:
created_by=self.context.get("request").user.username, note = Note.objects.create(
**validated_data, created_by=self.context.get("request").user.username,
) **validated_data,
)
note.contributors.set(contributors) except IntegrityError:
note.language.set(language) raise exceptions.ValidationError(
note.topic.set(topic) code="duplicate", detail="note for this timeslot already exists."
)
else:
note.contributors.set(contributors)
note.language.set(language)
note.topic.set(topic)
# optional nested objects # optional nested objects
for link_data in links_data: for link_data in links_data:
NoteLink.objects.create(note=note, **link_data) NoteLink.objects.create(note=note, **link_data)
note.save() note.save()
return note return note
def update(self, instance, validated_data): def update(self, instance, validated_data):
"""Update and return an existing Note instance, given the validated data.""" """Update and return an existing Note instance, given the validated data."""
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment