Refactor/recipe routes (#370)

* format with black

* black format

* flake8

* remove bar exceptions

* remove test for depreciated route

* recipe settings editr

* add sqlite

Co-authored-by: hay-kot <hay-kot@pm.me>
This commit is contained in:
Hayden
2021-04-29 21:46:27 -08:00
committed by GitHub
parent 5dafe8fbb5
commit 1dc051f562
38 changed files with 179 additions and 224 deletions

View File

@@ -1,5 +1,3 @@
from datetime import timedelta
from fastapi import APIRouter, Depends, status
from fastapi.exceptions import HTTPException
from fastapi.security import OAuth2PasswordRequestForm

View File

@@ -1,5 +1,4 @@
import shutil
from datetime import timedelta
from fastapi import APIRouter, Depends, File, UploadFile, status, HTTPException
from fastapi.responses import FileResponse
@@ -34,8 +33,8 @@ async def get_all_users(
):
if not current_user.admin:
raise HTTPException( status.HTTP_403_FORBIDDEN )
raise HTTPException(status.HTTP_403_FORBIDDEN)
return db.users.get_all(session)
@@ -67,7 +66,6 @@ async def reset_user_password(
db.users.update_password(session, id, new_password)
@router.put("/{id}")
async def update_user(
id: int,
@@ -109,7 +107,7 @@ async def update_user_image(
try:
[x.unlink() for x in app_dirs.USER_DIR.join(id).glob("profile_image.*")]
except:
except Exception:
pass
dest = app_dirs.USER_DIR.joinpath(id, f"profile_image.{extension}")
@@ -118,7 +116,7 @@ async def update_user_image(
shutil.copyfileobj(profile_image.file, buffer)
if not dest.is_file:
raise HTTPException( status.HTTP_500_INTERNAL_SERVER_ERROR )
raise HTTPException(status.HTTP_500_INTERNAL_SERVER_ERROR)
@router.put("/{id}/password")
@@ -133,12 +131,11 @@ async def update_password(
match_passwords = verify_password(password_change.current_password, current_user.password)
match_id = current_user.id == id
if not ( match_passwords and match_id ):
raise HTTPException( status.HTTP_401_UNAUTHORIZED )
if not (match_passwords and match_id):
raise HTTPException(status.HTTP_401_UNAUTHORIZED)
new_password = get_password_hash(password_change.new_password)
db.users.update_password(session, id, new_password)
@router.delete("/{id}")
@@ -150,13 +147,10 @@ async def delete_user(
""" Removes a user from the database. Must be the current user or a super user"""
if id == 1:
raise HTTPException(
status_code=status.HTTP_403_FORBIDDEN,
detail='SUPER_USER'
)
raise HTTPException(status_code=status.HTTP_403_FORBIDDEN, detail="SUPER_USER")
if current_user.id == id or current_user.admin:
try:
db.users.delete(session, id)
except:
raise HTTPException( status.HTTP_400_BAD_REQUEST )
except Exception:
raise HTTPException(status.HTTP_400_BAD_REQUEST)

View File

@@ -8,6 +8,7 @@ from mealie.routes.deps import get_current_user
from mealie.schema.sign_up import SignUpIn, SignUpOut, SignUpToken
from mealie.schema.user import UserIn, UserInDB
from sqlalchemy.orm.session import Session
from fastapi import HTTPException, status
router = APIRouter(prefix="/api/users/sign-ups", tags=["User Signup"])
@@ -33,7 +34,7 @@ async def create_user_sign_up_key(
""" Generates a Random Token that a new user can sign up with """
if not current_user.admin:
raise HTTPException( status.HTTP_403_FORBIDDEN )
raise HTTPException(status.HTTP_403_FORBIDDEN)
sign_up = {
"token": str(uuid.uuid1().hex),
@@ -43,7 +44,6 @@ async def create_user_sign_up_key(
return db.sign_ups.create(session, sign_up)
@router.post("/{token}")
async def create_user_with_token(
token: str,
@@ -55,12 +55,12 @@ async def create_user_with_token(
# Validate Token
db_entry: SignUpOut = db.sign_ups.get(session, token, limit=1)
if not db_entry:
raise HTTPException( status.HTTP_401_UNAUTHORIZED )
raise HTTPException(status.HTTP_401_UNAUTHORIZED)
# Create User
new_user.admin = db_entry.admin
new_user.password = get_password_hash(new_user.password)
data = db.users.create(session, new_user.dict())
db.users.create(session, new_user.dict())
# DeleteToken
db.sign_ups.delete(session, token)
@@ -74,6 +74,6 @@ async def delete_token(
):
""" Removed a token from the database """
if not current_user.admin:
raise HTTPException( status.HTTP_403_FORBIDDEN )
raise HTTPException(status.HTTP_403_FORBIDDEN)
db.sign_ups.delete(session, token)