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

Commit 10 Wordbucket : add explanation in exist word function(can't add duplicate explanation)

Assignment1 : Wordbucket GitHub Link

Commit 10 Wordbucket : add explanation in exist word function(can't add duplicate explanation)

Commits on Mar 5, 2018
unit test

เพิ่ม test ก่อน
- แยกการ add word ออกจาก function home_page() ใน views.py ออกเป็น function ใหม่ add_word โดยดึง test 2 จาก test ของ home_page
- เพิ่ม function add_explanation() โดยมี 2 tests คล้าย add word โดยมีการ test
    - save post request
    - redirect หลัง post

@@ -11,20 +11,6 @@ def test_uses_home_template(self):
response = self.client.get('/')
self.assertTemplateUsed(response, 'home.html')
- def test_can_save_a_POST_request(self):
- self.client.post('/', data={'word_input': 'A new list word','explanation_input': 'yes it is'})
-
- self.assertEqual(Word.objects.count(), 1)
- self.assertEqual(Explanation.objects.count(), 1)
- new_word = Word.objects.first()
- self.assertEqual(new_word.word, 'A new list word')
-
- def test_redirects_after_POST(self):
- response = self.client.post('/', data={'word_input': 'A new list word','explanation_input': 'yes it is'})
-
- self.assertEqual(response.status_code, 302)
- self.assertEqual(response['location'], '/')
-
def test_home_page_returns_correct_html(self):
response = self.client.get('/')
@@ -136,9 +122,52 @@ def test_displays_only_explanation_for_that_word(self):
self.assertContains(response, 'itemey 2')
self.assertNotContains(response, 'other word item 1')
self.assertNotContains(response, 'other word item 2')
-'''
+
class NewWordTest(TestCase):
+
+ def test_can_save_a_POST_request(self):
+ self.client.post('/add_word', data={'word_input': 'A new list word','explanation_input': 'yes it is'})
+
+ self.assertEqual(Word.objects.count(), 1)
+ self.assertEqual(Explanation.objects.count(), 1)
+ new_word = Word.objects.first()
+ self.assertEqual(new_word.word, 'A new list word')
+
+
+ def test_redirects_after_POST(self):
+ response = self.client.post('/add_word', data={'word_input': 'A new list word','explanation_input': 'yes it is'})
+
+ self.assertEqual(response.status_code, 302)
+ self.assertEqual(response['location'], '/')
+
class NewExplanationTest(TestCase):
+
+ def test_can_save_a_POST_request_to_an_existing_word(self):
+ other_word = Word.objects.create()
+ correct_word = Word.objects.create()
+
+ self.client.post(
+ '/%d/add_explanation' % (correct_word.id,),
+ data={'explanation_input': 'A new explanation for an existing word'}
+ )
+
+ self.assertEqual(Explanation.objects.count(), 1)
+ new_explanation = Explanation.objects.first()
+ self.assertEqual(new_explanation.explanation_text, 'A new explanation for an existing word')
+ self.assertEqual(new_explanation.word, correct_word)
+
+
+ def test_redirects_to_word_view(self):
+ other_word = Word.objects.create()
+ correct_word = Word.objects.create()
+
+ response = self.client.post(
+ '/%d/add_explanation' % (correct_word.id,),
+ data={'explanation_input': 'A new item for an existing word'}
+ )
+
+ self.assertRedirects(response, '/%d/' % (correct_word.id,))
+'''
class VoteTest(TestCase):
class SearchAndBrowseTest(TestCase):'''

urls.py

แก้ urls.py ที่ใส่ ผิดพลาดใน commit ที่แล้ว(แต่ยังไม่ได้ใช้เลยไม่รู้ตัว) มี "/" เกินมา ใน path()

@@ -4,8 +4,8 @@
app_name = 'wordbucket'
urlpatterns = [
path('', views.home_page, name='home'),
- path('add_word/', views.add_word, name='add_word'),
- path('search/', views.search, name='search'),
+ path('add_word', views.add_word, name='add_word'),
+ path('search', views.search, name='search'),
re_path(r'^(\d+)/$', views.view_word, name='detail'),
re_path(r'^(\d+)/add_explanation$', views.add_explanation, name='add_explanation'),
re_path(r'^(\d+)/like$', views.vote_like, name='like'),

views.py

- เพิ่ม d_message ใน function view_word ไว้ใส่ message เผื่อ explanation ที่เพิ่มในหน้า view_word ซ้ำ
- แยก add word ใน home_page() เป็น function add_word()
- เพิ่ม function add_explanation ในการเพิ่ม explanation ลงในหน้า view_word (ที่ใช้ template deatail.html)


@@ -4,31 +4,42 @@
def home_page(request):
d_message = ""
words = Word.objects.order_by('-date_pub')[:5]
- if request.method == 'POST':
- word_reference = str(request.POST['word_input'])
- # query for duplicate word
- 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_)
- 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)
- d_message = "duplicate word, your explanation add to existing word."
- return render(request, 'home.html', {'words': words, 'd_message': d_message})
return render(request, 'home.html', {'words': words, 'd_message': d_message})
def view_word(request, word_id):
+ d_message = ""
word_ = Word.objects.get(id=word_id)
- return render(request, 'detail.html', {'word': word_})
+ return render(request, 'detail.html', {'word': word_, 'd_message': d_message})
def add_word(request):
- pass
+ d_message = ""
+ words = Word.objects.order_by('-date_pub')[:5]
+ word_reference = str(request.POST['word_input'])
+ # query for duplicate word
+ 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_)
+ 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)
+ d_message = "duplicate word, your explanation add to existing word."
+ return render(request, 'home.html', {'words': words, 'd_message': d_message})
-def add_explanation(request):
- pass
+def add_explanation(request, word_id):
+ word_ = Word.objects.get(id=word_id)
+ explanation_reference = str(request.POST['explanation_input'])
+ # 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_)
+ return redirect('/%d/' % (word_.id,))
+ else :
+ d_message = "duplicate explanation, please enter new explanation."
+ return render(request, 'detail.html', {'word': word_, 'd_message': d_message})
+
def vote_like(request):
pass

/templates/detail.html

เพิ่ม d_message ในหน้า detail.html ไว้ใส่ message เผื่อ explanation ที่เพิ่มในหน้า view_word ซ้ำ โดย action="/{{ word.id }}/add_explanation" ตาม path ใน urls.py

@@ -4,7 +4,8 @@
</head>
<body>
<h1>Word Bucket</h1>
- <form method="POST" id="form1">
+ <h4>{{ d_message }}</h4>
+ <form method="POST" id="form1" action="/{{ word.id }}/add_explanation">
<input name="explanation_input" id="id_new_eplanation" placeholder="Add explanation" />
{% csrf_token %}
</form>

/templates/home.html

กำหนด action="/add_word ตาม path ใน urls.py


@@ -5,7 +5,7 @@
<body>
<h1>Word Bucket</h1>
<h4>{{ d_message }}</h4>
- <form method="POST" id="form1">
+ <form method="POST" id="form1" action="/add_word">
<input name="word_input" id="id_new_word" placeholder="Add new word" />
<input name="explanation_input" id="id_new_eplanation" placeholder="Add explanation" />
{% csrf_token %}


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

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