From b1821fbad5518c8ae56b7b5c2b1ea2afbbd25c7a Mon Sep 17 00:00:00 2001 From: Timothy McCurrach Date: Wed, 20 Jan 2021 16:12:24 +0000 Subject: [PATCH] Fixed #32360 -- Added system check for FILE_UPLOAD_TEMP_DIR setting. --- AUTHORS | 1 + django/core/checks/__init__.py | 1 + django/core/checks/files.py | 19 +++++++++++++++++ django/core/checks/registry.py | 1 + docs/ref/checks.txt | 15 ++++++++++++++ tests/check_framework/test_files.py | 32 +++++++++++++++++++++++++++++ 6 files changed, 69 insertions(+) create mode 100644 django/core/checks/files.py create mode 100644 tests/check_framework/test_files.py diff --git a/AUTHORS b/AUTHORS index 2c7881bfb3..a92baaceb9 100644 --- a/AUTHORS +++ b/AUTHORS @@ -897,6 +897,7 @@ answer newbie questions, and generally made Django that much better: Tim Givois Tim Graham Tim Heap + Tim McCurrach Tim Saylor Tobias Kunze Tobias McNulty diff --git a/django/core/checks/__init__.py b/django/core/checks/__init__.py index 37b531fe5f..7eb526a41c 100644 --- a/django/core/checks/__init__.py +++ b/django/core/checks/__init__.py @@ -8,6 +8,7 @@ from .registry import Tags, register, run_checks, tag_exists import django.core.checks.async_checks # NOQA isort:skip import django.core.checks.caches # NOQA isort:skip import django.core.checks.database # NOQA isort:skip +import django.core.checks.files # NOQA isort:skip import django.core.checks.model_checks # NOQA isort:skip import django.core.checks.security.base # NOQA isort:skip import django.core.checks.security.csrf # NOQA isort:skip diff --git a/django/core/checks/files.py b/django/core/checks/files.py new file mode 100644 index 0000000000..5f76ae5a17 --- /dev/null +++ b/django/core/checks/files.py @@ -0,0 +1,19 @@ +from pathlib import Path + +from django.conf import settings + +from . import Error, Tags, register + + +@register(Tags.files) +def check_setting_file_upload_temp_dir(app_configs, **kwargs): + setting = getattr(settings, 'FILE_UPLOAD_TEMP_DIR', None) + if setting and not Path(setting).is_dir(): + return [ + Error( + f"The FILE_UPLOAD_TEMP_DIR setting refers to the nonexistent " + f"directory '{setting}'.", + id="files.E001", + ), + ] + return [] diff --git a/django/core/checks/registry.py b/django/core/checks/registry.py index 46a04d78ff..d54538e367 100644 --- a/django/core/checks/registry.py +++ b/django/core/checks/registry.py @@ -13,6 +13,7 @@ class Tags: caches = 'caches' compatibility = 'compatibility' database = 'database' + files = 'files' models = 'models' security = 'security' signals = 'signals' diff --git a/docs/ref/checks.txt b/docs/ref/checks.txt index 1025549370..7761a2a323 100644 --- a/docs/ref/checks.txt +++ b/docs/ref/checks.txt @@ -82,6 +82,7 @@ Django's system checks are organized using the following tags: regular checks do. They are only run by the :djadmin:`migrate` command or if you specify configured database aliases using the ``--database`` option when calling the :djadmin:`check` command. +* ``files``: Checks files related configuration. * ``models``: Checks of model, field, and manager definitions. * ``security``: Checks security related configuration. * ``signals``: Checks on signal declarations and handler registrations. @@ -97,6 +98,10 @@ Some checks may be registered with multiple tags. The ``sites`` tag was added. +.. versionchanged:: 4.0 + + The ``files`` tag was added. + Core system checks ================== @@ -150,6 +155,16 @@ If you're using MySQL or MariaDB, the following checks will be performed: * **mysql.W003**: MySQL/MariaDB may not allow unique ``CharField``\s to have a ``max_length`` > 255. +Managing files +-------------- + +.. versionadded:: 4.0 + +The following checks verify your setup for :doc:`/topics/files`: + +* **files.E001**: The :setting:`FILE_UPLOAD_TEMP_DIR` setting refers to the + nonexistent directory ````. + Model fields ------------ diff --git a/tests/check_framework/test_files.py b/tests/check_framework/test_files.py new file mode 100644 index 0000000000..6b30220e37 --- /dev/null +++ b/tests/check_framework/test_files.py @@ -0,0 +1,32 @@ +from pathlib import Path + +from django.core.checks import Error +from django.core.checks.files import check_setting_file_upload_temp_dir +from django.test import SimpleTestCase + + +class FilesCheckTests(SimpleTestCase): + def test_file_upload_temp_dir(self): + tests = [ + None, + '', + Path.cwd(), + str(Path.cwd()), + ] + for setting in tests: + with self.subTest(setting), self.settings(FILE_UPLOAD_TEMP_DIR=setting): + self.assertEqual(check_setting_file_upload_temp_dir(None), []) + + def test_file_upload_temp_dir_nonexistent(self): + for setting in ['nonexistent', Path('nonexistent')]: + with self.subTest(setting), self.settings(FILE_UPLOAD_TEMP_DIR=setting): + self.assertEqual( + check_setting_file_upload_temp_dir(None), + [ + Error( + "The FILE_UPLOAD_TEMP_DIR setting refers to the " + "nonexistent directory 'nonexistent'.", + id='files.E001', + ), + ], + )