Fakultas Ilmu Komputer UI

Commit 41e8ea49 authored by Muhammad Ilham Peruzzi's avatar Muhammad Ilham Peruzzi
Browse files

make new_list function

parent 87d1b323
......@@ -12,7 +12,7 @@
</div>
<hr>
<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>
......
......@@ -12,7 +12,7 @@
</div>
<hr>
<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>
......
......@@ -53,34 +53,6 @@ class HomePageTest(TestCase):
self.assertIn('Bio', html_response)
self.assertTrue(len(about_me()) >= 100)
def test_home_page_can_save_a_POST_request(self):
request = HttpRequest()
request.method = 'POST'
request.POST['item_text'] = 'A new list item'
request.POST['bio'] = about_me()
response = home_page(request)
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'], '/lists/the-only-list-in-the-world/')
def test_home_page_only_saves_items_when_necessary(self):
request = HttpRequest()
home_page(request)
self.assertEqual(Item.objects.count(), 0)
def test_home_page_redirects_after_POST(self):
request = HttpRequest()
request.method = 'POST'
request.POST['item_text'] = 'A new list item'
response = home_page(request)
self.assertEqual(response.status_code, 302)
self.assertEqual(response['location'], '/lists/the-only-list-in-the-world/')
def test_automatic_comment_when_no_items(self):
self.assertEqual(Item.objects.count(), 0)
request = HttpRequest()
......@@ -120,4 +92,14 @@ class ListViewTest(TestCase):
Item.objects.create(text='itemey 2')
response = self.client.get('/lists/the-only-list-in-the-world/')
self.assertContains(response, 'itemey 1')
self.assertContains(response, 'itemey 2')
\ No newline at end of file
self.assertContains(response, 'itemey 2')
def test_saving_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.assertRedirects(response, '/lists/the-only-list-in-the-world/')
......@@ -8,10 +8,6 @@ response = {}
def home_page(request):
html = 'homepage.html'
response["bio"] = about_me()
if request.method == 'POST':
Item.objects.create(text=request.POST['item_text'])
return redirect('/lists/the-only-list-in-the-world/')
return render(request, html, {'bio':about_me()})
def about_me():
......@@ -22,4 +18,8 @@ def about_me():
def view_list(request):
items = Item.objects.all()
return render(request, 'list.html', {'items': items})
\ No newline at end of file
return render(request, '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
......@@ -20,5 +20,6 @@ from lists import views
urlpatterns = [
url(r'^$', views.home_page , name='home'),
url(r'^lists/the-only-list-in-the-world/$', views.view_list,name='view_list'),
url(r'^lists/new$', views.new_list, name='new_list'),
#url(r'^admin/', admin.site.urls),
]
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