Selaa lähdekoodia

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

david 5 vuotta sitten
vanhempi
commit
4c8d0876ba
4 muutettua tiedostoa jossa 62 lisäystä ja 18 poistoa
  1. 3 2
      SCRATCHPAD.txt
  2. 48 11
      lists/tests.py
  3. 9 4
      lists/views.py
  4. 2 1
      superlists/urls.py

+ 3 - 2
SCRATCHPAD.txt

@@ -6,8 +6,9 @@
 + Support more than 1 list!
 
 - Adjust model so that items are associated with different lists
-+ Add unique URLs for each list
+- Add unique URLs for each list
 - Add a URL for creating a new list via POST
-+ Add URLs for adding a new item to an existing list via POST
+- Add URLs for adding a new item to an existing list via POST
++ Refactor away some duplication in urls.py
 
 + Make a URL to view all lists made (Incase you loose the link that link can be found again)

+ 48 - 11
lists/tests.py

@@ -25,31 +25,43 @@ class HomePageTest(TestCase):
         self.assertTemplateUsed(response, 'home.html')
 
     def test_can_save_POST_request(self):
-        response = self.client.post('/', data={'item_text': 'A new list item'})
+        response = 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('/', data={'item_text': 'A new list item'})
-        self.assertEqual(response.status_code, 302)
-        self.assertEqual(response['location'], '/lists/the-only-list/')
+        response = self.client.post('/lists/new', data={'item_text': 'A new list item'})
+        new_list = List.objects.first()
+        self.assertRedirects(response, f'/lists/{new_list.id}/')
 
 class ListViewTest(TestCase):
 
     def test_uses_list_template(self):
-        response = self.client.get('/lists/the-only-list/')
+        list_ = List.objects.create()
+        response = self.client.get(f'/lists/{list_.id}/')
         self.assertTemplateUsed(response, 'list.html')
 
-    def test_displays_all_items(self):
-        list_ = List.objects.create()
-        Item.objects.create(text='itemey 1', listi=list_)
-        Item.objects.create(text='itemey 2', listi=list_)
+    def test_displays_all_items_for_that_list(self):
+        correct = List.objects.create()
+        Item.objects.create(text='itemey 1', listi=correct)
+        Item.objects.create(text='itemey 2', listi=correct)
+        other = List.objects.create()
+        Item.objects.create(text='other list item 1', listi=other)
+        Item.objects.create(text='other list item 2', listi=other)
 
-        response = self.client.get('/lists/the-only-list/')
+        response = self.client.get(f'/lists/{correct.id}/')
 
         self.assertContains(response, 'itemey 1')
         self.assertContains(response, 'itemey 2')
+        self.assertNotContains(response, 'other list item 1')
+        self.assertNotContains(response, 'other list item 2')
+    
+    def test_passes_correct_list_to_template(self):
+        other_list = List.objects.create()
+        correct_list = List.objects.create()
+        response = self.client.get(f'/lists/{correct_list.id}/')
+        self.assertEqual(response.context['list'], correct_list)
 
 class ListAndItemModelsTest(TestCase):
 
@@ -90,4 +102,29 @@ class NewListTest(TestCase):
 
     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/')
+        new_list = List.objects.first()
+        self.assertRedirects(response, f'/lists/{new_list.id}/')
+
+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(
+            f'/lists/{correct_list.id}/add_item',
+            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.listi, correct_list)
+
+
+    def test_redirects_to_list_view(self):
+        other_list = List.objects.create()
+        correct_list = List.objects.create()
+        response = self.client.post(
+            f'/lists/{correct_list.id}/add_item',
+            data={'item_text': 'A new item for an existing list'}
+        )
+        self.assertRedirects(response, f'/lists/{correct_list.id}/')

+ 9 - 4
lists/views.py

@@ -5,11 +5,16 @@ from lists.models import Item, List
 def home_page(request):
     return render(request, 'home.html')
 
-def view_list(request):
-    items = Item.objects.all()
-    return render(request, 'list.html', {'items': items})
+def view_list(request, list_id):
+    list_ = List.objects.get(id=list_id)
+    return render(request, 'list.html', {'list': list_})
 
 def new_list(request):
     list_ = List.objects.create()
     Item.objects.create(text=request.POST['item_text'], listi=list_)
-    return redirect('/lists/the-only-list/')
+    return redirect(f'/lists/{list_.id}/')
+
+def add_item(request, list_id):
+    list_ = List.objects.get(id=list_id)
+    Item.objects.create(text=request.POST['item_text'], listi=list_)
+    return redirect(f'/lists/{list_.id}/')

+ 2 - 1
superlists/urls.py

@@ -19,5 +19,6 @@ from lists 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/$', 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'),
 ]