summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSrinivas Reddy Thatiparthy <thatiparthysreenivas@gmail.com>2017-07-16 11:30:03 +0530
committerTim Graham <timograham@gmail.com>2017-07-29 09:15:46 -0400
commitb306c0c1a311ad42314423bbf70619579a94454b (patch)
tree5523310bc2eb117c161c11161190c49fbae975ee
parent99e7bba443e3ff1a115d930dbb6dfcf9bccb027c (diff)
Fixed #28264 -- Made FilePathField sort files and directories when recursive=True.
-rw-r--r--django/forms/fields.py4
-rw-r--r--tests/forms_tests/field_tests/filepathfield_test_dir/h/__init__.py0
-rw-r--r--tests/forms_tests/field_tests/filepathfield_test_dir/j/__init__.py0
-rw-r--r--tests/forms_tests/field_tests/test_filepathfield.py14
4 files changed, 13 insertions, 5 deletions
diff --git a/django/forms/fields.py b/django/forms/fields.py
index ce4261cbf5..3750e85af1 100644
--- a/django/forms/fields.py
+++ b/django/forms/fields.py
@@ -1083,12 +1083,12 @@ class FilePathField(ChoiceField):
if recursive:
for root, dirs, files in sorted(os.walk(self.path)):
if self.allow_files:
- for f in files:
+ for f in sorted(files):
if self.match is None or self.match_re.search(f):
f = os.path.join(root, f)
self.choices.append((f, f.replace(path, "", 1)))
if self.allow_folders:
- for f in dirs:
+ for f in sorted(dirs):
if f == '__pycache__':
continue
if self.match is None or self.match_re.search(f):
diff --git a/tests/forms_tests/field_tests/filepathfield_test_dir/h/__init__.py b/tests/forms_tests/field_tests/filepathfield_test_dir/h/__init__.py
new file mode 100644
index 0000000000..e69de29bb2
--- /dev/null
+++ b/tests/forms_tests/field_tests/filepathfield_test_dir/h/__init__.py
diff --git a/tests/forms_tests/field_tests/filepathfield_test_dir/j/__init__.py b/tests/forms_tests/field_tests/filepathfield_test_dir/j/__init__.py
new file mode 100644
index 0000000000..e69de29bb2
--- /dev/null
+++ b/tests/forms_tests/field_tests/filepathfield_test_dir/j/__init__.py
diff --git a/tests/forms_tests/field_tests/test_filepathfield.py b/tests/forms_tests/field_tests/test_filepathfield.py
index cc1cca3e36..44f6aff521 100644
--- a/tests/forms_tests/field_tests/test_filepathfield.py
+++ b/tests/forms_tests/field_tests/test_filepathfield.py
@@ -30,6 +30,8 @@ class FilePathFieldTest(SimpleTestCase):
('/filepathfield_test_dir/c/e.py', 'e.py'),
('/filepathfield_test_dir/c/f/__init__.py', '__init__.py'),
('/filepathfield_test_dir/c/f/g.py', 'g.py'),
+ ('/filepathfield_test_dir/h/__init__.py', '__init__.py'),
+ ('/filepathfield_test_dir/j/__init__.py', '__init__.py'),
]
path = os.path.join(PATH, 'filepathfield_test_dir') + '/'
@@ -59,7 +61,6 @@ class FilePathFieldTest(SimpleTestCase):
def test_recursive(self):
f = FilePathField(path=self.path, recursive=True, match=r'^.*?\.py$')
- f.choices.sort()
expected = [
('/filepathfield_test_dir/__init__.py', '__init__.py'),
('/filepathfield_test_dir/a.py', 'a.py'),
@@ -70,14 +71,19 @@ class FilePathFieldTest(SimpleTestCase):
('/filepathfield_test_dir/c/e.py', 'c/e.py'),
('/filepathfield_test_dir/c/f/__init__.py', 'c/f/__init__.py'),
('/filepathfield_test_dir/c/f/g.py', 'c/f/g.py'),
+ ('/filepathfield_test_dir/h/__init__.py', 'h/__init__.py'),
+ ('/filepathfield_test_dir/j/__init__.py', 'j/__init__.py'),
+
]
self.assertChoices(f, expected)
def test_allow_folders(self):
f = FilePathField(path=self.path, allow_folders=True, allow_files=False)
self.assertChoices(f, [
- ('/filepathfield_test_dir/c', 'c')],
- )
+ ('/filepathfield_test_dir/c', 'c'),
+ ('/filepathfield_test_dir/h', 'h'),
+ ('/filepathfield_test_dir/j', 'j'),
+ ])
def test_recursive_no_folders_or_files(self):
f = FilePathField(path=self.path, recursive=True, allow_folders=False, allow_files=False)
@@ -87,5 +93,7 @@ class FilePathFieldTest(SimpleTestCase):
f = FilePathField(path=self.path, recursive=True, allow_folders=True, allow_files=False)
self.assertChoices(f, [
('/filepathfield_test_dir/c', 'c'),
+ ('/filepathfield_test_dir/h', 'h'),
+ ('/filepathfield_test_dir/j', 'j'),
('/filepathfield_test_dir/c/f', 'c/f'),
])