Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
La Quadrature du Net
rpteam
Revue de Press
Commits
80cd52d3
Commit
80cd52d3
authored
Jul 04, 2019
by
Okhin
Browse files
Adding an update function to the article serializer
parent
6736c0b8
Pipeline
#2632
passed with stages
in 3 minutes and 15 seconds
Changes
3
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
apps/rp/api/serializers.py
View file @
80cd52d3
...
@@ -37,3 +37,16 @@ class ArticleSerializer(serializers.ModelSerializer):
...
@@ -37,3 +37,16 @@ class ArticleSerializer(serializers.ModelSerializer):
if
article
is
not
None
:
if
article
is
not
None
:
article
.
save
()
article
.
save
()
return
article
return
article
def
update
(
self
,
instance
,
validated_data
):
tags
=
validated_data
.
pop
(
"tags"
)
# Let's update the classic fields of the
# instance first
for
(
k
,
v
)
in
validated_data
.
items
():
setattr
(
instance
,
k
,
v
)
# Let's set the tags to what's provided
instance
.
tags
.
set
(
*
[
t
.
name
for
t
in
tags
])
instance
.
save
()
return
instance
apps/rp/tests/test_article.py
View file @
80cd52d3
...
@@ -13,7 +13,10 @@ from rp.views.articles import ArticleList
...
@@ -13,7 +13,10 @@ from rp.views.articles import ArticleList
class
TestArticle
(
TestCase
):
class
TestArticle
(
TestCase
):
def
setUp
(
self
):
def
setUp
(
self
):
self
.
article
=
ArticleFactory
(
archive
=
False
,
quote
=
False
,
speak
=
False
)
self
.
article
=
ArticleFactory
(
archive
=
False
,
quote
=
False
,
speak
=
False
)
self
.
newarticle
=
ArticleFactory
(
status
=
'NEW'
,
archive
=
False
,
quote
=
False
,
speak
=
False
)
self
.
newarticle
=
ArticleFactory
(
status
=
'NEW'
,
archive
=
False
,
quote
=
False
,
speak
=
False
)
def
test_init
(
self
):
def
test_init
(
self
):
assert
RpConfig
.
name
==
"rp"
assert
RpConfig
.
name
==
"rp"
...
@@ -229,6 +232,19 @@ class TestArticleApi(TestCase):
...
@@ -229,6 +232,19 @@ class TestArticleApi(TestCase):
assert
r
.
status_code
==
200
assert
r
.
status_code
==
200
assert
r
.
data
[
'count'
]
==
2
*
ArticleList
.
paginate_by
assert
r
.
data
[
'count'
]
==
2
*
ArticleList
.
paginate_by
def
test_api_edit
(
self
):
self
.
user
.
user_permissions
.
add
(
Permission
.
objects
.
get
(
codename
=
'can_edit'
))
self
.
client
.
force_login
(
user
=
self
.
user
)
a
=
self
.
articles
[
0
]
a
.
title
=
'Zog Zog'
a
.
screenshot
=
"test.png"
r
=
self
.
client
.
put
(
'/api/articles/{}/'
.
format
(
a
.
pk
),
a
.
__dict__
)
assert
r
.
status_code
==
200
a
.
refresh_from_db
()
assert
a
.
title
==
'Zog Zog'
def
test_api_filter_tag
(
self
):
def
test_api_filter_tag
(
self
):
tag
=
self
.
articles
[
0
].
tags
.
all
()[
1
]
tag
=
self
.
articles
[
0
].
tags
.
all
()[
1
]
...
...
apps/rp/views/articles.py
View file @
80cd52d3
...
@@ -161,7 +161,7 @@ class ArticleEdit(LoginRequiredMixin, PermissionRequiredMixin, UpdateView):
...
@@ -161,7 +161,7 @@ class ArticleEdit(LoginRequiredMixin, PermissionRequiredMixin, UpdateView):
return
self
.
render_to_response
(
context
)
return
self
.
render_to_response
(
context
)
def
form_valid
(
self
,
form
):
def
form_valid
(
self
,
form
):
self
.
object
=
form
.
save
()
self
.
object
=
form
.
save
(
commit
=
False
)
if
"preview"
in
self
.
request
.
POST
:
if
"preview"
in
self
.
request
.
POST
:
self
.
success_url
=
reverse
(
"rp:article-preview"
,
args
=
[
self
.
object
.
id
])
self
.
success_url
=
reverse
(
"rp:article-preview"
,
args
=
[
self
.
object
.
id
])
...
@@ -169,7 +169,7 @@ class ArticleEdit(LoginRequiredMixin, PermissionRequiredMixin, UpdateView):
...
@@ -169,7 +169,7 @@ class ArticleEdit(LoginRequiredMixin, PermissionRequiredMixin, UpdateView):
self
.
success_url
=
reverse
(
"rp:article-view"
,
args
=
[
self
.
object
.
id
])
self
.
success_url
=
reverse
(
"rp:article-view"
,
args
=
[
self
.
object
.
id
])
elif
"publish"
in
self
.
request
.
POST
:
elif
"publish"
in
self
.
request
.
POST
:
self
.
object
.
publish
()
self
.
object
.
publish
()
self
.
object
.
save
()
self
.
object
.
save
()
return
HttpResponseRedirect
(
self
.
get_success_url
())
return
HttpResponseRedirect
(
self
.
get_success_url
())
...
...
Write
Preview
Supports
Markdown
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