Skip to content

Instantly share code, notes, and snippets.

@joshcartme
Last active December 11, 2015 05:28
Show Gist options
  • Save joshcartme/4552317 to your computer and use it in GitHub Desktop.
Save joshcartme/4552317 to your computer and use it in GitHub Desktop.
Monkey patch filebrowser_safe's get_path to return the name specified in THEMES for the current host rather than '' thereby restricting that domain to a sub directory of the uploads dir. The specified directory is created if it does not exist.
import os
from copy import deepcopy
from django.conf import settings
from django.core.files.storage import default_storage
from mezzanine.core.request import current_request
from filebrowser_safe import functions
from filebrowser_safe.settings import DIRECTORY
original_get_path = deepcopy(functions.get_path)
def host_theme_restricted_get_path(path):
"""
Monkey patch filebrowser_safe's get_path to return the name specified in
HOST_THEMES for the current host rather than '' thereby restricting
that domain to a sub directory of the uploads dir. The specified
directory is created if it does not exist.
"""
request = current_request()
host = request.get_host().split(":")[0].lower()
try:
dir_name = settings.HOST_THEMES[host]
except KeyError:
pass
else:
if not default_storage.isdir(os.path.join(DIRECTORY, dir_name)):
os.makedirs(os.path.join(settings.MEDIA_ROOT, DIRECTORY, dir_name))
if not path.split('/')[0] == dir_name:
path = '%s/%s' % (dir_name, path)
return original_get_path(path)
functions.get_path = host_theme_restricted_get_path
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment