Skip to content
Snippets Groups Projects
Commit df0a759f authored by Jean-Baptiste's avatar Jean-Baptiste
Browse files

syntax: Refactor after code review

parent d7df4f31
No related branches found
No related tags found
1 merge request!24Resolve "Get URL of saved resource from its model"
Pipeline #735 passed
...@@ -27,12 +27,14 @@ django-admin startproject myldpserver ...@@ -27,12 +27,14 @@ django-admin startproject myldpserver
``` ```
3. Create your django model inside a file myldpserver/myldpserver/models.py 3. Create your django model inside a file myldpserver/myldpserver/models.py
Note that container_path will be use to resolve instance iri and container iri
In the future it could also be used to auto configure django router (e.g. urls.py)
``` ```
from djangoldp.models import LDPModel from djangoldp.models import Model
class Todo(LDPModel): class Todo(Model):
ldp_path = "/my-path/" container_path = "/my-path/"
name = models.CharField(max_length=255) name = models.CharField(max_length=255)
deadline = models.DateTimeField() deadline = models.DateTimeField()
......
...@@ -2,23 +2,23 @@ from django.conf import settings ...@@ -2,23 +2,23 @@ from django.conf import settings
from django.db import models from django.db import models
class LDPModel(models.Model): class Model(models.Model):
ldp_path = None container_path = None
def get_resource_path(self): def get_absolute_url(self):
return LDPModel.resource_path(self) return Model.resource_id(self)
def get_container_path(self): def get_container_id(self):
return LDPModel.container_path(self) return Model.container_id(self)
@classmethod @classmethod
def resource_path(cls, instance): def resource_id(cls, instance):
return "{}{}".format(LDPModel.container_path(instance), instance.pk) return "{}{}".format(Model.container_id(instance), instance.pk)
@classmethod @classmethod
def container_path(cls, instance): def container_id(cls, instance):
if isinstance(instance, cls): if isinstance(instance, cls):
path = instance.ldp_path path = instance.container_path
else: else:
from django.urls import get_resolver from django.urls import get_resolver
view_name = '{}-list'.format(instance._meta.object_name.lower()) view_name = '{}-list'.format(instance._meta.object_name.lower())
......
from django.conf import settings from django.conf import settings
from django.db import models from django.db import models
from djangoldp.models import LDPModel from djangoldp.models import Model
class Skill(models.Model): class Skill(models.Model):
...@@ -29,7 +29,7 @@ class Dummy(models.Model): ...@@ -29,7 +29,7 @@ class Dummy(models.Model):
some = models.CharField(max_length=255, blank=True, null=True) some = models.CharField(max_length=255, blank=True, null=True)
class LDPDummy(LDPModel): class LDPDummy(Model):
some = models.CharField(max_length=255, blank=True, null=True) some = models.CharField(max_length=255, blank=True, null=True)
ldp_path = "ldp-dummys" container_path = "ldp-dummys"
...@@ -2,7 +2,7 @@ import unittest ...@@ -2,7 +2,7 @@ import unittest
from django.test import TestCase from django.test import TestCase
from djangoldp.models import LDPModel from djangoldp.models import Model
from djangoldp.tests.models import Dummy, LDPDummy from djangoldp.tests.models import Dummy, LDPDummy
...@@ -10,15 +10,15 @@ class LDPModelTest(TestCase): ...@@ -10,15 +10,15 @@ class LDPModelTest(TestCase):
def test_class_not_inheriting_ldp_model(self): def test_class_not_inheriting_ldp_model(self):
dummy = Dummy.objects.create(some="text") dummy = Dummy.objects.create(some="text")
self.assertEquals("/dummys/", LDPModel.container_path(dummy)) self.assertEquals("/dummys/", Model.container_id(dummy))
self.assertEquals("/dummys/{}".format(dummy.pk), LDPModel.resource_path(dummy)) self.assertEquals("/dummys/{}".format(dummy.pk), Model.resource_id(dummy))
def test_class_inheriting_ldp_model(self): def test_class_inheriting_ldp_model(self):
dummy = LDPDummy.objects.create(some="text") dummy = LDPDummy.objects.create(some="text")
self.assertEquals("/ldp-dummys/", dummy.get_container_path()) self.assertEquals("/ldp-dummys/", dummy.get_container_id())
self.assertEquals("/ldp-dummys/{}".format(dummy.pk), dummy.get_resource_path()) self.assertEquals("/ldp-dummys/{}".format(dummy.pk), dummy.get_absolute_url())
self.assertEquals("/ldp-dummys/", LDPModel.container_path(dummy)) self.assertEquals("/ldp-dummys/", Model.container_id(dummy))
self.assertEquals("/ldp-dummys/{}".format(dummy.pk), LDPModel.resource_path(dummy)) self.assertEquals("/ldp-dummys/{}".format(dummy.pk), Model.resource_id(dummy))
@unittest.skip("futur feature: avoid urls.py on apps") @unittest.skip("futur feature: avoid urls.py on apps")
...@@ -28,4 +28,4 @@ class LDPModelTest(TestCase): ...@@ -28,4 +28,4 @@ class LDPModelTest(TestCase):
view_name = '{}-list'.format(dummy._meta.object_name.lower()) view_name = '{}-list'.format(dummy._meta.object_name.lower())
path = '/{}'.format(get_resolver().reverse_dict[view_name][0][0][0], dummy.pk) path = '/{}'.format(get_resolver().reverse_dict[view_name][0][0][0], dummy.pk)
self.assertEquals(path, dummy.get_resource_path()) self.assertEquals(path, dummy.get_absolute_url())
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