Skip to main content

No project description provided

Project description

fastapi-crud-admin

Python CI pypi

Install


> pip install fastapi-crud-admin

Dependencies


  • FastAPI
  • SqlAlchemy
  • SqlLite

Features


  • CRUD API of all tables of SqlAlchemy with FastAPI
  • Custom interceptor for each CRUD API
  • Custom authentication

Example


1. Create a FastAPI Admin instance

from fastapi import FastAPI
from fastapi_crud_admin.admin import FastAPIAdmin

_app = FastAPI()

admin = FastAPIAdmin(
    app=_app,
    db_url="sqlite+aiosqlite:///./test.db"
)

2. Register sqlalchemy models in admin and create crud router for models

from sqlalchemy.orm import declarative_base
from sqlalchemy import Column, Integer, String

Base = declarative_base()

class Example(Base):
    __tablename__ = 'tb_example'
    index = Column(Integer, primary_key=True, autoincrement=True)
    name = Column(String(length=200), nullable=False)

admin.register_schema_meta(models=[Example])
admin.register_schema_router()

3. (Optional) Register custom interceptor for crud router

from fastapi_crud_admin.enum import HttpMethod
from fastapi.responses import Response

def before_handler(http_method: HttpMethod, queries: dict, columns: dict) -> None:
    print(f"Before handler. Method: {http_method}")
    print(f"queries: {queries}")
    print(f"columns: {columns}")


def after_handler(response: Response) -> Response:
    print(f"After handler. Result: {response}")
    return response

admin.register_interceptor(table_name="tb_example", before_handler=before_handler, after_handler=after_handler)

4. (Optional) Register custom authentication for crud router

import bcrypt

def password_verifier(src: str, dest: str) -> bool:
    return bcrypt.checkpw(dest.encode('utf-8'), src.encode('utf-8'))

admin.register_authentication(
    user_name="admin",
    password=bcrypt.hashpw("admin".encode('utf-8'), bcrypt.gensalt()).decode('utf-8'),
    password_verifier=password_verifier
)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

fastapi_crud_admin-0.0.5.tar.gz (6.8 kB view hashes)

Uploaded Source

Built Distribution

fastapi_crud_admin-0.0.5-py3-none-any.whl (10.1 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page