|
| 1 | +<style> |
| 2 | + .md-typeset h1, |
| 3 | + .md-content__button { |
| 4 | + display: none; |
| 5 | + } |
| 6 | +</style> |
| 7 | + |
| 8 | +<p align="center"> |
| 9 | + <a href="https://igorbenav.github.io/crudadmin/"> |
| 10 | + <img src="assets/CRUDAdmin.png" alt="CRUDAdmin logo" width="45%" height="auto"> |
| 11 | + </a> |
| 12 | +</p> |
| 13 | +<p align="center" markdown=1> |
| 14 | + <i>Modern admin interface for FastAPI with built-in authentication, event tracking, and security features</i> |
| 15 | +</p> |
| 16 | +<p align="center" markdown=1> |
| 17 | +<a href="https://pypi.org/project/crudadmin"> |
| 18 | + <img src="https://img.shields.io/pypi/v/crudadmin?color=%2334D058&label=pypi%20package" alt="Package version"/> |
| 19 | +</a> |
| 20 | +<a href="https://pypi.org/project/crudadmin"> |
| 21 | + <img src="https://img.shields.io/pypi/pyversions/crudadmin.svg?color=%2334D058" alt="Supported Python versions"/> |
| 22 | +</a> |
| 23 | +</p> |
| 24 | +<hr> |
| 25 | +<p align="justify"> |
| 26 | +<b>CRUDAdmin</b> is a robust admin interface generator for <b>FastAPI</b> applications, offering secure authentication, comprehensive event tracking, and essential monitoring features. Built on top of FastCRUD and SQLAlchemy, it helps you create production-ready admin panels with minimal configuration. |
| 27 | +</p> |
| 28 | +<hr> |
| 29 | + |
| 30 | +## Features |
| 31 | + |
| 32 | +- **Session-based Authentication**: Secure session management with inactivity timeouts and concurrent session limits |
| 33 | +- **Built-in Security**: IP restrictions, HTTPS enforcement, and secure cookie handling |
| 34 | +- **Event Tracking**: Comprehensive audit logs for all admin actions with user attribution |
| 35 | +- **Health Monitoring**: Real-time system status dashboard with key metrics |
| 36 | +- **Auto-generated Interface**: Creates admin UI directly from your SQLAlchemy models |
| 37 | +- **Smart Filtering**: Type-aware field filtering and efficient search |
| 38 | +- **Modern UI**: Clean interface with dark/light theme support |
| 39 | + |
| 40 | +## Requirements |
| 41 | + |
| 42 | +Before installing CRUDAdmin, ensure you have: |
| 43 | + |
| 44 | +* **Python**: Version 3.9 or newer |
| 45 | +* **FastAPI**: Latest version for the web framework |
| 46 | +* **SQLAlchemy**: Version 2.0+ for database operations |
| 47 | +* **Pydantic**: Version 2.0+ for data validation |
| 48 | + |
| 49 | +## Installing |
| 50 | + |
| 51 | +To install, just run: |
| 52 | + |
| 53 | +```sh |
| 54 | +pip install crudadmin |
| 55 | +``` |
| 56 | + |
| 57 | +Or, if using poetry: |
| 58 | + |
| 59 | +```sh |
| 60 | +poetry add crudadmin |
| 61 | +``` |
| 62 | + |
| 63 | +## Usage |
| 64 | + |
| 65 | +Here's a quick example to get you started: |
| 66 | + |
| 67 | +### Define Your Models and Schemas |
| 68 | + |
| 69 | +```python title="models.py" |
| 70 | +from sqlalchemy.orm import DeclarativeBase |
| 71 | +from sqlalchemy import Column, Integer, String |
| 72 | + |
| 73 | +class Base(DeclarativeBase): |
| 74 | + pass |
| 75 | + |
| 76 | +class User(Base): |
| 77 | + __tablename__ = "users" |
| 78 | + id = Column(Integer, primary_key=True) |
| 79 | + username = Column(String, unique=True) |
| 80 | + email = Column(String) |
| 81 | + role = Column(String) |
| 82 | +``` |
| 83 | + |
| 84 | +```python title="schemas.py" |
| 85 | +from pydantic import BaseModel, EmailStr |
| 86 | + |
| 87 | +class UserCreate(BaseModel): |
| 88 | + username: str |
| 89 | + email: EmailStr |
| 90 | + role: str = "user" |
| 91 | + |
| 92 | +class UserUpdate(BaseModel): |
| 93 | + email: EmailStr | None = None |
| 94 | + role: str | None = None |
| 95 | +``` |
| 96 | + |
| 97 | +### Set Up the Admin Interface |
| 98 | + |
| 99 | +```python title="main.py" |
| 100 | +from contextlib import asynccontextmanager |
| 101 | +from fastapi import FastAPI |
| 102 | +from sqlalchemy.ext.asyncio import AsyncSession, create_async_engine |
| 103 | +from crudadmin import CRUDAdmin |
| 104 | +import os |
| 105 | + |
| 106 | +# Database setup |
| 107 | +engine = create_async_engine("sqlite+aiosqlite:///app.db") |
| 108 | +session = AsyncSession(engine) |
| 109 | + |
| 110 | +# Create admin interface |
| 111 | +admin = CRUDAdmin( |
| 112 | + session=session, |
| 113 | + SECRET_KEY=os.environ.get("ADMIN_SECRET_KEY"), |
| 114 | + initial_admin={ |
| 115 | + "username": "admin", |
| 116 | + "password": "secure_password123" |
| 117 | + } |
| 118 | +) |
| 119 | + |
| 120 | +# Add models to admin |
| 121 | +admin.add_view( |
| 122 | + model=User, |
| 123 | + create_schema=UserCreate, |
| 124 | + update_schema=UserUpdate, |
| 125 | + allowed_actions={"view", "create", "update"} |
| 126 | +) |
| 127 | + |
| 128 | +# Setup FastAPI with proper initialization |
| 129 | +@asynccontextmanager |
| 130 | +async def lifespan(app: FastAPI): |
| 131 | + # Initialize database tables |
| 132 | + async with engine.begin() as conn: |
| 133 | + await conn.run_sync(Base.metadata.create_all) |
| 134 | + |
| 135 | + # Initialize admin interface |
| 136 | + await admin.initialize() |
| 137 | + yield |
| 138 | + |
| 139 | +# Create and mount the app |
| 140 | +app = FastAPI(lifespan=lifespan) |
| 141 | +app.mount("/admin", admin.app) |
| 142 | +``` |
| 143 | + |
| 144 | +### Enable Security Features |
| 145 | + |
| 146 | +CRUDAdmin offers robust security options out of the box: |
| 147 | + |
| 148 | +```python |
| 149 | +admin = CRUDAdmin( |
| 150 | + session=session, |
| 151 | + SECRET_KEY=SECRET_KEY, |
| 152 | + # Security settings |
| 153 | + allowed_ips=["10.0.0.1"], |
| 154 | + allowed_networks=["192.168.1.0/24"], |
| 155 | + secure_cookies=True, |
| 156 | + enforce_https=True, |
| 157 | + # Session settings |
| 158 | + max_sessions_per_user=5, |
| 159 | + session_timeout_minutes=30 |
| 160 | +) |
| 161 | +``` |
| 162 | + |
| 163 | +### Enable Event Tracking |
| 164 | + |
| 165 | +Track all admin actions with built-in audit logs: |
| 166 | + |
| 167 | +```python |
| 168 | +admin = CRUDAdmin( |
| 169 | + session=session, |
| 170 | + SECRET_KEY=SECRET_KEY, |
| 171 | + track_events=True, |
| 172 | + admin_db_url="postgresql+asyncpg://user:pass@localhost/admin_logs" |
| 173 | +) |
| 174 | +``` |
| 175 | + |
| 176 | +## Current Limitations |
| 177 | + |
| 178 | +The following features are in development: |
| 179 | + |
| 180 | +- No file upload support yet |
| 181 | +- No custom admin views (model-based only) |
| 182 | +- No custom field widgets |
| 183 | +- No SQLAlchemy relationship support |
| 184 | +- No export functionality |
| 185 | + |
| 186 | +## License |
| 187 | + |
| 188 | +This project is licensed under the MIT License - see the [LICENSE](community/LICENSE.md) file for details. |
0 commit comments