djangodjango-tinymcedjango-filebrowser

Dynamic image upload/browsing path for django-tinymce


I would like to use tinyMCE as the editor for my django application, but have run into some trouble. I have everything setup, but it appears there is no way to specify the upload path for the image insert/upload function. I have two specific scenarios where this becomes a problem:

django-tinymce-filebrowser automatically sets the upload path to mce_filebrowser/%Y/%m/%d. There doesn't appear to be an option to change this path in any way.

django-filebrowser has options for setting the upload directory in settings.py, but I haven't been able to find any record of someone overriding that path for a specific modelform instance. The closest I found was Django filebrowser, model specific directory parameter for FileBrowserField, but I think the solution there isn't applicable to my situation.

Is anyone using another filebrowser for django-tinymce? Did you have a similar problem and find a fix for it? I'd appreciate any points in the right direction.


Solution

  • I used a similar approach but instead of modifying the django-filebrowser code I ended up extending the browse() method in a subclass of FileBrowserSite and making the modification there:

    from django.core.files.storage import DefaultStorage
    from filebrowser.sites import FileBrowserSite
    
    class FileBrowserSite(FileBrowserSite):
        def browse(self, request):
            self.directory = self.directory + str(request.user) + '/'
            return super(FileBrowserSite, self).browse(request)
    
    storage = DefaultStorage()
    site = FileBrowserSite(name='file', storage=storage)
    site.directory = "content/"
    

    I put this piece of code on a file called filebrowser.py and the then on my urls.py I did:

    from .filebrowser import site
    
    urlpatterns = [
        url(r'^admin/content/file/', include(site.urls)),  
    ]
    

    I think is a much cleaner approach than modifying the source code, and is working like charm on my project.