[Django]-Dynamic image upload/browsing path for django-tinymce

3πŸ‘

βœ…

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.

πŸ‘€Erasmo

1πŸ‘

I’ve, somewhat hacked this. I’m on grappelli 2.7.2, and django-filebrowser 3.6.1.

I’ve just added a line to the django-filebrowser file: sites.py:
on the method browse() of the class FileBrowserSite, line ~273, I’ve added:

self._directory_set(DIRECTORY + str(request.user) + "/")

It’s important though that there is already a directory created for every user, you can make a post_save signal to create a directory every time a user is created. This solution will not relate the files with the object, but I think that this may be a good start for you and it will isolate files of different users.

For me, this is currently working just fine, I can imagine that this is not the most perfect way to do it, feedback is very welcome.

πŸ‘€OriolJ

1πŸ‘

I’ve extended a little bit the answer by Erasmo. Generally, it works great. Thanks! However, as OriolJ pointed out, every user needs a created directory to use the filebrowser. It is recommended to avoid using signals, so I added the functionality to the custom FileBrowserSite.

filebrowser.py

import os
from django.conf import settings
from django.core.files.storage import DefaultStorage
from filebrowser.sites import FileBrowserSite


class FileBrowserSite(FileBrowserSite):
    def browse(self, request):
        # get directory path from settings to avoid recursion
        self.directory = settings.DIRECTORY + str(request.user) + '/'
        # create a directory for a user if it does not already exist
        full_path = self.storage.location + '/' + self.directory
        if not os.path.exists(full_path):
            os.makedirs(full_path)
        return super().browse(request)


storage = DefaultStorage()


site = FileBrowserSite(name='file', storage=storage)

settings.py

FILEBROWSER_DIRECTORY = 'user_content/'
DIRECTORY = ''

urls.py

from .filebrowser import site

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

Hope this slight update will save someone a couple of minutes.

πŸ‘€bilbohhh

Leave a comment