Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
D
djangoldp
Manage
Activity
Members
Labels
Plan
Issues
99
Issue boards
Milestones
Wiki
Code
Merge requests
5
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container Registry
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
djangoldp-packages
djangoldp
Commits
9ee2a850
Commit
9ee2a850
authored
6 years ago
by
Jean-Baptiste
Browse files
Options
Downloads
Patches
Plain Diff
update: manage slug on custom model
parent
df0a759f
No related branches found
Branches containing commit
No related tags found
Tags containing commit
1 merge request
!24
Resolve "Get URL of saved resource from its model"
Pipeline
#726
passed with stage
Stage: test
in 27 seconds
Changes
3
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
djangoldp/models.py
+10
-3
10 additions, 3 deletions
djangoldp/models.py
djangoldp/serializers.py
+1
-1
1 addition, 1 deletion
djangoldp/serializers.py
djangoldp/tests/urls.py
+2
-1
2 additions, 1 deletion
djangoldp/tests/urls.py
with
13 additions
and
5 deletions
djangoldp/models.py
+
10
−
3
View file @
9ee2a850
from
django.conf
import
settings
from
django.db
import
models
from
django.urls
import
get_resolver
class
Model
(
models
.
Model
):
container_path
=
None
def
get_container_path
(
self
):
return
self
.
container_path
def
get_absolute_url
(
self
):
return
Model
.
resource_id
(
self
)
...
...
@@ -13,16 +17,19 @@ class Model(models.Model):
@classmethod
def
resource_id
(
cls
,
instance
):
return
"
{}{}
"
.
format
(
Model
.
container_id
(
instance
),
instance
.
pk
)
view_name
=
'
{}-detail
'
.
format
(
instance
.
_meta
.
object_name
.
lower
())
slug_field
=
'
/{}
'
.
format
(
get_resolver
().
reverse_dict
[
view_name
][
0
][
0
][
1
][
0
])
if
slug_field
.
startswith
(
'
/
'
):
slug_field
=
slug_field
[
1
:]
return
"
{}{}
"
.
format
(
Model
.
container_id
(
instance
),
getattr
(
instance
,
slug_field
))
@classmethod
def
container_id
(
cls
,
instance
):
if
isinstance
(
instance
,
cls
):
path
=
instance
.
container_path
else
:
from
django.urls
import
get_resolver
view_name
=
'
{}-list
'
.
format
(
instance
.
_meta
.
object_name
.
lower
())
path
=
'
/{}
'
.
format
(
get_resolver
().
reverse_dict
[
view_name
][
0
][
0
][
0
]
,
instance
.
pk
)
path
=
'
/{}
'
.
format
(
get_resolver
().
reverse_dict
[
view_name
][
0
][
0
][
0
])
if
not
path
.
startswith
(
"
/
"
):
path
=
"
/{}
"
.
format
(
path
)
...
...
This diff is collapsed.
Click to expand it.
djangoldp/serializers.py
+
1
−
1
View file @
9ee2a850
...
...
@@ -41,7 +41,7 @@ class LDListMixin:
try
:
object_list
=
dictionary
[
"
@graph
"
]
view_name
=
'
{}-list
'
.
format
(
self
.
parent
.
Meta
.
model
.
_meta
.
object_name
.
lower
())
part_id
=
'
/{}
'
.
format
(
get_resolver
().
reverse_dict
[
view_name
][
0
][
0
][
0
]
,
self
.
parent
.
instance
.
pk
)
part_id
=
'
/{}
'
.
format
(
get_resolver
().
reverse_dict
[
view_name
][
0
][
0
][
0
])
obj
=
next
(
filter
(
lambda
o
:
part_id
in
o
[
'
@id
'
],
object_list
))
list
=
super
().
get_value
(
obj
)
try
:
...
...
This diff is collapsed.
Click to expand it.
djangoldp/tests/urls.py
+
2
−
1
View file @
9ee2a850
from
django.conf
import
settings
from
djangoldp.tests.models
import
Skill
,
JobOffer
,
Message
,
Thread
,
Dummy
from
djangoldp.tests.models
import
Skill
,
JobOffer
,
Message
,
Thread
,
Dummy
,
LDPDummy
from
djangoldp.views
import
LDPViewSet
from
django.conf.urls
import
url
...
...
@@ -12,4 +12,5 @@ urlpatterns = [
url
(
r
'
^threads/
'
,
LDPViewSet
.
urls
(
model
=
Thread
,
nested_fields
=
[
"
message_set
"
],
permission_classes
=
())),
url
(
r
'
^users/
'
,
LDPViewSet
.
urls
(
model
=
settings
.
AUTH_USER_MODEL
,
permission_classes
=
[])),
url
(
r
'
^dummys/
'
,
LDPViewSet
.
urls
(
model
=
Dummy
,
permission_classes
=
[])),
url
(
r
'
^ldp-dummys/
'
,
LDPViewSet
.
urls
(
model
=
LDPDummy
,
permission_classes
=
[])),
]
\ No newline at end of file
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment