mirror of
https://github.com/django/django.git
synced 2025-03-12 02:12:38 +00:00
Fixed #29689 -- Improved performance of FileSystemStorage.listdir() and FilePathField with os.scandir().
This commit is contained in:
parent
371ece2f06
commit
a0ca4b5694
@ -309,11 +309,11 @@ class FileSystemStorage(Storage):
|
|||||||
def listdir(self, path):
|
def listdir(self, path):
|
||||||
path = self.path(path)
|
path = self.path(path)
|
||||||
directories, files = [], []
|
directories, files = [], []
|
||||||
for entry in os.listdir(path):
|
for entry in os.scandir(path):
|
||||||
if os.path.isdir(os.path.join(path, entry)):
|
if entry.is_dir():
|
||||||
directories.append(entry)
|
directories.append(entry.name)
|
||||||
else:
|
else:
|
||||||
files.append(entry)
|
files.append(entry.name)
|
||||||
return directories, files
|
return directories, files
|
||||||
|
|
||||||
def path(self, name):
|
def path(self, name):
|
||||||
|
@ -5,6 +5,7 @@ Field classes.
|
|||||||
import copy
|
import copy
|
||||||
import datetime
|
import datetime
|
||||||
import math
|
import math
|
||||||
|
import operator
|
||||||
import os
|
import os
|
||||||
import re
|
import re
|
||||||
import uuid
|
import uuid
|
||||||
@ -1104,17 +1105,16 @@ class FilePathField(ChoiceField):
|
|||||||
f = os.path.join(root, f)
|
f = os.path.join(root, f)
|
||||||
self.choices.append((f, f.replace(path, "", 1)))
|
self.choices.append((f, f.replace(path, "", 1)))
|
||||||
else:
|
else:
|
||||||
try:
|
choices = []
|
||||||
for f in sorted(os.listdir(self.path)):
|
for f in os.scandir(self.path):
|
||||||
if f == '__pycache__':
|
if f.name == '__pycache__':
|
||||||
continue
|
continue
|
||||||
full_file = os.path.join(self.path, f)
|
if (((self.allow_files and f.is_file()) or
|
||||||
if (((self.allow_files and os.path.isfile(full_file)) or
|
(self.allow_folders and f.is_dir())) and
|
||||||
(self.allow_folders and os.path.isdir(full_file))) and
|
(self.match is None or self.match_re.search(f.name))):
|
||||||
(self.match is None or self.match_re.search(f))):
|
choices.append((f.path, f.name))
|
||||||
self.choices.append((full_file, f))
|
choices.sort(key=operator.itemgetter(1))
|
||||||
except OSError:
|
self.choices.extend(choices)
|
||||||
pass
|
|
||||||
|
|
||||||
self.widget.choices = self.choices
|
self.widget.choices = self.choices
|
||||||
|
|
||||||
|
@ -39,11 +39,11 @@ class PathNotImplementedStorage(storage.Storage):
|
|||||||
def listdir(self, path):
|
def listdir(self, path):
|
||||||
path = self._path(path)
|
path = self._path(path)
|
||||||
directories, files = [], []
|
directories, files = [], []
|
||||||
for entry in os.listdir(path):
|
for entry in os.scandir(path):
|
||||||
if os.path.isdir(os.path.join(path, entry)):
|
if entry.is_dir():
|
||||||
directories.append(entry)
|
directories.append(entry.name)
|
||||||
else:
|
else:
|
||||||
files.append(entry)
|
files.append(entry.name)
|
||||||
return directories, files
|
return directories, files
|
||||||
|
|
||||||
def delete(self, name):
|
def delete(self, name):
|
||||||
|
Loading…
x
Reference in New Issue
Block a user