Fakultas Ilmu Komputer UI

Commit 9a839a81 authored by Muhammad Ilham Peruzzi's avatar Muhammad Ilham Peruzzi
Browse files

new URL + view for adding to existing lists. FT passes

parent ce7426c8
......@@ -12,19 +12,19 @@
</div>
<hr>
<h1>Your To-Do list</h1>
<form method="POST" action="/lists/new">
<form method="POST" action="/lists/{{ list.id }}/add_item">
<input name="item_text" id="id_new_item" placeholder="Enter a to-do item" />
{% csrf_token %}
</form>
{% if items|length == 0 %}
{% if list.item_set.all|length == 0 %}
<p>yey, waktunya berlibur</p>
{% elif items|length < 5 %}
{% elif list.item_set.all|length < 5 %}
<p>sibuk tapi santai</p>
{% else %}
<p>oh tidak</p>
{% endif %}
<table id="id_list_table">
{% for item in items %}
{% for item in list.item_set.all %}
<tr>
<td>{{ forloop.counter }}: {{ item.text }}</td>
</tr>
......
......@@ -132,3 +132,30 @@ class ListViewTest(TestCase):
response = self.client.post('/lists/new',data={'item_text': 'A new list item'})
new_list = List.objects.first()
self.assertRedirects(response, '/lists/%d/' % (new_list.id,))
def test_passes_correct_list_to_template(self):
other_list = List.objects.create()
correct_list = List.objects.create()
response = self.client.get('/lists/%d/' % (correct_list.id,))
self.assertEqual(response.context['list'], correct_list)
class NewItemTest(TestCase):
def test_can_save_a_POST_request_to_an_existing_list(self):
other_list = List.objects.create()
correct_list = List.objects.create()
self.client.post(
'/lists/%d/add_item' % (correct_list.id,),
data={'item_text': 'A new item for an existing list'}
)
self.assertEqual(Item.objects.count(), 1)
new_item = Item.objects.first()
self.assertEqual(new_item.text, 'A new item for an existing list')
self.assertEqual(new_item.list, correct_list)
def test_redirects_to_list_view(self):
other_list = List.objects.create()
correct_list = List.objects.create()
response = self.client.post('/lists/%d/add_item' % (correct_list.id,),data={'item_text': 'A new item for an existing list'})
self.assertRedirects(response, '/lists/%d/' % (correct_list.id,))
......@@ -18,10 +18,14 @@ def about_me():
def view_list(request, list_id):
list_ = List.objects.get(id=list_id)
items = Item.objects.filter(list=list_)
return render(request, 'list.html', {'items': items})
return render(request, 'list.html', {'list': list_})
def new_list(request):
list_ = List.objects.create()
Item.objects.create(text=request.POST['item_text'], list=list_)
return redirect('/lists/%d/' % (list_.id,))
def add_item(request, list_id):
list_ = List.objects.get(id=list_id)
Item.objects.create(text=request.POST['item_text'], list=list_)
return redirect('/lists/%d/' % (list_.id,))
\ No newline at end of file
......@@ -19,7 +19,8 @@ from lists import views
urlpatterns = [
url(r'^$', views.home_page , name='home'),
url(r'^lists/(.+)/$', views.view_list, name='view_list'),
url(r'^lists/(\d+)/$', views.view_list, name='view_list'),
url(r'^lists/(\d+)/add_item$', views.add_item, name='add_item'),
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