From d66a2168a506464d0c3ffbeb3af34496a2d7a593 Mon Sep 17 00:00:00 2001 From: Gabriel Augendre Date: Sat, 6 Mar 2021 15:11:17 +0100 Subject: [PATCH] Allow empty tags in live preview --- articles/tests/test_api_views.py | 10 ++++++++++ articles/views/api.py | 8 ++++++-- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/articles/tests/test_api_views.py b/articles/tests/test_api_views.py index 6bb9239..87c75dd 100644 --- a/articles/tests/test_api_views.py +++ b/articles/tests/test_api_views.py @@ -57,6 +57,16 @@ def test_render_article_doesnt_save(published_article, client: Client): assert published_article.content == original_content +@pytest.mark.django_db +def test_render_article_no_tags(published_article, client: Client): + client.force_login(published_article.author) + api_res = client.post( + reverse("api-render-article", kwargs={"article_pk": published_article.pk}), + data={"content": published_article.content, "tag_ids": ""}, + ) + assert api_res.status_code == 200 + + def post_article(client: Client, article: Article, content: str): return client.post( reverse("api-render-article", kwargs={"article_pk": article.pk}), diff --git a/articles/views/api.py b/articles/views/api.py index 9b9c24b..8fd3423 100644 --- a/articles/views/api.py +++ b/articles/views/api.py @@ -17,6 +17,10 @@ def render_article(request, article_pk): has_code = request.POST.get("has_code") if has_code is not None: article.has_code = has_code == "true" - tags = Tag.objects.filter(pk__in=map(int, request.POST.get("tag_ids").split(","))) - html = render(request, template, context={"article": article, "tags": tags}) + context = {"article": article} + tags = request.POST.get("tag_ids") + if tags: + tags = Tag.objects.filter(pk__in=map(int, tags.split(","))) + context["tags"] = tags + html = render(request, template, context=context) return HttpResponse(html)