def clean_code(self): data = self.cleaned_data['code'] if self.check_is_empty(data): raise forms.ValidationError("No file submitted.") if not self.check_size(data): raise forms.ValidationError("File size exceeded max size, component can not be uploaded.") self.check_filename(data) # get allowed file types upload_ext = splitext(data.name)[1] t = CodeComponent.objects.filter(id=self.prefix) allowed_list = t[0].allowed.split(",") name_okay = False if not any([data.name.endswith(ext) for ext in allowed_list]): msg = None msg_allowed = "Allowed types are:" for k in CODE_TYPES: if k[0] in allowed_list: msg_allowed = msg_allowed + " " + k[1] + "," if k[0] == upload_ext: msg = "File extension incorrect. File appears to be %s." % (k[1]) if msg is None: msg = "Unable to determine file type (%s)." % upload_ext raise forms.ValidationError(msg + " " +msg_allowed[:-1] + ".") else: return data
def validate_filename(config_filename, filename_type, uploaded_filename): if not config_filename: # no filename to check, so pass. pass elif filename_type == 'INS': if config_filename.lower() != uploaded_filename.lower(): raise forms.ValidationError('File name must be "%s".' % (config_filename, )) elif filename_type == 'MAT': if config_filename != uploaded_filename: raise forms.ValidationError('File name must be "%s".' % (config_filename, )) elif filename_type == 'EXT': if not uploaded_filename.endswith(config_filename): raise forms.ValidationError('File name must have extension "%s".' % (config_filename, )) elif filename_type == 'REX': regex = re.compile(config_filename) if not regex.match(uploaded_filename): raise forms.ValidationError( 'The filename is not in the correct format. It must match the regular expression "%s".' % (config_filename, )) else: raise ValueError("Unexpected filename_type for submission component.")
def clean_code(self): data = self.cleaned_data['code'] if self.check_is_empty(data): raise forms.ValidationError("No file submitted.") if not self.check_size(data): raise forms.ValidationError( "File size exceeded max size, component can not be uploaded." ) validate_filename(self.component.filename, self.component.filename_type, data.name) return data
def clean_filename_type(self): filename_type = self.data['filename_type'] filename = self.data['filename'] if filename_type == 'REX': try: re.compile(filename) except re.error as e: msg = str(e) raise forms.ValidationError( 'Given filename is not a valid regular expression. Error: "%s".' % (msg)) return filename_type
def clean_code(self): data = self.cleaned_data['code'] if self.check_is_empty(data): raise forms.ValidationError("No file submitted.") if not self.check_size(data): raise forms.ValidationError( "File size exceeded max size, component can not be uploaded." ) if not self.component.filename: # no filename to check, so pass. pass elif self.component.filename_type == 'INS': if self.component.filename.lower() != data.name.lower(): raise forms.ValidationError('File name must be "%s".' % (self.component.filename)) elif self.component.filename_type == 'MAT': if self.component.filename != data.name: raise forms.ValidationError('File name must be "%s".' % (self.component.filename)) elif self.component.filename_type == 'EXT': if not data.name.endswith(self.component.filename): raise forms.ValidationError( 'File name must have extension "%s".' % (self.component.filename)) elif self.component.filename_type == 'REX': regex = re.compile(self.component.filename) if not regex.match(data.name): raise forms.ValidationError( 'The filename is not in the correct format. It must match the regular expression "%s".' % (self.component.filename)) else: raise ValueError( "Unexpected filename_type for submission component.") return data
def clean_allowed(self): data = self.data.getlist('allowed') if len(data)==0: raise forms.ValidationError("No file types selected") return ",".join(data)