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

syntax: Refactor after code review

parent d7df4f31
Pipeline #2253 passed with stage
in 27 seconds
......@@ -27,12 +27,14 @@ django-admin startproject myldpserver
```
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):
ldp_path = "/my-path/"
class Todo(Model):
container_path = "/my-path/"
name = models.CharField(max_length=255)
deadline = models.DateTimeField()
......
......@@ -2,23 +2,23 @@ from django.conf import settings
from django.db import models
class LDPModel(models.Model):
ldp_path = None
class Model(models.Model):
container_path = None
def get_resource_path(self):
return LDPModel.resource_path(self)
def get_absolute_url(self):
return Model.resource_id(self)
def get_container_path(self):
return LDPModel.container_path(self)
def get_container_id(self):
return Model.container_id(self)
@classmethod
def resource_path(cls, instance):
return "{}{}".format(LDPModel.container_path(instance), instance.pk)
def resource_id(cls, instance):
return "{}{}".format(Model.container_id(instance), instance.pk)
@classmethod
def container_path(cls, instance):
def container_id(cls, instance):
if isinstance(instance, cls):
path = instance.ldp_path
path = instance.container_path
else:
from django.urls import get_resolver
view_name = '{}-list'.format(instance._meta.object_name.lower())
......
from django.conf import settings
from django.db import models
from djangoldp.models import LDPModel
from djangoldp.models import Model
class Skill(models.Model):
......@@ -29,7 +29,7 @@ class Dummy(models.Model):
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)
ldp_path = "ldp-dummys"
container_path = "ldp-dummys"
......@@ -2,7 +2,7 @@ import unittest
from django.test import TestCase
from djangoldp.models import LDPModel
from djangoldp.models import Model
from djangoldp.tests.models import Dummy, LDPDummy
......@@ -10,15 +10,15 @@ class LDPModelTest(TestCase):
def test_class_not_inheriting_ldp_model(self):
dummy = Dummy.objects.create(some="text")
self.assertEquals("/dummys/", LDPModel.container_path(dummy))
self.assertEquals("/dummys/{}".format(dummy.pk), LDPModel.resource_path(dummy))
self.assertEquals("/dummys/", Model.container_id(dummy))
self.assertEquals("/dummys/{}".format(dummy.pk), Model.resource_id(dummy))
def test_class_inheriting_ldp_model(self):
dummy = LDPDummy.objects.create(some="text")
self.assertEquals("/ldp-dummys/", dummy.get_container_path())
self.assertEquals("/ldp-dummys/{}".format(dummy.pk), dummy.get_resource_path())
self.assertEquals("/ldp-dummys/", LDPModel.container_path(dummy))
self.assertEquals("/ldp-dummys/{}".format(dummy.pk), LDPModel.resource_path(dummy))
self.assertEquals("/ldp-dummys/", dummy.get_container_id())
self.assertEquals("/ldp-dummys/{}".format(dummy.pk), dummy.get_absolute_url())
self.assertEquals("/ldp-dummys/", Model.container_id(dummy))
self.assertEquals("/ldp-dummys/{}".format(dummy.pk), Model.resource_id(dummy))
@unittest.skip("futur feature: avoid urls.py on apps")
......@@ -28,4 +28,4 @@ class LDPModelTest(TestCase):
view_name = '{}-list'.format(dummy._meta.object_name.lower())
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())
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