mirror of
https://github.com/django/django.git
synced 2025-06-05 03:29:12 +00:00
Refs #35326 -- Removed FileSystemStorage.OS_OPEN_FLAGS per deprecation timeline.
This commit is contained in:
parent
bc3f3031d8
commit
17ae61a5d4
@ -1,5 +1,4 @@
|
|||||||
import os
|
import os
|
||||||
import warnings
|
|
||||||
from datetime import datetime, timezone
|
from datetime import datetime, timezone
|
||||||
from urllib.parse import urljoin
|
from urllib.parse import urljoin
|
||||||
|
|
||||||
@ -9,7 +8,6 @@ from django.core.files.move import file_move_safe
|
|||||||
from django.core.signals import setting_changed
|
from django.core.signals import setting_changed
|
||||||
from django.utils._os import safe_join
|
from django.utils._os import safe_join
|
||||||
from django.utils.deconstruct import deconstructible
|
from django.utils.deconstruct import deconstructible
|
||||||
from django.utils.deprecation import RemovedInDjango60Warning
|
|
||||||
from django.utils.encoding import filepath_to_uri
|
from django.utils.encoding import filepath_to_uri
|
||||||
from django.utils.functional import cached_property
|
from django.utils.functional import cached_property
|
||||||
|
|
||||||
@ -23,9 +21,6 @@ class FileSystemStorage(Storage, StorageSettingsMixin):
|
|||||||
Standard filesystem storage
|
Standard filesystem storage
|
||||||
"""
|
"""
|
||||||
|
|
||||||
# RemovedInDjango60Warning: remove OS_OPEN_FLAGS.
|
|
||||||
OS_OPEN_FLAGS = os.O_WRONLY | os.O_CREAT | os.O_EXCL | getattr(os, "O_BINARY", 0)
|
|
||||||
|
|
||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
location=None,
|
location=None,
|
||||||
@ -40,16 +35,6 @@ class FileSystemStorage(Storage, StorageSettingsMixin):
|
|||||||
self._directory_permissions_mode = directory_permissions_mode
|
self._directory_permissions_mode = directory_permissions_mode
|
||||||
self._allow_overwrite = allow_overwrite
|
self._allow_overwrite = allow_overwrite
|
||||||
setting_changed.connect(self._clear_cached_properties)
|
setting_changed.connect(self._clear_cached_properties)
|
||||||
# RemovedInDjango60Warning: remove this warning.
|
|
||||||
if self.OS_OPEN_FLAGS != os.O_WRONLY | os.O_CREAT | os.O_EXCL | getattr(
|
|
||||||
os, "O_BINARY", 0
|
|
||||||
):
|
|
||||||
warnings.warn(
|
|
||||||
"Overriding OS_OPEN_FLAGS is deprecated. Use "
|
|
||||||
"the allow_overwrite parameter instead.",
|
|
||||||
RemovedInDjango60Warning,
|
|
||||||
stacklevel=2,
|
|
||||||
)
|
|
||||||
|
|
||||||
@cached_property
|
@cached_property
|
||||||
def base_location(self):
|
def base_location(self):
|
||||||
@ -127,12 +112,7 @@ class FileSystemStorage(Storage, StorageSettingsMixin):
|
|||||||
| os.O_EXCL
|
| os.O_EXCL
|
||||||
| getattr(os, "O_BINARY", 0)
|
| getattr(os, "O_BINARY", 0)
|
||||||
)
|
)
|
||||||
# RemovedInDjango60Warning: when the deprecation ends, replace with:
|
if self._allow_overwrite:
|
||||||
# if self._allow_overwrite:
|
|
||||||
# open_flags = open_flags & ~os.O_EXCL
|
|
||||||
if self.OS_OPEN_FLAGS != open_flags:
|
|
||||||
open_flags = self.OS_OPEN_FLAGS
|
|
||||||
elif self._allow_overwrite:
|
|
||||||
open_flags = open_flags & ~os.O_EXCL
|
open_flags = open_flags & ~os.O_EXCL
|
||||||
fd = os.open(full_path, open_flags, 0o666)
|
fd = os.open(full_path, open_flags, 0o666)
|
||||||
_file = None
|
_file = None
|
||||||
|
@ -318,3 +318,6 @@ to remove usage of these features.
|
|||||||
* The ``check`` keyword argument of ``CheckConstraint`` is removed.
|
* The ``check`` keyword argument of ``CheckConstraint`` is removed.
|
||||||
|
|
||||||
* The ``get_cache_name()`` method of ``FieldCacheMixin`` is removed.
|
* The ``get_cache_name()`` method of ``FieldCacheMixin`` is removed.
|
||||||
|
|
||||||
|
* The ``OS_OPEN_FLAGS`` attribute of
|
||||||
|
:class:`~django.core.files.storage.FileSystemStorage` is removed.
|
||||||
|
@ -25,18 +25,11 @@ from django.core.files.uploadedfile import (
|
|||||||
)
|
)
|
||||||
from django.db.models import FileField
|
from django.db.models import FileField
|
||||||
from django.db.models.fields.files import FileDescriptor
|
from django.db.models.fields.files import FileDescriptor
|
||||||
from django.test import (
|
from django.test import LiveServerTestCase, SimpleTestCase, TestCase, override_settings
|
||||||
LiveServerTestCase,
|
|
||||||
SimpleTestCase,
|
|
||||||
TestCase,
|
|
||||||
ignore_warnings,
|
|
||||||
override_settings,
|
|
||||||
)
|
|
||||||
from django.test.utils import requires_tz_support
|
from django.test.utils import requires_tz_support
|
||||||
from django.urls import NoReverseMatch, reverse_lazy
|
from django.urls import NoReverseMatch, reverse_lazy
|
||||||
from django.utils import timezone
|
from django.utils import timezone
|
||||||
from django.utils._os import symlinks_supported
|
from django.utils._os import symlinks_supported
|
||||||
from django.utils.deprecation import RemovedInDjango60Warning
|
|
||||||
|
|
||||||
from .models import (
|
from .models import (
|
||||||
Storage,
|
Storage,
|
||||||
@ -609,69 +602,6 @@ class CustomStorageTests(FileStorageTests):
|
|||||||
self.storage.delete(second)
|
self.storage.delete(second)
|
||||||
|
|
||||||
|
|
||||||
# RemovedInDjango60Warning: Remove this class.
|
|
||||||
class OverwritingStorage(FileSystemStorage):
|
|
||||||
"""
|
|
||||||
Overwrite existing files instead of appending a suffix to generate an
|
|
||||||
unused name.
|
|
||||||
"""
|
|
||||||
|
|
||||||
# Mask out O_EXCL so os.open() doesn't raise OSError if the file exists.
|
|
||||||
OS_OPEN_FLAGS = FileSystemStorage.OS_OPEN_FLAGS & ~os.O_EXCL
|
|
||||||
|
|
||||||
def get_available_name(self, name, max_length=None):
|
|
||||||
"""Override the effort to find an used name."""
|
|
||||||
return name
|
|
||||||
|
|
||||||
|
|
||||||
# RemovedInDjango60Warning: Remove this test class.
|
|
||||||
class OverwritingStorageOSOpenFlagsWarningTests(SimpleTestCase):
|
|
||||||
storage_class = OverwritingStorage
|
|
||||||
|
|
||||||
def setUp(self):
|
|
||||||
self.temp_dir = tempfile.mkdtemp()
|
|
||||||
self.addCleanup(shutil.rmtree, self.temp_dir)
|
|
||||||
|
|
||||||
def test_os_open_flags_deprecation_warning(self):
|
|
||||||
msg = "Overriding OS_OPEN_FLAGS is deprecated. Use the allow_overwrite "
|
|
||||||
msg += "parameter instead."
|
|
||||||
with self.assertWarnsMessage(RemovedInDjango60Warning, msg) as ctx:
|
|
||||||
self.storage = self.storage_class(
|
|
||||||
location=self.temp_dir, base_url="/test_media_url/"
|
|
||||||
)
|
|
||||||
self.assertEqual(ctx.filename, __file__)
|
|
||||||
|
|
||||||
|
|
||||||
# RemovedInDjango60Warning: Remove this test class.
|
|
||||||
@ignore_warnings(category=RemovedInDjango60Warning)
|
|
||||||
class OverwritingStorageOSOpenFlagsTests(FileStorageTests):
|
|
||||||
storage_class = OverwritingStorage
|
|
||||||
|
|
||||||
def test_save_overwrite_behavior(self):
|
|
||||||
"""Saving to same file name twice overwrites the first file."""
|
|
||||||
name = "test.file"
|
|
||||||
self.assertFalse(self.storage.exists(name))
|
|
||||||
content_1 = b"content one"
|
|
||||||
content_2 = b"second content"
|
|
||||||
f_1 = ContentFile(content_1)
|
|
||||||
f_2 = ContentFile(content_2)
|
|
||||||
stored_name_1 = self.storage.save(name, f_1)
|
|
||||||
try:
|
|
||||||
self.assertEqual(stored_name_1, name)
|
|
||||||
self.assertTrue(self.storage.exists(name))
|
|
||||||
self.assertTrue(os.path.exists(os.path.join(self.temp_dir, name)))
|
|
||||||
with self.storage.open(name) as fp:
|
|
||||||
self.assertEqual(fp.read(), content_1)
|
|
||||||
stored_name_2 = self.storage.save(name, f_2)
|
|
||||||
self.assertEqual(stored_name_2, name)
|
|
||||||
self.assertTrue(self.storage.exists(name))
|
|
||||||
self.assertTrue(os.path.exists(os.path.join(self.temp_dir, name)))
|
|
||||||
with self.storage.open(name) as fp:
|
|
||||||
self.assertEqual(fp.read(), content_2)
|
|
||||||
finally:
|
|
||||||
self.storage.delete(name)
|
|
||||||
|
|
||||||
|
|
||||||
class OverwritingStorageTests(FileStorageTests):
|
class OverwritingStorageTests(FileStorageTests):
|
||||||
storage_class = FileSystemStorage
|
storage_class = FileSystemStorage
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user