Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pre-setting FileObject._is_folder for performance #131

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@
*.pyc
*.eggs/
*.egg-info
.idea
mezzanine-git/
9 changes: 6 additions & 3 deletions filebrowser_safe/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@

class FileObjectAPI(object):
""" A mixin class providing file properties. """
def __init__(self, path):
def __init__(self, path, is_folder=None):
self._is_folder = is_folder
self.head = os.path.dirname(path)
self.filename = os.path.basename(path)
self.filename_lower = self.filename.lower()
Expand Down Expand Up @@ -94,6 +95,8 @@ def folder(self):

@cached_property
def is_folder(self):
if self._is_folder is not None:
return self._is_folder
return default_storage.isdir(self.path)

@property
Expand Down Expand Up @@ -121,9 +124,9 @@ class FileObject(FileObjectAPI):

where path is a relative path to a storage location.
"""
def __init__(self, path):
def __init__(self, path, *args, **kwargs):
self.path = path
super(FileObject, self).__init__(path)
super(FileObject, self).__init__(path, *args, **kwargs)

@property
def name(self):
Expand Down
2 changes: 1 addition & 1 deletion filebrowser_safe/locale/cs/LC_MESSAGES/django.po
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ msgstr[1] "%(counter) výsledky"
#: templates/filebrowser/include/toolbar.html:9
#, python-format
msgid "%(full_result_count)s total"
msgstr "%(full_result_count) celkem"
msgstr "%(full_result_count)s celkem"

#: templates/filebrowser/include/search.html:5
msgid "Clear Restrictions"
Expand Down
41 changes: 36 additions & 5 deletions filebrowser_safe/storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
# PYTHON IMPORTS
import os
import shutil
import posixpath

# DJANGO IMPORTS
from django.core.files.move import file_move_safe
Expand Down Expand Up @@ -112,9 +113,13 @@ def makedirs(self, name):

def rmtree(self, name):
name = self._normalize_name(self._clean_name(name))
dirlist = self.listdir(self._encode_name(name))
for item in dirlist:
item.delete()
directories, files = self.listdir(self._encode_name(name))

for key in files:
self.delete('/'.join([name, key]))

for dirname in directories:
self.rmtree('/'.join([name, dirname]))


class GoogleStorageMixin(StorageMixin):
Expand All @@ -133,7 +138,7 @@ def isdir(self, name):
return False

name = self._normalize_name(self._clean_name(name))
dirlist = self.bucket.list(self._encode_name(name))
dirlist = self.listdir(self._encode_name(name))

# Check whether the iterator is empty
for item in dirlist:
Expand Down Expand Up @@ -163,6 +168,32 @@ def makedirs(self, name):

def rmtree(self, name):
name = self._normalize_name(self._clean_name(name))
dirlist = self.bucket.list(self._encode_name(name))
dirlist = self.listdir(self._encode_name(name))
for item in dirlist:
item.delete()

def _clean_name(self, name):
"""
Cleans the name so that Windows style paths work
"""
return clean_name(name)


def clean_name(name):
"""
Cleans the name so that Windows style paths work
"""
# Normalize Windows style paths
clean_name = posixpath.normpath(name).replace('\\', '/')

# os.path.normpath() can strip trailing slashes so we implement
# a workaround here.
if name.endswith('/') and not clean_name.endswith('/'):
# Add a trailing slash as it was stripped.
clean_name = clean_name + '/'

# Given an empty string, os.path.normpath() will return ., which we don't want
if clean_name == '.':
clean_name = ''

return clean_name
2 changes: 1 addition & 1 deletion filebrowser_safe/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ def browse(request):
# CREATE FILEOBJECT
url_path = "/".join([s.strip("/") for s in
[get_directory(), path.replace("\\", "/"), file] if s.strip("/")])
fileobject = FileObject(url_path)
fileobject = FileObject(url_path, is_folder=file in dir_list)


# FILTER / SEARCH
Expand Down