Can I use only TemporaryFileUploadHandler for a specific FileField?

For the site I just created, I created a custom FileField, AudioFileField. In AudioFileForm now I want to check if the file is an audio file or not. For this, I use sox, commandlinetool, which I invoke through a subprocess. In the forms of the to_python function, I copied the code from ImageFileField:

#Either we have a path or we
# have to create a temporary one.
if hasattr(data, 'temporary_file_path'):
    file = data.temporary_file_path()
else:
    if hasattr(data, 'read'):
        file = StringIO(data.read())
    else:
        file = StringIO(data['content'])
    # save file to temporary_file_path? Where is temporary_file_path?
    # can i get temporary_file_path from settings, defaults?

check = subprocess.Popen([sox,'--i','-t','%s'%self.path], shell=False, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
filetype = check.communicate()[0]
if not filetype:
    raise forms.ValidationError('File is not an audiofile')

Thinking about this, I thought it might be useful to just make TemporaryFileUploadHandler used for AudioFileField. This will save me the trouble of writing my own code to create a temporary file. How can i do this?

+3
source share
1 answer

TemporaryFileUploadhandler . :)

+1

Source: https://habr.com/ru/post/1796545/


All Articles