FastAPI-based admin interface with authentication, event logging and CRUD operations
Project description
CRUDAdmin
Modern admin interface for FastAPI with built-in authentication, event tracking, and security features
CRUDAdmin is a robust admin interface generator for FastAPI 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.
Documentation: https://igorbenav.github.io/crudadmin/
Features
- 🔒 Session-based Authentication: Secure session management with inactivity timeouts and concurrent session limits
- 🛡️ Built-in Security: IP restrictions, HTTPS enforcement, and secure cookie handling
- 📝 Event Tracking: Comprehensive audit logs for all admin actions with user attribution
- 🏥 Health Monitoring: Real-time system status dashboard with key metrics
- 📊 Auto-generated Interface: Creates admin UI directly from your SQLAlchemy models
- 🔍 Smart Filtering: Type-aware field filtering and efficient search
- 🌗 Modern UI: Clean interface with dark/light theme support
Requirements
Before installing CRUDAdmin, ensure you have:
- FastAPI: Latest version for the web framework
- SQLAlchemy: Version 2.0+ for database operations
- Pydantic: Version 2.0+ for data validation
Installing
pip install crudadmin
Or using poetry:
poetry add crudadmin
Usage
CRUDAdmin offers a straightforward way to create admin interfaces. Here's how to get started:
Define Your Models and Schemas
models.py
from sqlalchemy.orm import DeclarativeBase
from sqlalchemy import Column, Integer, String
class Base(DeclarativeBase):
pass
class User(Base):
__tablename__ = "users"
id = Column(Integer, primary_key=True)
username = Column(String, unique=True)
email = Column(String)
role = Column(String)
schemas.py
from pydantic import BaseModel, EmailStr
class UserCreate(BaseModel):
username: str
email: EmailStr
role: str = "user"
class UserUpdate(BaseModel):
email: EmailStr | None = None
role: str | None = None
Set Up the Admin Interface
main.py
from contextlib import asynccontextmanager
from fastapi import FastAPI
from sqlalchemy.ext.asyncio import AsyncSession, create_async_engine
from crudadmin import CRUDAdmin
import os
# Database setup
engine = create_async_engine("sqlite+aiosqlite:///app.db")
session = AsyncSession(engine)
# Create admin interface
admin = CRUDAdmin(
session=session,
SECRET_KEY=os.environ.get("ADMIN_SECRET_KEY"),
initial_admin={
"username": "admin",
"password": "secure_password123"
}
)
# Add models to admin
admin.add_view(
model=User,
create_schema=UserCreate,
update_schema=UserUpdate,
allowed_actions={"view", "create", "update"}
)
# Setup FastAPI with proper initialization
@asynccontextmanager
async def lifespan(app: FastAPI):
# Initialize database tables
async with engine.begin() as conn:
await conn.run_sync(Base.metadata.create_all)
# Initialize admin interface
await admin.initialize()
yield
# Create and mount the app
app = FastAPI(lifespan=lifespan)
app.mount("/admin", admin.app)
Enable Event Tracking
admin = CRUDAdmin(
session=session,
SECRET_KEY=SECRET_KEY,
track_events=True,
admin_db_url="postgresql+asyncpg://user:pass@localhost/admin_logs"
)
@asynccontextmanager
async def lifespan(app: FastAPI):
async with engine.begin() as conn:
await conn.run_sync(Base.metadata.create_all)
await admin.initialize() # Creates event tracking tables
yield
Configure Security Features
admin = CRUDAdmin(
session=session,
SECRET_KEY=SECRET_KEY,
# Security settings
allowed_ips=["10.0.0.1"],
allowed_networks=["192.168.1.0/24"],
secure_cookies=True,
enforce_https=True,
# Session settings
max_sessions_per_user=5,
session_timeout_minutes=30
)
@asynccontextmanager
async def lifespan(app: FastAPI):
async with engine.begin() as conn:
await conn.run_sync(Base.metadata.create_all)
await admin.initialize() # Initializes security features
yield
Current Limitations (coming soon)
- No file upload support yet
- No custom admin views (model-based only)
- No custom field widgets
- No SQLAlchemy relationship support
- No export functionality
Similar Projects
- Django Admin: The inspiration for this project
- Flask-Admin: Similar project for Flask
- Sqladmin: Another FastAPI admin interface
License
This project is licensed under the MIT License - see the LICENSE file for details.
Contact
Igor Benav – @igorbenav – igormagalhaesr@gmail.com github.com/igorbenav
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file crudadmin-0.1.0.tar.gz.
File metadata
- Download URL: crudadmin-0.1.0.tar.gz
- Upload date:
- Size: 176.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.5.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6652c603c2ea3c3aabb40481cea66165a521729f2d8762b3f961afcb6f07780e
|
|
| MD5 |
1dc993b66a81be5e7016deb30dc84d7f
|
|
| BLAKE2b-256 |
4bc72cf50aa18e54bcae96548e4069a62ab2fe5510efcbe3815f2ca23c849c8f
|
File details
Details for the file crudadmin-0.1.0-py3-none-any.whl.
File metadata
- Download URL: crudadmin-0.1.0-py3-none-any.whl
- Upload date:
- Size: 193.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.5.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
88380b33ec19b6514f08cc1a2ef1ccc9bb1248aaebe52e5f378cb49d1c0f5ce4
|
|
| MD5 |
5d150bf407c66e7e23fde0782e520b9a
|
|
| BLAKE2b-256 |
8c17a2fa418308c6250aa9e95b8915ce0476ce7310bd20a7a09c5af1b88ad3cf
|