How to get TinyMCE and FileBrowser to auto-create sub-folders for uploaded media?

46 views
Skip to first unread message

Richard Cooke

unread,
Sep 15, 2016, 10:36:15 AM9/15/16
to Django Grappelli
I'm using:

Django (1.10.1)
django-filebrowser (3.7.2)
django-grappelli (2.8.1)

And the TinyMCE V3 bundled with Grappelli.  I have a project that tracks electronic repairs.  I want to allow the user to attach images, movies, PDFs and DOC files to the repair comments field.

Each repair has its own unique number.

The current - default - behavior of TinyMCE and FileBrowswer will put all uploads into the "uploads" folder.  This will become a huge mess!

I'm looking for a way to sort uploads into the repair folder.  OR maybe it would be "good enough" to pre-pend the repair number to the file name?

The folder would be ideal I think.

Has anybody seen or done something like this?  I'm starting to dig into the technical docs.  A little tricky for TinyMCE V3 - the docs are mostly erased off of the web!


Thanks in advance!

Rich.

Richard Cooke

unread,
Sep 19, 2016, 10:42:29 AM9/19/16
to Django Grappelli
I solved this by adding code to the model's SAVE function to create the desired folder when the user SAVES the record.

import os
from django.conf import settings

   
def save(self, *args, **kwargs):
        job_path
= os.path.join(settings.MEDIA_ROOT, settings.FILEBROWSER_DIRECTORY, self.number)
       
try:
            logger
.debug("Trying to create folder %s", job_path)
            os
.makedirs(job_path)
            logger
.debug('---    Job Folder created!       ---')
       
except OSError:
           
if not os.path.isdir(job_path):
                logger
.debug('---    Job Folder creation error!  ---')
               
raise
           
else:
                logger
.debug('---    Job Folder already exists!  ---')
       
# Call 'real' save method:
       
super(Job, self).save(*args, **kwargs) # Call the "real" save() method.


This builds the target folder name with absolute path on the server.  There is a lot of "discussion" whether its Cricket to use OSError to detect if the folder already exists. If your on the "other" side of the debate, feel free to use an IF statement with isdir() first instead. 

The next part is a change to the start of the custom filebrowse function:

  var cmsURL = '/admin/filebrowser/browse/?pop=2';
 
var mypath = mypath = django.jQuery('#id_number').attr('value');
  console
.log("mypath using #id: " + mypath);
 
if (!mypath) {
    mypath
= django.jQuery('div.grp-readonly')[0].innerHTML;
    console
.log("mypath readonly: " + mypath);
 
}
  cmsURL
= cmsURL + '&type=' + type;
  cmsURL
= cmsURL + '&dir=' + mypath;
  console
.log(cmsURL);


Because my form has a read-only mode for low level users.  The field that holds the folder name may be displayed as an "input" field, or as read-only DIV text.  The first attempt to define mypath works for input fields.  The IF detects if that failed (mypath will be NULL) and tries grabbing it from the HTML text.  I'm not happy with my selector, if the order of fields changes it will likely start grabbing the wrong one.  But it will do for now.  After (hopefully) finding the right folder name, I then append it to the FileBrowser URL as '&dir=folder'.  This is making use of the fact FileBrowser looks for a "dir" value in the calling URL.  If found, it tries to CD to it before it opens the browser.  Very handy.  Not sure its documented....

If FileBrowser can't CD into the folder, it will display a pretty pink error message.  I have trained staff to "fix" that by backing out of the file browser and image inserter dialogs, press SAVE AND CONTINUE - which creates the folder - and try again.

Hope this helps somebody else!

Rich.


Reply all
Reply to author
Forward
0 new messages