Files
python-tdd/src/apps/dashboard/forms.py

33 lines
966 B
Python
Raw Normal View History

from django import forms
from django.core.exceptions import ValidationError
from .models import Item
DUPLICATE_ITEM_ERROR = "You've already logged this to your note"
EMPTY_ITEM_ERROR = "You can't have an empty note item"
class ItemForm(forms.Form):
text = forms.CharField(
error_messages = {"required": EMPTY_ITEM_ERROR},
required=True,
)
def save(self, for_note):
return Item.objects.create(
note=for_note,
text=self.cleaned_data["text"],
)
class ExistingNoteItemForm(ItemForm):
def __init__(self, for_note, *args, **kwargs):
super().__init__(*args, **kwargs)
self._for_note = for_note
def clean_text(self):
text = self.cleaned_data["text"]
if self._for_note.item_set.filter(text=text).exists():
raise forms.ValidationError(DUPLICATE_ITEM_ERROR)
2026-01-23 22:39:12 -05:00
return text
2026-01-23 22:39:12 -05:00
def save(self):
return super().save(for_note=self._for_note)