apps.dashboard.views refactored to handle new item POST requests; add_item() FBV eliminated for newfound redundancy
This commit is contained in:
@@ -27,7 +27,7 @@ class DashViewTest(TestCase):
|
|||||||
response = self.client.get(f'/apps/dashboard/{mylist.id}/')
|
response = self.client.get(f'/apps/dashboard/{mylist.id}/')
|
||||||
parsed = lxml.html.fromstring(response.content)
|
parsed = lxml.html.fromstring(response.content)
|
||||||
[form] = parsed.cssselect('form[method=POST]')
|
[form] = parsed.cssselect('form[method=POST]')
|
||||||
self.assertEqual(form.get('action'), f"/apps/dashboard/{mylist.id}/add-item")
|
self.assertEqual(form.get('action'), f"/apps/dashboard/{mylist.id}/")
|
||||||
inputs = form.cssselect('input')
|
inputs = form.cssselect('input')
|
||||||
self.assertIn('item_text', [input.get('name') for input in inputs])
|
self.assertIn('item_text', [input.get('name') for input in inputs])
|
||||||
|
|
||||||
@@ -45,6 +45,31 @@ class DashViewTest(TestCase):
|
|||||||
self.assertContains(response, 'itemey 2')
|
self.assertContains(response, 'itemey 2')
|
||||||
self.assertNotContains(response, 'other list item')
|
self.assertNotContains(response, 'other list item')
|
||||||
|
|
||||||
|
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'/apps/dashboard/{correct_list.id}/',
|
||||||
|
data={'item_text': 'A new item for an existing list'},
|
||||||
|
)
|
||||||
|
|
||||||
|
self.assertEqual(Item.objects.count(), 1)
|
||||||
|
new_item = Item.objects.get()
|
||||||
|
self.assertEqual(new_item.text, 'A new item for an existing list')
|
||||||
|
self.assertEqual(new_item.list, correct_list)
|
||||||
|
|
||||||
|
def test_POST_redirects_to_list_view(self):
|
||||||
|
other_list = List.objects.create()
|
||||||
|
correct_list = List.objects.create()
|
||||||
|
|
||||||
|
response = self.client.post(
|
||||||
|
f'/apps/dashboard/{correct_list.id}/',
|
||||||
|
data={'item_text': 'A new item for an existing list'},
|
||||||
|
)
|
||||||
|
|
||||||
|
self.assertRedirects(response, f'/apps/dashboard/{correct_list.id}/')
|
||||||
|
|
||||||
class NewListTest(TestCase):
|
class NewListTest(TestCase):
|
||||||
def test_can_save_a_POST_request(self):
|
def test_can_save_a_POST_request(self):
|
||||||
self. client.post('/apps/dashboard/newlist', data={'item_text': 'A new list item'})
|
self. client.post('/apps/dashboard/newlist', data={'item_text': 'A new list item'})
|
||||||
@@ -68,29 +93,3 @@ class NewListTest(TestCase):
|
|||||||
self.client.post("/apps/dashboard/newlist", data={"item_text": ""})
|
self.client.post("/apps/dashboard/newlist", data={"item_text": ""})
|
||||||
self.assertEqual(List.objects.count(), 0)
|
self.assertEqual(List.objects.count(), 0)
|
||||||
self.assertEqual(Item.objects.count(), 0)
|
self.assertEqual(Item.objects.count(), 0)
|
||||||
|
|
||||||
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'/apps/dashboard/{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.get()
|
|
||||||
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(
|
|
||||||
f'/apps/dashboard/{correct_list.id}/add-item',
|
|
||||||
data={'item_text': 'A new item for an existing list'},
|
|
||||||
)
|
|
||||||
|
|
||||||
self.assertRedirects(response, f'/apps/dashboard/{correct_list.id}/')
|
|
||||||
|
|||||||
@@ -4,5 +4,4 @@ from . import views
|
|||||||
urlpatterns = [
|
urlpatterns = [
|
||||||
path('newlist', views.new_list, name='new_list'),
|
path('newlist', views.new_list, name='new_list'),
|
||||||
path('<int:list_id>/', views.view_list, name='view_list'),
|
path('<int:list_id>/', views.view_list, name='view_list'),
|
||||||
path('<int:list_id>/add-item', views.add_item, name='add_item'),
|
|
||||||
]
|
]
|
||||||
|
|||||||
@@ -19,10 +19,7 @@ def new_list(request):
|
|||||||
|
|
||||||
def view_list(request, list_id):
|
def view_list(request, list_id):
|
||||||
our_list = List.objects.get(id=list_id)
|
our_list = List.objects.get(id=list_id)
|
||||||
return render(request, 'apps/dashboard/list.html', {'list': our_list})
|
if request.method == "POST":
|
||||||
|
|
||||||
def add_item(request, list_id):
|
|
||||||
our_list = List.objects.get(id=list_id)
|
|
||||||
Item.objects.create(text=request.POST['item_text'], list=our_list)
|
Item.objects.create(text=request.POST['item_text'], list=our_list)
|
||||||
return redirect(f'/apps/dashboard/{our_list.id}/')
|
return redirect(f'/apps/dashboard/{our_list.id}/')
|
||||||
|
return render(request, 'apps/dashboard/list.html', {'list': our_list})
|
||||||
|
|||||||
@@ -10,9 +10,9 @@ class NewVisitorTest(FunctionalTest):
|
|||||||
def test_can_start_a_todo_list(self):
|
def test_can_start_a_todo_list(self):
|
||||||
self.browser.get(self.live_server_url)
|
self.browser.get(self.live_server_url)
|
||||||
|
|
||||||
self.assertIn('Dashboard | ', self.browser.title)
|
self.assertIn('Dashboard', self.browser.title)
|
||||||
header_text = self.browser.find_element(By.TAG_NAME, 'h1').text
|
header_text = self.browser.find_element(By.TAG_NAME, 'h1').text
|
||||||
self.assertIn('Dashboard | ', header_text)
|
self.assertIn('Dashboard', header_text)
|
||||||
|
|
||||||
inputbox = self.browser.find_element(By.ID, 'id-new-item')
|
inputbox = self.browser.find_element(By.ID, 'id-new-item')
|
||||||
self.assertEqual(inputbox.get_attribute('placeholder'), 'Enter a to-do item')
|
self.assertEqual(inputbox.get_attribute('placeholder'), 'Enter a to-do item')
|
||||||
|
|||||||
@@ -3,7 +3,7 @@
|
|||||||
{% block title_text %}Your to-do list{% endblock title_text %}
|
{% block title_text %}Your to-do list{% endblock title_text %}
|
||||||
{% block header_text %}Your to-do list{% endblock header_text %}
|
{% block header_text %}Your to-do list{% endblock header_text %}
|
||||||
|
|
||||||
{% block form_action %}/apps/dashboard/{{ list.id }}/add-item{% endblock form_action %}
|
{% block form_action %}/apps/dashboard/{{ list.id }}/{% endblock form_action %}
|
||||||
|
|
||||||
{% block table %}
|
{% block table %}
|
||||||
<table id="id-list-table" class="table">
|
<table id="id-list-table" class="table">
|
||||||
|
|||||||
Reference in New Issue
Block a user