-
Notifications
You must be signed in to change notification settings - Fork 408
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
Add FileBrowseUploadField #236
Comments
Is it possible to get code of FileBrowseUploadField? Is it published anywhere? |
I can't find this in any of the branches. Is this available? |
the code is currently not available. we've been using this field for a while and we'll probably add it with the next release. the ticket is mainly a reminder for myself. |
I was about to add a feature request ticket based on user feedback - "Lots of clicks to upload then select an image for FileBrowseFields' I was going to suggest adding a second js button to FileBrowseField that:
I suspect this might be related? |
yes, it's related ... although I'd try to avoid the upload-page. |
I just tried it and it works great. However - it's rather thrown the baby out with the bath-water. The great thing about FileBrowseField is being able to use existing uploads. The problem with it is that it's laborious for the equally common case of a new upload. FileBrowseUploadField solves the latter but leaves you not much better off than if you were using a normal FileField. I am hoping to have the best of both - i.e. a field with two buttons - browse and upload. |
I did a really dumb and naive implementation - I basically diffed the two fields and combined them into one: https://gist.github.com/andybak/a5d9bcffd06abdeae2e9 Amazingly it seems to work although it's only had cursory testing so far. If you're interested I can put together a proper pull request. |
@andybak you are right – it should contain 2 buttons (upload, browse). a PR is very much appreciated. |
@sehmaschine - slightly tricky as I'm using the no-grappelli fork. There's some slight differences in the template and fields.py that I don't fully understand the reasons for. It also means I can't test against your version very easily.. How about I create a separate repo with standalone replacement field in it. It should be fairly trivial for you to incorporate from that. |
OK: https://github.com/DjangoAdminHackers/django-filebrowser-browse-upload-field I'm slightly hampered by the lack of docs for the new field. My expectation of the correct behaviour might be incorrect. What's 'temp' about the temp_upload_dir and how does it interact with upload_to and directory? If I have the following:
I was expecting the file to end up in 'events' - maybe after form.save() If we are combining browse and upload then I would imagine we need something like this. Allowing browsing of the temp_upload_dir doesn't seem like the correct behaviour. |
Alternatively - maybe temp_upload_dir isn't intended to be 'temp' at all - in which case why not just use upload_to? Speaking of which - unless I've missed something 'upload_to' doesn't seem to be used for anything. And shouldn't https://github.com/sehmaschine/django-filebrowser/blob/master/filebrowser/fields.py#L162 be something like:
|
I've updated https://github.com/DjangoAdminHackers/django-filebrowser-browse-upload-field I went with a simpler solution of not using temp uploads at all. There are drawbacks to this (it will clutter up the upload_to dir with images that are never saved) but it was a heck of a lot simpler. I've also included a FilebrowserSite subclass that makes allows us to update the thumbnail after upload. It was a single line change - I added the following to ret_json:
but I couldn't do this without copy+pasting the entire method. (Actually - I could have parsed the json and reconstructed the response but that was yukky) I figure that you'll want to modify the method directly if you incorporate this in core. If not then I'll rethink that method as it will probably break if you change the original. |
@andybak one note (without checking your code): the temp upload dir is there, because upload_to doesn't need to exist when the file is being uploaded (e.g. if upload_to contains the ID of an object which is being added – that object is saved AFTER the file is uploaded). |
besides, it's much easier to clean up the temp dir. and since we need a versions-dir anyway, why not add a temp dir as well. |
OK. My simpler solution works for my use case (I never have a scenario where upload_to doesn't exist) But using your original upload field - the file never got moved from the temp to the real directory. What was I doing wrong? Where's the code that does this or does it not actually get moved? |
you are right ... we used a signal for moving the file. I think that a scenario where upload_to doesn't exist is pretty common (maybe it's just me, but we have that all the time when using the object ID as the document folder). |
I spent a while trying to decide where I would put this functionality. I think I settled on the pre_save method of the model field before I chose to go with the simpler 'non-temp-dir' approach. I'm generally not a fan of signals except as a way for one app to respond to something in a different app. Using signals inside an app seems worse than taking action in a method. Do you agree and if so - do you have any opinion on which method should be responsible for this? Also - can you post your signal code so I can take a look?
Yeah. I agree. I just don't need myself immediately. |
I can't think of another solution besides using signals. But I'm open for any suggestions. Here's what we currently use with our projects ... Please note that this is a custom solution (e.g., we check for a field name "image" with signals, because we know that this field exists). |
I'm trying to think of a generic solution. Aside from my other doubts about signals - this means that any model that uses FileBrowseUploadField has to write - or at least wire up - a signal handler. I wonder if some 'contribute_to_class' magic could be used?: https://github.com/carljm/django-model-utils/blob/master/model_utils/fields.py#L104 |
contribute_to_class is looking pretty plausible. I should have something working tomorrow. Related question - should upload_to etc accept callables the same as FileField? |
upload_to should definitely accept callables. |
agreed, |
I've actually ended up using contribute_to_class to attach the signals automatically. There wasn't another way I could find to trigger this action after the model was saved (ModelFields only have a pre_save method hook and I had trouble getting access to the instance in other methods) The latest commit seems to work ok: https://github.com/DjangoAdminHackers/django-filebrowser-browse-upload-field I'm going to tackle passing callables to upload_to next but I'm hoping that's fairly simple now we have a model field method than runs post-save. |
Callables work. What doesn't work is the upload when you use inlines and the dynamic 'add another'. To fix this I'll need to move the javascript out of the field template and into form.media as a normal static file - then rewrite it work dynamically and not have Django stuff hardcoded into it. |
though experimental, since we've used it for a while without any bigger issues we should add this field to the filebrowser.
The text was updated successfully, but these errors were encountered: