Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

move user specific db operations to db/users dir #176

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion {{cookiecutter.project_slug}}/backend/app/alembic/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from sqlalchemy import engine_from_config
from sqlalchemy import pool

from app.db.models import Base
from app.db.session import Base

# this is the Alembic Config object, which provides
# access to the values within the .ini file in use.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from app.db import models
from app.db.users.models import User


def test_get_users(client, test_superuser, superuser_token_headers):
Expand All @@ -19,7 +19,7 @@ def test_delete_user(client, test_superuser, test_db, superuser_token_headers):
f"/api/v1/users/{test_superuser.id}", headers=superuser_token_headers
)
assert response.status_code == 200
assert test_db.query(models.User).all() == []
assert test_db.query(User).all() == []


def test_delete_user_not_found(client, superuser_token_headers):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
import typing as t

from app.db.session import get_db
from app.db.crud import (
from app.db.users.crud import (
get_users,
get_user,
create_user,
delete_user,
edit_user,
)
from app.db.schemas import UserCreate, UserEdit, User, UserOut
from app.db.users.schemas import UserCreate, UserEdit, User, UserOut
from app.core.auth import get_current_active_user, get_current_active_superuser

users_router = r = APIRouter()
Expand Down
30 changes: 14 additions & 16 deletions {{cookiecutter.project_slug}}/backend/app/core/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
from fastapi import Depends, HTTPException, status
from jwt import PyJWTError

from app.db import models, schemas, session
from app.db.crud import get_user_by_email, create_user
from app.db import session
from app.db.users.crud import get_user_by_email, create_user
from app.db.users.models import User
from app.db.users.schemas import TokenData, UserCreate
from app.core import security


Expand All @@ -23,7 +25,7 @@ async def get_current_user(
if email is None:
raise credentials_exception
permissions: str = payload.get("permissions")
token_data = schemas.TokenData(email=email, permissions=permissions)
token_data = TokenData(email=email, permissions=permissions)
except PyJWTError:
raise credentials_exception
user = get_user_by_email(db, token_data.email)
Expand All @@ -33,16 +35,16 @@ async def get_current_user(


async def get_current_active_user(
current_user: models.User = Depends(get_current_user),
current_user: User = Depends(get_current_user),
):
if not current_user.is_active:
raise HTTPException(status_code=400, detail="Inactive user")
return current_user


async def get_current_active_superuser(
current_user: models.User = Depends(get_current_user),
) -> models.User:
current_user: User = Depends(get_current_user),
) -> User:
if not current_user.is_superuser:
raise HTTPException(
status_code=403, detail="The user doesn't have enough privileges"
Expand All @@ -63,13 +65,9 @@ def sign_up_new_user(db, email: str, password: str):
user = get_user_by_email(db, email)
if user:
return False # User already exists
new_user = create_user(
db,
schemas.UserCreate(
email=email,
password=password,
is_active=True,
is_superuser=False,
),
)
return new_user
return create_user(db, UserCreate(
email=email,
password=password,
is_active=True,
is_superuser=False,
))
Empty file.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from sqlalchemy import Boolean, Column, Integer, String

from .session import Base
from app.db.session import Base


class User(Base):
Expand Down
4 changes: 2 additions & 2 deletions {{cookiecutter.project_slug}}/backend/app/initial_data.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#!/usr/bin/env python3

from app.db.session import get_db
from app.db.crud import create_user
from app.db.schemas import UserCreate
from app.db.users.crud import create_user
from app.db.users.schemas import UserCreate
from app.db.session import SessionLocal


Expand Down
10 changes: 5 additions & 5 deletions {{cookiecutter.project_slug}}/backend/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

from app.core import config, security
from app.db.session import Base, get_db
from app.db import models
from app.db.users.models import User
from app.main import app


Expand Down Expand Up @@ -100,12 +100,12 @@ def get_password_hash() -> str:


@pytest.fixture
def test_user(test_db) -> models.User:
def test_user(test_db) -> User:
"""
Make a test user in the database
"""

user = models.User(
user = User(
email="[email protected]",
hashed_password=get_password_hash(),
is_active=True,
Expand All @@ -116,12 +116,12 @@ def test_user(test_db) -> models.User:


@pytest.fixture
def test_superuser(test_db) -> models.User:
def test_superuser(test_db) -> User:
"""
Superuser for testing
"""

user = models.User(
user = User(
email="[email protected]",
hashed_password=get_password_hash(),
is_superuser=True,
Expand Down