from django import forms from .models import Workshop class SeatsForm(forms.Form): seats = forms.IntegerField(min_value=1, max_value=4, label="Seats") class WorkshopForm(forms.ModelForm): class Meta: model = Workshop fields = ["title", "slug", "venue", "starts_at", "capacity", "remaining"] def clean(self): data = super().clean() if data.get("remaining") is not None and data.get("capacity") is not None: if data["remaining"] > data["capacity"]: raise forms.ValidationError("Remaining seats cannot exceed capacity.") return data class NoteUploadForm(forms.Form): note = forms.FileField(label="Plain-text access note") def clean_note(self): note = self.cleaned_data["note"] if note.size > 2048: raise forms.ValidationError("Use a text note under 2048 bytes.") try: text = note.read().decode("utf-8") except UnicodeDecodeError as error: raise forms.ValidationError("Use UTF-8 plain text.") from error if "\x00" in text or "<" in text: raise forms.ValidationError("Use plain text without markup.") note.seek(0) return note