Skip to content

Commit

Permalink
Merge pull request #138 from Kinto/137-keep-old-files-on-delete
Browse files Browse the repository at this point in the history
Do not delete attachment when record is deleted if keep_old_files setting is true (#137)
  • Loading branch information
leplatrem authored Jul 5, 2018
2 parents 8e235a3 + a05c58c commit c367189
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 2 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 6 additions & 0 deletions kinto_attachment/listeners.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand Down
18 changes: 18 additions & 0 deletions kinto_attachment/tests/test_views_attachment.py
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,24 @@ 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)

self.assertTrue(self.backend.exists(location))


class HeartbeartTest(BaseWebTestS3, unittest.TestCase):
def test_attachments_is_added_to_heartbeat_view(self):
Expand Down
7 changes: 6 additions & 1 deletion kinto_attachment/views/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,12 @@ 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))

print(keep_old_files)
if not keep_old_files:
utils.delete_attachment(request)

# Remove metadata.
record = {"data": {}}
Expand Down

0 comments on commit c367189

Please sign in to comment.