views:

32

answers:

1

In certain situation users can send temporary files to my server. I would like to keep track of those temporary files (since they are used later and I would like to know, when I can remove them - or when they weren't used and can be collected). What kind of model should I use for it? I will send those files using AJAX (and iframe).

EDIT

If I use in the model FileField, how should I handle file upload? Could you show some example snippet, how my function should put file from request.FILES to a FielField.

+1  A: 

How you store the files is independent of whether or not they come via AJAX. Your view will still need to process the multipart form data, and store it in your DB and server filesystem, like any other uploaded file in Django.

As far as the model goes, how about something like this?

class TemporaryFileWrapper(models.Model):
   """
   Holds an arbitrary file and notes when it was last accessed
   """
   uploaded_file = models.FileField(upload_to="/foo/bar/baz/")
   uploading_user = models.ForeignKey(User)
   uploaded = models.DateTimeField(blank=True, null=True, auto_now_add=True)          
   last_accessed = models.DateTimeField(blank=True, null=True, 
                                        auto_now_add=False, auto_now=False)


   def read_file(record_read=True):
      #...some code here to read the uploaded_file
      if record_read:
          self.last_accessed = datetime.datetime.now()
          self.save()

For basic file upload handling see the official documentation, but where the example has the handle_uploaded_file() method, you need some code that creates a TemporaryFileWrapper object, something like this, depending on your needs:

....
form = ProviderSelfEditForm(request.POST, request.FILES) #this is where you bind files and postdata to the form from the HTTP request
if form.is_valid():
     temp_file_wrapper = TemporaryFileWrapper()
     temp_file_wrapper.uploaded_file = 
                       form.cleaned_data['name_of_file_field_in_your_form)
     temp_file_wrapper.uploading_user = request.user #needs an authenticated user
     temp_file_wrapper.save()

     return HttpResponseRedirect('/success/url/')
stevejalim
Could you also suggest me something on the question in the update?
gruszczy
Yeah, that's the point - I don't have the form. I am just getting `UploadedFile` from `request.FILES`. Can I somehow assign file from this to a FileField object?
gruszczy
Make a simple form to bind the data to and then access it the way I've shown OR read the tutorial I've linked (specifically, http://docs.djangoproject.com/en/dev/topics/http/file-uploads/?from=olddocs#handling-uploaded-files) to handle a file that isn't bound to a form instead.
stevejalim
I've ready this tutorial even before asking the question. However, the file there is just saved to some location, instead of assigning it to a `FileField`. Can this be done? The solution with a dummy form will work, but if this can be done simpler (without creating unnecessary form), then I would be glad to know it. Even for the sheer sake of knowing ;-)
gruszczy