Skip to content

Commit

Permalink
Merge branch 'a258001251842882_refactoring'
Browse files Browse the repository at this point in the history
  • Loading branch information
vmaksymiv committed Sep 12, 2017
2 parents 5a73ee7 + e2b59e4 commit fa7be70
Showing 1 changed file with 28 additions and 6 deletions.
34 changes: 28 additions & 6 deletions src/openprocurement/api/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,13 +127,18 @@ def raise_operation_error(request, message):
def upload_file(request, blacklisted_fields=DOCUMENT_BLACKLISTED_FIELDS, whitelisted_fields=DOCUMENT_WHITELISTED_FIELDS):
first_document = request.validated['documents'][-1] if 'documents' in request.validated and request.validated['documents'] else None
if 'data' in request.validated and request.validated['data']:
document = check_document(request, request.validated['document'], 'body', {})
document = request.validated['document']
check_document(request, document, 'body')

if first_document:
for attr_name in type(first_document)._fields:
if attr_name in whitelisted_fields:
setattr(document, attr_name, getattr(first_document, attr_name))
elif attr_name not in blacklisted_fields and attr_name not in request.validated['json_data']:
setattr(document, attr_name, getattr(first_document, attr_name))

document_route = request.matched_route.name.replace("collection_", "")
document = update_document_url(request, document, document_route, {})
return document
if request.content_type == 'multipart/form-data':
data = request.validated['file']
Expand Down Expand Up @@ -286,7 +291,7 @@ def set_ownership(item, request):
item.owner_token = generate_id()


def check_document(request, document, document_container, route_kwargs):
def check_document(request, document, document_container):
url = document.url
parsed_url = urlparse(url)
parsed_query = dict(parse_qsl(parsed_url.query))
Expand Down Expand Up @@ -322,14 +327,31 @@ def check_document(request, document, document_container, route_kwargs):
request.errors.add(document_container, 'url', "Document url invalid.")
request.errors.status = 422
raise error_handler(request.errors)


def update_document_url(request, document, document_route, route_kwargs):
key = urlparse(document.url).path.split('/')[-1]
route_kwargs.update({'_route_name': document_route,
'document_id': document.id,
'_query': {'download': key}})
document_path = request.current_route_path(**route_kwargs)
document.url = '/' + '/'.join(document_path.split('/')[3:])
return document


def check_document_batch(request, document, document_container, route_kwargs):
check_document(request, document, document_container)

document_route = request.matched_route.name.replace("collection_", "")
# Following piece of code was written by leits, so no one knows how it works
# and why =)
# To redefine document_route to get appropriate real document route when bid
# is created with documents? I hope so :)
if "Documents" not in document_route:
specified_document_route_end = (document_container.lower().rsplit('documents')[0] + ' documents').lstrip().title()
document_route = ' '.join([document_route[:-1], specified_document_route_end])
route_kwargs.update({'_route_name': document_route, 'document_id': document.id, '_query': {'download': key}})
document_path = request.current_route_path(**route_kwargs)
document.url = '/' + '/'.join(document_path.split('/')[3:])
return document

return update_document_url(request, document, document_route, route_kwargs)


def request_params(request):
Expand Down

0 comments on commit fa7be70

Please sign in to comment.