Advertisement
Guest User

Untitled

a guest
Nov 21st, 2014
777
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. from django.conf import settings
  2.  
  3. def serve_file(path, filename):
  4.     with open(path, "rb") as excel:
  5.         data = excel.read()
  6.     response = HttpResponse(data,content_type='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet')
  7.     response['Content-Disposition'] = 'attachment; filename=%s' % filename
  8.     return response
  9.    
  10. def excel_report(request, id):
  11.     # we assume that settings.VAR_PATH is set to some ABSOLUTE path
  12.     # to a readable / writable directory outside the code base    
  13.     folder = os.path.join(settings.VAR_PATH, 'excel', 'reports')
  14.     filename = '%s_Report.xlsx' % id
  15.     path = os.path.join(folder, filename)
  16.     if not os.path.exists(path):
  17.         # XXX
  18.         # whatever jar_wrapper do, it should do it in a known absolute path - one should
  19.     # NEVER rely on current working directory - AND it should be outside the
  20.         # code base.
  21.         # here we assume that one can pass the desired path to jar_wrapper.
  22.         tmp = os.path.join(settings.VAR_PATH, 'excel', 'tmp', filename)
  23.         args = ['ServerExcel.jar', id, tmp]
  24.         result = jar_wrapper(*args)
  25.         if not result:
  26.             return HttpResponse(json.dumps({"no":"excel","no one": "cry"}))
  27.         shutil.move(tmp, folder)
  28.        
  29.     return serve_file(path, filename)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement