def download(request): if not ENABLE_DOWNLOAD.get(): return serve_403_error(request) try: file_format = 'csv' if 'csv' == request.POST.get( 'type') else 'xls' if 'xls' == request.POST.get('type') else 'json' facet = json.loads(request.POST.get('facet', '{}')) json_response = search(request) response = json.loads(json_response.content) if facet: response['response']['docs'] = response['normalized_facets'][0][ 'docs'] collection = facet if not collection['template']['fieldsSelected']: facet['fields'] = facet['template']['fieldsAttributes'] else: collection = json.loads(request.POST.get('collection', '{}')) if file_format == 'json': docs = response['response']['docs'] resp = JsonResponse(docs, safe=False) resp['Content-Disposition'] = 'attachment; filename="%s.%s"' % ( 'query_result', file_format) return resp else: return export_download( response, file_format, collection, user_agent=request.META.get('HTTP_USER_AGENT')) except Exception as e: raise PopupException(_("Could not download search results: %s") % e)
def download(request): try: file_format = 'csv' if 'csv' == request.POST.get( 'type') else 'xls' if 'xls' == request.POST.get('type') else 'json' facet = json.loads(request.POST.get('facet', '{}')) json_response = search(request) response = json.loads(json_response.content) if facet: response['response']['docs'] = response['normalized_facets'][0][ 'docs'] collection = facet else: collection = json.loads(request.POST.get('collection', '{}')) if file_format == 'json': docs = response['response']['docs'] resp = JsonResponse(docs, safe=False) resp['Content-Disposition'] = 'attachment; filename=%s.%s' % ( 'query_result', file_format) return resp else: return export_download(response, file_format, collection) except Exception, e: raise PopupException(_("Could not download search results: %s") % e)