﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
14063	Validating form file fields is hard	Ole Laursen	nobody	"I have an application where I need to validate a file uploaded through a form. I'd like to do this with the clean_xxx() method for the field. However, I need the file to be present in the file system because the validation is using external libraries and applications.

I don't think that's an unreasonable requirement, but it is not straightforward with the current API. After some digging, I thought I could use the storage classes, something like
{{{
f = self.cleaned_data['myfile']
storage = FileSystemStorage(location=""/tmp"")
path = storage.save(tempfile.mktemp(dir=""/tmp""), f)
try:
    if not validate_file(path):
        raise forms.ValidationError(...)
finally:
    storage.delete(path)
}}}

But this doesn't work with the temporary file backend because it opens and closes the file, and as far as I can tell, there's no straight-forward way of reopening it again. So currently, I'm doing something like this:

{{{
f = self.cleaned_data['myfile']
if hasattr(f, 'temporary_file_path'):
    path = f.temporary_file_path()
    cleanup = False
else:
    import tempfile
    path = tempfile.mktemp(dir=""/tmp"")
    from django.core.files.storage import FileSystemStorage
    storage = FileSystemStorage(location=""/tmp"")
    path = storage.save(path, f)
    cleanup = True
    
try:
    if not validate_file(path):
        raise forms.ValidationError(...)
finally:
    if cleanup:
        storage.delete(path)
}}}
It occurs to me that the API ought to have a more elegant way of doing this?"	New feature	new	File uploads/storage	1.2	Normal				Accepted	0	0	0	0	0	0
