Beispiel #1
0
def handle_export_request():
    try:
        with benchmark("handle export request"):
            data = parse_export_request()
            query_helper = QueryHelper(data)
            ids_by_type = query_helper.get_ids()
        with benchmark("Generate CSV array"):
            converter = Converter(ids_by_type=ids_by_type)
            csv_data = converter.to_array()
        with benchmark("Generate CSV string"):
            csv_string = generate_csv_string(csv_data)
        with benchmark("Make response."):
            object_names = "_".join(converter.get_object_names())
            filename = "{}.csv".format(object_names)
            headers = [
                ("Content-Type", "text/csv"),
                ("Content-Disposition",
                 "attachment; filename='{}'".format(filename)),
            ]
            return current_app.make_response((csv_string, 200, headers))
    except BadQueryException as exception:
        raise BadRequest(exception.message)
    except:  # pylint: disable=bare-except
        logger.exception("Export failed")
    raise BadRequest("Export failed due to server error.")
def make_export(objects):
    """Make export"""
    query_helper = QueryHelper(objects)
    ids_by_type = query_helper.get_ids()
    converter = ExportConverter(ids_by_type=ids_by_type)
    csv_data = converter.export_csv_data()
    object_names = "_".join(converter.get_object_names())
    return csv_data, object_names
Beispiel #3
0
def make_export(objects):
  """Make export"""
  query_helper = QueryHelper(objects)
  ids_by_type = query_helper.get_ids()
  converter = ExportConverter(ids_by_type=ids_by_type)
  csv_data = converter.export_csv_data()
  object_names = "_".join(converter.get_object_names())
  return csv_data, object_names
Beispiel #4
0
def make_export(objects):
  """Make export"""
  query_helper = QueryHelper(objects)
  ids_by_type = query_helper.get_ids()
  converter = Converter(ids_by_type=ids_by_type)
  csv_data = converter.to_array()
  object_names = "_".join(converter.get_object_names())
  return generate_csv_string(csv_data), object_names
Beispiel #5
0
def handle_export_request():
    try:
        with benchmark("handle export request"):
            data = parse_export_request()
            objects = data.get("objects")
            export_to = data.get("export_to")
            query_helper = QueryHelper(objects)
            ids_by_type = query_helper.get_ids()
        with benchmark("Generate CSV array"):
            converter = Converter(ids_by_type=ids_by_type)
            csv_data = converter.to_array()
        with benchmark("Generate CSV string"):
            csv_string = generate_csv_string(csv_data)
        with benchmark("Make response."):
            object_names = "_".join(converter.get_object_names())
            filename = "{}.csv".format(object_names)

            if export_to == "gdrive":
                credentials = get_credentials()

                http_auth = credentials.authorize(httplib2.Http())
                drive_service = discovery.build('drive', 'v3', http=http_auth)

                # make export to sheets
                file_metadata = {
                    'name': filename,
                    'mimeType': 'application/vnd.google-apps.spreadsheet'
                }
                media = http.MediaInMemoryUpload(csv_string,
                                                 mimetype='text/csv',
                                                 resumable=True)
                gfile = drive_service.files().create(
                    body=file_metadata,
                    media_body=media,
                    fields='id, name, parents').execute()
                headers = [
                    ('Content-Type', 'application/json'),
                ]
                return current_app.make_response(
                    (json.dumps(gfile), 200, headers))
            if export_to == "csv":
                headers = [
                    ("Content-Type", "text/csv"),
                    ("Content-Disposition",
                     "attachment; filename='{}'".format(filename)),
                ]
                return current_app.make_response((csv_string, 200, headers))
    except BadQueryException as exception:
        raise BadRequest(exception.message)
    except:  # pylint: disable=bare-except
        logger.exception("Export failed")
    raise BadRequest("Export failed due to server error.")
Beispiel #6
0
def handle_export_request():
  try:
    with benchmark("handle export request"):
      data = parse_export_request()
      objects = data.get("objects")
      export_to = data.get("export_to")
      query_helper = QueryHelper(objects)
      ids_by_type = query_helper.get_ids()
    with benchmark("Generate CSV array"):
      converter = Converter(ids_by_type=ids_by_type)
      csv_data = converter.to_array()
    with benchmark("Generate CSV string"):
      csv_string = generate_csv_string(csv_data)
    with benchmark("Make response."):
      object_names = "_".join(converter.get_object_names())
      filename = "{}.csv".format(object_names)

      if export_to == "gdrive":
        credentials = get_credentials()

        http_auth = credentials.authorize(httplib2.Http())
        drive_service = discovery.build('drive', 'v3', http=http_auth)

        # make export to sheets
        file_metadata = {
            'name': filename,
            'mimeType': 'application/vnd.google-apps.spreadsheet'
        }
        media = http.MediaInMemoryUpload(csv_string,
                                         mimetype='text/csv',
                                         resumable=True)
        gfile = drive_service.files().create(
            body=file_metadata,
            media_body=media,
            fields='id, name, parents').execute()
        headers = [('Content-Type', 'application/json'), ]
        return current_app.make_response((json.dumps(gfile), 200, headers))
      if export_to == "csv":
        headers = [
            ("Content-Type", "text/csv"),
            ("Content-Disposition",
             "attachment; filename='{}'".format(filename)),
        ]
        return current_app.make_response((csv_string, 200, headers))
  except BadQueryException as exception:
    raise BadRequest(exception.message)
  except:  # pylint: disable=bare-except
    logger.exception("Export failed")
  raise BadRequest("Export failed due to server error.")
Beispiel #7
0
def handle_export_request():
  """Export request handler"""
  # pylint: disable=too-many-locals
  try:
    with benchmark("handle export request data"):
      data = parse_export_request()
      objects = data.get("objects")
      export_to = data.get("export_to")
      current_time = data.get("current_time")
      query_helper = QueryHelper(objects)
      ids_by_type = query_helper.get_ids()
    with benchmark("Generate CSV array"):
      converter = Converter(ids_by_type=ids_by_type)
      csv_data = converter.to_array()
    with benchmark("Generate CSV string"):
      csv_string = generate_csv_string(csv_data)
    with benchmark("Make response."):
      object_names = "_".join(converter.get_object_names())
      filename = "{}_{}.csv".format(object_names, current_time)
      if export_to == "gdrive":
        gfile = fa.create_gdrive_file(csv_string, filename)
        headers = [('Content-Type', 'application/json'), ]
        return current_app.make_response((json.dumps(gfile), 200, headers))
      if export_to == "csv":
        headers = [
            ("Content-Type", "text/csv"),
            ("Content-Disposition",
             "attachment; filename='{}'".format(filename)),
        ]
        return current_app.make_response((csv_string, 200, headers))
  except BadQueryException as exception:
    raise BadRequest(exception.message)
  except Unauthorized as ex:
    raise Unauthorized("{} Try to reload /export page".format(ex.message))
  except HttpAccessTokenRefreshError:
    del flask.session['credentials']
    raise Unauthorized('Unable to get valid credentials.'
                       ' Try to reload /export page')
  except HttpError as e:
    message = json.loads(e.content).get("error").get("message")
    if e.resp.code == 401:
      raise Unauthorized("{} Try to reload /export page".format(message))
    raise InternalServerError(message)
  except Exception as e:  # pylint: disable=broad-except
    logger.exception("Export failed: %s", e.message)
    raise InternalServerError("Export failed due to internal server error.")