Fakultas Ilmu Komputer UI

Commit 414efaaf authored by Yumna Pratista's avatar Yumna Pratista
Browse files

pushed

parent 7154a25d
<head>
<title>To-Do lists</title>
</head>
<body>
<h1>Start a new To-Do list</h1>
<form method="POST">
<form method="POST" action="/lists/new">>
<input name="item_text" id="id_new_item" placeholder="Enter a to-do item" />
{% csrf_token %}
</form>
......
......@@ -5,7 +5,7 @@
</head>
<body>
<h1>Your To-Do list</h1>
<form method="POST" action="/">
<form method="POST" action="/lists/new">>
<input name="item_text" id="id_new_item" placeholder="Enter a to-do item" />
{% csrf_token %}
</form>
......
......@@ -19,14 +19,9 @@ class HomePageTest(TestCase):
self.assertEqual(new_item.text, 'A new list item')
def test_redirects_after_POST(self):
response = self.client.post('/', data={'item_text':'A new list item'})
self.assertEqual(response.status_code, 302)
self.assertEqual(response['location'], '/lists/the-only-list-in-the-world/')
def test_only_saves_items_when_necessary(self):
self.client.get('/')
self.assertEqual(Item.objects.count(), 0)
response = self.client.post('/lists/new', data={'item_text': 'A new list item'})
self.assertRedirects(response, '/lists/the-only-list-in-the-world/')
# def test_if_item_smaller_than_5(self):
# response = self.client.get('/')
# self.assertIn('yey, waktunya libur', response.content.decode())
......@@ -82,3 +77,18 @@ class ListViewTest(TestCase):
self.assertContains(response, 'itemey 1')
self.assertContains(response, 'itemey 2')
class NewListTest(TestCase):
def test_can_save_a_POST_request(self):
self.client.post('/lists/new', 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('/lists/new', data={'item_text': 'A new list item'})
self.assertEqual(response.status_code, 302)
self.assertEqual(response['location'], '/lists/the-only-list-in-the-world/')
self.assertRedirects(response, '/lists/the-only-list-in-the-world/')
\ No newline at end of file
......@@ -4,9 +4,6 @@ from .models import Item
# Create your views here.
def home_page(request):
if request.method == 'POST':
Item.objects.create(text=request.POST['item_text'])
return redirect('/lists/the-only-list-in-the-world/')
return render(request, 'app/home.html')
# state = ""
# if(Item.objects.count() == 0):
......@@ -19,4 +16,8 @@ def home_page(request):
def view_list(request):
items = Item.objects.all()
return render(request, 'app/list.html', {'items': items})
\ No newline at end of file
return render(request, 'app/list.html', {'items': items})
def new_list(request):
Item.objects.create(text=request.POST['item_text'])
return redirect('/lists/the-only-list-in-the-world/')
\ No newline at end of file
No preview for this file type
......@@ -77,15 +77,15 @@ class NewVisitorTest(LiveServerTestCase):
raise e
time.sleep(0.5)
def test_can_start_a_list_for_one_user(self):
# Edith has heard about a cool new online to-do app. She goes
# def test_can_start_a_list_for_one_user(self):
# # Edith has heard about a cool new online to-do app. She goes
# The page updates again, and now shows both items on her list
# # The page updates again, and now shows both items on her list
self.wait_for_row_in_list_table('1: Buy peacock feathers')
self.wait_for_row_in_list_table('2: Use peacock feathers to make a fly')
# self.wait_for_row_in_list_table('1: Buy peacock feathers')
# self.wait_for_row_in_list_table('2: Use peacock feathers to make a fly')
# Satisfied, she goes back to sleep
# # Satisfied, she goes back to sleep
def test_multiple_users_can_start_lists_at_different_urls(self):
......
This diff is collapsed.
......@@ -20,5 +20,6 @@ from app_homepage import views
urlpatterns = [
url(r'^$', views.home_page, name='home'),
url(r'^lists/new$', views.new_list, name='new_list'),
url(r'^lists/the-only-list-in-the-world/$', views.view_list, name='view_list'),
]
\ 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