Commit 80cd52d3 authored by okhin's avatar okhin 🚴

Adding an update function to the article serializer

parent 6736c0b8
Pipeline #2632 passed with stages
in 3 minutes and 15 seconds
......@@ -37,3 +37,16 @@ class ArticleSerializer(serializers.ModelSerializer):
if article is not None:
article.save()
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
......@@ -13,7 +13,10 @@ from rp.views.articles import ArticleList
class TestArticle(TestCase):
def setUp(self):
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):
assert RpConfig.name == "rp"
......@@ -229,6 +232,19 @@ class TestArticleApi(TestCase):
assert r.status_code == 200
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):
tag = self.articles[0].tags.all()[1]
......
......@@ -161,7 +161,7 @@ class ArticleEdit(LoginRequiredMixin, PermissionRequiredMixin, UpdateView):
return self.render_to_response(context)
def form_valid(self, form):
self.object = form.save()
self.object = form.save(commit=False)
if "preview" in self.request.POST:
self.success_url = reverse("rp:article-preview", args=[self.object.id])
......@@ -169,7 +169,7 @@ class ArticleEdit(LoginRequiredMixin, PermissionRequiredMixin, UpdateView):
self.success_url = reverse("rp:article-view", args=[self.object.id])
elif "publish" in self.request.POST:
self.object.publish()
self.object.save()
self.object.save()
return HttpResponseRedirect(self.get_success_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