40260-vm/core/forms.py
Flatlogic Bot 1a0d620188 AI
2026-06-13 09:48:20 +00:00

50 lines
1.8 KiB
Python

from django import forms
from django.core.validators import URLValidator
from django.core.exceptions import ValidationError
from .models import ThreatScan
class ThreatScanForm(forms.Form):
scan_type = forms.ChoiceField(
choices=ThreatScan.ScanType.choices,
widget=forms.RadioSelect,
initial=ThreatScan.ScanType.URL,
label="What do you want to scan?",
)
content = forms.CharField(
label="URL, email, or message",
max_length=5000,
widget=forms.Textarea(attrs={
"rows": 6,
"placeholder": "Paste a suspicious URL, email, SMS, or chat message. Raw text is analyzed in-memory and not stored.",
}),
)
store_metadata = forms.BooleanField(
required=False,
initial=True,
label="Save privacy-safe metadata for my dashboard",
help_text="Only a short sanitized preview, hash, score, and explanation are stored — not the raw submission.",
)
def clean_content(self):
content = self.cleaned_data["content"].strip()
if len(content) < 6:
raise ValidationError("Please enter enough text to analyze.")
return content
def clean(self):
cleaned = super().clean()
scan_type = cleaned.get("scan_type")
content = cleaned.get("content")
if scan_type == ThreatScan.ScanType.URL and content:
candidate = content.strip()
if not candidate.startswith(("http://", "https://")):
candidate = f"https://{candidate}"
try:
URLValidator()(candidate)
except ValidationError as exc:
raise ValidationError("Enter a valid URL, or switch the scan type to Email / Message.") from exc
cleaned["content"] = candidate
return cleaned