Skip to content

Commit

Permalink
Merge pull request #36 from c2corg/amorvan_delete_filenames
Browse files Browse the repository at this point in the history
Add view delete
  • Loading branch information
arnaud-morvan authored Feb 8, 2017
2 parents 3ca8f1f + e55afbb commit a4cd07f
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 1 deletion.
1 change: 1 addition & 0 deletions c2corg_images/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ def cors_headers(request, response):
config.add_route('ping', '/ping')
config.add_route('upload', '/upload')
config.add_route('publish', '/publish')
config.add_route('delete', '/delete')
config.add_static_view(name='active', path=os.environ['ACTIVE_FOLDER'])
config.add_subscriber(add_cors_headers_response_callback, NewRequest)

Expand Down
2 changes: 2 additions & 0 deletions c2corg_images/storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,8 @@ def put(self, key, path):
shutil.copyfile(path, self.object_path(key))

def delete(self, key):
if not self.exists(key):
return
os.unlink(self.object_path(key))

def copy(self, key, other_storage):
Expand Down
20 changes: 20 additions & 0 deletions c2corg_images/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,3 +105,23 @@ def publish(request):
for key in resized_keys(filename):
incoming_storage.move(key, active_storage)
return {'success': True}


@view_config(route_name='delete', renderer='json')
def delete(request):
if request.POST['secret'] != os.environ['API_SECRET_KEY']:
raise HTTPForbidden('Bad secret key')
filenames = request.POST.getall('filenames')
for filename in filenames:
try:
active_storage.delete(filename)
except Exception:
log.error('Deleting {} failed'.format(filename), exc_info=True)

for key in resized_keys(filename):
try:
active_storage.delete(key)
except Exception:
log.error('Deleting {} failed'.format(key), exc_info=True)

return {'success': True}
3 changes: 3 additions & 0 deletions tests/inside/test_storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ def standard_protocol(self):
self.active_storage.delete(key)
assert not self.active_storage.exists(key)

# delete file that does not exists
self.active_storage.delete('test_not_exists.jpg')

def resizing_protocol(self):
# for resizing object is in active storage
self.active_storage.put(key, source_file)
Expand Down
32 changes: 31 additions & 1 deletion tests/inside/test_views.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import unittest
from unittest.mock import patch
from unittest.mock import patch, call
from webtest import TestApp
from c2corg_images import app

Expand All @@ -23,3 +23,33 @@ def test_publish_good_secret(self, publish_mock):
{'secret': 'good_secret',
'filename': 'test.png'},
status=200)

@patch('c2corg_images.views.active_storage.delete')
def test_delete_svg(self, delete_mock):
"""Test delete with one svg filename"""
self.app.post('/delete',
{'secret': 'good_secret',
'filenames': ['test.svg']},
status=200)
delete_mock.assert_has_calls([
call('test.svg'),
call('testBI.jpg'),
call('testMI.jpg'),
call('testSI.jpg')])

@patch('c2corg_images.views.active_storage.delete')
def test_delete_multi(self, delete_mock):
"""Test delete with multiple filenames"""
self.app.post('/delete',
{'secret': 'good_secret',
'filenames': ['test1.jpg', 'test2.jpg']},
status=200)
delete_mock.assert_has_calls([
call('test1.jpg'),
call('test1BI.jpg'),
call('test1MI.jpg'),
call('test1SI.jpg'),
call('test2.jpg'),
call('test2BI.jpg'),
call('test2MI.jpg'),
call('test2SI.jpg')])

0 comments on commit a4cd07f

Please sign in to comment.