from django import forms from django.core.exceptions import ValidationError from django.utils.translation import gettext_lazy as _ class MessageForm(forms.Form): message = forms.CharField(max_length=80, help_text=_("Up to 80 characters.")) class LanguageForm(forms.Form): language = forms.ChoiceField(choices=(("", _("Choose a language")), ("python", "Python"), ("django", "Django"), ("htmx", "HTMX"))) class FilterForm(forms.Form): q = forms.CharField(label=_("Filter examples"), max_length=40, required=False) status = forms.ChoiceField(required=False, choices=(("", _("All statuses")), ("ready", _("Ready")), ("draft", _("Draft")))) class UploadForm(forms.Form): file = forms.FileField() def clean_file(self): uploaded = self.cleaned_data["file"] if uploaded.size > 256 * 1024: raise ValidationError(_("Choose a file smaller than 256 KB.")) if uploaded.content_type not in {"text/plain", "application/pdf"}: raise ValidationError(_("Choose a plain text file or PDF.")) return uploaded