django admin uploaded file processing - django

Here's what I want to do, and I did not find something similar in my search so far.
In my admin page, I have a Filefield in my model. The rest of the fields are all read only.
I want to be able to upload a file and process it immediately and to extract info from it to assign to these read only fields.
I thought of overriding the clean_(modelfield) method for this FileField and do this parsing and assigning stuff in it. But this is not done right after the file is uploaded, right? I thought this is done when the form/entry is saved.
Next I thought of adding a custom button to this admin form called 'process' which can be clicked after the file is uploaded. This would trigger the assignment of values to the read only fields.
But I am not able to decide on what is the best approach to process the file and display the updated fields in one page without too much of tinkering.
Any thoughts? Thanks

There are two solutions that I can think of with my limited knowledge. Since, by default, the file upload will only start once the request is posted, an alternative way needs to be designed.
1. Upload file via a script and process the file: Use a script (eg: JQuery script) to upload the file and once upload is complete, trigger a script (onComplete event) to render the values into read-only field. This entire process can be associated to your "Process" button or a time-delayed trigger once the FileField is changed.
2 Custom form for file upload: You can detach the file field and other fields (read only fields that you mentioned). If you design a custom form with just the file upload field and once the user submits the request, you can render another form with rendered initial values in the read only fields. That way you need not have any script but you will have to have 2 forms.
Hope this helps. If you find any other solution, do share it :)

Related

How to use uploaded file info to fill in form fields

I am trying to create a file upload form where a user uploads a file and then I do some processing on it. After processing the file, I want to redirect the user to another form that is partially filled in already from the data I gathered from that file.
I've been reading the docs and found something on multipart forms, but don't know if this is what I should be looking into. I've found example code on uploading files and how to build forms (love Django docs!), but I'm not sure how to tie the two together.
A potential problem I've thought of is how to make sure that a user doesn't somehow skip to the other form without uploading a file. The second form should only be accessible after uploading the file and having it verified for the info I need from it.
You probably want to store the parsed information from the file in the session. That way you can check when displaying the form if the data exists in the session, and if not redirect back to the upload form. If it does exist, you can simply use it as the initial data for the form.

Django Admin - Create 'temporary' entry

Is there a way to make it so the Django Admin panel creates a temporary model when you click the "add" button?
I want to be able to 'attach' multiple files / media to a particular model entry which would involve uploading the files at the time of creation. I can't do this until the model has a pk as obviously I can't create a link between the uploaded file and the entry.
I am using the Content-Type framework to create the attachment between my uploaded file (which is wrapped in a class)
I noticed that Wordpress for example creates what is called an 'auto draft' when you click the "new post" button to get around problem.
If I understand correctly, you want to take care that the filename of your uploaded file corresponds with the model's PK where the file-fields are used.
There is nothing you must change in the django-admin, but make some adjustions on your model:
First, make use of "upload_to" in your filefield. I usually set the filename to a uuid4-value to make sure it's unique.
After saving the model, you can rename the file if you want. The best place is in a function that is triggered by a post-save-signal. But if you only want to ensure, that the filename is unique, the filename-generation by uuid should work.

Prompt a confirmation page for a form with FileInput field in Django

I'm developing an admin interface (without a model) for a data restore process. The form on the page allows user to upload a restore file. Now, what I'd like to do is, when the user submit the form, I want to upload the file first, pre-process it, then show a confirmation page to user.
In the pre-process, assuming the form is valid, I unpackage the restore file and extract the backup time from it, then show the backup time (and possibly other information from the restore file) to user in the confirmation page.
My problem is I don't know what the best way is to save the file state. Ideally I'd like to pass the uploaded file to the confirmation page form then when user agrees to continue, I use the restore file to actually restore the system. However, I can't seem to figure out how to do it.
So what I did was to save the file first, then somehow include the filepath in the confirmation page in a hidden field. However, there's a security risk by doing this since the filepath can be modified when user submits the confirmation form.
What's the best way to tackle this problem?
Thanks!
Store the filepath location in the session. You can access the session from your view:
def your_view(request):
...
request.session['file_path'] = 'the file path'
Then in another view get it out:
def your_other_view(request):
...
request.session.get('file_path')
and pass it to your template for use.
You don't have to worry about security, (from the Django docs):
It stores data on the server side and abstracts the sending and receiving of cookies. Cookies contain a session ID – not the data itself.
I tend to solve this problem using a model, that keeps track of a file, and when it was last updated/uploaded. That way in your hidden input you can embed an ID you retrieve later - so if an attacker wants to mess with your form field, all they're going to get is a different backup file (and you could probably write extra validation to handle that).
I'd recommend you create a temporary model to handle your file for you, and then just delete it when you're done processing the file (at the end of the second step).

Django admin action with file upload - process file content and put it in database

I want to add some functionality in admin that allows me to upload file and then process it.
I already have a model that I can edit trough the admin, but I want to be able to "upload" multiple items with this file processing.
How can I achieve this in the admin?
you can use Django Filebrowser. This app can handle multiple uploads with progressbar and have many other things.
The solutions I used is the one described in my comment. Create an empty model (with pass instead of fields), custom admin form and place your logic in the form.

upload field of an invalid form points to nowhere

This may be a question with a very simple answer, however I couldn't come across to one on the internet.
I am writing a Django application. I have a form with an unrequired ImageField. After the user has submitted the form (with an image), if the form is invalid, on the serverside I populate the form with the request data and files (eg: form = FooForm(request.DATA, request.FILES, instance = foo)and send the response back to the client.
On the client the other fields are displayed OK, but the image field doesn't point to anywhere. As the image field is not required, the user usually overlooks the situation and resubmits the form without the image.
I am lost on even whether this is an issue or this is how it is expected to work anyway. If this is an issue please state so that I provide more details.
This is an unavoidable result of the way browser security works. It's not possible for any server-side system to set the initial value of a file upload field, as it does for any other input field. This is to stop malicious sites uploading content from your computer without your permission - otherwise, a site could set the field to default to your password file, for example.
Obviously, this is is a problem if you actually want the file the user uploaded - if the form was initially invalid, it won't have been saved, and the second time through the file will be lost unless the user uploads it again. One possible solution might be to put the file upload on its own form as a second page - once the form has been validated and saved, the second form is displayed which only has the file on it. The Trac bugtracker works like this, by having a checkbox on the main form for 'I have a file to upload', and taking the user to that form if the checkbox is selected.