Skip to content

Commit

Permalink
Finish buttons functionality and adding some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
finziamit committed Jan 18, 2023
1 parent dba63ad commit a308dac
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 28 deletions.
4 changes: 2 additions & 2 deletions projboard/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
}
19 changes: 9 additions & 10 deletions projboard/templates/editArticle/edit_article.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,16 @@

<head>
<!-- Smarticle CSS -->
<link rel="stylesheet" type="text/css" href="{% static 'css/edit_articles.css' %}">
<link rel="stylesheet" type="text/css" href="{% static 'css/edit_article.css' %}">

<title>Smarticle</title>
</head>

<body>
<div class="article-container">
<h1 class="edit-article">Edit Article</h1>
<form method="POST">
{% csrf_token %} {{ form.as_p }}
<input type="submit" value="Publish" />
</form>
</div>
</body>
<div class="article-container">
<h1 class="edit-article">Edit Article</h1>
<form method="POST">
{% csrf_token %} {{ form.as_p }}
<input type="submit" value="Submit" />
</form>
</div>
{% endblock %}
63 changes: 63 additions & 0 deletions projboard/tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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
Expand Down Expand Up @@ -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
35 changes: 19 additions & 16 deletions projboard/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

0 comments on commit a308dac

Please sign in to comment.