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

Commit 11 Wordbucket : add search function

Assignment1 : Wordbucket GitHub Link

Commit 11 Wordbucket : can add explanation with word

Commits on Mar 7, 2018

functional test

- เพิ่ม user story ในส่วนของการ search เล็กน้อยในตอนท้าย โดยพิมพ์แค่ 'we' จะเจอ 'weeb' คำที่ add เข้าไปก่อนหน้า แล้วสามารถคลิกเข้าไปดู detail ได้
@@ -42,7 +42,7 @@ def check_for_row_in_explanation_table(self, row_text):
time.sleep(0.5)
- def test_can_start_a_list_and_retrieve_it_later(self):
+ def test_can_start_a_list_and_retrieve_it_later_and_search(self):
# Ann has heard about a cool new online word app. She goes
# to check out its homepage
self.browser.get(self.live_server_url)
@@ -81,6 +81,14 @@ def test_can_start_a_list_and_retrieve_it_later(self):
self.check_for_row_in_list_table('weeb')
self.check_for_row_in_list_table('PogChamp')
+ # She type "we" in search text box
+ inputbox = self.browser.find_element_by_id('id_search')
+ inputbox.send_keys('we')
+ inputbox.send_keys(Keys.ENTER)
+
+ # Now html render 'search' page. and 'weeb' url appear on her screen
+ self.check_for_row_in_list_table('weeb')
+
def test_can_view_the_word_explanation_and_add_exist_word_new_explanation(self):
# Ann has heard about a cool new online word app. She goes
# to check out its homepage
@@ -127,6 +135,7 @@ def test_can_view_the_word_explanation_and_add_exist_word_new_explanation(self):
# "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!')
+
if __name__ == '__main__':
unittest.main(warnings='ignore')

templates/detail.html

- เพิ่มปุ่มกดกลับหน้า home page

@@ -18,5 +18,7 @@ <h2>{{ word }}</h2>
<tr><td>explanation {{ forloop.counter }} : {{ explanation.explanation_text }}</td></tr>
{% endfor %}
</table>
+ <br>
+ <a href="{% url 'wordbucket:home' %}">HOME</a></td></tr>
</body>
</html>

/templates/home.html

- หน้า home มี form input สำหรับพิมพ์ search

@@ -17,5 +17,12 @@ <h4>{{ d_message }}</h4>
<tr><td id="word.word"><a href="{% url 'wordbucket:detail' word.id %}">{{ word.word }}</a></td></tr>
{% endfor %}
</table>
+ <br>
+ <form action = "/search" id="form_search" method="POST">
+ <input tpye="text" id="id_search" name = "search_input" placeholder="Search word" />
+ <input type = "submit" form="form_search" value = "Search">
+ {% csrf_token %}
+ </form>
+
</body>
</html>

unit test

- เพิ่ม tests สำหรับ search และ browse (ผมวางแผนว่าจะใช้ function search ใน browse(เช่น browse หมวด a ก็จะคำที่ขึ้นต้นด้วย a ขึ้นมา)) โดยมี 3 tests
     - test ใช้ template
     - test render หลัง post คำที่ search
     - test ว่า return ถูกคำหรือไม่

@@ -167,8 +167,25 @@ def test_redirects_to_word_view(self):
)
self.assertRedirects(response, '/%d/' % (correct_word.id,))
+
+class SearchAndBrowseTest(TestCase):
+
+ def test_uses_search_template(self):
+ response = self.client.get('/search')
+ self.assertTemplateUsed(response, 'search.html')
+
+ def test_render_after_POST(self):
+ response = self.client.post('/search', data={'search_input': 'A new list word'})
+ self.assertEqual(response.status_code, 200)
+
+ def test_return_correct_text(self):
+ self.client.post('/add_word', data={'word_input': 'A new list word','explanation_input': 'yes it is'})
+ response = self.client.post('/search', data={'search_input': 'A new list word'})
+ html = response.content.decode('utf8')
+ self.assertIn('A new list word', html)
+
'''
class VoteTest(TestCase):
-class SearchAndBrowseTest(TestCase):'''
+'''

views.py

- เพิ่ม function search โดย มี 2 แบบคือ

  1. ได้รับค่ามาจากการ post (สำหรับ search ปกติ โดยจะ search แบบ word__contains ซึ่งคำที่ search อยู่ส่วนไหนของคำใน database ก็ได้) ทำไปแล้วใน commit นี้
  2. (ใน commit 12) เป็นค่าที่ใส่มาตาม path (สามารถแก้ที่ url ของ browser ได้ โดยจะ search แบบ ) ซึ่งวางแผนว่าจะทำต่อในอนาคต
ถ้าหาไม่เจอจะแสดง message "WORD not found" โดยคำศัพท์ที่ query search เจอจะเก็บไว้ในตัวแปร word_found 


@@ -40,12 +40,22 @@ def add_explanation(request, word_id):
d_message = "duplicate explanation, please enter new explanation."
return render(request, 'detail.html', {'word': word_, 'd_message': d_message})
+def search(request):
+ word_reference = 'no'
+ if request.method == 'POST':
+ word_reference = str(request.POST['search_input'])
+ if word_reference != 'no' :
+ word_found = Word.objects.filter(word__contains=word_reference)
+ if not word_found :
+ message = "WORD not found"
+ return render(request, 'search.html', {'message': message})
+ else :
+ return render(request, 'search.html', {'word_found': word_found})
+ else :
+ return render(request, 'search.html')
def vote_like(request):
pass
def vote_dislike(request):
pass
-
-def search(request):
- pass

หมายเหตุ : โดยเนื่องจากผม commit -am จะ track การเปลี่ยนแปลงให้ add file template search เข้ามาแล้วเพิ่งรู้ตัวตอน commit "add login system (django build-in)" ว่า commit -am จะไม่ track ไฟล์ที่ยังไม่ถูก add (ที่รู้ตัวเพราะต้อง add หน้า login)

templates/search.html

- หน้า search โดยเมื่อพิมพ์ในหน้า home จะ render หน้านี้ ใน commit นี้จะได้แค่จาก word ที่รับจาก post มา

@@ -0,0 +1,37 @@
+<html>
+ <head>
+ <title>Word Bucket</title>
+ </head>
+ <body>
+
+ <h1>Search Word Bucket </h1>
+
+ <form action = "/search/byword" method="POST">
+ <input tpye="text" name = "search_input" placeholder="Search word" />
+ <br>
+ <input type = "submit" value = "Submit">
+ {% csrf_token %}
+ </form>
+ {% if message %}
+ <p>{{ message }}</p>
+ {% endif %}
+
+ {% if word_found %}
+ <table id="id_word_table">
+ {% for word in word_found %}
+ <tr><td id="word.word"><a href="{% url 'wordbucket:detail' word.id %}">{{ word.word }}</a></td></tr>
+ {% endfor %}
+ </table>
+ {% endif %}
+
+ <br>
+ <a href="{% url 'wordbucket:home' %}">HOME</a></td></tr>
+ </body>
+</html>




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

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