From 3b4bb6cd6ca283f5fa90e93ebe0e5298dc6a2884 Mon Sep 17 00:00:00 2001 From: Jean-Baptiste <bleme@pm.me> Date: Tue, 2 Jul 2019 18:24:27 +0200 Subject: [PATCH] update: remove foreignkey link when json contains empty string --- djangoldp/serializers.py | 7 ++++++- djangoldp/tests/models.py | 4 ++-- djangoldp/tests/runner.py | 5 +++-- djangoldp/tests/tests_temp.py | 19 ++++++++++++------ djangoldp/tests/tests_update.py | 34 ++++++++++++++++++++++++++++++++- 5 files changed, 57 insertions(+), 12 deletions(-) diff --git a/djangoldp/serializers.py b/djangoldp/serializers.py index e4526d15..4e37dec9 100644 --- a/djangoldp/serializers.py +++ b/djangoldp/serializers.py @@ -292,6 +292,8 @@ class LDPSerializer(HyperlinkedModelSerializer): fields = '__all__' def to_internal_value(self, data): + if data is '': + return '' if self.url_field_name in data: if not isinstance(data, Mapping): message = self.error_messages['invalid'].format( @@ -450,7 +452,10 @@ class LDPSerializer(HyperlinkedModelSerializer): for attr, value in validated_data.items(): if isinstance(value, dict): value = self.update_dict_value(attr, instance, value) - setattr(instance, attr, value) + if value is '' and not isinstance(getattr(instance, attr), str): + setattr(instance, attr, None) + else: + setattr(instance, attr, value) instance.save() diff --git a/djangoldp/tests/models.py b/djangoldp/tests/models.py index eeab3e1b..b23632ae 100644 --- a/djangoldp/tests/models.py +++ b/djangoldp/tests/models.py @@ -30,6 +30,7 @@ class JobOffer(Model): class Conversation(models.Model): description = models.CharField(max_length=255, blank=True, null=True) author_user = models.ForeignKey(settings.AUTH_USER_MODEL) + peer_user = models.ForeignKey(settings.AUTH_USER_MODEL, blank=True, null=True, related_name="peers_conv") class UserProfile(Model): @@ -59,7 +60,6 @@ class Invoice(Model): title = models.CharField(max_length=255, blank=True, null=True) date = models.DateField(blank=True, null=True) - class Meta: depth = 2 permission_classes = [AnonymousReadOnly] @@ -91,4 +91,4 @@ class Post(Model): auto_author = 'author' -get_user_model()._meta.serializer_fields = ['@id', 'username', 'first_name', 'last_name', 'email', 'userprofile', 'conversation_set'] \ No newline at end of file +get_user_model()._meta.serializer_fields = ['@id', 'username', 'first_name', 'last_name', 'email', 'userprofile', 'conversation_set'] diff --git a/djangoldp/tests/runner.py b/djangoldp/tests/runner.py index 2f2958ca..82859e21 100644 --- a/djangoldp/tests/runner.py +++ b/djangoldp/tests/runner.py @@ -59,9 +59,10 @@ failures = test_runner.run_tests([ 'djangoldp.tests.tests_anonymous_permissions', 'djangoldp.tests.tests_update', 'djangoldp.tests.tests_auto_author', - # 'djangoldp.tests.tests_temp' 'djangoldp.tests.tests_get', - 'djangoldp.tests.tests_delete' + 'djangoldp.tests.tests_delete', + # 'djangoldp.tests.tests_temp' + ]) if failures: sys.exit(failures) diff --git a/djangoldp/tests/tests_temp.py b/djangoldp/tests/tests_temp.py index 309931fd..8a4b4d0f 100644 --- a/djangoldp/tests/tests_temp.py +++ b/djangoldp/tests/tests_temp.py @@ -1,11 +1,18 @@ -import json +from django.contrib.auth.models import User +from django.test import TestCase +from rest_framework.test import APIRequestFactory, APIClient +from rest_framework.utils import json -from rest_framework.test import APITestCase +from djangoldp.serializers import LDPSerializer +from djangoldp.tests.models import Post, UserProfile +from djangoldp.tests.models import Skill, JobOffer, Conversation, Message -from djangoldp.models import Model -from djangoldp.tests.models import Invoice +class TestTemp(TestCase): -class TestTemp(APITestCase): - pass + def setUp(self): + self.factory = APIRequestFactory() + self.client = APIClient() + def tearDown(self): + pass diff --git a/djangoldp/tests/tests_update.py b/djangoldp/tests/tests_update.py index 6f1e677d..4e31ea7d 100644 --- a/djangoldp/tests/tests_update.py +++ b/djangoldp/tests/tests_update.py @@ -358,4 +358,36 @@ class Update(TestCase): response = self.client.put('/users/{}/'.format(user.pk), data=json.dumps(body), content_type='application/ld+json') self.assertEqual(response.status_code, 200) - self.assertIn('conversation_set', response.data) \ No newline at end of file + self.assertIn('conversation_set', response.data) + + def test_missing_field_should_not_be_removed_with_fk_relation(self): + user = User.objects.create(username="alex", password="test") + peer = User.objects.create(username="sylvain", password="test2") + conversation = Conversation.objects.create(author_user=user, peer_user=peer, description="conversation description") + body = [ + { + '@id': "/conversations/{}/".format(conversation.pk), + 'http://happy-dev.fr/owl/#description': "conversation update", + } + ] + response = self.client.put('/conversations/{}/'.format(conversation.pk), data=json.dumps(body), + content_type='application/ld+json') + self.assertEqual(response.status_code, 200) + self.assertIn('peer_user', response.data) + + def test_empty_field_should_be_removed_with_fk_relation(self): + user = User.objects.create(username="alex", password="test") + peer = User.objects.create(username="sylvain", password="test2") + conversation = Conversation.objects.create(author_user=user, peer_user=peer, description="conversation description") + body = [ + { + '@id': "/conversations/{}/".format(conversation.pk), + 'http://happy-dev.fr/owl/#description': "conversation update", + 'http://happy-dev.fr/owl/#peer_user': "" + } + ] + response = self.client.put('/conversations/{}/'.format(conversation.pk), data=json.dumps(body), + content_type='application/ld+json') + self.assertEqual(response.status_code, 200) + self.assertEqual(response.data['peer_user'], None) + -- GitLab