from django import forms from centralisedFileSystem.models import ScreenPlay from django.core.validators import FileExtensionValidator LANG_CHOICES =( ("en", "English"), ("en", "Other"), ) class AuditForm(forms.Form): screenplay_name = forms.CharField(label='Screenplay name', max_length=30,required=True) author_name = forms.CharField(label='Author', max_length=30, required= True) script_file = forms.FileField(required=True,validators=[FileExtensionValidator(allowed_extensions=['pdf','docx','txt','fdx'],message="Only PDF, DOCX, FDX, or TXT files are allowed!")]) language = forms.ChoiceField(label=' Language',choices=LANG_CHOICES,required=True) def clean(self): cleaned_data = super().clean() screenplay_name = cleaned_data.get('screenplay_name') author_name = cleaned_data.get('author_name') language = cleaned_data.get('language') script_file = cleaned_data.get('script_file') if str(author_name).isdigit(): self.add_error('author_name',"Author name can't be digit's only") if not screenplay_name: self.add_error('screenplay_name', "ScreenPlay name is required") if not author_name: self.add_error('author_name', "Author name is required") if not language: self.add_error('language', "Language is required") if not script_file: self.add_error('script_file', "") if ScreenPlay.objects.filter(name=screenplay_name).exists(): self.add_error('screenplay_name', "Screenplay name already exists") return cleaned_data def non_field_errors(self): error_messages = self.errors.get('__all__', self.error_class()) if self.errors.get('screenplay_name') and self.errors.get('author_name') and self.errors.get('script_file') and self.errors.get('language'): error_messages.clear() # Clear non-field errors if all fields have errors return error_messages