def export_js_localized_strings(request): """ Export all unlicalized strings in files. """ response = http.HttpResponseNotModified() try: # write to a file file = tempfile.TemporaryFile() # get strings strings = utils.all_unlocalized_js_strings(True) for s in strings: file.write('"%s": "",\n' % s) file.seek(0) filename = time.strftime("strings_%Y%m%d_%H%M%S.js", time.localtime()) response = http.StreamingHttpResponse(utils.file_iterator(file)) response['Content-Type'] = 'application/octet-stream' response['Content-Disposition'] = 'attachment;filename="%s"' % filename except Exception, e: message = "Can't export game data: %s" % e logger.log_tracemsg(message) file.close() return render(request, 'fail.html', {"message": message})
def export_game_data(request): """ Export game world files. """ response = http.HttpResponseNotModified() file_type = request.GET.get("file_type", None) # get data's zip zipfile = None try: zipfile = tempfile.TemporaryFile() exporter.export_zip_all(zipfile, file_type) zipfile.seek(0) filename = time.strftime("worlddata_%Y%m%d_%H%M%S.zip", time.localtime()) response = http.StreamingHttpResponse(utils.file_iterator(zipfile)) response['Content-Type'] = 'application/octet-stream' response['Content-Disposition'] = 'attachment;filename="%s"' % filename except Exception, e: message = "Can't export game data: %s" % e logger.log_tracemsg(message) zipfile.close() return render(request, 'fail.html', {"message": message})
def file_response(file_obj, filename): """ Respond a file. Args: file_obj: (file object) file to send. filename: (string) filename. """ response = StreamingHttpResponse(file_iterator(file_obj)) response['Content-Type'] = 'application/octet-stream' response['Content-Disposition'] = 'attachment;filename="%s"' % filename return response
def export_data_single(request): """ Export a data table. """ response = http.HttpResponseNotModified() model_name = request.GET.get("model_name", None) file_type = request.GET.get("file_type", None) if not file_type: # Default file type. file_type = "csv" writer_class = writers.get_writer(file_type) if not writer_class: return render(request, 'fail.html', {"message": "Can not export this type of file."}) # Get tempfile's name. temp_name = tempfile.mktemp() temp_file = None try: exporter.export_file(temp_name, model_name, file_type) temp_file = open(temp_name, "rb") filename = model_name + "." + writer_class.file_ext response = http.StreamingHttpResponse( utils.file_iterator(temp_file, erase=True)) response['Content-Type'] = 'application/octet-stream' response['Content-Disposition'] = 'attachment;filename="%s"' % filename except Exception, e: message = "Can't export game data: %s" % e logger.log_tracemsg(message) if temp_file: temp_file.close() try: os.remove(temp_name) except Exception, e: pass