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

Commit 12 Wordbucket : change search function for add browse (a-z)

Assignment1 : Wordbucket GitHub Link

Commit 12 Wordbucket : change search function for add browse (a-z)

Commits on Mar 11, 2018

update README.md

- เพิ่มคำอธิบาย repository ของ assignment ที่ทำ โดยมี template ตาม link

- "#" หัวข้อใหญ่สุด
- "##"... หัวข้อรองลงมาเรื่อย(ยิ่ง # มากยิ่งเล็กลง)
- "-" ข้างหน้าจะเป็น list ต่างๆ

84  README.md
@@ -1,2 +1,86 @@
# wordbucket
+Assignment 1 : My webapp. < WordBucket > by django framework
+เป็นส่วนหนึ่งของวิชา Software Development Practice II
+ภาษาที่ไม่มีการนำไปใช้ในชีวิตประจำวันเราเรียกว่า “ภาษาที่ตายแล้ว” ปัจจุบันแต่ละวันที่คำศัพท์เกิดใหม่มากมาย
+เช่น
+- weeb A weeb (/wi b/) is a non-Japanese male who watches and is a fan of CGDCT anime, has a waifu, a waifu pillow and is obsessed with Japan. [Credit](https://www.urbandictionary.com/define.php?term=weeb)
+
+- Jagoogala = just google it!
+ซึ่ง webapp. ที่อยากสร้างคือ word bank ให้ user มาแชร์ และ เก็บคำศัพท์ใหม่ๆที่เกิดขึ้นในปัจจุบัน
+
+## Features
+
+* Add word (with description)
+* Search (find word)
+* Browse (a-z)
+* Vote (useful or not)
+* Remove word (for Admin ex. Racism’s word…)
+
+Domain name we thought “https://WordBucket.com/”
+
+==== Future Features ====
+* login system
+* csv
+
+
+# Django part
+
+## Model
+
+3 classes
+* word
+* explanation
+* like and dislike
+
+## View
+
+* home_page : หน้า home page ของ webapp.
+* add_word : เพิ่มคำศัพท์พร้อมคำอธิบาย (ถ้าคำศัพท์ซ้ำจะโชว์ url ของคำศัพท์นั้นๆ เพื่อให้ user เข้าไปเพิ่ม คำอธิบาย ของตัวเองใน คำศัพท์ที่มีอยู่แล้วได้)
+* view_word : ดูในแต่คำมีคำอธิบายอะไรบ้าง
+* add_explanation : ใส่คำอธิบายเพิ่มในคำๆนั้น
+* vote_like : โหวตชอบ
+* vote_dislike : โหวตไม่ชอบ
+* search : ค้นหาคำศัพท์
+
+==== Future View function ====
+(after login system)
+* remove_word (for admin) : ลบคำศัพท์สำหรับ เช่น Racism’s word
+* user_vote : โหวตของ user
+
+## URL config
+
+link urls.py ของ project กับ urls.py ของ app. name เหมือนชื่อ function ทุกอัน
+
+* home_page > '' (path)
+* add_word > 'add' (path)
+* view_word > r'^(\d+)/$' (repath)
+* add_explanation > r'^(\d+)/add_explanation$' (re_path)
+* vote_like > r'^(\d+)/vote_like$' (re_path)
+* vote_dislike > r'^(\d+)/vote_dislike$' (re_path)
+* search > r'^search/(.+)$' (re_path)
+
+## Tests
+
+functional test
+
+unit test
+
+now have 7 classes
+
+* HomePageTest
+* WordViewTest
+* NewWordTest
+* NewExplanationTest
+* VoteTest
+* AllAroundModelsTest
+* SearchAndBrowseTest
+
+
+## รายชื่อสมาชิกกลุ่ม
+
+* นาย ศุภณ์กัญจน์ สัตตพงศ์ 5701012620128 [blog link](https://b2-5720128.blogspot.com/)
+* นาย ไอยคุปต์ อาภรณ์ศิริ 5701012630204 [blog link](http://sdp-5730204.blogspot.com/)
+* นาย ณฤดล ทรงอนุสรณ์ 5701012610122 [blog link](https://softwaredevii.blogspot.com/)
+
+

unit test

- แก้ test เดิมเปลี่ยน url ของ search ธรรมดาเป็น /search/byword สำหรับ search ธรรมดาจะได้ไม่ไปเรียกใช้ search แบบที่ 2 (link)


@@ -171,16 +171,16 @@ def test_redirects_to_word_view(self):
class SearchAndBrowseTest(TestCase):
def test_uses_search_template(self):
- response = self.client.get('/search')
+ response = self.client.get('/search/byword')
self.assertTemplateUsed(response, 'search.html')
def test_render_after_POST(self):
- response = self.client.post('/search', data={'search_input': 'A new list word'})
+ response = self.client.post('/search/byword', 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'})
+ response = self.client.post('/search/byword', data={'search_input': 'A new list word'})
html = response.content.decode('utf8')
self.assertIn('A new list word', html)

urls.py

- เปลี่ยนไปใช้ repath เพราะจะใช้ regular expression โดยเปลี่ยน path เป็น search/(word ใดๆ)


@@ -5,7 +5,7 @@
urlpatterns = [
path('', views.home_page, name='home'),
path('add_word', views.add_word, name='add_word'),
- path('search', views.search, name='search'),
+ re_path(r'^search/(\w+)$', 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

- import string เพิ่มวนลูปเก็บตัวอักษรในเก็บไว้ในตัวแปร alphabets และส่งในหน้า home สำหรับ browse function
- เพิ่ม search แบบที่ 2 (link)

@@ -1,10 +1,12 @@
from django.shortcuts import redirect, render
from wordbucket.models import Word, Explanation, Like_and_dislike
+import string
def home_page(request):
d_message = ""
words = Word.objects.order_by('-date_pub')[:5]
- return render(request, 'home.html', {'words': words, 'd_message': d_message})
+ alphabets = string.ascii_lowercase
+ return render(request, 'home.html', {'words': words, 'd_message': d_message, 'alphabets': alphabets})
def view_word(request, word_id):
d_message = ""
@@ -40,10 +42,11 @@ 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):
+def search(request, word_search):
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 :
@@ -52,7 +55,12 @@ def search(request):
else :
return render(request, 'search.html', {'word_found': word_found})
else :
- return render(request, 'search.html')
+ word_found = Word.objects.filter(word__startswith=word_search)
+ 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})
def vote_like(request):
pass

/templates/home.html

- เพิ่ม alphabets และวนลูปใส่ path สำหรับ browse function
- และเปลี่ยน url ของ search ธรรมดาเป็น /search/byword สำหรับ search ธรรมดาจะได้ไม่ไปเรียกใช้ search แบบที่ 2 (link)


@@ -4,6 +4,11 @@
</head>
<body>
<h1>Word Bucket</h1>
+ <br>browse<br>
+ {% for alphabet in alphabets %}
+ <a href="{% url 'wordbucket:search' alphabet %}">{{ alphabet }}</a>&nbsp;
+ {% endfor %}
+ <br>
<h4>{{ d_message }}</h4>
<form method="POST" id="form1" action="/add_word">
<input name="word_input" id="id_new_word" placeholder="Add new word" />
@@ -18,7 +23,7 @@ <h4>{{ d_message }}</h4>
{% endfor %}
</table>
<br>
- <form action = "/search" id="form_search" method="POST">
+ <form action = "/search/byword" 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 %}

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

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