Exemplo n.º 1
0
async def add_contacts(ref: str, data: List[Contact], db: DBClient=client):
    """Add one or more contacts to company"""
    logging.info(f">>> {__name__}:{add_contacts.__name__}")
    updated = await crud.add_contacts(db, ref, data)
    if updated:
        return utils.create_aliased_response(
            {"response": updated, "count": len(updated)}
        )
    return utils.create_aliased_response({"response": [], "count": 0})
Exemplo n.º 2
0
async def read_evidences(limit: int = 50,
                         skip: int = 0,
                         db: DBClient = client):
    logging.info(f">>> {__name__}:{read_evidences.__name__}")
    evidences = await crud.get_multi(db, limit, skip)
    return utils.create_aliased_response(
        ManyGPQEvidencesResponse(response=evidences, count=len(evidences)))
Exemplo n.º 3
0
async def read_company(ref: str, db: DBClient=client):
    """Read company info"""
    logging.info(f">>> {__name__}:{read_company.__name__}")
    company = await crud.get(db, ref)
    if company:
        return utils.create_aliased_response(CompanyResponse(response=company))
    return utils.create_404_response()
Exemplo n.º 4
0
async def read_user(username: str, db: DBClient = client):
    """Read user by id, username, or email"""
    logging.info(">>> " + __name__ + ":" + read_user.__name__)
    user = await crud.get(db, username)
    if user:
        return utils.create_aliased_response(UserResponse(response=user))
    return utils.create_404_response()
Exemplo n.º 5
0
async def read_persona(ref: str, db: DBClient = client):
    """Read persona info"""
    logging.info(f">>> {__name__}:{read_persona.__name__}")
    persona = await crud.get(db, ref)
    if persona:
        return utils.create_aliased_response(PersonaResponse(response=persona))
    return utils.create_404_response()
Exemplo n.º 6
0
async def read_personas(limit: int = 50, skip: int = 0, db: DBClient = client):
    """Read personas"""
    logging.info(f">>> {__name__}:{read_personas.__name__}")
    personas = await crud.get_multi(db, limit, skip)
    # personas = await crud.get_multi_filtered(db, {"prj_id": ObjectId("5e870e7d4c789fb7d8301909")}, limit, skip)
    return utils.create_aliased_response(
        ManyPersonasResponse(response=personas, count=len(personas)))
Exemplo n.º 7
0
async def remove_contact(ref: str, contact: dict, db: DBClient=client):
    """
    Example dict: `{"email": "*****@*****.**"}`
    """
    logging.info(f">>> {__name__}:{remove_contact.__name__}")
    removed = await crud.remove_contact(db, ref, contact)
    # print(removed)
    return utils.create_aliased_response({"response": removed})
Exemplo n.º 8
0
async def update_company(ref: str, data: CompanyUpdate, db: DBClient=client):
    """Update company info"""
    logging.info(f">>> {__name__}:{update_company.__name__}")
    found = await crud.get(db, ref)
    if not found:
        return utils.create_404_response()
    company = await crud.update(db, found["_id"], data)
    if company:
        return utils.create_aliased_response(CompanyResponse(response=company))
    return utils.create_422_response()
Exemplo n.º 9
0
async def read_by_project(id: str,
                          limit: int = 50,
                          skip: int = 0,
                          db: DBClient = client):
    logging.info(f">>> {__name__}:{read_by_project.__name__}")
    if not ObjectId.is_valid(id):
        return utils.create_422_response("Invalid ObjectId")
    evidences = await crud.get_by_project(db, id, limit, skip)
    return utils.create_aliased_response(
        ManyGPQEvidencesResponse(response=evidences, count=len(evidences)))
Exemplo n.º 10
0
async def read_project_personas(prj_id: str,
                                limit: int = 50,
                                skip: int = 0,
                                db: DBClient = client):
    """Read project personas"""
    logging.info(f">>> {__name__}:{read_project_personas.__name__}")
    prj_id = ObjectId(prj_id)
    personas = await crud.get_multi_filtered(db, {"prj_id": prj_id}, limit,
                                             skip)
    return utils.create_aliased_response(
        ManyPersonasResponse(response=personas, count=len(personas)))
Exemplo n.º 11
0
async def update_user(ref: str, data: UserUpdate, db: DBClient = client):
    """Update user info"""
    logging.info(">>> " + __name__ + ":" + update_user.__name__)
    user = await crud.get(db, ref)
    if not user:
        return utils.create_404_response()

    user = await crud.update(db, user["_id"], data)
    if user:
        return utils.create_aliased_response(UserResponse(response=user))
    return utils.create_404_response()
Exemplo n.º 12
0
async def update_persona(ref: str, data: PersonaUpdate, db: DBClient = client):
    """Update persona info"""
    logging.info(f">>> {__name__}:{update_persona.__name__}")
    persona = await crud.get(db, ref)
    if not persona:
        return utils.create_404_response()

    updated = await crud.update(db, persona["_id"], data)
    if updated:
        return utils.create_aliased_response(PersonaResponse(response=updated))
    return utils.create_422_response()
Exemplo n.º 13
0
async def create_company(
    data: CompanyCreate,
    db: DBClient=client,
    current_user: UserInDB=Depends(get_current_user)):
    """Create new company"""
    logging.info(f">>> {__name__}:{create_company.__name__}")
    context = current_user['context']
    data.created_by = context
    # print(data)
    company = await crud.create(db, data)
    if company:
        return utils.create_aliased_response(CompanyResponse(response=company))
    return utils.create_500_response("Company creation failed")
Exemplo n.º 14
0
async def create_license(data: UserCreate, db: DBClient = client):
    """Required fields: `fullname`, `username`, `email`, `password`."""
    logging.info(">>> " + __name__ + ":" + create_license.__name__)
    data = data.dict()
    data['type'] = config.USERTYPE_LICENSE
    data = UserCreate(**data)

    msg = await crud.check_free_email_username(db, data.username, data.email)
    if msg:
        return utils.error_400_response(msg)

    user = await crud.create(db, data)
    if user:
        return utils.create_aliased_response(UserResponse(response=user))

    return utils.create_500_response("User creation failed")
Exemplo n.º 15
0
async def create_persona(prj_id: str,
                         data: PersonaCreate,
                         db: DBClient = client):
    """Create new persona"""
    logging.info(f">>> {__name__}:{create_persona.__name__}")
    project = await get_project(db, prj_id)
    if not project:
        return utils.create_422_response("Could not find the referred project")

    hashed_pwd = get_password_hash(data.password)
    _dict = data.dict()
    _dict["prj_id"] = prj_id
    _dict["hashed_password"] = hashed_pwd
    logging.info(">>> ================")
    model = PersonaInDB(**_dict)
    logging.info(">>> ================")
    persona = await crud.create(db, model)
    if persona:
        return utils.create_aliased_response(PersonaResponse(response=persona))
    return utils.create_500_response("Persona creation failed")
Exemplo n.º 16
0
async def update_user_me(
        *,
        password: str = Body(None),
        fullname: str = Body(None),
        email: EmailStr = Body(None),
        current_user: UserInDB = Depends(get_current_active_user),
        db: DBClient = client):
    """
    Update own user
    """
    # user_in = UserUpdate(**current_user.dict())
    user_in = UserUpdate(**current_user)
    if password is not None:
        user_in.password = password
    if fullname is not None:
        user_in.fullname = fullname
    if user_in.email is not None:
        user_in.email = email
    user = await crud.update(db, current_user["_id"], user_in)
    if user:
        return utils.create_aliased_response(UserResponse(response=user))
    return utils.create_404_response()
Exemplo n.º 17
0
async def create(prj_id: str = Body(...),
                 username: str = Body(...),
                 rows: int = Body(GPQ_TOTAL_ITEMS),
                 db: DBClient = client):

    logging.info(f">>> {__name__}:{create.__name__}")
    collection = get_collection(db, DOCTYPE_PERSONA)
    persona = await get_by_dict(collection, {
        "prj_id": ObjectId(prj_id),
        "username": username
    })
    if not persona:
        return utils.create_422_response(
            "Coul not verify project and/or persona")

    if not rows or rows == 0:
        rows = GPQ_TOTAL_ITEMS

    fullname = persona["fullname"]
    rs = await crud.create_template(db, prj_id, username, fullname, rows)
    if not rs:
        return utils.create_500_response("GPQ template creation failed")
    return utils.create_aliased_response(GPQEvidenceResponse(response=rs))