def import_by_url(self, url): # TODO: replace with chain # http://docs.celeryproject.org/en/latest/userguide/tasks.html#task-synchronous-subtasks parsed_url = urlparse.urlparse(url) file_name = parsed_url.path.split('/')[-1] temp_file_path = os.path.join(get_temp_dir(), file_name) try: # TODO: refactor download_file to take file handle instead # of path download_file(url, temp_file_path) except RuntimeError as exc: error_msg = "Problem downloading ISA-Tab file from: " + url logger.error("%s. %s", error_msg, exc) return { "success": False, "message": error_msg } return { "success": True, "message": "File imported.", "data": { "temp_file_path": temp_file_path } }
def import_by_url(self, url): # TODO: replace with chain # http://docs.celeryproject.org/en/latest/userguide/tasks.html#task-synchronous-subtasks parsed_url = urlparse.urlparse(url) file_name = parsed_url.path.split('/')[-1] temp_file_path = os.path.join(get_temp_dir(), file_name) try: # TODO: refactor download_file to take file handle instead # of path download_file(url, temp_file_path) except DownloadError as e: error_msg = "Problem downloading ISA-Tab file from: " + url logger.error("%s. %s", (error_msg, e)) return { "success": False, "message": error_msg } return { "success": True, "message": "File imported.", "data": { "temp_file_path": temp_file_path } }
def post(self, request, *args, **kwargs): form = ImportISATabFileForm(request.POST, request.FILES) if form.is_valid(): f = form.cleaned_data['isa_tab_file'] url = form.cleaned_data['isa_tab_url'] logger.debug("ISA-Tab URL: %s", url) context = RequestContext(request, {'form': form}) if url: # TODO: replace with chain # http://docs.celeryproject.org/en/latest/userguide/tasks.html#task-synchronous-subtasks u = urlparse(url) file_name = u.path.split('/')[-1] temp_file_path = os.path.join(get_temp_dir(), file_name) try: # TODO: refactor download_file to take file handle instead # of path download_file(url, temp_file_path) except DownloadError as e: logger.error("Problem downloading ISA-Tab file. %s", e) error = "Problem downloading ISA-Tab file from: " + url context = RequestContext(request, {'form': form, 'error': error}) return render_to_response(self.template_name, context_instance=context) else: temp_file_path = os.path.join(get_temp_dir(), f.name) try: handle_uploaded_file(f, temp_file_path) except IOError as e: error_msg = "Error writing ISA-Tab file to disk." error_msg += " IOError: %s, file name: %s, error: %s" logger.error(error_msg, e.errno, e.filename, e.strerror) error = "Error writing ISA-Tab file to disk" context = RequestContext(request, {'form': form, 'error': error}) return render_to_response(self.template_name, context_instance=context) logger.debug("Temp file name: '%s'", temp_file_path) dataset_uuid = (parse_isatab.delay( request.user.username, False, temp_file_path ).get())[0] # TODO: exception handling (OSError) os.unlink(temp_file_path) if dataset_uuid: # TODO: redirect to the list of analysis samples for the given # UUID return HttpResponseRedirect( reverse(self.success_view_name, args=(dataset_uuid,))) else: error = 'Problem parsing ISA-Tab file' context = RequestContext(request, {'form': form, 'error': error}) return render_to_response(self.template_name, context_instance=context) else: # submitted form is not valid context = RequestContext(request, {'form': form}) return render_to_response(self.template_name, context_instance=context)
def get(self, request, *args, **kwargs): # a workaround for automatic ISA archive import after logging in try: url_from_cookie = request.COOKIES[self.isa_tab_cookie_name] except KeyError: logger.info("ISA-Tab URL was not provided") form = ImportISATabFileForm() context = RequestContext(request, {'form': form}) return render_to_response(self.template_name, context_instance=context) form = ImportISATabFileForm({'isa_tab_url': url_from_cookie}) if form.is_valid(): url = form.cleaned_data['isa_tab_url'] else: context = RequestContext(request, {'form': form}) response = render_to_response(self.template_name, context_instance=context) response.delete_cookie(self.isa_tab_cookie_name) return response u = urlparse.urlparse(url) file_name = u.path.split('/')[-1] temp_file_path = os.path.join(get_temp_dir(), file_name) try: # TODO: refactor download_file to take file handle instead of path download_file(url, temp_file_path) except DownloadError as e: logger.error("Problem downloading ISA-Tab file. %s", e) error = "Problem downloading ISA-Tab file from: '{}'".format(url) context = RequestContext(request, {'form': form, 'error': error}) response = render_to_response(self.template_name, context_instance=context) response.delete_cookie(self.isa_tab_cookie_name) return response logger.debug("Temp file name: '%s'", temp_file_path) dataset_uuid = parse_isatab.delay(request.user.username, False, temp_file_path).get()[0] # TODO: exception handling os.unlink(temp_file_path) if dataset_uuid: if 'ajax' in kwargs and kwargs['ajax']: return HttpResponse( json.dumps({'new_data_set_uuid': dataset_uuid}), 'application/json' ) else: response = HttpResponseRedirect( reverse(self.success_view_name, args=(dataset_uuid,))) response.delete_cookie(self.isa_tab_cookie_name) return response else: error = "Problem parsing ISA-Tab file" context = RequestContext(request, {'form': form, 'error': error}) response = render_to_response(self.template_name, context_instance=context) response.delete_cookie(self.isa_tab_cookie_name) return response
def get(self, request, *args, **kwargs): # a workaround for automatic ISA archive import after logging in try: url_from_cookie = request.COOKIES[self.isa_tab_cookie_name] except KeyError: logger.info("ISA-Tab URL was not provided") form = ImportISATabFileForm() context = RequestContext(request, {'form': form}) return render_to_response(self.template_name, context_instance=context) form = ImportISATabFileForm({'isa_tab_url': url_from_cookie}) if form.is_valid(): url = form.cleaned_data['isa_tab_url'] else: context = RequestContext(request, {'form': form}) response = render_to_response(self.template_name, context_instance=context) response.delete_cookie(self.isa_tab_cookie_name) return response u = urlparse.urlparse(url) file_name = u.path.split('/')[-1] temp_file_path = os.path.join(get_temp_dir(), file_name) try: # TODO: refactor download_file to take file handle instead of path download_file(url, temp_file_path) except DownloadError as e: logger.error("Problem downloading ISA-Tab file. %s", e) error = "Problem downloading ISA-Tab file from: '{}'".format(url) context = RequestContext(request, {'form': form, 'error': error}) response = render_to_response(self.template_name, context_instance=context) response.delete_cookie(self.isa_tab_cookie_name) return response logger.debug("Temp file name: '%s'", temp_file_path) dataset_uuid = parse_isatab.delay(request.user.username, False, temp_file_path).get()[0] # TODO: exception handling os.unlink(temp_file_path) if dataset_uuid: if 'ajax' in kwargs and kwargs['ajax']: return HttpResponse( json.dumps({'new_data_set_uuid': dataset_uuid}), 'application/json') else: response = HttpResponseRedirect( reverse(self.success_view_name, args=(dataset_uuid, ))) response.delete_cookie(self.isa_tab_cookie_name) return response else: error = "Problem parsing ISA-Tab file" context = RequestContext(request, {'form': form, 'error': error}) response = render_to_response(self.template_name, context_instance=context) response.delete_cookie(self.isa_tab_cookie_name) return response
def import_by_url(url): # TODO: replace with chain # http://docs.celeryproject.org/en/latest/userguide/tasks.html#task-synchronous-subtasks parsed_url = urlparse.urlparse(url) file_name = parsed_url.path.split('/')[-1] temp_file_path = os.path.join(tempfile.gettempdir(), file_name) try: # TODO: refactor download_file to take file handle instead # of path download_file(url, temp_file_path) except RuntimeError as exc: error_msg = "Problem downloading ISA-Tab file from: " + url logger.error("%s. %s", error_msg, exc) return _error_message(error_msg) return _success_message(temp_file_path)
def get(self, request, *args, **kwargs): # a workaround for automatic ISA archive import after logging in try: url_from_cookie = request.COOKIES[self.isa_tab_cookie_name] except KeyError: logger.info("ISA-Tab URL was not provided") form = ImportISATabFileForm() context = RequestContext(request, {'form': form}) return render_to_response(self.template_name, context_instance=context) form = ImportISATabFileForm({'isa_tab_url': url_from_cookie}) if form.is_valid(): url = form.cleaned_data['isa_tab_url'] else: context = RequestContext(request, {'form': form}) response = render_to_response(self.template_name, context_instance=context) response.delete_cookie(self.isa_tab_cookie_name) return response u = urlparse.urlparse(url) file_name = u.path.split('/')[-1] temp_file_path = os.path.join(get_temp_dir(), file_name) try: # TODO: refactor download_file to take file handle instead of path download_file(url, temp_file_path) except RuntimeError as exc: logger.error("Problem downloading ISA-Tab file. %s", exc) error = "Problem downloading ISA-Tab file from: '{}'".format(url) context = RequestContext(request, {'form': form, 'error': error}) response = render_to_response(self.template_name, context_instance=context) response.delete_cookie(self.isa_tab_cookie_name) return response logger.debug("Temp file name: '%s'", temp_file_path) try: dataset_uuid = parse_isatab( request.user.username, False, temp_file_path ) except ParserException as e: error_message = "{} {}".format( PARSER_ERROR_MESSAGE, e.message ) logger.error(error_message) return HttpResponseBadRequest(error_message) except Exception as e: error_message = "{} {}".format( PARSER_UNEXPECTED_ERROR_MESSAGE, traceback.format_exc(e) ) logger.error(error_message) return HttpResponseBadRequest( PARSER_UNEXPECTED_ERROR_MESSAGE + e.message ) try: os.unlink(temp_file_path) except OSError as exc: logger.error("Couldn't unlink ISATab's `temp_file_path`: %s %s", temp_file_path, exc) if dataset_uuid: if 'ajax' in kwargs and kwargs['ajax']: return JsonResponse({'new_data_set_uuid': dataset_uuid}) else: response = HttpResponseRedirect( reverse(self.success_view_name, args=(dataset_uuid,))) response.delete_cookie(self.isa_tab_cookie_name) return response else: error = "Problem parsing ISA-Tab file" context = RequestContext(request, {'form': form, 'error': error}) response = render_to_response(self.template_name, context_instance=context) response.delete_cookie(self.isa_tab_cookie_name) return response
def get(self, request, *args, **kwargs): # a workaround for automatic ISA archive import after logging in try: url_from_cookie = request.COOKIES[self.isa_tab_cookie_name] except KeyError: logger.info("ISA-Tab URL was not provided") form = ImportISATabFileForm() context = RequestContext(request, {'form': form}) return render_to_response(self.template_name, context_instance=context) form = ImportISATabFileForm({'isa_tab_url': url_from_cookie}) if form.is_valid(): url = form.cleaned_data['isa_tab_url'] else: context = RequestContext(request, {'form': form}) response = render_to_response(self.template_name, context_instance=context) response.delete_cookie(self.isa_tab_cookie_name) return response u = urlparse.urlparse(url) file_name = u.path.split('/')[-1] temp_file_path = os.path.join(tempfile.gettempdir(), file_name) try: # TODO: refactor download_file to take file handle instead of path download_file(url, temp_file_path) except RuntimeError as exc: logger.error("Problem downloading ISA-Tab file. %s", exc) error = "Problem downloading ISA-Tab file from: '{}'".format(url) context = RequestContext(request, {'form': form, 'error': error}) response = render_to_response(self.template_name, context_instance=context) response.delete_cookie(self.isa_tab_cookie_name) return response logger.debug("Temp file name: '%s'", temp_file_path) try: dataset_uuid = parse_isatab( request.user.username, False, temp_file_path ) except ParserException as e: error_message = "{} {}".format( PARSER_ERROR_MESSAGE, e.message ) logger.error(error_message) return HttpResponseBadRequest(error_message) except Exception as e: error_message = "{} {}".format( PARSER_UNEXPECTED_ERROR_MESSAGE, traceback.format_exc(e) ) logger.error(error_message) return HttpResponseBadRequest( PARSER_UNEXPECTED_ERROR_MESSAGE + e.message ) try: os.unlink(temp_file_path) except OSError as exc: logger.error("Couldn't unlink ISATab's `temp_file_path`: %s %s", temp_file_path, exc) if dataset_uuid: if 'ajax' in kwargs and kwargs['ajax']: return JsonResponse({'new_data_set_uuid': dataset_uuid}) else: response = HttpResponseRedirect( reverse(self.success_view_name, args=(dataset_uuid,))) response.delete_cookie(self.isa_tab_cookie_name) return response else: error = "Problem parsing ISA-Tab file" context = RequestContext(request, {'form': form, 'error': error}) response = render_to_response(self.template_name, context_instance=context) response.delete_cookie(self.isa_tab_cookie_name) return response