initial commit

worked on async alembic migrations, User model.

Current issue:

web_1      | INFO:     172.20.0.1:62958 - "POST /users/ HTTP/1.1" 500 Internal Server Error
web_1      | ERROR:    Exception in ASGI application
web_1      | Traceback (most recent call last):
...
web_1      | response
web_1      |   value is not a valid dict (type=type_error.dict)

when trying to create new user
This commit is contained in:
2022-12-19 01:18:25 +03:00
commit b3c5c032cf
19 changed files with 1663 additions and 0 deletions

2
.env.example Normal file
View File

@@ -0,0 +1,2 @@
SECRET_KEY = ""
DATABASE_URL = "postgresql://postgres:postgres@db:5432/foo"

12
.gitignore vendored Normal file
View File

@@ -0,0 +1,12 @@
*.log
*.pot
*.pyc
*.mo
.idea
.vscode
.DS_Store
.reload
*.sql
/static/*
/media/*
.env

17
Dockerfile Normal file
View File

@@ -0,0 +1,17 @@
# Pull base image
FROM python:3.10
# Set environment varibles
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1
WORKDIR /code/
# Install dependencies
RUN pip install pipenv
COPY Pipfile Pipfile.lock /code/
RUN pipenv install --system --dev
COPY . /code/
EXPOSE 8000

22
Pipfile Normal file
View File

@@ -0,0 +1,22 @@
[[source]]
url = "https://pypi.org/simple"
verify_ssl = true
name = "pypi"
[packages]
fastapi = {extras = ["all"], version = "*"}
fastapi-sqlalchemy = "*"
pydantic = "*"
alembic = "*"
sqlmodel = "*"
asyncpg = "==0.27.0"
sqlalchemy = {extras = ["asyncio"], version = "*"}
python-multipart = "*"
python-jose = {extras = ["cryptography"], version = "*"}
passlib = {extras = ["bcrypt"], version = "*"}
python-dotenv = "*"
[dev-packages]
[requires]
python_version = "3.10"

1000
Pipfile.lock generated Normal file

File diff suppressed because it is too large Load Diff

14
README.md Normal file
View File

@@ -0,0 +1,14 @@
# FastAPI Boilerplate
Compact template for FastAPI-based projects. SQLAlchemy ORM and Alembic migrations included.
## Alembic
To add new models to migrations you need to import them inside `migrations/env.py`.
To quickly migrate:
docker-compose exec web bash
pipenv run alembic upgrate head
So that all `.env` varibles will be catched properly

103
alembic.ini Normal file
View File

@@ -0,0 +1,103 @@
# A generic, single database configuration.
[alembic]
# path to migration scripts
script_location = migrations
# template used to generate migration file names; The default value is %%(rev)s_%%(slug)s
# Uncomment the line below if you want the files to be prepended with date and time
# file_template = %%(year)d_%%(month).2d_%%(day).2d_%%(hour).2d%%(minute).2d-%%(rev)s_%%(slug)s
# sys.path path, will be prepended to sys.path if present.
# defaults to the current working directory.
prepend_sys_path = .
# timezone to use when rendering the date within the migration file
# as well as the filename.
# If specified, requires the python-dateutil library that can be
# installed by adding `alembic[tz]` to the pip requirements
# string value is passed to dateutil.tz.gettz()
# leave blank for localtime
# timezone =
# max length of characters to apply to the
# "slug" field
# truncate_slug_length = 40
# set to 'true' to run the environment during
# the 'revision' command, regardless of autogenerate
# revision_environment = false
# set to 'true' to allow .pyc and .pyo files without
# a source .py file to be detected as revisions in the
# versions/ directory
# sourceless = false
# version location specification; This defaults
# to migrations/versions. When using multiple version
# directories, initial revisions must be specified with --version-path.
# The path separator used here should be the separator specified by "version_path_separator" below.
# version_locations = %(here)s/bar:%(here)s/bat:migrations/versions
# version path separator; As mentioned above, this is the character used to split
# version_locations. The default within new alembic.ini files is "os", which uses os.pathsep.
# If this key is omitted entirely, it falls back to the legacy behavior of splitting on spaces and/or commas.
# Valid values for version_path_separator are:
#
# version_path_separator = :
# version_path_separator = ;
# version_path_separator = space
version_path_separator = os # Use os.pathsep. Default configuration used for new projects.
# the output encoding used when revision files
# are written from script.py.mako
# output_encoding = utf-8
sqlalchemy.url = postgresql+asyncpg://postgres:postgres@db:5432/foo
[post_write_hooks]
# post_write_hooks defines scripts or Python functions that are run
# on newly generated revision scripts. See the documentation for further
# detail and examples
# format using "black" - use the console_scripts runner, against the "black" entrypoint
# hooks = black
# black.type = console_scripts
# black.entrypoint = black
# black.options = -l 79 REVISION_SCRIPT_FILENAME
# Logging configuration
[loggers]
keys = root,sqlalchemy,alembic
[handlers]
keys = console
[formatters]
keys = generic
[logger_root]
level = WARN
handlers = console
qualname =
[logger_sqlalchemy]
level = WARN
handlers =
qualname = sqlalchemy.engine
[logger_alembic]
level = INFO
handlers =
qualname = alembic
[handler_console]
class = StreamHandler
args = (sys.stderr,)
level = NOTSET
formatter = generic
[formatter_generic]
format = %(levelname)-5.5s [%(name)s] %(message)s
datefmt = %H:%M:%S

0
app/__init__.py Normal file
View File

49
app/db.py Normal file
View File

@@ -0,0 +1,49 @@
from sqlalchemy import future
from sqlalchemy.ext.asyncio import AsyncSession, create_async_engine
from sqlalchemy.orm import sessionmaker, declarative_base
from app.settings import settings
Base = declarative_base()
# engine = create_async_engine(settings.DATABASE_URL, echo=True, future=True)
# Session = sessionmaker(engine, class_=AsyncSession, expire_on_commit=False)
# def get_session():
# with Session() as session:
# yield session
class AsyncDatabaseSession:
def __init__(self):
self._session = None
self._engine = None
self._engine = create_async_engine(
settings.DATABASE_URL,
future=True,
echo=True,
)
self._session = sessionmaker(
self._engine, expire_on_commit=False, class_=AsyncSession
)()
def __getattr__(self, name):
return getattr(self._session, name)
# def init(self):
db = AsyncDatabaseSession()
# async def get_session() -> AsyncSession:
# async_session = sessionmaker(
# engine, class_=AsyncSession, expire_on_commit=False
# )
# async with async_session() as session:
# yield session
# # from sqlalchemy import create_engine
# # from sqlalchemy.ext.declarative import declarative_base
# # from sqlalchemy.orm import sessionmaker
# engine = create_engine(settings.DATABASE_URL)
# SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)

16
app/main.py Normal file
View File

@@ -0,0 +1,16 @@
from fastapi import FastAPI
from users.views import router as users_router
app = FastAPI()
app.include_router(users_router)
@app.get("/")
async def main():
return {"Hello": "World"}

9
app/settings.py Normal file
View File

@@ -0,0 +1,9 @@
import os
from pydantic import BaseSettings
class Settings(BaseSettings):
SECRET_KEY: str = os.getenv("SECRET_KEY", "")
DATABASE_URL: str = os.getenv("DATABASE_URL", "")
settings = Settings()

30
docker-compose.yml Normal file
View File

@@ -0,0 +1,30 @@
version: "3"
services:
db:
image: postgres:11
ports:
- "5432:5432"
environment:
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=postgres
- POSTGRES_DB=foo
web:
build: .
command: bash -c "pipenv run uvicorn app.main:app --host 0.0.0.0 --port 8000 --reload"
volumes:
- .:/code
ports:
- "8000:8000"
depends_on:
- db
pgadmin:
image: dpage/pgadmin4
environment:
- PGADMIN_DEFAULT_EMAIL=pgadmin4@pgadmin.org
- PGADMIN_DEFAULT_PASSWORD=admin
ports:
- "5050:80"
depends_on:
- db

1
migrations/README Normal file
View File

@@ -0,0 +1 @@
Generic single-database configuration with an async dbapi.

94
migrations/env.py Normal file
View File

@@ -0,0 +1,94 @@
import asyncio
from logging.config import fileConfig
from sqlalchemy import engine_from_config
from sqlalchemy import pool
from sqlalchemy.engine import Connection
from sqlalchemy.ext.asyncio import AsyncEngine
from alembic import context
from app.settings import settings
import users
from users.models import *
# this is the Alembic Config object, which provides
# access to the values within the .ini file in use.
config = context.config
# Interpret the config file for Python logging.
# This line sets up loggers basically.
if config.config_file_name is not None:
fileConfig(config.config_file_name)
config.set_main_option('sqlalchemy.url', settings.DATABASE_URL)
# add your model's MetaData object here
# for 'autogenerate' support
# from myapp import mymodel
# target_metadata = mymodel.Base.metadata
target_metadata = [users.models.Base.metadata]
# other values from the config, defined by the needs of env.py,
# can be acquired:
# my_important_option = config.get_main_option("my_important_option")
# ... etc.
def run_migrations_offline() -> None:
"""Run migrations in 'offline' mode.
This configures the context with just a URL
and not an Engine, though an Engine is acceptable
here as well. By skipping the Engine creation
we don't even need a DBAPI to be available.
Calls to context.execute() here emit the given string to the
script output.
"""
url = config.get_main_option("sqlalchemy.url")
context.configure(
url=url,
target_metadata=target_metadata,
literal_binds=True,
dialect_opts={"paramstyle": "named"},
)
with context.begin_transaction():
context.run_migrations()
def do_run_migrations(connection: Connection) -> None:
context.configure(connection=connection, target_metadata=target_metadata)
with context.begin_transaction():
context.run_migrations()
async def run_migrations_online() -> None:
"""Run migrations in 'online' mode.
In this scenario we need to create an Engine
and associate a connection with the context.
"""
connectable = AsyncEngine(
engine_from_config(
config.get_section(config.config_ini_section),
prefix="sqlalchemy.",
poolclass=pool.NullPool,
future=True,
)
)
async with connectable.connect() as connection:
await connection.run_sync(do_run_migrations)
await connectable.dispose()
if context.is_offline_mode():
run_migrations_offline()
else:
asyncio.run(run_migrations_online())

25
migrations/script.py.mako Normal file
View File

@@ -0,0 +1,25 @@
"""${message}
Revision ID: ${up_revision}
Revises: ${down_revision | comma,n}
Create Date: ${create_date}
"""
from alembic import op
import sqlalchemy as sa
import sqlmodel
${imports if imports else ""}
# revision identifiers, used by Alembic.
revision = ${repr(up_revision)}
down_revision = ${repr(down_revision)}
branch_labels = ${repr(branch_labels)}
depends_on = ${repr(depends_on)}
def upgrade() -> None:
${upgrades if upgrades else "pass"}
def downgrade() -> None:
${downgrades if downgrades else "pass"}

View File

@@ -0,0 +1,36 @@
"""adds user table
Revision ID: 33916b50e076
Revises:
Create Date: 2022-12-18 21:48:43.575781
"""
from alembic import op
import sqlalchemy as sa
import sqlmodel
# revision identifiers, used by Alembic.
revision = '33916b50e076'
down_revision = None
branch_labels = None
depends_on = None
def upgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
op.create_table('users',
sa.Column('id', sa.String(), nullable=False),
sa.Column('username', sa.String(), nullable=True),
sa.Column('created_at', sa.DateTime(), nullable=True),
sa.PrimaryKeyConstraint('id')
)
op.create_index(op.f('ix_users_created_at'), 'users', ['created_at'], unique=False)
# ### end Alembic commands ###
def downgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
op.drop_index(op.f('ix_users_created_at'), table_name='users')
op.drop_table('users')
# ### end Alembic commands ###

0
users/__init__.py Normal file
View File

83
users/models.py Normal file
View File

@@ -0,0 +1,83 @@
# from sqlmodel import SQLModel, Field
from sqlalchemy import Column, String, DateTime, delete
from sqlalchemy import update as sqlalchemy_update, delete as sqlalchemy_delete
from sqlalchemy.future import select
from app.db import Base, db
from datetime import datetime
from uuid import uuid4
class User(Base):
__tablename__ = "users"
id = Column(String, primary_key=True)
username = Column(String)
created_at = Column(DateTime, index=True, default=datetime.utcnow)
def __repr__(self):
return (
f"<{self.__class__.__name__}("
f"id={self.id}, "
f"username={self.username}, "
f")>"
)
@classmethod
async def create(cls, **kwargs):
user = cls(id=str(uuid4()), **kwargs)
db.add(user)
try:
await db.commit()
except Exception:
await db.rollback()
raise
return user
@classmethod
async def update(cls, id, **kwargs):
query = (
sqlalchemy_update(cls)
.where(cls.id == id)
.values(**kwargs)
.execution_options(synchronize_session="fetch")
)
await db.execute(query)
try:
await db.commit()
except Exception:
await db.rollback()
raise
@classmethod
async def get(cls, id):
query = select(cls).where(cls.id == id)
users = await db.execute(query)
(user,) = users.first()
return user
@classmethod
async def get_all(cls):
query = select(cls)
users = await db.execute(query)
users = users.scalars().all()
return users
@classmethod
async def delete(cls, id):
query = sqlalchemy_delete(cls).where(cls.id == id)
await db.execute(query)
try:
await db.commit()
except Exception:
await db.rollback()
raise
return True
# class UserBase(SQLModel):
# username: str
# full_name: str
# email: str
# hashed_password: str
# disabled: bool
# class User(UserBase, table=True):
# id: int = Field(default=None, primary_key=True)

150
users/views.py Normal file
View File

@@ -0,0 +1,150 @@
from datetime import datetime, timedelta
from fastapi import APIRouter, Depends, HTTPException, status
from fastapi.security import OAuth2PasswordBearer, OAuth2PasswordRequestForm
from starlette.status import HTTP_401_UNAUTHORIZED
from sqlalchemy.orm import Session
from sqlalchemy import select
from passlib.context import CryptContext
from jose import JWTError, jwt
# from app.db import get_session
from app.settings import settings
from pydantic import BaseModel
from typing import List
from users.models import User
ALGORITHM = "HS256"
ACCESS_TOKEN_EXPIRE_MINUTES = 30
oauth2_scheme = OAuth2PasswordBearer(tokenUrl="token")
# router = APIRouter()
pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto")
# def verify_password(plain_password, hashed_password):
# return pwd_context.verify(plain_password, hashed_password)
# def get_password_hash(password):
# return pwd_context.hash(password)
# def authenticate_user(username: str, password: str, db: Session):
# statement = select(User).where(User.username == username)
# result = db.execute(statement).fetchone()
# if result:
# print(result.User.__dict__)
# if not verify_password(password, result.User.hashed_password):
# return False
# return result.User
# def create_access_token(data: dict, expires_delta: timedelta | None = None):
# to_encode = data.copy()
# if expires_delta:
# expire = datetime.utcnow() + expires_delta
# else:
# expire = datetime.utcnow() + timedelta(minutes=15)
# to_encode.update({"exp": expire})
# encoded_jwt = jwt.encode(to_encode, settings.SECRET_KEY, algorithm=ALGORITHM)
# return encoded_jwt
# async def get_current_user(token: str = Depends(oauth2_scheme)):
# credentials_exception = HTTPException(
# status_code=status.HTTP_401_UNAUTHORIZED,
# detail="Could not validate credentials",
# headers={"WWW-Authenticate": "Bearer"},
# )
# try:
# payload = jwt.decode(token, settings.SECRET_KEY, algorithms=[ALGORITHM])
# username: str = payload.get("sub", None)
# if username is None:
# raise credentials_exception
# token_data = TokenData(username=username)
# except JWTError:
# raise credentials_exception
# user = get_user(username=token_data.username)
# if user is None:
# raise credentials_exception
# return user
# async def get_current_active_user(current_user: User = Depends(get_current_user)):
# if current_user.disabled:
# raise HTTPException(status_code=400, detail="Inactive user")
# return current_user
# @router.post("/register/", tags=["users"])
# async def register(db: Session = Depends(get_session)):
# pass
# @router.get("/login/", tags=["users"])
# async def login(session: Session = Depends(get_session)):
# username = "johndoe"
# statement = select(User).where(User.username == username)
# result = session.execute(statement).first()
# print(result)
# return {"hi": "there"}
# @router.post("/token", tags=["users"])
# async def login_for_access_token(form_data: OAuth2PasswordRequestForm = Depends(), db: Session = Depends(get_session)):
# user = authenticate_user(form_data.username, form_data.password, db)
# if not user:
# raise HTTPException(
# status_code=HTTP_401_UNAUTHORIZED,
# detail="Incorrect username or password",
# headers={"WWW-Authenticate": "Bearer"},
# )
# access_token_expires = timedelta(minutes=ACCESS_TOKEN_EXPIRE_MINUTES)
# access_token = create_access_token(
# data = {"sub": user.username}, expires_delta=access_token_expires
# )
# return {"access_token": access_token, "token_type": "bearer"}
class UserSchema(BaseModel):
username: str
class UserSerializer(BaseModel):
id: str
username: str
class Config:
orm_mode = True
router = APIRouter(
prefix="/users",
)
@router.post("/", response_model=UserSerializer)
async def create_user(user: UserSchema):
print(user)
print(type(user))
user = await User.create(**user.dict())
return user
@router.get("/{id}", response_model=UserSerializer)
async def get_user(id: str):
user = await User.get(id)
return user
@router.get("/", response_model=List[UserSerializer])
async def get_all_users():
users = await User.get_all()
return users
@router.put("/{id}", response_model=UserSerializer)
async def update(id: str, user: UserSchema):
user = await User.update(id, **user.dict())
return user
@router.delete("/{id}", response_model=bool)
async def delete_user(id: str):
return await User.delete(id)