วันเสาร์ที่ 14 เมษายน พ.ศ. 2561

Commit 13 Wordbucket : add votes(like and dislike) function

Assignment1 : Wordbucket GitHub Link

Commit 13 Wordbucket : add votes(like and dislike) function

Commits on Mar 21, 2018

functional tests

- โกงนิดหน่อย(เว้นวรรคตามใน template detail)โดยแก้ให้มี ปุ่ม like dislike ในตาราง explanation


@@ -133,8 +133,8 @@ def test_can_view_the_word_explanation_and_add_exist_word_new_explanation(self):
# now the page "weeb" word
# "awesome!" as an item in a "weeb" word table
- self.check_for_row_in_explanation_table('explanation 1 : otaku!')
- self.check_for_row_in_explanation_table('explanation 2 : non japanese otaku!')
+ self.check_for_row_in_explanation_table('explanation 1 : otaku!\n 0 LIKE 0 DISLIKE')
+ self.check_for_row_in_explanation_table('explanation 2 : non japanese otaku!\n 0 LIKE 0 DISLIKE')
if __name__ == '__main__':

/templates/detail.html

- เพิ่ม ปุ่ม(text) สำหรับ เรียกใช้ function like and dislike ของ explanation นั้นๆ

@@ -15,7 +15,16 @@ <h2>{{ word }}</h2>
<table id="id_explanation_table">
{% for explanation in word.explanation_set.all %}
- <tr><td>explanation {{ forloop.counter }} : {{ explanation.explanation_text }}</td></tr>
+ <tr>
+ <td>explanation {{ forloop.counter }} : {{ explanation.explanation_text }}
+ {% for vote in explanation.like_and_dislike_set.all %}<br>
+ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
+ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
+ {{ vote.votes_like }} &nbsp;<a href="{% url 'wordbucket:like' explanation.id %}">LIKE</a>
+ &nbsp; {{ vote.votes_dislike }} &nbsp;<a href="{% url 'wordbucket:dislike' explanation.id %}">DISLIKE</a>
+ {% endfor %}<br><br>
+ </td>
+ </tr>
{% endfor %}
</table>
<br>

unit tests

- เพิ่ม test redirect หลังกด like และ dislike (มีการเปลี่ยนแปลงค่าก่อน redirect)

@@ -184,8 +184,43 @@ def test_return_correct_text(self):
html = response.content.decode('utf8')
self.assertIn('A new list word', html)
-'''
class VoteTest(TestCase):
-'''
+
+ def test_redirects_like_to_word_view(self):
+ word_ = Word()
+ word_.save()
+
+ explanation_ = Explanation()
+ explanation_.explanation_text = 'test'
+ explanation_.word = word_
+ explanation_.save()
+
+ votes_like = Like_and_dislike()
+ votes_like.votes_like = 0
+ votes_like.explanation = explanation_
+ votes_like.save()
+
+ response = self.client.get(
+ '/%d/like' % (explanation_.id,)
+ )
+
+ def test_redirects_dislike_to_word_view(self):
+ word_ = Word()
+ word_.save()
+
+ explanation_ = Explanation()
+ explanation_.explanation_text = 'test'
+ explanation_.word = word_
+ explanation_.save()
+
+ votes_dislike = Like_and_dislike()
+ votes_dislike.votes_dislike = 0
+ votes_dislike.explanation = explanation_
+ votes_dislike.save()
+
+ response = self.client.get(
+ '/%d/like' % (explanation_.id,)
+ )
+ self.assertRedirects(response, '/%d/' % (explanation_.id,))

views.py

- เพิ่มให้ใน ทุก function ที่มีการสร้าง object ของตาราง explanation จะสร้าง object ของตาราง like_and_dislike ไปด้วย
- เพิ่ม function like และ dislike (คล้ายกันแค่เก็บค่าคนละตัวแปร) โดยถ้ากดปุ่มจะเพิ่ม ค่า integer vote_like/vote_dislike ที่เก็บไว้ขึ้นมา 1

@@ -1,4 +1,5 @@
-from django.shortcuts import redirect, render
+from django.shortcuts import redirect, render, HttpResponseRedirect
+from django.urls import reverse
from wordbucket.models import Word, Explanation, Like_and_dislike
import string
@@ -21,12 +22,14 @@ def add_word(request):
d_query = Word.objects.filter(word=word_reference)
if not d_query :
word_ = Word.objects.create(word = request.POST['word_input'])
- Explanation.objects.create(explanation_text=request.POST['explanation_input'], word=word_)
+ explanation_ = Explanation.objects.create(explanation_text=request.POST['explanation_input'], word=word_)
+ Like_and_dislike.objects.create(votes_like = 0, votes_dislike = 0, explanation = explanation_)
return redirect('/')
else :
# duplacate word id
dword_id = Word.objects.get(word = word_reference)
- Explanation.objects.create(explanation_text=request.POST['explanation_input'], word=dword_id)
+ explanation_ = Explanation.objects.create(explanation_text=request.POST['explanation_input'], word=dword_id)
+ Like_and_dislike.objects.create(votes_like = 0, votes_dislike = 0, explanation = explanation_)
d_message = "duplicate word, your explanation add to existing word."
return render(request, 'home.html', {'words': words, 'd_message': d_message})
@@ -36,7 +39,8 @@ def add_explanation(request, word_id):
# query for duplicate explanation
d_query = Explanation.objects.filter(explanation_text=explanation_reference)
if not d_query :
- Explanation.objects.create(explanation_text=request.POST['explanation_input'], word=word_)
+ explanation_ = Explanation.objects.create(explanation_text=request.POST['explanation_input'], word=word_)
+ Like_and_dislike.objects.create(votes_like = 0, votes_dislike = 0, explanation = explanation_)
return redirect('/%d/' % (word_.id,))
else :
d_message = "duplicate explanation, please enter new explanation."
@@ -62,8 +66,27 @@ def search(request, word_search):
else :
return render(request, 'search.html', {'word_found': word_found})
-def vote_like(request):
- pass
+def vote_like(request, explanation_id):
+ explanation_ = Explanation.objects.get(id=explanation_id)
+ selected_explanation = explanation_.like_and_dislike_set.all()
+ vote_like_ = selected_explanation.count()
+ if vote_like_ == 0 :
+ Like_and_dislike.objects.create(vote_like=1, explanation=explanation_)
+ else :
+ for voteslike in selected_explanation :
+ voteslike.votes_like += 1
+ voteslike.save()
+ return HttpResponseRedirect(reverse('wordbucket:detail', args=(explanation_.word.id,)))
+
-def vote_dislike(request):
- pass
+def vote_dislike(request, explanation_id):
+ explanation_ = Explanation.objects.get(id=explanation_id)
+ selected_explanation = explanation_.like_and_dislike_set.all()
+ vote_dislike_ = selected_explanation.count()
+ if vote_dislike_ == 0 :
+ Like_and_dislike.objects.create(vote_dislike=1, explanation=explanation_)
+ else :
+ for votesdislike in selected_explanation :
+ votesdislike.votes_dislike += 1
+ votesdislike.save()
+ return HttpResponseRedirect(reverse('wordbucket:detail', args=(explanation_.word.id,)))




ไม่มีความคิดเห็น:

แสดงความคิดเห็น