new form model in apps.dashboard.forms to handle existing item-lists; new test model in .tests.test_forms to accommodate; also new method in .test_views, but skipped for now
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
from django import forms
|
||||
from django.core.exceptions import ValidationError
|
||||
from .models import Item
|
||||
|
||||
DUPLICATE_ITEM_ERROR = "You've already logged this to your list"
|
||||
EMPTY_ITEM_ERROR = "You can't have an empty list item"
|
||||
|
||||
class ItemForm(forms.models.ModelForm):
|
||||
@@ -20,3 +22,14 @@ class ItemForm(forms.models.ModelForm):
|
||||
def save(self, for_list):
|
||||
self.instance.list = for_list
|
||||
return super().save()
|
||||
|
||||
class ExistingListItemForm(ItemForm):
|
||||
def __init__(self, for_list, *args, **kwargs):
|
||||
super().__init__(*args, **kwargs)
|
||||
self.instance.list = for_list
|
||||
|
||||
def clean_text(self):
|
||||
text = self.cleaned_data["text"]
|
||||
if self.instance.list.item_set.filter(text=text).exists():
|
||||
raise forms.ValidationError(DUPLICATE_ITEM_ERROR)
|
||||
return text
|
||||
Reference in New Issue
Block a user