started lyric app (in earnest this time); added bootstrap-classed navbar w. login form to base.html; tweaked apps.dashboard.tests.test_views to accomodate multiple forms on same page
This commit is contained in:
@@ -16,26 +16,27 @@ class HomePageTest(TestCase):
|
|||||||
def test_renders_input_form(self):
|
def test_renders_input_form(self):
|
||||||
response = self.client.get('/')
|
response = self.client.get('/')
|
||||||
parsed = lxml.html.fromstring(response.content)
|
parsed = lxml.html.fromstring(response.content)
|
||||||
[form] = parsed.cssselect('form[method=POST]')
|
forms = parsed.cssselect('form[method=POST]')
|
||||||
self.assertEqual(form.get('action'), '/apps/dashboard/newlist')
|
self.assertIn("/apps/dashboard/new_list", [form.get("action") for form in forms])
|
||||||
inputs = form.cssselect('input')
|
[form] = [form for form in forms if form.get("action") == "/apps/dashboard/new_list"]
|
||||||
self.assertIn('text', [input.get('name') for input in inputs])
|
inputs = form.cssselect("input")
|
||||||
|
self.assertIn("text", [input.get("name") for input in inputs])
|
||||||
|
|
||||||
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={'text': 'A new list item'})
|
self. client.post("/apps/dashboard/new_list", data={"text": "A new list item"})
|
||||||
self.assertEqual(Item.objects.count(), 1)
|
self.assertEqual(Item.objects.count(), 1)
|
||||||
new_item = Item.objects.get()
|
new_item = Item.objects.get()
|
||||||
self.assertEqual(new_item.text, 'A new list item')
|
self.assertEqual(new_item.text, "A new list item")
|
||||||
|
|
||||||
def test_redirects_after_POST(self):
|
def test_redirects_after_POST(self):
|
||||||
response = self.client.post('/apps/dashboard/newlist', data={'text': 'A new list item'})
|
response = self.client.post("/apps/dashboard/new_list", data={"text": "A new list item"})
|
||||||
new_list = List.objects.get()
|
new_list = List.objects.get()
|
||||||
self.assertRedirects(response, f'/apps/dashboard/{new_list.id}/')
|
self.assertRedirects(response, f"/apps/dashboard/{new_list.id}/")
|
||||||
|
|
||||||
# Post invalid input helper
|
# Post invalid input helper
|
||||||
def post_invalid_input(self):
|
def post_invalid_input(self):
|
||||||
return self.client.post("/apps/dashboard/newlist", data={"text": ""})
|
return self.client.post("/apps/dashboard/new_list", data={"text": ""})
|
||||||
|
|
||||||
def test_for_invalid_input_nothing_saved_to_db(self):
|
def test_for_invalid_input_nothing_saved_to_db(self):
|
||||||
self.post_invalid_input()
|
self.post_invalid_input()
|
||||||
@@ -53,44 +54,46 @@ class NewListTest(TestCase):
|
|||||||
class ListViewTest(TestCase):
|
class ListViewTest(TestCase):
|
||||||
def test_uses_list_template(self):
|
def test_uses_list_template(self):
|
||||||
mylist = List.objects.create()
|
mylist = List.objects.create()
|
||||||
response = self.client.get(f'/apps/dashboard/{mylist.id}/')
|
response = self.client.get(f"/apps/dashboard/{mylist.id}/")
|
||||||
self.assertTemplateUsed(response, 'apps/dashboard/list.html')
|
self.assertTemplateUsed(response, "apps/dashboard/list.html")
|
||||||
|
|
||||||
def test_renders_input_form(self):
|
def test_renders_input_form(self):
|
||||||
mylist = List.objects.create()
|
mylist = List.objects.create()
|
||||||
response = self.client.get(f'/apps/dashboard/{mylist.id}/')
|
url = f"/apps/dashboard/{mylist.id}/"
|
||||||
|
response = self.client.get(url)
|
||||||
parsed = lxml.html.fromstring(response.content)
|
parsed = lxml.html.fromstring(response.content)
|
||||||
[form] = parsed.cssselect('form[method=POST]')
|
forms = parsed.cssselect("form[method=POST]")
|
||||||
self.assertEqual(form.get('action'), f"/apps/dashboard/{mylist.id}/")
|
self.assertIn(url, [form.get("action") for form in forms])
|
||||||
inputs = form.cssselect('input')
|
[form] = [form for form in forms if form.get("action") == url]
|
||||||
self.assertIn('text', [input.get('name') for input in inputs])
|
inputs = form.cssselect("input")
|
||||||
|
self.assertIn("text", [input.get("name") for input in inputs])
|
||||||
|
|
||||||
def test_displays_only_items_for_that_list(self):
|
def test_displays_only_items_for_that_list(self):
|
||||||
# Given/Arrange
|
# Given/Arrange
|
||||||
correct_list = List.objects.create()
|
correct_list = List.objects.create()
|
||||||
Item.objects.create(text='itemey 1', list=correct_list)
|
Item.objects.create(text="itemey 1", list=correct_list)
|
||||||
Item.objects.create(text='itemey 2', list=correct_list)
|
Item.objects.create(text="itemey 2", list=correct_list)
|
||||||
other_list = List.objects.create()
|
other_list = List.objects.create()
|
||||||
Item.objects.create(text='other list item', list=other_list)
|
Item.objects.create(text="other list item", list=other_list)
|
||||||
# When/Act
|
# When/Act
|
||||||
response = self.client.get(f'/apps/dashboard/{correct_list.id}/')
|
response = self.client.get(f"/apps/dashboard/{correct_list.id}/")
|
||||||
# Then/Assert
|
# Then/Assert
|
||||||
self.assertContains(response, 'itemey 1')
|
self.assertContains(response, "itemey 1")
|
||||||
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):
|
def test_can_save_a_POST_request_to_an_existing_list(self):
|
||||||
other_list = List.objects.create()
|
other_list = List.objects.create()
|
||||||
correct_list = List.objects.create()
|
correct_list = List.objects.create()
|
||||||
|
|
||||||
self.client.post(
|
self.client.post(
|
||||||
f'/apps/dashboard/{correct_list.id}/',
|
f"/apps/dashboard/{correct_list.id}/",
|
||||||
data={'text': 'A new item for an existing list'},
|
data={"text": "A new item for an existing list"},
|
||||||
)
|
)
|
||||||
|
|
||||||
self.assertEqual(Item.objects.count(), 1)
|
self.assertEqual(Item.objects.count(), 1)
|
||||||
new_item = Item.objects.get()
|
new_item = Item.objects.get()
|
||||||
self.assertEqual(new_item.text, 'A new item for an existing list')
|
self.assertEqual(new_item.text, "A new item for an existing list")
|
||||||
self.assertEqual(new_item.list, correct_list)
|
self.assertEqual(new_item.list, correct_list)
|
||||||
|
|
||||||
def test_POST_redirects_to_list_view(self):
|
def test_POST_redirects_to_list_view(self):
|
||||||
@@ -98,11 +101,11 @@ class ListViewTest(TestCase):
|
|||||||
correct_list = List.objects.create()
|
correct_list = List.objects.create()
|
||||||
|
|
||||||
response = self.client.post(
|
response = self.client.post(
|
||||||
f'/apps/dashboard/{correct_list.id}/',
|
f"/apps/dashboard/{correct_list.id}/",
|
||||||
data={'text': 'A new item for an existing list'},
|
data={"text": "A new item for an existing list"},
|
||||||
)
|
)
|
||||||
|
|
||||||
self.assertRedirects(response, f'/apps/dashboard/{correct_list.id}/')
|
self.assertRedirects(response, f"/apps/dashboard/{correct_list.id}/")
|
||||||
|
|
||||||
# Post invalid input helper
|
# Post invalid input helper
|
||||||
def post_invalid_input(self):
|
def post_invalid_input(self):
|
||||||
|
|||||||
@@ -2,6 +2,6 @@ from django.urls import path
|
|||||||
from . import views
|
from . import views
|
||||||
|
|
||||||
urlpatterns = [
|
urlpatterns = [
|
||||||
path('newlist', views.new_list, name='new_list'),
|
path('new_list', 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'),
|
||||||
]
|
]
|
||||||
|
|||||||
0
src/apps/lyric/__init__.py
Normal file
0
src/apps/lyric/__init__.py
Normal file
3
src/apps/lyric/admin.py
Normal file
3
src/apps/lyric/admin.py
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
from django.contrib import admin
|
||||||
|
|
||||||
|
# Register your models here.
|
||||||
5
src/apps/lyric/apps.py
Normal file
5
src/apps/lyric/apps.py
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
from django.apps import AppConfig
|
||||||
|
|
||||||
|
|
||||||
|
class LyricConfig(AppConfig):
|
||||||
|
name = 'lyric'
|
||||||
0
src/apps/lyric/migrations/__init__.py
Normal file
0
src/apps/lyric/migrations/__init__.py
Normal file
3
src/apps/lyric/models.py
Normal file
3
src/apps/lyric/models.py
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
from django.db import models
|
||||||
|
|
||||||
|
# Create your models here.
|
||||||
3
src/apps/lyric/tests.py
Normal file
3
src/apps/lyric/tests.py
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
from django.test import TestCase
|
||||||
|
|
||||||
|
# Create your tests here.
|
||||||
3
src/apps/lyric/views.py
Normal file
3
src/apps/lyric/views.py
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
from django.shortcuts import render
|
||||||
|
|
||||||
|
# Create your views here.
|
||||||
@@ -6,16 +6,35 @@
|
|||||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||||
<meta name="author" content="Disco DeDisco">
|
<meta name="author" content="Disco DeDisco">
|
||||||
<meta name="robots" content="noindex, nofollow">
|
<meta name="robots" content="noindex, nofollow">
|
||||||
<title>Dashboard | {% block title_text %}{% endblock title_text %}</title>
|
<title>Earthman RPG | {% block title_text %}{% endblock title_text %}</title>
|
||||||
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap/5.3.8/css/bootstrap.min.css"/>
|
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap/5.3.8/css/bootstrap.min.css"/>
|
||||||
</head>
|
</head>
|
||||||
|
|
||||||
<body>
|
<body>
|
||||||
<div class="container">
|
<div class="container">
|
||||||
|
<div class="navbar">
|
||||||
|
<div class="container-fluid">
|
||||||
|
<a href="/" class="navbar-brand">Welcome, Earthman</a>
|
||||||
|
<form method="POST" action="/apps/lyric/send_login_email">
|
||||||
|
<div class="input-group">
|
||||||
|
<label for="id_email_input" class="navbar-text me-2">
|
||||||
|
enter your email to log in
|
||||||
|
</label>
|
||||||
|
<input
|
||||||
|
id="id_email_input"
|
||||||
|
class="form-control"
|
||||||
|
name="email"
|
||||||
|
placeholder="your@email.here"
|
||||||
|
>
|
||||||
|
{% csrf_token %}
|
||||||
|
</div>
|
||||||
|
</form>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
<div class="row justify-content-center p-5 bg-body-tertiary rounded-3">
|
<div class="row justify-content-center p-5 bg-body-tertiary rounded-3">
|
||||||
<div class="col-lg-6 text-center">
|
<div class="col-lg-6 text-center">
|
||||||
<h1 class="display-1 mb-4">Dashboard</h1>
|
<h2 class="display-1 mb-4">{% block header_text %}{% endblock header_text %}</h2>
|
||||||
<h2 class="display-2 mb-2">{% block header_text %}{% endblock header_text %}</h2>
|
|
||||||
|
|
||||||
<form method="POST" action="{% block form_action %}{% endblock form_action %}">
|
<form method="POST" action="{% block form_action %}{% endblock form_action %}">
|
||||||
{% csrf_token %}
|
{% csrf_token %}
|
||||||
|
|||||||
Reference in New Issue
Block a user