예제 #1
0
def lambda_handler(event, context):
  formatter = logging.Formatter('[%(levelname)s]\t%(asctime)s.%(msecs)dZ\t%(aws_request_id)s\t[%(module)s#%(funcName)s %(lineno)d]\t%(message)s')

  logger = logging.getLogger()
  for handler in logger.handlers:
    handler.setFormatter(formatter)
    handler.setLevel(logging.DEBUG)

  logger = logging.getLogger(__name__)
  logger.setLevel(logging.DEBUG)

  try:
    tmpl = Template(DYNAMO_HOST, DYNAMO_PORT)
    req = RequestDecorator(event)
  except Exception as e:
    raise e

  user_id = req.get_identity_id()
  username = req.get_username()
  if user_id and username:
    tmpl.set_user_id(user_id)

  path_params = req.get_path_params()
  if "project_id" in path_params:
    project_id = path_params["project_id"]
    tmpl.set_project_id(project_id)
    logger.info("requested project_id: {}".format(project_id))
  
  status_code = 200
  ret = ""

  try:
    if req.get_method() == "GET":
      if "template_id" in path_params:
        ret = tmpl.get(path_params["template_id"])
      else:
        ret = tmpl.list()

    elif req.get_method() == "POST":
      config = req.get_body()
      ret = tmpl.create(
          **config
      )
    elif req.get_method() == "DELETE":
      ret = tmpl.delete(path_params["template_id"])

    logger.info("processing successfully. return value: {}".format(ret))

  except ActionDeniedError as e:
    status_code = 403
    ret = e
    logger.error("permission denied.")
    logger.exception(e)
  except Exception as e:
    status_code = 400
    ret = e
    logger.error("invalid request?? processing failed...")
    logger.exception(e)

  return make_response(status_code=status_code, body=ret)
예제 #2
0
def lambda_handler(event, context):
    formatter = logging.Formatter(
        '[%(levelname)s]\t%(asctime)s.%(msecs)dZ\t%(aws_request_id)s\t[%(module)s#%(funcName)s %(lineno)d]\t%(message)s'
    )

    logger = logging.getLogger()
    for handler in logger.handlers:
        handler.setFormatter(formatter)
        handler.setLevel(logging.DEBUG)

    logger = logging.getLogger(__name__)
    logger.setLevel(logging.DEBUG)

    try:
        work = Work(DYNAMO_HOST, DYNAMO_PORT)
        req = RequestDecorator(event)
    except Exception as e:
        raise e

    user_id = req.get_identity_id()
    username = req.get_username()
    if user_id and username:
        work.set_user_id(user_id)

    path_params = req.get_path_params()
    if "project_id" in path_params:
        project_id = path_params["project_id"]
        work.set_project_id(project_id)
        logger.info("requested project_id: {}".format(project_id))

    if "place_id" in path_params:
        place_id = path_params["place_id"]
        work.set_place_id(place_id)
        logger.info("requested place_id: {}".format(place_id))

    status_code = 200
    ret = ""

    try:
        if req.get_method() == "GET":
            if "place_id" in locals():
                if "children" in req.get_path():
                    # /projects/{project_id}/places/{place_id}/children
                    ret = work.list_children(place_id)
                else:
                    # /projects/{project_id}/places/{place_id}
                    ret = work.show_place(place_id)
            # /projects/{project_id}/places
            else:
                ret = work.list_children(project_id)

        elif req.get_method() == "POST":
            name = req.get_body()["name"]
            # /projects/{project_id}/places/{place_id}
            if "place_id" in locals():
                ret = work.create_place(name, place_id)

            # /projects/{project_id}/places
            else:
                ret = work.create_place(name)

        # /projects/{project_id}/places/{place_id}
        elif req.get_method() == "PUT":
            name = req.get_body()["name"]
            ret = work.update_place(name)

        # /projects/{project_id}/places/{place_id}
        elif req.get_method() == "DELETE":
            ret = work.delete_place(place_id)

        elif req.get_method() == "OPTIONS":
            ret = []
        logger.info("processing successfully. return value: {}".format(ret))

    except ActionDeniedError as e:
        status_code = 403
        ret = e
        logger.error("permission denied.")
        logger.exception(e)
    except Exception as e:
        status_code = 400
        ret = e
        logger.error("invalid request?? processing failed...")
        logger.exception(e)

    return make_response(status_code=status_code, body=ret)
예제 #3
0
def lambda_handler(event, context):
    formatter = logging.Formatter(
        '[%(levelname)s]\t%(asctime)s.%(msecs)dZ\t%(aws_request_id)s\t[%(module)s#%(funcName)s %(lineno)d]\t%(message)s'
    )

    logger = logging.getLogger()
    for handler in logger.handlers:
        handler.setFormatter(formatter)
        handler.setLevel(logging.DEBUG)

    logger = logging.getLogger(__name__)
    logger.setLevel(logging.DEBUG)

    try:
        work = Work(DYNAMO_HOST, DYNAMO_PORT)
        req = RequestDecorator(event)
    except Exception as e:
        raise e

    user_id = req.get_identity_id()
    username = req.get_username()
    if user_id and username:
        work.set_user_id(user_id)

    path_params = req.get_path_params()
    if "project_id" in path_params:
        project_id = path_params["project_id"]
        work.set_project_id(project_id)
        logger.info("requested project_id: {}".format(project_id))

    if "target_id" in path_params:
        target_id = path_params["target_id"]
        work.set_target_id(target_id)
        logger.info("requested target_id: {}".format(target_id))

    if "photo_id" in path_params:
        photo_id = path_params["photo_id"]
        work.set_photo_id(photo_id)
        logger.info("requested photo_id: {}".format(photo_id))

    status_code = 200
    ret = ""

    try:
        if req.get_method() == "GET" and target_id:
            # /projects/{project_id}/targets/{target_id}/photos/{photo_id}
            if "photo_id" in locals():
                ret = work.show_photo(photo_id, encode=True)
            else:
                # /projects/{project_id}/targets/{target_id}/photos
                ret = work.list_photo(target_id)
            if isinstance(ret, str):
                ret = {"data": ret}

        elif req.get_method() == "POST" and target_id:
            type = req.get_body()["type"]
            enc_data = req.get_body()["data"]
            data = b64decode(enc_data)
            # /projects/{project_id}/targets/{target_id}/photos
            ret = work.create_photo(target_id, type, data)

        # /projects/{project_id}/targets/{target_id}/photos/{photo_id}
        elif req.get_method() == "PUT" and target_id:
            type = req.get_body()["type"]
            ret = work.update_photo(target_id, type, photo_id)

        # /projects/{project_id}/targets/{target_id}/photos/{photo_id}
        elif req.get_method() == "DELETE" and photo_id:
            ret = work.delete_photo(target_id, photo_id)

        elif req.get_method() == "OPTIONS":
            ret = []
        logger.info("processing successfully. return value: {}...".format(
            str(ret)[:500]))

    except ActionDeniedError as e:
        status_code = 403
        ret = e
        logger.error("permission denied.")
        logger.exception(e)
    except Exception as e:
        status_code = 400
        ret = e
        logger.error("invalid request?? processing failed...")
        logger.exception(e)

    return make_response(status_code=status_code, body=ret)
예제 #4
0
def lambda_handler(event, context):
    formatter = logging.Formatter(
        '[%(levelname)s]\t%(asctime)s.%(msecs)dZ\t%(aws_request_id)s\t[%(module)s#%(funcName)s %(lineno)d]\t%(message)s'
    )

    logger = logging.getLogger()
    for handler in logger.handlers:
        handler.setFormatter(formatter)
        handler.setLevel(logging.DEBUG)

    logger = logging.getLogger(__name__)
    logger.setLevel(logging.DEBUG)

    try:
        gen = Generate(DYNAMO_HOST, DYNAMO_PORT)
        req = RequestDecorator(event)
    except Exception as e:
        raise e

    user_id = req.get_identity_id()
    username = req.get_username()
    if user_id and username:
        gen.set_user_id(user_id)

    path_params = req.get_path_params()
    if "project_id" in path_params:
        project_id = path_params["project_id"]
        gen.set_project_id(project_id)
        logger.info("requested project_id: {}".format(project_id))

    status_code = 200
    ret = ""

    try:
        if req.get_method() == "GET":
            ret = gen.list()

        elif req.get_method() == "POST":
            # /projects/{project_id}/generate/{type}
            body = req.get_body()
            gen_type = body["type"]
            logger.info("requested generate type: {}".format(gen_type))

            if gen_type == "zip":

                config = make_config(args=body,
                                     project_id=project_id,
                                     config_type="zip")
                ret = gen.gen_zip(project_id=project_id, **config)
                if isinstance(ret, str):
                    ret = {"download_url": ret}

            elif gen_type == "excel":

                config = make_config(args=body,
                                     project_id=project_id,
                                     config_type="excel")
                ret = gen.gen_excel_doc(project_id=project_id, **config)
                if isinstance(ret, str):
                    ret = {"download_url": ret}
        elif req.get_method() == "PUT":
            generated_file_id = path_params["generated_file_id"]
            ret = gen.gen_download_url(generated_file_id)

        logger.info("processing successfully. return value: {}".format(ret))

    except ActionDeniedError as e:
        status_code = 403
        ret = e
        logger.error("permission denied.")
        logger.exception(e)
    except Exception as e:
        status_code = 400
        ret = e
        logger.error("invalid request?? processing failed...")
        logger.exception(e)

    return make_response(status_code=status_code, body=ret)
예제 #5
0
def lambda_handler(event, context):
    formatter = logging.Formatter(
        '[%(levelname)s]\t%(asctime)s.%(msecs)dZ\t%(aws_request_id)s\t[%(module)s#%(funcName)s %(lineno)d]\t%(message)s'
    )

    logger = logging.getLogger()
    for handler in logger.handlers:
        handler.setFormatter(formatter)
        handler.setLevel(logging.DEBUG)

    logger = logging.getLogger(__name__)
    logger.setLevel(logging.DEBUG)

    try:
        corp = Corporation(DYNAMO_HOST, DYNAMO_PORT)
        req = RequestDecorator(event)
    except Exception as e:
        raise e

    corporation_id = req.get_identity_id()
    #username = req.get_username()
    if corporation_id:
        corp.set_corporation_id(corporation_id)

    path_params = req.get_path_params()
    if "user_id" in path_params:
        user_id = path_params["user_id"]

    status_code = 200
    ret = ""
    # /corps
    try:
        if req.get_method() == "GET":
            # /corporation
            if "users" in req.get_path():
                ret = corp.list_users()
            else:
                ret = corp.show()

        elif req.get_method() == "POST":
            body = req.get_body()
            if corporation_id:
                body.update({"corporation_id": corporation_id})
                ret = corp.create_user(**body)

        #elif req.get_method() == "PUT":
        #  name = req.get_body()["name"]
        #  ret = corp.update_corp(name, start_on, complete_on)

        elif req.get_method() == "DELETE":
            if "/corporation/users/" in req.get_path() and "user_id" in locals(
            ):
                ret = corp.delete_user(user_id)

        elif req.get_method() == "OPTIONS":
            ret = []
        logger.info("processing successfully. return value: {}".format(ret))

    except ActionDeniedError as e:
        status_code = 403
        ret = e
        logger.error("permission denied.")
        logger.exception(e)
    except Exception as e:
        status_code = 400
        ret = e
        logger.error("invalid request?? processing failed...")
        logger.exception(e)

    return make_response(status_code=status_code, body=ret)
예제 #6
0
def lambda_handler(event, context):
    formatter = logging.Formatter(
        '[%(levelname)s]\t%(asctime)s.%(msecs)dZ\t%(aws_request_id)s\t[%(module)s#%(funcName)s %(lineno)d]\t%(message)s'
    )

    logger = logging.getLogger()
    for handler in logger.handlers:
        handler.setFormatter(formatter)
        handler.setLevel(logging.DEBUG)

    logger = logging.getLogger(__name__)
    logger.setLevel(logging.DEBUG)

    try:
        print(dir(Notification))
        notification = Notification(DYNAMO_HOST, DYNAMO_PORT)
        req = RequestDecorator(event)
    except Exception as e:
        raise e

    path_params = req.get_path_params()
    user_id = req.get_identity_id()
    username = req.get_username()
    if req.get_method() != "OPTIONS":
        if path_params["user_id"] == user_id:
            notification.set_user_id(user_id)
        else:
            raise ActionDeniedError

    notification_id = None
    if "notification_id" in path_params:
        notification_id = path_params["notification_id"]
        notification.set_notification_id(notification_id)
        logger.info("requested notification_id: {}".format(notification_id))

    status_code = 200
    ret = ""
    try:
        if req.get_method() == "GET":
            # /users/{user_id}/notifications/{notification_id}
            if notification_id:
                ret = notification.show_notification()

            # /users/{user_id}/notifications
            else:
                ret = notification.list_notifications()

        elif req.get_method() == "PUT":
            read = req.get_body()["read"]
            if "notification_ids" in req.get_body():
                notification_ids = req.get_body()["notification_ids"]
                ret = notification.update_notification(
                    read, notification_ids=notification_ids)
            else:
                ret = notification.update_notification(read)

        elif req.get_method() == "DELETE":
            #if "notification_ids" in req.get_body():
            #  notification_ids = req.get_body()["notification_ids"]
            #  ret = notification.delete_notification(notification_ids=notification_ids)
            #else:
            ret = notification.delete_notification()

        elif req.get_method() == "OPTIONS":
            ret = []
        logger.info("processing successfully. return value: {}".format(ret))

    except ActionDeniedError as e:
        status_code = 403
        ret = e
        logger.error("permission denied.")
        logger.exception(e)
    except Exception as e:
        status_code = 400
        ret = e
        logger.error("invalid request?? processing failed...")
        logger.exception(e)

    return make_response(status_code=status_code, body=ret)
예제 #7
0
def lambda_handler(event, context):
    formatter = logging.Formatter(
        '[%(levelname)s]\t%(asctime)s.%(msecs)dZ\t%(aws_request_id)s\t[%(module)s#%(funcName)s %(lineno)d]\t%(message)s'
    )

    logger = logging.getLogger()
    for handler in logger.handlers:
        handler.setFormatter(formatter)
        handler.setLevel(logging.DEBUG)

    logger = logging.getLogger(__name__)
    logger.setLevel(logging.DEBUG)

    try:
        project = ProjectUser(DYNAMO_HOST, DYNAMO_PORT)
        req = RequestDecorator(event)
    except Exception as e:
        raise e

    user_id = req.get_identity_id()
    username = req.get_username()
    if user_id and username:
        project.set_user_id(user_id)
        logger.info("requested user_id: {}".format(user_id))

    path_params = req.get_path_params()
    project_id = None
    if "project_id" in path_params:
        project_id = path_params["project_id"]
        project.set_project_id(project_id)
        logger.info("requested project_id: {}".format(project_id))

    specified_user_id = None
    if "user_id" in path_params:
        specified_user_id = path_params["user_id"]
        logger.info(
            "requested specified user_id: {}".format(specified_user_id))

    status_code = 200
    ret = ""
    # /projects
    try:
        if req.get_method() == "GET":
            # /projects/xxxx/users/yyyy
            if specified_user_id:
                # userの情報取得
                ret = project.show_user(specified_user_id)

            else:
                ret = project.list_users()

        # /projects
        elif req.get_method() == "POST":
            action = req.get_body()["action"]
            if action == "join":
                ret = project.join_user()

        elif req.get_method() == "PUT":
            action = req.get_body()["action"]
            if action == "accept":
                ret = project.accept_user(specified_user_id)

            elif action == "reject":
                ret = project.reject_user(specified_user_id)

            elif action == "update":
                role = req.get_body()["role"]
                ret = project.update_role(specified_user_id, role)

        elif req.get_method() == "DELETE":
            ret = project.delete_user(specified_user_id)

        elif req.get_method() == "OPTIONS":
            ret = []
        logger.info("processing successfully. return value: {}".format(ret))

    except ActionDeniedError as e:
        status_code = 403
        ret = e
        logger.error("permission denied.")
        logger.exception(e)
    except Exception as e:
        status_code = 400
        ret = e
        logger.error("invalid request?? processing failed...")
        logger.exception(e)

    return make_response(status_code=status_code, body=ret)
예제 #8
0
def lambda_handler(event, context):
    formatter = logging.Formatter(
        '[%(levelname)s]\t%(asctime)s.%(msecs)dZ\t%(aws_request_id)s\t[%(module)s#%(funcName)s %(lineno)d]\t%(message)s'
    )

    logger = logging.getLogger()
    for handler in logger.handlers:
        handler.setFormatter(formatter)
        handler.setLevel(logging.DEBUG)

    logger = logging.getLogger(__name__)
    logger.setLevel(logging.DEBUG)

    try:
        project = Project(DYNAMO_HOST, DYNAMO_PORT)
        req = RequestDecorator(event)
    except Exception as e:
        raise e

    user_id = req.get_identity_id()
    username = req.get_username()
    if user_id and username:
        project.set_user_id(user_id)

    path_params = req.get_path_params()
    project_id = None
    if "project_id" in path_params:
        project_id = path_params["project_id"]
        project.set_project_id(project_id)
        logger.info("requested project_id: {}".format(project_id))

    status_code = 200
    ret = ""
    # /projects
    try:
        if req.get_method() == "GET":
            # /projects/xxxx-xxxx-xxxx-xxxx
            if project_id:
                ret = project.show_project()

            else:
                ret = project.list_projects()

        # /projects
        elif req.get_method() == "POST":
            if "import" in req.get_path():
                csv = req.get_body()["csv"]
                ret = project.import_csv(csv, base64enc=True)

            else:
                name = req.get_body()["name"]
                start_on = req.get_body()["start_on"]
                complete_on = req.get_body()["complete_on"]
                ret = project.create_project(name, start_on, complete_on)

        elif req.get_method() == "PUT":
            name = req.get_body()["name"]
            start_on = req.get_body()["start_on"]
            complete_on = req.get_body()["complete_on"]
            ret = project.update_project(name, start_on, complete_on)

        elif req.get_method() == "DELETE":
            ret = project.delete_project()

        elif req.get_method() == "OPTIONS":
            ret = []
        logger.info("processing successfully. return value: {}".format(ret))

    except ActionDeniedError as e:
        status_code = 403
        ret = e
        logger.error("permission denied.")
        logger.exception(e)
    except Exception as e:
        status_code = 400
        ret = e
        logger.error("invalid request?? processing failed...")
        logger.exception(e)

    return make_response(status_code=status_code, body=ret)