Commit de45279c authored by Jean-Baptiste's avatar Jean-Baptiste

update: federated POST and PUT

parent fde2f2c8
from django.conf import settings
from django.contrib.auth.models import User
from django.db import models
from django.db.models.base import ModelBase
......@@ -138,7 +139,7 @@ class Model(models.Model):
@classmethod
def is_external(cls, value):
try:
return value.urlid is not None
return value.urlid is not None and not value.urlid.startswith(settings.SITE_URL)
except:
return False
......
......@@ -339,6 +339,8 @@ class LDPSerializer(HyperlinkedModelSerializer):
super().__init__(**kwargs)
def get_value(self, dictionary):
if self.field_name == 'urlid':
self.field_name = '@id'
try:
object_list = dictionary["@graph"]
if self.parent.instance is None:
......@@ -354,6 +356,9 @@ class LDPSerializer(HyperlinkedModelSerializer):
except KeyError:
value = super().get_value(dictionary)
if self.field_name == '@id' and value == './':
self.field_name = 'urlid'
return None
return self.manage_empty(value)
def manage_empty(self, value):
......@@ -384,6 +389,8 @@ class LDPSerializer(HyperlinkedModelSerializer):
fields = '__all__'
def to_internal_value(self, data):
if self.url_field_name in data and not 'urlid' in data and data[self.url_field_name].startswith('http'):
data['urlid'] = data[self.url_field_name]
if data is '':
return ''
if self.url_field_name in data:
......@@ -397,6 +404,7 @@ class LDPSerializer(HyperlinkedModelSerializer):
ret = OrderedDict()
errors = OrderedDict()
fields = list(filter(lambda x: x.field_name in data, self._writable_fields))
for field in fields:
......
......@@ -40,7 +40,7 @@ class JobOffer(Model):
authenticated_perms = ['inherit', 'change', 'add']
owner_perms = ['inherit', 'delete', 'control']
nested_fields = ["skills"]
serializer_fields = ["@id", "title", "skills", "recent_skills", "resources", "slug", "some_skill"]
serializer_fields = ["@id", "title", "skills", "recent_skills", "resources", "slug", "some_skill", "urlid"]
container_path = "job-offers/"
lookup_field = 'slug'
......
......@@ -268,6 +268,19 @@ class Save(TestCase):
self.assertEqual(response.data['resources']['ldp:contains'][0]['@id'], "http://testserver/resources/{}/".format(resource.pk))
self.assertEqual(response.data['title'], "new job")
def test_nested_container_federated(self):
resource = Resource.objects.create()
body = {
'http://happy-dev.fr/owl/#@id': "http://external.job/job/1",
}
response = self.client.post('/resources/{}/joboffers/'.format(resource.pk),
data=json.dumps(body),
content_type='application/ld+json')
self.assertEqual(response.status_code, 201)
self.assertEqual(response.data['resources']['ldp:contains'][0]['@id'], "http://testserver/resources/{}/".format(resource.pk))
self.assertEqual(response.data['@id'], "http://external.job/job/1")
def test_embedded_context_2(self):
body = {
'@id': "./",
......
......@@ -12,3 +12,4 @@ class TestTemp(TestCase):
def tearDown(self):
pass
......@@ -264,7 +264,7 @@ class Update(TestCase):
def test_put_resource(self):
post = Post.objects.create(content="content")
body = [{
'@id': '/posts/{}/'.format(post.pk),
'@id': 'http://testserver.com/posts/{}/'.format(post.pk),
'http://happy-dev.fr/owl/#content': "post content"}]
response = self.client.put('/posts/{}/'.format(post.pk), data=json.dumps(body),
content_type='application/ld+json')
......@@ -401,7 +401,7 @@ class Update(TestCase):
job = JobOffer.objects.create(title="first title", slug="job")
body = {
'http://happy-dev.fr/owl/#joboffers': {
'@id': 'http://testserver/job-offers/{}/'.format(job.slug),
'@id': 'http://testserver.com/job-offers/{}/'.format(job.slug),
}
}
......@@ -410,7 +410,7 @@ class Update(TestCase):
content_type='application/ld+json')
self.assertEqual(response.status_code, 200)
self.assertEqual(response.data['joboffers']['ldp:contains'][0]['@id'],
"http://testserver/job-offers/{}/".format(job.slug))
"http://testserver.com/job-offers/{}/".format(job.slug))
self.assertEqual(response.data['joboffers']['ldp:contains'][0]['title'], "first title")
def test_m2m_new_link_bis(self):
......@@ -419,9 +419,9 @@ class Update(TestCase):
body = {
'http://happy-dev.fr/owl/#joboffers':
{
'@id': "http://testserver/resources/{}/joboffers/".format(resource.pk),
'@id': "http://testserver.com/resources/{}/joboffers/".format(resource.pk),
'ldp:contains': [
{'@id': 'http://testserver/job-offers/{}/'.format(job.slug),
{'@id': 'http://testserver.com/job-offers/{}/'.format(job.slug),
'http://happy-dev.fr/owl/#title': "new job",
},
]
......@@ -433,7 +433,7 @@ class Update(TestCase):
content_type='application/ld+json')
self.assertEqual(response.status_code, 200)
self.assertEqual(response.data['joboffers']['ldp:contains'][0]['@id'],
"http://testserver/job-offers/{}/".format(job.slug))
"http://testserver.com/job-offers/{}/".format(job.slug))
self.assertEqual(response.data['joboffers']['ldp:contains'][0]['title'], "new job")
def test_m2m_new_link_embedded(self):
......@@ -463,7 +463,7 @@ class Update(TestCase):
# '@id': "http://testserver/resources/{}/joboffers/".format(resource.pk),
'ldp:contains': [
{
'@id': 'http://testserver/job-offers/{}/'.format(job.slug),
'@id': 'http://testserver.com/job-offers/{}/'.format(job.slug),
'http://happy-dev.fr/owl/#title': "new job",
}
]
......@@ -475,5 +475,20 @@ class Update(TestCase):
content_type='application/ld+json')
self.assertEqual(response.status_code, 200)
self.assertEqual(response.data['joboffers']['ldp:contains'][0]['@id'],
"http://testserver/job-offers/{}/".format(job.slug))
"http://testserver.com/job-offers/{}/".format(job.slug))
self.assertEqual(response.data['joboffers']['ldp:contains'][0]['title'], "new job")
def test_m2m_new_link_federated(self):
resource = Resource.objects.create()
body = {
'http://happy-dev.fr/owl/#joboffers': {
'http://happy-dev.fr/owl/#@id': 'http://external.job/job/1',
}
}
response = self.client.put('/resources/{}/'.format(resource.pk),
data=json.dumps(body),
content_type='application/ld+json')
self.assertEqual(response.status_code, 200)
self.assertEqual(response.data['joboffers']['ldp:contains'][0]['@id'],
"http://external.job/job/1")
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment