-
Notifications
You must be signed in to change notification settings - Fork 19
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Form support lacks file upload #67
Comments
Maybe this is usable: |
@gsxdsm Basically, in the form_upload branch, Tetra creates a As far as I found out, like stated above, I think that it should go this way.
|
i pushed what I have in my local form_support branch. |
Got it thanks for the detailed breakdown. Starting to dig in there! |
@gsxdsm Oh, and in case you didn't see it: I started a small example project to test the file upload: |
Excellent thank you! |
@gsxdsm Did you make any progress? I have time tomorrow to help out here/code - if you tell me what you are doing ATM, I could drop in and help a bit? |
Not yet, have been a bit swamped - no notable progress. I have been looking at Laravel Livewire to see how they approach this, but nothing concrete. |
Still looking into this, not making much progress but will keep you updated this weekend |
Have been thinking about the cleanup of the temporary files - don't want to take another dependency here, but I'm thinking we need some sort of job/task. Did you have thoughts on the mechanism for the cleanup? |
Maybe kicking off something asynchronously from runserver? |
Okay, a quick test of creating an async job spawned from runserver to run at a period interval to do the cleanup seems to work. With this in place, I'll work on the rest of the upload process. |
You mean a regular job? Because |
Sounds good, will take that approach and focus on getting the rest working. |
please tell me if I can assist. the form_support branch has already some code in it towards that aim, but not very much. If you add a PR, please do against that branch. |
Yes will do - I am working off of the form_support branch and the form test project, thanks! It is fairly complex, will keep you posted if I get stuck further. |
As @waqasidrees07 got stuck in a very early stage, please feel free to contact me at any stage, so you don't make the same errors again as I did ;-) |
Quick update: I have the change event being fired in Javascript and testing the file upload using the FormData (rather than a form tag). Took a while to get my head around the overall flow, but thanks for the pointers - I see a path now to implementing. At least for single files! (Multi will need to come later) |
Getting closer! I have the javascript posting the file to a server method for the temporary upload on change! Debugging a bit, but making progress. |
Here's where I am:
The hardest part was getting the interplay between the server method and javascript working, but that's all done. The rest should be pretty straightforward. One question - do we want:
I'm inclined to do #3, but want to get your POV. Also, I'm thinking the TTL for the temp files should probably be a default of maybe 15 minutes? But should be overridable in settings? Finally - I was going to make this work for multiple files, but I now realize django doesn't really support multiple files in a single FormField, right? |
That's great news, really. I think that a GC procedure should be available definitely as management command, on the long term it could be a subcommand of "tetra", like I think I would really leave it up to the user. There may be use cases where the upload-auto-GC makes sense, in some (more critical) environments this would be a no-go... So it would be absolutely ok if you just implement the GC procedure (e.g. in tetra.utils), I (or you?) can make a makagement command later too. |
I'm thinking through the step of moving the file from temporary storage to its permanent location and I'm guessing we'd want to use the FileField.upload_to property, right? (https://docs.djangoproject.com/en/5.1/ref/models/fields/#django.db.models.FileField.upload_to). If this is the case, I'd assume we'd also use the storage method on the File (or Image) field as well, right? We'd probably want to use the storage method on the file (or default) for the temporary file storage too. Or did you have something more customizable/user controllable in mind? |
Okay, have the file moving and deletion of temp file working now. Tomorrow I will cleanup and submit something in the branch to take a look - we can iterate on that while I work on validation and the garbage collection. |
…Still needs error checking, validation, and cleanup of files.
Actually I just put together a quick commit now to show what's done so far so you can provide feedback @nerdoc - would love feedback/suggestions. I need to do a lot more testing and refinement, but the basic approach is here |
That's great, exactly how I thought it should be. I just hat a few minutes to look over the code, did not test it - but AFAICT this is definitely the way to go. Thanks, really. That was a few months of agony that comes to an end now ;-) |
2 Things came into my mind while reading the code:
Just to be kept in mind. |
Yes - I can merge them, I started with them merged and debated splitting it up or not, but will merge back. Yes, _uploadFile is definitely something that needs a bit of scrutiny. The csrf check helps a little, but I am going to take another look at livewire's approach to see if there are additional checks/holes there. One quick thing I was thinking was to have the method become a no-op if the component doesn't have a FileField... |
Yes, that would be some additional security. Maybe you could add just a TODO in the |
Updated with a todo for security checks and restricted the upload endpoint to only components with a file field |
Add start of Form file upload support, addresses #67.
@gsxdsm I've tested the file upload a bit. The temporary upload works great. But if the field is marked as required, the form complains about the upload field not being filled. when the form is validated wrong. |
Ah! I'll fix that. And add the cleanup feature. I also want to make the temporary folder name something the user can override |
Wait for later today. I'll push a commit that fixes some problems and merges in form support and dir-components.
Update: pushed a commit, working still on fixing some edge cases with dir compoents. But the main part is online in main. Breaks a few things, especially libraries are not usable the old way now. Just use packages now.
|
Will take a look tonight |
I identified a few issues that break the workflow, and may not be related to each other, but need to be fixed before upload is working properly. I'll put them into the first post to get an overview. |
Getting closer. instead of handlign the file copying using storage in the submit handler, the saving of the I am experimenting with this, but it should work. |
@gsxdsm could you maybe contact me on Matrix, if you're there? |
Yes I will reach out asap / later today |
I pushed my work so far - but it still does not work as intended. updated the open issues in the first post above. |
I got it working finally. File upload does work as intended. @gsxdsm Thank you very much for helping, I wouldn't have done this without your help and code frame.
There are still a few smaller issues, but the code mostly works. Any polishing more than welcome. |
With 71604c1 the form is cleared after What is not working extremely well: When you add a file to the input field, it is uploaded instantly and saved as temp file, so far, so good. But if the user presses F5 in the browser now (which is not a good idea, I know), the file is lost, stays in the temp folder, but the component looses its handle to it. I don't know why this happens. |
Excellent, this looks great thank you!!!! Will take a look at 71604c1 |
Hello again, a happy new year to everyone! In e.g. Europe, GDPR requires user consent before processing user data. And uploading + saving the file is processing user data. So I think the whole process of uploading with the change event may not be legal in some (most?) cases. The two legal ways of circumventing this problem are:
<input name="some_file" type="file" :disabled="!terms_conditions"> Mind the ":" before disabled (or use x-bind:). So, this must be definitly added to the docs, because Devs MUST know about it, to decide whether to inform the user before uploading, or add a Wizard. |
While
FormComponent
works reasonably well, It completely lacks file upload support. The problem is separated in a few parts:enctype="multipart/form-data"
attribute to allow file uploadscallServerMethod()
andgetStateWithChildren(...)
methods closely resemble the code @waqasidrees07 wrote to upload the file in his PR Waqas/enhancement/form support #70. But it has to be done in tetra.core.js.In the test_tetra_forms repository, there should ideally only be changed some minimal things like an added
<form enctype="multipart/form-data"></form>
tag.UPDATE:
upload partially works, but there are a few issues that keep it from working properly, they must be fixed in order to close this issue:
django.core.files.upladedfile.TemporaryUploadedFile
than using own temp file storage in_upload_temp_file()
. Maybe modify Django's upload handler on the fly in FormComponents to always useTemporaryFileUploadHandler
- and keep track of the uploaded file name?The text was updated successfully, but these errors were encountered: