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

33 lines
963 B
Python
Raw Normal View History

from django import forms
from django.core.exceptions import ValidationError
from .models import Line
DUPLICATE_LINE_ERROR = "You've already logged this to your post"
EMPTY_LINE_ERROR = "You can't have an empty post line"
class LineForm(forms.Form):
text = forms.CharField(
error_messages = {"required": EMPTY_LINE_ERROR},
required=True,
)
def save(self, for_post):
return Line.objects.create(
post=for_post,
text=self.cleaned_data["text"],
)
class ExistingPostLineForm(LineForm):
def __init__(self, for_post, *args, **kwargs):
super().__init__(*args, **kwargs)
self._for_post = for_post
def clean_text(self):
text = self.cleaned_data["text"]
if self._for_post.lines.filter(text=text).exists():
raise forms.ValidationError(DUPLICATE_LINE_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_post=self._for_post)