Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
D
Django LDP
Project overview
Project overview
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
20
Issues
20
List
Boards
Labels
Milestones
Merge Requests
5
Merge Requests
5
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Packages
Packages
Container Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Startin blox
djangoldp-packages
Django LDP
Commits
cee01ada
Commit
cee01ada
authored
Sep 11, 2019
by
Jean-Baptiste
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
update: auto urlid on save
parent
de45279c
Changes
8
Hide whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
121 additions
and
18 deletions
+121
-18
djangoldp/migrations/0003_auto_20190911_0931.py
djangoldp/migrations/0003_auto_20190911_0931.py
+21
-0
djangoldp/models.py
djangoldp/models.py
+20
-4
djangoldp/tests/models.py
djangoldp/tests/models.py
+2
-1
djangoldp/tests/runner.py
djangoldp/tests/runner.py
+3
-2
djangoldp/tests/tests_ldp_model.py
djangoldp/tests/tests_ldp_model.py
+2
-2
djangoldp/tests/tests_save.py
djangoldp/tests/tests_save.py
+42
-8
djangoldp/tests/tests_temp.py
djangoldp/tests/tests_temp.py
+30
-0
djangoldp/tests/tests_update.py
djangoldp/tests/tests_update.py
+1
-1
No files found.
djangoldp/migrations/0003_auto_20190911_0931.py
0 → 100644
View file @
cee01ada
# -*- coding: utf-8 -*-
# Generated by Django 1.11 on 2019-09-11 09:31
from
__future__
import
unicode_literals
from
django.db
import
migrations
import
djangoldp.fields
class
Migration
(
migrations
.
Migration
):
dependencies
=
[
(
'djangoldp'
,
'0002_auto_20190906_0642'
),
]
operations
=
[
migrations
.
AlterField
(
model_name
=
'ldpsource'
,
name
=
'urlid'
,
field
=
djangoldp
.
fields
.
LDPUrlField
(
blank
=
True
,
null
=
True
,
unique
=
True
),
),
]
djangoldp/models.py
View file @
cee01ada
...
...
@@ -2,8 +2,10 @@ from django.conf import settings
from
django.contrib.auth.models
import
User
from
django.db
import
models
from
django.db.models.base
import
ModelBase
from
django.db.models.signals
import
pre_save
,
post_save
from
django.dispatch
import
receiver
from
django.urls
import
get_resolver
from
django.utils.datastructures
import
MultiValueDict
,
MultiValueDict
KeyError
from
django.utils.datastructures
import
MultiValueDictKeyError
from
django.utils.decorators
import
classonlymethod
from
djangoldp.fields
import
LDPUrlField
...
...
@@ -14,7 +16,10 @@ User._meta.owner_field = "id"
class
Model
(
models
.
Model
):
urlid
=
LDPUrlField
(
null
=
True
,
unique
=
True
)
urlid
=
LDPUrlField
(
blank
=
True
,
null
=
True
,
unique
=
True
)
def
__init__
(
self
,
*
args
,
**
kwargs
):
super
(
Model
,
self
)
.
__init__
(
*
args
,
**
kwargs
)
@
classmethod
def
get_view_set
(
cls
):
...
...
@@ -33,7 +38,10 @@ class Model(models.Model):
return
cls
.
__clean_path
(
path
)
def
get_absolute_url
(
self
):
return
Model
.
resource_id
(
self
)
if
self
.
urlid
is
None
or
self
.
urlid
!=
''
:
return
'{}{}'
.
format
(
settings
.
BASE_URL
,
Model
.
resource_id
(
self
))
else
:
return
self
.
urlid
def
get_container_id
(
self
):
return
Model
.
container_id
(
self
)
...
...
@@ -57,7 +65,7 @@ class Model(models.Model):
else
:
object_name
=
instance_or_model
.
_meta
.
object_name
.
lower
()
view_name
=
'{}-detail'
.
format
(
object_name
)
try
:
try
:
slug_field
=
'/{}'
.
format
(
get_resolver
()
.
reverse_dict
[
view_name
][
0
][
0
][
1
][
0
])
except
MultiValueDictKeyError
:
slug_field
=
Model
.
get_meta
(
instance_or_model
,
'lookup_field'
,
'pk'
)
...
...
@@ -159,3 +167,11 @@ class LDPSource(Model):
def
__str__
(
self
):
return
"{}: {}"
.
format
(
self
.
federation
,
self
.
urlid
)
@
receiver
([
post_save
])
def
auto_urlid
(
sender
,
instance
,
**
kwargs
):
if
isinstance
(
instance
,
Model
)
and
(
instance
.
urlid
is
None
or
instance
.
urlid
==
''
):
instance
.
urlid
=
instance
.
get_absolute_url
()
instance
.
save
()
djangoldp/tests/models.py
View file @
cee01ada
...
...
@@ -24,7 +24,7 @@ class Skill(Model):
class
JobOffer
(
Model
):
title
=
models
.
CharField
(
max_length
=
255
,
blank
=
True
,
null
=
True
)
title
=
models
.
CharField
(
max_length
=
255
,
null
=
True
)
skills
=
models
.
ManyToManyField
(
Skill
,
blank
=
True
)
slug
=
models
.
SlugField
(
blank
=
True
,
null
=
True
,
unique
=
True
)
date
=
models
.
DateTimeField
(
auto_now_add
=
True
,
blank
=
True
)
...
...
@@ -58,6 +58,7 @@ class Conversation(models.Model):
class
Resource
(
Model
):
joboffers
=
models
.
ManyToManyField
(
JobOffer
,
blank
=
True
,
related_name
=
'resources'
)
description
=
models
.
CharField
(
max_length
=
255
)
class
Meta
:
anonymous_perms
=
[
'view'
,
'add'
,
'delete'
,
'add'
,
'change'
,
'control'
]
...
...
djangoldp/tests/runner.py
View file @
cee01ada
...
...
@@ -49,7 +49,8 @@ settings.configure(DEBUG=False,
'djangoldp.tests'
,
),
SITE_URL
=
'http://happy-dev.fr'
,
REST_FRAMEWORK
=
{
BASE_URL
=
'http://happy-dev.fr'
,
REST_FRAMEWORK
=
{
'DEFAULT_PAGINATION_CLASS'
:
'djangoldp.pagination.LDPPagination'
,
'PAGE_SIZE'
:
5
},
...
...
@@ -71,7 +72,7 @@ failures = test_runner.run_tests([
'djangoldp.tests.tests_delete'
,
'djangoldp.tests.tests_sources'
,
'djangoldp.tests.tests_pagination'
,
'djangoldp.tests.tests_temp'
#
'djangoldp.tests.tests_temp'
])
if
failures
:
...
...
djangoldp/tests/tests_ldp_model.py
View file @
cee01ada
...
...
@@ -16,7 +16,7 @@ class LDPModelTest(TestCase):
def
test_class_inheriting_ldp_model
(
self
):
dummy
=
LDPDummy
.
objects
.
create
(
some
=
"text"
)
self
.
assertEquals
(
"/ldpdummys/"
,
dummy
.
get_container_id
())
self
.
assertEquals
(
"/ldpdummys/{}/"
.
format
(
dummy
.
pk
),
dummy
.
get_absolute_url
())
self
.
assertEquals
(
"
http://happy-dev.fr
/ldpdummys/{}/"
.
format
(
dummy
.
pk
),
dummy
.
get_absolute_url
())
self
.
assertEquals
(
"/ldpdummys/"
,
Model
.
container_id
(
dummy
))
self
.
assertEquals
(
"/ldpdummys/{}/"
.
format
(
dummy
.
pk
),
Model
.
resource_id
(
dummy
))
...
...
@@ -33,6 +33,6 @@ class LDPModelTest(TestCase):
from
django.urls
import
get_resolver
dummy
=
LDPDummy
.
objects
.
create
(
some
=
"text"
)
view_name
=
'{}-list'
.
format
(
dummy
.
_meta
.
object_name
.
lower
())
path
=
'/{}{}/'
.
format
(
get_resolver
()
.
reverse_dict
[
view_name
][
0
][
0
][
0
],
dummy
.
pk
)
path
=
'
http://happy-dev.fr
/{}{}/'
.
format
(
get_resolver
()
.
reverse_dict
[
view_name
][
0
][
0
][
0
],
dummy
.
pk
)
self
.
assertEquals
(
path
,
dummy
.
get_absolute_url
())
djangoldp/tests/tests_save.py
View file @
cee01ada
...
...
@@ -3,7 +3,7 @@ from rest_framework.utils import json
from
djangoldp.models
import
Model
from
djangoldp.serializers
import
LDPSerializer
from
djangoldp.tests.models
import
Skill
,
JobOffer
,
Invoice
,
LDPDummy
,
Resource
from
djangoldp.tests.models
import
Skill
,
JobOffer
,
Invoice
,
LDPDummy
,
Resource
,
Post
class
Save
(
TestCase
):
...
...
@@ -88,8 +88,8 @@ class Save(TestCase):
self
.
assertIs
(
result
.
skills
.
count
(),
0
)
def
test_save_m2m_graph_with_nested
(
self
):
skill1
=
Skill
.
objects
.
create
(
title
=
"skill1"
,
obligatoire
=
"obligatoire"
)
skill2
=
Skill
.
objects
.
create
(
title
=
"skill2"
,
obligatoire
=
"obligatoire"
)
skill1
=
Skill
.
objects
.
create
(
title
=
"skill1"
,
obligatoire
=
"obligatoire"
,
slug
=
"a"
)
skill2
=
Skill
.
objects
.
create
(
title
=
"skill2"
,
obligatoire
=
"obligatoire"
,
slug
=
"b"
)
job
=
{
"@graph"
:
[
{
"title"
:
"job test"
,
...
...
@@ -111,9 +111,9 @@ class Save(TestCase):
self
.
assertEquals
(
result
.
skills
.
all
()[
0
]
.
title
,
"skill3 NEW"
)
# creation on the fly
def
test_save_without_nested_fields
(
self
):
skill1
=
Skill
.
objects
.
create
(
title
=
"skill1"
,
obligatoire
=
"obligatoire"
)
skill2
=
Skill
.
objects
.
create
(
title
=
"skill2"
,
obligatoire
=
"obligatoire"
)
job
=
{
"title"
:
"job test"
}
skill1
=
Skill
.
objects
.
create
(
title
=
"skill1"
,
obligatoire
=
"obligatoire"
,
slug
=
"a"
)
skill2
=
Skill
.
objects
.
create
(
title
=
"skill2"
,
obligatoire
=
"obligatoire"
,
slug
=
"b"
)
job
=
{
"title"
:
"job test"
,
"slug"
:
"c"
}
meta_args
=
{
'model'
:
JobOffer
,
'depth'
:
2
,
'fields'
:
(
"@id"
,
"title"
,
"skills"
)}
...
...
@@ -265,7 +265,8 @@ class Save(TestCase):
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
[
'resources'
][
'ldp:contains'
][
0
][
'@id'
],
"http://testserver/resources/{}/"
.
format
(
resource
.
pk
))
self
.
assertEqual
(
response
.
data
[
'title'
],
"new job"
)
def
test_nested_container_federated
(
self
):
...
...
@@ -278,7 +279,8 @@ class Save(TestCase):
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
[
'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
):
...
...
@@ -294,3 +296,35 @@ class Save(TestCase):
response
=
self
.
client
.
post
(
'/posts/'
,
data
=
json
.
dumps
(
body
),
content_type
=
'application/ld+json'
)
self
.
assertEqual
(
response
.
status_code
,
201
)
def
test_auto_id
(
self
):
body
=
{
'@id'
:
"./"
,
'content'
:
"post update"
,
'peer_user'
:
""
,
'@context'
:
{
"@vocab"
:
"http://happy-dev.fr/owl/#"
,
}
}
response
=
self
.
client
.
post
(
'/posts/'
,
data
=
json
.
dumps
(
body
),
content_type
=
'application/ld+json'
)
self
.
assertEqual
(
response
.
status_code
,
201
)
saved_post
=
Post
.
objects
.
get
(
pk
=
1
)
self
.
assertEqual
(
saved_post
.
urlid
,
"https://happy-dev.fr/posts/1/"
)
def
test_auto_id
(
self
):
body
=
{
'@id'
:
"./"
,
'content'
:
"post update"
,
'peer_user'
:
""
,
'@context'
:
{
"@vocab"
:
"http://happy-dev.fr/owl/#"
,
}
}
response
=
self
.
client
.
post
(
'/posts/'
,
data
=
json
.
dumps
(
body
),
content_type
=
'application/ld+json'
)
self
.
assertEqual
(
response
.
status_code
,
201
)
saved_post
=
Post
.
objects
.
get
(
pk
=
1
)
self
.
assertEqual
(
saved_post
.
urlid
,
"http://happy-dev.fr/posts/1/"
)
djangoldp/tests/tests_temp.py
View file @
cee01ada
...
...
@@ -2,6 +2,8 @@ from django.contrib.auth.models import User
from
django.test
import
TestCase
from
rest_framework.test
import
APIRequestFactory
,
APIClient
from
djangoldp.tests.models
import
Resource
class
TestTemp
(
TestCase
):
...
...
@@ -13,3 +15,31 @@ class TestTemp(TestCase):
def
tearDown
(
self
):
pass
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_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"
)
djangoldp/tests/tests_update.py
View file @
cee01ada
...
...
@@ -450,7 +450,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://
testserve
r/job-offers/aaa/"
)
"http://
happy-dev.f
r/job-offers/aaa/"
)
self
.
assertEqual
(
response
.
data
[
'joboffers'
][
'ldp:contains'
][
0
][
'title'
],
"new job"
)
def
test_m2m_existing_link
(
self
):
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment