From 2ffd4c98e5f9ec240c4ca217db77400234f5c2fc Mon Sep 17 00:00:00 2001 From: Mathieu Leplatre Date: Wed, 4 Jul 2018 16:31:58 +0200 Subject: [PATCH 1/3] Do not delete attachment when record is deleted if keep_old_files setting is true (#137) --- CHANGELOG.rst | 4 +++- kinto_attachment/tests/test_views_attachment.py | 9 +++++++++ kinto_attachment/views/__init__.py | 6 +++++- 3 files changed, 17 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index b94e38f..efc4ef3 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -4,7 +4,9 @@ Changelog 3.1.0 (unreleased) ------------------ -- Nothing changed yet. +**Bug fix** + +- Do not delete attachment when record is deleted if ``keep_old_files`` setting is true (#137) 3.0.0 (2018-04-10) diff --git a/kinto_attachment/tests/test_views_attachment.py b/kinto_attachment/tests/test_views_attachment.py index 1b6d3bc..ee82d2e 100644 --- a/kinto_attachment/tests/test_views_attachment.py +++ b/kinto_attachment/tests/test_views_attachment.py @@ -475,6 +475,15 @@ def test_files_are_kept_when_attachment_is_replaced(self): self.assertTrue(self.backend.exists(location2)) self.assertTrue(self.backend.exists(location1)) + def test_files_are_kept_when_record_is_deleted(self): + resp = self.upload(status=201) + location = resp.json["location"] + record_uri = resp.headers['Location'] + + self.app.delete(record_uri) + + self.assertTrue(self.backend.exists(location)) + class HeartbeartTest(BaseWebTestS3, unittest.TestCase): def test_attachments_is_added_to_heartbeat_view(self): diff --git a/kinto_attachment/views/__init__.py b/kinto_attachment/views/__init__.py index e22bec6..206cac6 100644 --- a/kinto_attachment/views/__init__.py +++ b/kinto_attachment/views/__init__.py @@ -99,7 +99,11 @@ def post_attachment_view(request, file_field): def delete_attachment_view(request, file_field): - utils.delete_attachment(request) + settings = request.registry.settings + keep_old_files = asbool(settings.get('attachment.keep_old_files', False)) + + if not keep_old_files: + utils.delete_attachment(request) # Remove metadata. record = {"data": {}} From 24043d003bd2123b68c7cff28602d49525f1300e Mon Sep 17 00:00:00 2001 From: Mathieu Leplatre Date: Thu, 5 Jul 2018 00:11:29 +0200 Subject: [PATCH 2/3] Fix tests --- kinto_attachment/tests/test_views_attachment.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/kinto_attachment/tests/test_views_attachment.py b/kinto_attachment/tests/test_views_attachment.py index ee82d2e..dbd75c8 100644 --- a/kinto_attachment/tests/test_views_attachment.py +++ b/kinto_attachment/tests/test_views_attachment.py @@ -478,9 +478,8 @@ def test_files_are_kept_when_attachment_is_replaced(self): def test_files_are_kept_when_record_is_deleted(self): resp = self.upload(status=201) location = resp.json["location"] - record_uri = resp.headers['Location'] - self.app.delete(record_uri) + self.app.delete(self.record_uri, headers=self.headers) self.assertTrue(self.backend.exists(location)) From a05c58cb53194af33e4113ae725e815edb36925a Mon Sep 17 00:00:00 2001 From: Mathieu Leplatre Date: Thu, 5 Jul 2018 01:16:30 +0200 Subject: [PATCH 3/3] Fix listener --- kinto_attachment/listeners.py | 6 ++++++ kinto_attachment/tests/test_views_attachment.py | 10 ++++++++++ kinto_attachment/views/__init__.py | 1 + 3 files changed, 17 insertions(+) diff --git a/kinto_attachment/listeners.py b/kinto_attachment/listeners.py index 9c7c86a..de23951 100644 --- a/kinto_attachment/listeners.py +++ b/kinto_attachment/listeners.py @@ -2,6 +2,7 @@ from kinto.core.errors import http_error from pyramid.events import subscriber from pyramid.exceptions import HTTPBadRequest +from pyramid.settings import asbool from . import utils @@ -15,6 +16,11 @@ def on_delete_record(event): When a bucket or collection is deleted, it removes the attachments of every underlying records. """ + settings = event.request.registry.settings + keep_old_files = asbool(settings.get('attachment.keep_old_files', False)) + if keep_old_files: + return + # Retrieve attachments for these records using links. resource_name = event.payload['resource_name'] filter_field = '%s_uri' % resource_name diff --git a/kinto_attachment/tests/test_views_attachment.py b/kinto_attachment/tests/test_views_attachment.py index dbd75c8..981370f 100644 --- a/kinto_attachment/tests/test_views_attachment.py +++ b/kinto_attachment/tests/test_views_attachment.py @@ -475,9 +475,19 @@ def test_files_are_kept_when_attachment_is_replaced(self): self.assertTrue(self.backend.exists(location2)) self.assertTrue(self.backend.exists(location1)) + def test_files_are_kept_when_attachment_is_deleted(self): + resp = self.upload(status=201) + location = resp.json["location"] + self.assertTrue(self.backend.exists(location)) + + self.app.delete(self.record_uri + "/attachment", headers=self.headers) + + self.assertTrue(self.backend.exists(location)) + def test_files_are_kept_when_record_is_deleted(self): resp = self.upload(status=201) location = resp.json["location"] + self.assertTrue(self.backend.exists(location)) self.app.delete(self.record_uri, headers=self.headers) diff --git a/kinto_attachment/views/__init__.py b/kinto_attachment/views/__init__.py index 206cac6..3625c4e 100644 --- a/kinto_attachment/views/__init__.py +++ b/kinto_attachment/views/__init__.py @@ -102,6 +102,7 @@ def delete_attachment_view(request, file_field): settings = request.registry.settings keep_old_files = asbool(settings.get('attachment.keep_old_files', False)) + print(keep_old_files) if not keep_old_files: utils.delete_attachment(request)