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.