Пример #1
0
def create_list_for_user(user_list: schemas.UserListCreate,
                         db: Session = Depends(get_db),
                         user_id: int = Header(None),
                         token: str = Depends(oauth2_scheme)):
    token_validated = auth.auth_token(token)
    if not token_validated['auth']:
        raise HTTPException(status_code=401, detail="401 Unauthorized")
    user = crud.get_user(db, user_id)
    if not user.email == token_validated['email']:
        raise HTTPException(status_code=status.HTTP_403_FORBIDDEN,
                            detail="403 FORBIDDEN")
    return crud.create_user_list(db, user_list, user_id)
Пример #2
0
def read_lists_from_user(user_id: int = Header(None),
                         skip: int = 0,
                         limit: int = 30,
                         db: Session = Depends(get_db),
                         token: str = Depends(oauth2_scheme)):
    token_validated = auth.auth_token(token)
    if not token_validated['auth']:
        raise HTTPException(status_code=401, detail="401 Unauthorized")
    user = crud.get_user(db, user_id)
    if not user.email == token_validated['email']:
        raise HTTPException(status_code=status.HTTP_403_FORBIDDEN,
                            detail="403 FORBIDDEN")
    # lists = crud.get_lists_from_user(db, user_id, skip, limit)
    return user.lists
Пример #3
0
def read_user(db: Session = Depends(get_db),
              user_id: int = Header(None),
              token: str = Depends(oauth2_scheme)):
    token_validated = auth.auth_token(token)
    # token_validated = {'auth': False}
    if not token_validated['auth']:
        raise HTTPException(status_code=401, detail="401 Unauthorized")
    db_user = crud.get_user(db, user_id=user_id)
    if db_user is None:
        raise HTTPException(status_code=404, detail='404 User not found')
    if not token_validated['email'] == db_user.email:
        raise HTTPException(status_code=status.HTTP_403_FORBIDDEN,
                            detail="403 FORBIDDEN")
    return db_user
Пример #4
0
def change_exhibition_mode(db: Session = Depends(get_db),
                           owner_id: int = Header(None),
                           exhibition_mode: str = Header(None),
                           token: str = Depends(oauth2_scheme)):
    token_validated = auth.auth_token(token)
    if not token_validated['auth']:
        raise HTTPException(status_code=401, detail="401 Unauthorized")

    user = crud.get_user(db, owner_id)
    if not user.email == token_validated['email']:
        raise HTTPException(status_code=status.HTTP_403_FORBIDDEN,
                            detail="403 FORBIDDEN")

    return crud.change_exhibition_mode(db, owner_id, exhibition_mode)
Пример #5
0
def create_item_for_list(items: schemas.ItemCreate,
                         db: Session = Depends(get_db),
                         owner_id: int = Header(None),
                         owner_list_id: int = Header(None),
                         token: str = Depends(oauth2_scheme)):
    token_validated = auth.auth_token(token)
    if not token_validated['auth']:
        raise HTTPException(status_code=401, detail="401 Unauthorized")

    res = crud.get_list_by_id(db, owner_list_id)
    if owner_id != res.owner_id:
        raise HTTPException(status_code=status.HTTP_403_FORBIDDEN,
                            detail="403 FORBIDDEN")
    return crud.create_user_item(db, items, owner_list_id)
Пример #6
0
def login_user(login: schemas.Login,
               db: Session = Depends(get_db),
               token: str = Depends(oauth2_scheme)):
    token_validated = auth.auth_token(token)
    # token_validated = {'auth': True}
    if not token_validated['auth']:  # token not valid
        raise HTTPException(status_code=401, detail="401 Unauthorized")
    if not token_validated['email'] == login.email:
        raise HTTPException(status_code=status.HTTP_403_FORBIDDEN,
                            detail="403 FORBIDDEN")

    user = crud.get_user_by_email(db, login.email)
    if not user:
        raise HTTPException(status_code=status.HTTP_404_NOT_FOUND,
                            detail="NOT FOUND")
    return crud.get_user(db, user.id)
Пример #7
0
def get_items_from_a_list(db: Session = Depends(get_db),
                          owner_id: int = Header(None),
                          owner_list_id: int = Header(None),
                          token: str = Depends(oauth2_scheme)):
    token_validated = auth.auth_token(token)
    if not token_validated['auth']:
        raise HTTPException(status_code=401, detail="401 Unauthorized")

    res = crud.get_list_by_id(db, owner_list_id)
    if owner_id != res.owner_id:
        raise HTTPException(status_code=status.HTTP_403_FORBIDDEN,
                            detail="403 FORBIDDEN")
    if owner_list_id == 1:
        return crud.get_all_items_from_user(db, owner_id)
    else:
        return crud.get_items_by_id(db, owner_list_id)
Пример #8
0
def delete_item(db: Session = Depends(get_db),
                owner_id: int = Header(None),
                item_id: int = Header(None),
                token: str = Depends(oauth2_scheme)):
    token_validated = auth.auth_token(token)
    if not token_validated['auth']:
        raise HTTPException(status_code=401, detail="401 Unauthorized")
    user = crud.get_user(db, owner_id)
    if not user.email == token_validated['email']:
        raise HTTPException(status_code=status.HTTP_403_FORBIDDEN,
                            detail="403 FORBIDDEN")
    item = crud.get_item_by_id(db, item_id)
    lista = crud.get_list_by_id(db, item.owner_list_id)
    if not owner_id == lista.owner_id:
        raise HTTPException(status_code=status.HTTP_403_FORBIDDEN,
                            detail="403 FORBIDDEN")
    if crud.delete_item_by_id(db, item_id):
        return {'list_id': item.owner_list_id}
Пример #9
0
def create_user(user: schemas.UserCreate,
                db: Session = Depends(get_db),
                token: str = Depends(oauth2_scheme)):
    token_validated = auth.auth_token(token)
    print('[Users]token:', token)
    if not token_validated['auth']:  # token not valid
        raise HTTPException(status_code=401, detail="401 Unauthorized")
    if not token_validated['email'] == user.email:
        raise HTTPException(status_code=status.HTTP_403_FORBIDDEN,
                            detail="403 FORBIDDEN")
    db_user = crud.get_user_by_email(db, email=user.email)
    if db_user:
        # user already created gonna be returned
        return db_user
    user = crud.create_user(db=db, user=user)
    usr_list = schemas.UserListCreate(title="All")
    crud.create_user_list(db, user_list=usr_list, user_id=user.id)
    usr_list = schemas.UserListCreate(title="List 1")
    crud.create_user_list(db, user_list=usr_list, user_id=user.id)
    return user  # creation
Пример #10
0
def open_graph(url: str,
               db: Session = Depends(get_db),
               token: str = Depends(oauth2_scheme)):
    token_validated = auth.auth_token(token)
    # token_validated = {'auth': True}
    print(url)
    if not token_validated['auth']:
        raise HTTPException(status_code=401, detail="401 Unauthorized")
    if crud.get_user_by_email(db, token_validated['email']) is None:
        raise HTTPException(status_code=status.HTTP_403_FORBIDDEN,
                            detail="403 FORBIDDEN")

    try:
        res = og.OpenGraph(url=url)
    except ValueError:
        raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST,
                            detail='BAD REQUEST')
    except AttributeError:
        raise HTTPException(status_code=status.HTTP_406_NOT_ACCEPTABLE,
                            detail='NOT_ACCEPTABLE')
    if 'image' not in res or 'title' not in res:
        raise HTTPException(status_code=status.HTTP_406_NOT_ACCEPTABLE,
                            detail='NOT_ACCEPTABLE')
    return res