Skip to content

Commit

Permalink
[ftp] Add FTP_ALLOW_OVERWRITE setting (#1424)
Browse files Browse the repository at this point in the history
  • Loading branch information
jschneier authored Jul 6, 2024
1 parent fe759bf commit 855e58d
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ FTP
---

- Conform to ``BaseStorage`` interface (`#1423`_)
- Add ``FTP_ALLOW_OVERWRITE`` setting (`#1424`_)

.. _#1399: https://github.com/jschneier/django-storages/pull/1399
.. _#1381: https://github.com/jschneier/django-storages/pull/1381
Expand All @@ -44,6 +45,7 @@ FTP
.. _#1347: https://github.com/jschneier/django-storages/pull/1347
.. _#1421: https://github.com/jschneier/django-storages/pull/1421
.. _#1423: https://github.com/jschneier/django-storages/pull/1423
.. _#1424: https://github.com/jschneier/django-storages/pull/1424


1.14.3 (2024-05-04)
Expand Down
6 changes: 6 additions & 0 deletions docs/backends/ftp.rst
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@ Settings

Format as a url like ``"{scheme}://{user}:{passwd}@{host}:{port}/"``. Supports both FTP and FTPS connections via scheme.

``allow_overwrite`` or ``FTP_ALLOW_OVERWRITE``

default: ``False``

Set to ``True`` to overwrite files instead of appending additional characters.

``encoding`` or ``FTP_STORAGE_ENCODING``

default: ``latin-1``
Expand Down
4 changes: 4 additions & 0 deletions storages/backends/ftp.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ def get_default_settings(self):
"location": setting("FTP_STORAGE_LOCATION"),
"encoding": setting("FTP_STORAGE_ENCODING", "latin-1"),
"base_url": setting("BASE_URL", settings.MEDIA_URL),
"allow_overwrite": setting("FTP_ALLOW_OVERWRITE", False),
}

def _decode_location(self, location):
Expand Down Expand Up @@ -207,6 +208,9 @@ def delete(self, name):
raise FTPStorageException("Error when removing %s" % name)

def exists(self, name):
if self.allow_overwrite:
return False

self._start_connection()
try:
nlst = self._connection.nlst(os.path.dirname(name) + "/")
Expand Down
6 changes: 6 additions & 0 deletions tests/test_ftp.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,12 @@ def test_exists_error(self, mock_ftp):
with self.assertRaises(ftp.FTPStorageException):
self.storage.exists("foo")

@patch("ftplib.FTP", **{"return_value.nlst.return_value": ["foo", "foo2"]})
def test_exists_overwrite(self, mock_ftp):
with override_settings(FTP_ALLOW_OVERWRITE=True):
storage = ftp.FTPStorage(location=URL)
self.assertFalse(storage.exists("foo"))

@patch(
"ftplib.FTP",
**{
Expand Down

0 comments on commit 855e58d

Please sign in to comment.