From a308dac716a56e5d45c9844db6fafb103d97faaf Mon Sep 17 00:00:00 2001 From: Amit Finzi Date: Wed, 18 Jan 2023 14:24:38 +0200 Subject: [PATCH] Finish buttons functionality and adding some tests --- projboard/forms.py | 4 +- .../templates/editArticle/edit_article.html | 19 +++--- projboard/tests/test_client.py | 63 +++++++++++++++++++ projboard/views.py | 35 ++++++----- 4 files changed, 93 insertions(+), 28 deletions(-) diff --git a/projboard/forms.py b/projboard/forms.py index 71461be..2aae5be 100644 --- a/projboard/forms.py +++ b/projboard/forms.py @@ -41,7 +41,7 @@ def clean(self): class EditArticleForm(forms.ModelForm): class Meta: model = Article - fields = ('title', 'subject_id', 'content') + fields = ('user_id', 'title', 'subject_id', 'content') widgets = { - 'article_id': forms.HiddenInput(), + 'user_id': forms.HiddenInput(), } diff --git a/projboard/templates/editArticle/edit_article.html b/projboard/templates/editArticle/edit_article.html index cd82564..8968e05 100644 --- a/projboard/templates/editArticle/edit_article.html +++ b/projboard/templates/editArticle/edit_article.html @@ -6,17 +6,16 @@ - + Smarticle - -
-

Edit Article

-
- {% csrf_token %} {{ form.as_p }} - -
-
- \ No newline at end of file +
+

Edit Article

+
+ {% csrf_token %} {{ form.as_p }} + +
+
+{% endblock %} diff --git a/projboard/tests/test_client.py b/projboard/tests/test_client.py index fab2d1a..f018428 100644 --- a/projboard/tests/test_client.py +++ b/projboard/tests/test_client.py @@ -2,6 +2,7 @@ from projboard.models.article import Article from projboard.models.subject import Subject from projboard.models.user import User +from projboard.forms import EditArticleForm SEARCHINPUT = 'search_input' @@ -26,6 +27,13 @@ WRONG_TITLE_MESSAGE = "Article didn\'t found" WRONG_SUBJECT_MESSAGE = "Subject Not Valid" WRONG_USER_MESSAGE = "User Name Not Valid" +INVALID_ARTICLE = 1234567 +VALID_TITLE = 'valid title' +VALID_SUBJECT = 'Software' +VALID_CONTENT = 'valid content' +NEW_TITLE = 'new title' +NEW_SUBJECT = 'Hobbies' +NEW_CONTENT = 'new content' @pytest.mark.django_db @@ -353,3 +361,58 @@ def test_read_already_viewed_article(client, math_article, User2): num_views2 = response.context['article'].num_of_views # checking that num views didn't increased assert num_views2 == num_views1 + + +def test_delete_article(client, article): + # Test article in DB + assert article in set(Article.objects.all()) + + # Test deletion response + response = client.get(f"/delete_article/{article.id}/") + assert response.status_code == 302 + + # Test article deletion + assert article not in set(Article.objects.all()) + + +@pytest.mark.django_db +def test_fail_delete_article(client): + response = client.get(f"/delete_article/{INVALID_ARTICLE}/") + assert response.status_code == 404 + + +@pytest.mark.django_db +def test_edit_article(client, article): + response = client.get(f"/edit_article/{article.id}/", {"title": NEW_TITLE, + "subject_id": NEW_SUBJECT, + "content": NEW_CONTENT, + "user_id": article.user_id}) + assert response.status_code == 200 + + template_names = set(tmpl.origin.template_name for tmpl in response.templates) + assert 'editArticle/edit_article.html' in template_names + + # Test article before edit + assert article in set(Article.objects.all()) + assert article.content != NEW_CONTENT + assert article.title != NEW_TITLE + assert article.subject_id != NEW_SUBJECT + + # Test article after edit + + +@pytest.mark.django_db +def test_edit_invalid_article(client): + response = client.get(f"/edit_article/{INVALID_ARTICLE}/") + assert response.status_code == 404 + + +@pytest.mark.django_db +def test_edit_article_form(): + form_data = { + 'title': VALID_TITLE, + 'subject': VALID_SUBJECT, + 'content': VALID_CONTENT + } + form = EditArticleForm(data=form_data) + assert form.is_valid diff --git a/projboard/views.py b/projboard/views.py index 80c5cdb..171134d 100644 --- a/projboard/views.py +++ b/projboard/views.py @@ -162,33 +162,36 @@ def show_article(request, user_nickname, article_pk): def delete_article(request, article_pk): try: article = Article.objects.get(id=article_pk) + user = article.user_id article.delete() - return redirect(home_page) + nickname = user.nickname + return redirect(f"/my_articles/{nickname}/") - except User.DoesNotExist: + except Article.DoesNotExist: raise Http404() -def edit_article(request, article_pk): +def edit_article(request, article_pk=None): try: - article = Article.objects.get(article_pk) - - initial = {'article_id': article, 'title': article.title, - 'subject': article.subject_id, 'content': article.content} + article = Article.objects.get(id=article_pk) + user_id = article.user_id + title = article.title + subject_id = article.subject_id + content = article.content + initial = {'user_id': user_id, 'title': title, 'subject_id': subject_id, 'content': content} if request.method == "POST": - form = EditArticleForm(request.POST, initial=initial) + form = EditArticleForm(request.POST, initial) if form.is_valid(): - form.save() - return render(request, 'editArticle/edit_article.html', initial) - # elif request.method == "DELETE": - # raise Http404() + article.edit(form.data['title'], form.data['content'], form.cleaned_data['subject_id']) + return redirect(f"/my_articles/{user_id.nickname}/") + elif request.method == "DELETE": + raise Http404() else: form = EditArticleForm(initial=initial) return render(request, 'editArticle/edit_article.html', { - 'article_pk': article_pk, + 'user_id': user_id, 'form': form, }) - except User.DoesNotExist: - # raise Http404() - return redirect(home_page) + except Article.DoesNotExist: + raise Http404()