Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
R
rp
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
14
Issues
14
List
Boards
Labels
Service Desk
Milestones
Merge Requests
1
Merge Requests
1
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Operations
Operations
Incidents
Environments
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
La Quadrature du Net
rpteam
rp
Commits
f6491a65
Commit
f6491a65
authored
May 14, 2019
by
Okhin
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
We're nowusing a nested tag serializer for the API
parent
7d8b06e1
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
12 additions
and
15 deletions
+12
-15
apps/rp/api/serializers.py
apps/rp/api/serializers.py
+11
-14
apps/rp/tests/test_article.py
apps/rp/tests/test_article.py
+1
-1
No files found.
apps/rp/api/serializers.py
View file @
f6491a65
from
rest_framework
import
serializers
from
taggit.m
anagers
import
TaggableManager
from
taggit.m
odels
import
Tag
from
rp.models
import
Article
class
TagListSerializer
(
serializers
.
Field
):
class
Meta
:
model
=
TaggableManager
(
blank
=
True
)
def
to_representation
(
self
,
obj
):
if
type
(
obj
)
is
not
list
:
return
[
tag
.
name
for
tag
in
obj
.
all
()]
return
obj
class
TagListSerializer
(
serializers
.
ModelSerializer
):
name
=
serializers
.
CharField
(
max_length
=
200
)
def
to_internal_value
(
self
,
data
):
return
[
tag
.
strip
()
for
tag
in
data
.
split
(
","
)]
class
Meta
:
model
=
Tag
fields
=
(
'name'
,
)
class
ArticleSerializer
(
serializers
.
ModelSerializer
):
#: List of short tags to describe the article (eg. "Privacy", "Copyright")
tags
=
TagListSerializer
(
help_text
=
"""
List of short tags to describe the article (eg."Privacy, Copyright").
Must be a list of tags, coma separated (or an empty string).
"""
,
default
=
""
)
List of short tags to describe the article (eg."Privacy", "Copyright").
It must be a valid JSON list of items with a field named name.
For instance [{"name": "Privacy"}, {"name": "Copyright"}]
"""
,
many
=
True
,
required
=
False
)
class
Meta
:
model
=
Article
...
...
apps/rp/tests/test_article.py
View file @
f6491a65
...
...
@@ -158,7 +158,7 @@ class TestArticleApi(TestCase):
{
'url'
:
a
.
url
,
'title'
:
a
.
title
,
'tags'
:
''
})
assert
[
t
.
name
for
t
in
a
.
tags
.
all
()]
==
r
.
data
[
'tags'
]
assert
[
t
.
name
for
t
in
a
.
tags
.
all
()]
==
[
t
[
'name'
]
for
t
in
r
.
data
[
'tags'
]
]
def
test_api_recover
(
self
):
# Can we recover if we're no Jedis
...
...
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