A production-ready FastAPI + MongoDB backend for managing legal documents with version control, intelligent text comparison, smart notifications, and metadata management.
| Feature | Description |
|---|---|
| Document Management | Create, retrieve, list, and soft/hard delete legal documents |
| Version Control | Immutable versioning — old content is never overwritten |
| Text Comparison | Line-by-line diff using Python difflib |
| Smart Notifications | Background task fires when content changes significantly (≥40%) |
| Metadata Updates | Update document title without creating a new version |
| Safe Delete | Soft-delete with deleted=true; hard delete available on demand |
| Crash Safety | try/except guards around every database write |
- Python 3.10+
- FastAPI — async web framework
- Motor — async MongoDB driver
- MongoDB — document storage
- Pydantic v2 — data validation
- difflib — built-in text comparison
- FastAPI BackgroundTasks — async notifications
smart_doc_manager/
├── app/
│ ├── main.py # App entry point, router registration
│ ├── database.py # MongoDB connection (Motor)
│ ├── models.py # Internal data model definitions
│ ├── schemas.py # Pydantic request/response schemas
│ ├── routes/
│ │ ├── document.py # Document CRUD
│ │ ├── version.py # Version management
│ │ ├── compare.py # Version comparison
│ │ └── metadata.py # Title metadata update
│ ├── services/
│ │ ├── diff_service.py # difflib comparison logic
│ │ └── notification_service.py # Background notification logic
│ └── utils/
│ └── time.py # UTC timestamp helpers
├── requirements.txt
├── README.md
└── user_guide.md
- Python 3.10 or higher
- MongoDB running locally (
mongodb://localhost:27017) or a MongoDB Atlas URI
# 1. Navigate to the project directory
cd smart_doc_manager
# 2. Create a virtual environment
python -m venv venv
# 3. Activate it
# Windows:
venv\Scripts\activate
# macOS/Linux:
source venv/bin/activate
# 4. Install dependencies
pip install -r requirements.txt
# 5. (Optional) Create a .env file
# MONGO_URI=mongodb://localhost:27017
# DATABASE_NAME=smart_legal_db
# 6. Run the server
uvicorn app.main:app --reloadThe API will be available at http://127.0.0.1:8000
Interactive docs: http://127.0.0.1:8000/docs
| Method | Path | Description |
|---|---|---|
POST |
/documents/ |
Create a new document |
GET |
/documents/ |
List all active documents |
GET |
/documents/{doc_id} |
Get a document by ID |
DELETE |
/documents/{doc_id} |
Soft-delete a document |
DELETE |
/documents/{doc_id}/hard |
Hard-delete document + all versions |
| Method | Path | Description |
|---|---|---|
POST |
/versions/{doc_id} |
Upload a new version |
GET |
/versions/{doc_id} |
List all versions of a document |
GET |
/versions/{doc_id}/{ver_no} |
Get a specific version number |
DELETE |
/versions/{version_id} |
Soft-delete a single version |
| Method | Path | Description |
|---|---|---|
PUT |
/document/{doc_id}/title |
Update document title only |
| Method | Path | Description |
|---|---|---|
GET |
/compare?version1={id}&version2={id} |
Line-by-line diff of two versions |
curl -X POST http://localhost:8000/documents/ \
-H "Content-Type: application/json" \
-d '{
"title": "Non-Disclosure Agreement",
"content": "This agreement is entered into between Party A and Party B.",
"created_by": "john.doe@lawfirm.com"
}'curl -X POST http://localhost:8000/versions/<doc_id> \
-H "Content-Type: application/json" \
-d '{
"content": "This agreement is entered into between Party A, Party B, and Party C.",
"created_by": "jane.smith@lawfirm.com"
}'curl "http://localhost:8000/compare?version1=<ver1_id>&version2=<ver2_id>"curl -X PUT http://localhost:8000/document/<doc_id>/title \
-H "Content-Type: application/json" \
-d '{"title": "Mutual Non-Disclosure Agreement"}'curl -X DELETE http://localhost:8000/documents/<doc_id>When a new version is uploaded, the system compares it with the previous version in the background using SequenceMatcher. If the content has changed by 40% or more, a notification is:
- Logged to the server console.
- Saved to the
notificationsMongoDB collection.
The HTTP response is returned immediately — the user never waits for the notification process.
| Variable | Default | Description |
|---|---|---|
MONGO_URI |
mongodb://localhost:27017 |
MongoDB connection URI |
DATABASE_NAME |
smart_legal_db |
Target database name |
{
"_id": "ObjectId",
"title": "string",
"created_by": "string",
"created_at": "datetime (UTC)",
"deleted": "bool"
}{
"_id": "ObjectId",
"document_id": "string (ref to documents._id)",
"content": "string",
"version_number": "int",
"created_by": "string",
"created_at": "datetime (UTC)",
"deleted": "bool"
}{
"_id": "ObjectId",
"document_id": "string",
"message": "string",
"created_at": "datetime (UTC)"
}MIT