from django import forms from django.core.exceptions import ValidationError from .contact_uploads import validate_contact_attachments from .models import ContactSubmission class ContactForm(forms.ModelForm): attachments = forms.Field(required=False) class Meta: model = ContactSubmission fields = ["name", "title", "description", "email"] widgets = { "name": forms.TextInput( attrs={"placeholder": "Your full name", "autocomplete": "name"} ), "title": forms.TextInput(attrs={"placeholder": "Subject / topic"}), "description": forms.Textarea( attrs={"placeholder": "Write your message here…", "rows": 5} ), "email": forms.EmailInput( attrs={ "placeholder": "your@email.com (optional)", "autocomplete": "email", } ), } def __init__(self, *args, file_list=None, **kwargs): self.file_list = file_list super().__init__(*args, **kwargs) def clean(self): cleaned_data = super().clean() try: cleaned_data["attachments"] = validate_contact_attachments(self.file_list) except ValidationError as exc: self.add_error("attachments", exc) cleaned_data["attachments"] = [] return cleaned_data