from django import forms from .models import PotholeDetection ALLOWED_IMAGE_EXTENSIONS = ['jpg', 'jpeg', 'png', 'webp'] MAX_IMAGE_SIZE_MB = 20 class PotholeDetectionForm(forms.ModelForm): uploaded_image = forms.ImageField( label='Image de voirie', help_text=f"Formats acceptés : {', '.join(ALLOWED_IMAGE_EXTENSIONS).upper()} — taille max {MAX_IMAGE_SIZE_MB} MB", ) confidence_threshold = forms.FloatField( label='Seuil de confiance', min_value=0.1, max_value=0.9, initial=0.25, widget=forms.NumberInput(attrs={'step': '0.05', 'class': 'form-control'}), help_text='Valeur entre 0.1 et 0.9 (défaut : 0.25)', ) notes = forms.CharField( label='Notes', required=False, widget=forms.Textarea(attrs={'rows': 3, 'class': 'form-control'}), ) class Meta: model = PotholeDetection fields = ['uploaded_image', 'confidence_threshold', 'notes'] def clean_uploaded_image(self): image = self.cleaned_data.get('uploaded_image') if image: ext = image.name.rsplit('.', 1)[-1].lower() if ext not in ALLOWED_IMAGE_EXTENSIONS: raise forms.ValidationError( f"Format non supporté : {ext}. Formats acceptés : {', '.join(ALLOWED_IMAGE_EXTENSIONS)}." ) if image.size > MAX_IMAGE_SIZE_MB * 1024 * 1024: raise forms.ValidationError( f"L'image est trop volumineuse ({image.size / 1024 / 1024:.1f} MB). Taille max : {MAX_IMAGE_SIZE_MB} MB." ) return image