Skip to content

Commit

Permalink
Merge branch 'document_service'
Browse files Browse the repository at this point in the history
  • Loading branch information
kroman0 committed Nov 3, 2016
2 parents ca50510 + 3a5569d commit 290ea41
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 4 deletions.
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
}

setup(name='openprocurement.api',
version='2.3.29',
version='2.3.30',
description='openprocurement.api',
long_description=README,
classifiers=[
Expand Down
5 changes: 5 additions & 0 deletions src/openprocurement/api/migration.py
Original file line number Diff line number Diff line change
Expand Up @@ -696,6 +696,10 @@ def __init__(self, registry):
root = Root(request)
for i in results:
doc = i.doc
if 'documents' not in doc and 'awards' not in doc and 'bids' not in doc and 'questions' not in doc and 'complaints' not in doc and 'cancellations' not in doc and 'contracts' not in doc:
continue
if 'documents' in doc and any([i.get('url', '').startswith(registry.docservice_url) for i in doc['documents']]):
continue
model = registry.tender_procurementMethodTypes.get(doc.get('procurementMethodType', 'belowThreshold'))
if model:
try:
Expand All @@ -705,6 +709,7 @@ def __init__(self, registry):
except:
LOGGER.error("Failed migration of tender {} to schema 23.".format(doc.id), extra={'MESSAGE_ID': 'migrate_data_failed', 'TENDER_ID': doc.id})
else:
doc['dateModified'] = get_now().isoformat()
docs.append(doc)
if len(docs) >= 2 ** 7:
result = registry.db.update(docs)
Expand Down
13 changes: 13 additions & 0 deletions src/openprocurement/api/tests/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,19 @@ def request(method, url, **kwargs):
self._srequest = SESSION.request
SESSION.request = request

def setUpBadDS(self):
self.app.app.registry.docservice_url = 'http://localhost'
def request(method, url, **kwargs):
response = Response()
response.status_code = 403
response.encoding = 'application/json'
response._content = '"Unauthorized: upload_view failed permission check"'
response.reason = '403 Forbidden'
return response

self._srequest = SESSION.request
SESSION.request = request

def generate_docservice_url(self):
uuid = uuid4().hex
key = self.app.app.registry.docservice_key
Expand Down
16 changes: 16 additions & 0 deletions src/openprocurement/api/tests/document.py
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,22 @@ def test_patch_tender_document(self):
class TenderDocumentWithDSResourceTest(TenderDocumentResourceTest):
docservice = True

def test_create_tender_document_error(self):
self.tearDownDS()
response = self.app.post('/tenders/{}/documents'.format(self.tender_id),
upload_files=[('file', u'укр.doc', 'content')],
status=422)
self.assertEqual(response.status, '422 Unprocessable Entity')
self.assertEqual(response.content_type, 'application/json')
self.assertEqual(response.json['errors'][0]["description"], "Can't upload document to document service.")
self.setUpBadDS()
response = self.app.post('/tenders/{}/documents'.format(self.tender_id),
upload_files=[('file', u'укр.doc', 'content')],
status=422)
self.assertEqual(response.status, '422 Unprocessable Entity')
self.assertEqual(response.content_type, 'application/json')
self.assertEqual(response.json['errors'][0]["description"], "Can't upload document to document service.")

def test_create_tender_document_json_invalid(self):
response = self.app.post_json('/tenders/{}/documents'.format(self.tender_id),
{'data': {
Expand Down
9 changes: 6 additions & 3 deletions src/openprocurement/api/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,15 +182,18 @@ def upload_file(request, blacklisted_fields=DOCUMENT_BLACKLISTED_FIELDS):
auth=(request.registry.docservice_username, request.registry.docservice_password)
)
json_data = r.json()
except:
in_file.seek(0)
except Exception, e:
LOGGER.warning("Raised exception '{}' on uploading document to document service': {}.".format(type(e), e),
extra=context_unpack(request, {'MESSAGE_ID': 'document_service_exception'}, {'file_size': in_file.tell()}))
else:
if r.status_code == 200 and json_data.get('data', {}).get('url'):
doc_url = json_data['data']['url']
doc_hash = json_data['data']['hash']
break
else:
in_file.seek(0)
LOGGER.warning("Error {} on uploading document to document service '{}': {}".format(r.status_code, url, r.text),
extra=context_unpack(request, {'MESSAGE_ID': 'document_service_error'}, {'ERROR_STATUS': r.status_code, 'file_size': in_file.tell()}))
in_file.seek(0)
index -= 1
else:
request.errors.add('body', 'data', "Can't upload document to document service.")
Expand Down

0 comments on commit 290ea41

Please sign in to comment.