Skip to content

Commit

Permalink
Added processing secondary upload
Browse files Browse the repository at this point in the history
  • Loading branch information
kroman0 committed Jun 30, 2016
1 parent c13fe43 commit af87046
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 6 deletions.
16 changes: 11 additions & 5 deletions openprocurement/documentservice/storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ class NoContent(ValueError):
pass


class ContentUploaded(ValueError):
pass


class StorageRedirect(Exception):
def __init__(self, url):
self.url = url
Expand All @@ -54,13 +58,15 @@ def upload(self, post_file, uuid=None):
filename = get_filename(post_file.filename)
content_type = post_file.type
in_file = post_file.file
if uuid is not None and uuid not in self.storage:
raise KeyNotFound(uuid)
if uuid is None:
if uuid is not None:
if uuid not in self.storage:
raise KeyNotFound(uuid)
if self.storage[uuid]['Content']:
raise ContentUploaded(uuid)
key = self.storage[uuid]
else:
uuid = uuid4().hex
key = self.storage[uuid] = {}
else:
key = self.storage[uuid]
content = in_file.read()
key_md5 = key.get('md5')
md5hash = md5(content).hexdigest()
Expand Down
7 changes: 7 additions & 0 deletions openprocurement/documentservice/tests/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,13 @@ def test_upload_file_post(self):
self.assertEqual(response.content_type, 'application/json')
self.assertIn('http://localhost/get/', response.json['get_url'])

response = self.app.post(upload_url, upload_files=[('file', u'file.txt', 'content')], status=403)
self.assertEqual(response.status, '403 Forbidden')
self.assertEqual(response.content_type, 'application/json')
self.assertEqual(response.json['errors'], [
{u'description': u'Content already uploaded', u'name': u'doc_id', u'location': u'url'}
])

response = self.app.post(upload_url.replace('?', 'a?'), upload_files=[('file', u'file.doc', 'content')], status=403)
self.assertEqual(response.status, '403 Forbidden')
self.assertEqual(response.content_type, 'application/json')
Expand Down
8 changes: 7 additions & 1 deletion openprocurement/documentservice/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from base64 import b64encode, b64decode
from time import time
from urllib import quote, unquote
from openprocurement.documentservice.storage import StorageRedirect, MD5Invalid, KeyNotFound, NoContent
from openprocurement.documentservice.storage import StorageRedirect, MD5Invalid, KeyNotFound, NoContent, ContentUploaded

EXPIRES = 300

Expand Down Expand Up @@ -93,6 +93,12 @@ def upload_file_view(request):
"status": "error",
"errors": [{"location": "url", "name": "doc_id", "description": "Not Found"}]
}
except ContentUploaded:
request.response.status = 403
return {
"status": "error",
"errors": [{"location": "url", "name": "doc_id", "description": "Content already uploaded"}]
}
except MD5Invalid:
request.response.status = 403
return {
Expand Down

0 comments on commit af87046

Please sign in to comment.