Fakultas Ilmu Komputer UI

Commit 324d280a authored by Kevin Albert Simanjuntak's avatar Kevin Albert Simanjuntak
Browse files

Redirect after POST, and show all items in template

parent 29736e9f
Pipeline #20633 passed with stage
in 38 seconds
No preview for this file type
......@@ -18,8 +18,6 @@ class NewVisitorTest(unittest.TestCase):
# She is invited to enter a to-do item straight away
def test_can_start_a_list_and_retrieve_it_later(self):
# Edith has heard about a cool new online to-do app. She goes
# to check out its homepage
self.browser.get('http://localhost:8000/homepage')
# She notices the page title and header mention to-do lists
......@@ -33,24 +31,13 @@ class NewVisitorTest(unittest.TestCase):
inputbox.get_attribute('placeholder'),
'Enter a to-do item'
)
# She types "Buy peacock feathers" into a text box (Edith's hobby
# is tying fly-fishing lures)
inputbox.send_keys('Buy peacock feathers')
# When she hits enter, the page updates, and now the page lists
# "1: Buy peacock feathers" as an item in a to-do list table
inputbox.send_keys('Buy peacock feathers')
inputbox.send_keys(Keys.ENTER)
time.sleep(1)
table = self.browser.find_element_by_id('id_list_table')
rows = table.find_elements_by_tag_name('tr')
#self.assertTrue(any(row.text == '1: Buy peacock feathers' for row in rows),f"New to-do item did not appear in table")
self.assertIn('1: Buy peacock feathers', [row.text for row in rows])
time.sleep(5)
inputbox = self.browser.find_element_by_id('id_new_item')
inputbox.send_keys('Use peacock feathers to make a fly')
inputbox.send_keys(Keys.ENTER)
time.sleep(1)
time.sleep(5)
self.check_for_row_in_list_table('1: Buy peacock feathers')
self.check_for_row_in_list_table('2: Use peacock feathers to make a fly')
......
......@@ -9,7 +9,9 @@
{% csrf_token %}
</form>
<table id="id_list_table">
<tr><td>1: {{ new_item_text }}</td></tr>
</table>
{% for item in items %}
<tr><td>{{ forloop.counter }}: {{ item.text }}</td></tr>
{% endfor %}
</table>
</body>
</html>
\ No newline at end of file
......@@ -28,6 +28,27 @@ class HomePageTest(TestCase):
self.assertIn('A new list item', response.content.decode())
self.assertTemplateUsed(response, 'home.html')
def test_only_saves_items_when_necessary(self):
self.client.get('/homepage/')
self.assertEqual(Item.objects.count(), 0)
def test_can_save_a_POST_request(self):
response = self.client.post('/homepage/', data={'item_text': 'A new list item'})
self.assertEqual(Item.objects.count(), 1)
new_item = Item.objects.first()
self.assertEqual(new_item.text, 'A new list item')
self.assertEqual(response.status_code, 302)
self.assertEqual(response['location'], '/homepage/')
def test_displays_all_list_items(self):
Item.objects.create(text='itemey 1')
Item.objects.create(text='itemey 2')
response = self.client.get('/homepage/')
self.assertIn('itemey 1', response.content.decode())
self.assertIn('itemey 2', response.content.decode())
class ItemModelTest(TestCase):
def test_saving_and_retrieving_items(self):
......@@ -45,4 +66,15 @@ class ItemModelTest(TestCase):
first_saved_item = saved_items[0]
second_saved_item = saved_items[1]
self.assertEqual(first_saved_item.text, 'The first (ever) list item')
self.assertEqual(second_saved_item.text, 'Item the second')
\ No newline at end of file
self.assertEqual(second_saved_item.text, 'Item the second')
def test_can_save_a_POST_request(self):
self.client.post('/homepage/', data={'item_text': 'A new list item'})
self.assertEqual(Item.objects.count(), 1)
new_item = Item.objects.first()
self.assertEqual(new_item.text, 'A new list item')
def test_redirects_after_POST(self):
response = self.client.post('/homepage/', data={'item_text': 'A new list item'})
self.assertEqual(response.status_code, 302)
self.assertEqual(response['location'], '/homepage/')
\ No newline at end of file
from django.http import HttpResponse
from django.shortcuts import render
from django.shortcuts import redirect, render
from lists.models import Item
def home_page(request):
return render(request, 'home.html', {
'new_item_text': request.POST.get('item_text', ''),
})
\ No newline at end of file
if request.method == 'POST':
Item.objects.create(text=request.POST['item_text'])
return redirect('/homepage/')
items = Item.objects.all()
return render(request, 'home.html', {'items': items})
\ No newline at end of file
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment