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