removed reliance on built-in django ModelForms from templates/core/base.html, apps.dashboard.forms, .tests.test_forms & .test_views
This commit is contained in:
@@ -5,40 +5,28 @@ from .models import Item
|
|||||||
DUPLICATE_ITEM_ERROR = "You've already logged this to your list"
|
DUPLICATE_ITEM_ERROR = "You've already logged this to your list"
|
||||||
EMPTY_ITEM_ERROR = "You can't have an empty list item"
|
EMPTY_ITEM_ERROR = "You can't have an empty list item"
|
||||||
|
|
||||||
class ItemForm(forms.models.ModelForm):
|
class ItemForm(forms.Form):
|
||||||
class Meta:
|
text = forms.CharField(
|
||||||
model = Item
|
error_messages = {"required": EMPTY_ITEM_ERROR},
|
||||||
fields = ("text",)
|
required=True,
|
||||||
widgets = {
|
)
|
||||||
"text": forms.widgets.TextInput(
|
|
||||||
attrs={
|
|
||||||
"placeholder": "Enter a to-do item",
|
|
||||||
"class": "form-control form-control-lg",
|
|
||||||
}
|
|
||||||
),
|
|
||||||
}
|
|
||||||
error_messages = {"text": {"required": EMPTY_ITEM_ERROR}}
|
|
||||||
|
|
||||||
def is_valid(self):
|
|
||||||
result = super().is_valid()
|
|
||||||
if not result:
|
|
||||||
self.fields["text"].widget.attrs["class"] += " is-invalid"
|
|
||||||
return result
|
|
||||||
|
|
||||||
def save(self, for_list):
|
def save(self, for_list):
|
||||||
self.instance.list = for_list
|
return Item.objects.create(
|
||||||
return super().save()
|
list=for_list,
|
||||||
|
text=self.cleaned_data["text"],
|
||||||
|
)
|
||||||
|
|
||||||
class ExistingListItemForm(ItemForm):
|
class ExistingListItemForm(ItemForm):
|
||||||
def __init__(self, for_list, *args, **kwargs):
|
def __init__(self, for_list, *args, **kwargs):
|
||||||
super().__init__(*args, **kwargs)
|
super().__init__(*args, **kwargs)
|
||||||
self.instance.list = for_list
|
self._for_list = for_list
|
||||||
|
|
||||||
def clean_text(self):
|
def clean_text(self):
|
||||||
text = self.cleaned_data["text"]
|
text = self.cleaned_data["text"]
|
||||||
if self.instance.list.item_set.filter(text=text).exists():
|
if self._for_list.item_set.filter(text=text).exists():
|
||||||
raise forms.ValidationError(DUPLICATE_ITEM_ERROR)
|
raise forms.ValidationError(DUPLICATE_ITEM_ERROR)
|
||||||
return text
|
return text
|
||||||
|
|
||||||
def save(self):
|
def save(self):
|
||||||
return forms.models.ModelForm.save(self)
|
return super().save(for_list=self._for_list)
|
||||||
|
|||||||
@@ -8,12 +8,6 @@ from ..forms import (
|
|||||||
from ..models import Item, List
|
from ..models import Item, List
|
||||||
|
|
||||||
class ItemFormTest(TestCase):
|
class ItemFormTest(TestCase):
|
||||||
def test_form_item_has_placeholder_and_css_classes(self):
|
|
||||||
form = ItemForm()
|
|
||||||
rendered = form.as_p()
|
|
||||||
self.assertIn('placeholder="Enter a to-do item"', rendered)
|
|
||||||
self.assertIn('class="form-control form-control-lg"', rendered)
|
|
||||||
|
|
||||||
def test_form_validation_for_blank_items(self):
|
def test_form_validation_for_blank_items(self):
|
||||||
form = ItemForm(data={"text": ""})
|
form = ItemForm(data={"text": ""})
|
||||||
form.save()
|
form.save()
|
||||||
@@ -23,15 +17,6 @@ class ItemFormTest(TestCase):
|
|||||||
self.assertFalse(form.is_valid())
|
self.assertFalse(form.is_valid())
|
||||||
self.assertEqual(form.errors["text"], [EMPTY_ITEM_ERROR])
|
self.assertEqual(form.errors["text"], [EMPTY_ITEM_ERROR])
|
||||||
|
|
||||||
def test_invalid_form_has_bootstrap_is_invalid_css_class(self):
|
|
||||||
form = ItemForm(data={"text": ""})
|
|
||||||
self.assertFalse(form.is_valid())
|
|
||||||
field = form.fields["text"]
|
|
||||||
self.assertEqual(
|
|
||||||
field.widget.attrs["class"],
|
|
||||||
"form-control form-control-lg is-invalid",
|
|
||||||
)
|
|
||||||
|
|
||||||
def test_form_save_handles_saving_to_a_list(self):
|
def test_form_save_handles_saving_to_a_list(self):
|
||||||
mylist = List.objects.create()
|
mylist = List.objects.create()
|
||||||
form = ItemForm(data={"text": "do re mi"})
|
form = ItemForm(data={"text": "do re mi"})
|
||||||
@@ -41,11 +26,6 @@ class ItemFormTest(TestCase):
|
|||||||
self.assertEqual(new_item.list, mylist)
|
self.assertEqual(new_item.list, mylist)
|
||||||
|
|
||||||
class ExistingListItemFormTest(TestCase):
|
class ExistingListItemFormTest(TestCase):
|
||||||
def test_form_renders_item_text_input(self):
|
|
||||||
list_ = List.objects.create()
|
|
||||||
form = ExistingListItemForm(for_list=list_)
|
|
||||||
self.assertIn('placeholder="Enter a to-do item"', form.as_p())
|
|
||||||
|
|
||||||
def test_form_validation_for_blank_items(self):
|
def test_form_validation_for_blank_items(self):
|
||||||
list_ = List.objects.create()
|
list_ = List.objects.create()
|
||||||
form = ExistingListItemForm(for_list=list_, data={"text": ""})
|
form = ExistingListItemForm(for_list=list_, data={"text": ""})
|
||||||
|
|||||||
@@ -50,7 +50,7 @@ class NewListTest(TestCase):
|
|||||||
response = self.post_invalid_input()
|
response = self.post_invalid_input()
|
||||||
self.assertContains(response, html.escape(EMPTY_ITEM_ERROR))
|
self.assertContains(response, html.escape(EMPTY_ITEM_ERROR))
|
||||||
|
|
||||||
class DashViewTest(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}/')
|
||||||
@@ -122,6 +122,12 @@ class DashViewTest(TestCase):
|
|||||||
response = self.post_invalid_input()
|
response = self.post_invalid_input()
|
||||||
self.assertContains(response, html.escape(EMPTY_ITEM_ERROR))
|
self.assertContains(response, html.escape(EMPTY_ITEM_ERROR))
|
||||||
|
|
||||||
|
def test_for_invalid_input_sets_is_invalid_class(self):
|
||||||
|
response = self.post_invalid_input()
|
||||||
|
parsed = lxml.html.fromstring(response.content)
|
||||||
|
[input] = parsed.cssselect("input[name=text]")
|
||||||
|
self.assertIn("is-invalid", set(input.classes))
|
||||||
|
|
||||||
def test_duplicate_item_validation_errors_end_up_on_lists_page(self):
|
def test_duplicate_item_validation_errors_end_up_on_lists_page(self):
|
||||||
list1 = List.objects.create()
|
list1 = List.objects.create()
|
||||||
Item.objects.create(list=list1, text="lorem ipsum")
|
Item.objects.create(list=list1, text="lorem ipsum")
|
||||||
|
|||||||
@@ -18,10 +18,20 @@
|
|||||||
<h2 class="display-2 mb-2">{% 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 %}">
|
||||||
{{ form.text }}
|
|
||||||
{% csrf_token %}
|
{% csrf_token %}
|
||||||
|
<input
|
||||||
|
id="id_text"
|
||||||
|
name="text"
|
||||||
|
class="form-control form-control-lg{% if form.errors %} is-invalid{% endif %}"
|
||||||
|
placeholder="Enter a to-do item"
|
||||||
|
value="{{ form.text.value | default:'' }}"
|
||||||
|
aria-describedby="id_text_feedback"
|
||||||
|
required
|
||||||
|
/>
|
||||||
{% if form.errors %}
|
{% if form.errors %}
|
||||||
<div class="invalid-feedback">{{ form.errors.text }}</div>
|
<div id="id_text_feedback" class="invalid-feedback">
|
||||||
|
{{ form.errors.text.0 }}
|
||||||
|
</div>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
</form>
|
</form>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
Reference in New Issue
Block a user