def query_db(request): """Queries the database based on use input to the Query DB form and returns the raw results. """ form = QueryDBSearchForm(request.POST or None) if not form.is_valid(): template_params = {'querydb_form': form} template_params.update(GLOBAL_TEMPLATE_PARAMS) return render_to_response("search.html", template_params, context_instance=RequestContext(request)) results, count, row_index = _search(form) serialized = _serialize_results(results, count, row_index=row_index) return HttpResponse(json.dumps(serialized))
def download(request, size): """Prepares a DB query for download by creating a CSV and responding with a link to download the CSV from. """ form = QueryDBSearchForm(request.POST or None) if not form.is_valid(): template_params = {'querydb_form': form} template_params.update(GLOBAL_TEMPLATE_PARAMS) return render_to_response("search.html", template_params, context_instance=RequestContext(request)) results, count, row_index = _search(form) if size == 'all': serialized_results = _serialize_results(results, count, csv=True)['results'] elif size == 'page': serialized_results = _serialize_results(results, count, row_index=row_index, csv=True)['results'] data = tablib.Dataset(headers=serialized_results[0].keys()) data.json = json.dumps(serialized_results) filepath, fileid = _get_filepath() with open(os.path.join(settings.DOWNLOAD_DIR, filepath), 'wb') as fp: fp.write(data.csv) return HttpResponse('{"url": "/download_file/%d"}' % fileid)