Last active
January 2, 2016 16:19
-
-
Save thoslin/8329004 to your computer and use it in GitHub Desktop.
Use celery to refactor a time consuming view.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def export(request, **kwargs): | |
qs = get_queryset(**kwargs) | |
response = HttpResponse(mimetype='text/csv') | |
response['Content-Disposition'] = 'attachment; filename=foo.csv' | |
writer = csv.writer(response, dialect=csv.excel) | |
for i in qs: | |
writer.writerow([i.foo, i.bar, i.baz, ...]) | |
return response |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<title></title> | |
</head> | |
<body> | |
<div id="content">Please wait. <span id="loading"></span></div> | |
<script type="text/javascript" src="/path/to/jquery.js"></script> | |
<script> | |
$(function(){ | |
$.ajaxSetup({ cache: false, timeout: 360000 }); | |
var url = "/poll_for_download/"; | |
var i = 0; | |
(function worker() { | |
$.getJSON(url+"?task_id={{ task_id }}", function(data){ | |
if(data.filename) { | |
var file_url = url+"?filename="+data.filename; | |
$("#content").html("If your download doesn't start automatically, please click <a href='"+file_url+"'>here</a>."); | |
window.location.href = file_url; | |
} else { | |
setTimeout(worker, 5000); | |
} | |
}); | |
})(); | |
setInterval(function() { | |
i = ++i % 4; | |
$("#loading").html("loading"+Array(i+1).join(".")); | |
}, 1000); | |
}); | |
</script> | |
</body> | |
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from tasks import export_to_file | |
def export(request, **kwargs): | |
task = export_to_file.delay(**kwargs) | |
return render_to_response("poll_for_download.html", | |
{"task_id": task.task_id } | |
def poll_for_download(request): | |
task_id = request.GET.get("task_id") | |
filename = request.GET.get("filename") | |
if request.is_ajax(): | |
result = export_to_file.AsyncResult(task_id) | |
if result.ready(): | |
return HttpResponse(json.dumps({"filename": result.get()})) | |
return HttpResponse(json.dumps({"filename": None})) | |
try: | |
f = open("/path/to/export/"+filename) | |
except: | |
return HttpResponseForbidden() | |
else: | |
response = HttpResponse(file, mimetype='text/csv') | |
response['Content-Disposition'] = 'attachment; filename=%s' % filename | |
return response |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@task | |
def export_to_file(**kwargs): | |
filename = "%s.csv" % export_to_file.request.id | |
qs = get_queryset(**kwargs) | |
with open("%s%s" % ("/path/to/export/", filename), "w+") as f: | |
writer = csv.writer(f, dialect=csv.excel) | |
for i in qs: | |
writer.writerow([i.foo, i.bar, i.baz, ...]) | |
return filename |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment