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.")
Beispiel #2
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 #3
0
def make_import(csv_data, dry_run):
  """Make import"""
  try:
    converter = Converter(dry_run=dry_run, csv_data=csv_data)
    converter.import_csv()
    return converter.get_info()
  except Exception as e:  # pylint: disable=broad-except
    logger.exception("Import failed: %s", e.message)
    raise BadRequest("Import failed due to server error: %s" % e.message)
Beispiel #4
0
def handle_import_request():
    try:
        dry_run, csv_data = parse_import_request()
        converter = Converter(dry_run=dry_run, csv_data=csv_data)
        converter.import_csv()
        response_data = converter.get_info()
        response_json = json.dumps(response_data)
        headers = [("Content-Type", "application/json")]
        return current_app.make_response((response_json, 200, headers))
    except Exception as exception:
        current_app.logger.exception(exception)
    raise BadRequest("Import failed due to server error.")
Beispiel #5
0
def handle_import_request():
  try:
    dry_run, csv_data = parse_import_request()
    converter = Converter(dry_run=dry_run, csv_data=csv_data)
    converter.import_csv()
    response_data = converter.get_info()
    response_json = json.dumps(response_data)
    headers = [("Content-Type", "application/json")]
    return current_app.make_response((response_json, 200, headers))
  except:  # pylint: disable=bare-except
    logger.exception("Import failed")
  raise BadRequest("Import 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_import_request():
    """Import request handler"""
    dry_run, file_data = parse_import_request()
    csv_data = fa.get_gdrive_file(file_data)
    try:
        converter = Converter(dry_run=dry_run, csv_data=csv_data)
        converter.import_csv()
        response_data = converter.get_info()
        response_json = json.dumps(response_data)
        headers = [("Content-Type", "application/json")]
        return current_app.make_response((response_json, 200, headers))
    except:  # pylint: disable=bare-except
        logger.exception("Import failed")
    raise BadRequest("Import failed due to server error.")
Beispiel #8
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 #9
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.")
Beispiel #10
0
def handle_export_request():
    try:
        data = parse_export_request()
        query_helper = QueryHelper(data)
        converter = Converter(ids_by_type=query_helper.get_ids())
        csv_data = converter.to_array()
        csv_string = generate_csv_string(csv_data)

        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 Exception as exception:
        current_app.logger.exception(exception)
    raise BadRequest("Export failed due to server error.")