managing temporarily created files

84 views
Skip to first unread message

alghafli

unread,
Jul 21, 2014, 11:11:35 AM7/21/14
to django...@googlegroups.com
Hello django community,
I am currently developing my first web application. Since I am new to web development, I thought I should seek advice on what I am trying to implement at the moment.
Before I start, here are my questions: can django do this? if not, is there a python library that can do this? if not, am I doing it right?

I want to dynamically create a temporary odt file and upload it to the client. The file is dynamically created based on GET request parameters. It basically searches the database and fills the odt file with information. This file is temporary and should be deleted after the upload finishes.

Here is how I am planning to do it. The view function creates the file and fills it with information. The file is created in a directory which is added to the STATICFILES_DIRS. The view then redirects to the file url and the client downloads the file.
To delete the files, I am planning to create a class that handles temporary file creation and deletion. The class forces an upper limit to temporary files number, say 10. Any file created after 10 files will make the class delete older files based on modification time. I might as well create a thread that would delete files regularly but it would be a bit more complicated and unnecessary for this specific project.

Thank you,
Mohammad

Paul J Stevens

unread,
Jul 21, 2014, 11:50:11 AM7/21/14
to django...@googlegroups.com


On 21-07-14 17:09, alghafli wrote:

> I want to dynamically create a temporary odt file and upload it to the
> client. The file is dynamically created based on GET request parameters.
> It basically searches the database and fills the odt file with
> information. This file is temporary and should be deleted after the
> upload finishes.

tempfile.TemporaryFile is what you seem to want. The file object is
deleted automatically.

fd = tempfile.TemporaryFile()
fd.write(odt_data)
fd.seek(0)
return HttpResponse(
fd.read(),
content_type='application/vnd.oasis.opendocument.text'
)

But then you might as well skip the tempfile feature, and stream
directly from memory:

return HttpResponse(
odt_data,
content_type='application/vnd.oasis.opendocument.text'
)


> Here is how I am planning to do it. The view function creates the file
> and fills it with information. The file is created in a directory which
> is added to the STATICFILES_DIRS. The view then redirects to the file
> url and the client downloads the file.

Since your odt data is for one-time-only usage, you don't need to put
them in any kind of STATIC_FILES directory. That feature is of use to
offload download performance to the web-server, but for one-time-only
downloads there is no point.

> To delete the files, I am planning to create a class that handles
> temporary file creation and deletion. The class forces an upper limit to
> temporary files number, say 10. Any file created after 10 files will
> make the class delete older files based on modification time. I might as
> well create a thread that would delete files regularly but it would be a
> bit more complicated and unnecessary for this specific project.

Done already by the tempfile.TemporaryFile class, and totally redundant
when streaming from memory.


--
________________________________________________________________
Paul J Stevens pjstevns @ gmail, twitter, github, linkedin
www.nfg.nl/in...@nfg.nl/+31.85.877.99.97

alghafli

unread,
Jul 21, 2014, 12:45:24 PM7/21/14
to django...@googlegroups.com
Dear Paul,
Thank you for the fast and useful reply. However, I have two questions.
According to django documentation, HttpResponse accepts string
parameter. Does it also accept binary data. I am using python3 in which
string is a unicode string and binary data are stored in bytes class.
Second, I am planning to use ezodf library to create odt files. It
appears that there is no way I can access data from memory in this
library. do you know of any good python library that can create and
modify odt files?

Thank you
Reply all
Reply to author
Forward
0 new messages