Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Smart Legal Document Manager

A production-ready FastAPI + MongoDB backend for managing legal documents with version control, intelligent text comparison, smart notifications, and metadata management.


Features

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

Tech Stack

  • 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

Project Structure

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

Setup & Installation

Prerequisites

  • Python 3.10 or higher
  • MongoDB running locally (mongodb://localhost:27017) or a MongoDB Atlas URI

Steps

# 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 --reload

The API will be available at http://127.0.0.1:8000

Interactive docs: http://127.0.0.1:8000/docs


API Reference

Document Endpoints

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

Version Endpoints

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

Metadata Endpoint

Method Path Description
PUT /document/{doc_id}/title Update document title only

Compare Endpoint

Method Path Description
GET /compare?version1={id}&version2={id} Line-by-line diff of two versions

Example Requests

Create a Document

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"
  }'

Upload a New Version

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"
  }'

Compare Two Versions

curl "http://localhost:8000/compare?version1=<ver1_id>&version2=<ver2_id>"

Update Document Title

curl -X PUT http://localhost:8000/document/<doc_id>/title \
  -H "Content-Type: application/json" \
  -d '{"title": "Mutual Non-Disclosure Agreement"}'

Soft-Delete a Document

curl -X DELETE http://localhost:8000/documents/<doc_id>

Notification System

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:

  1. Logged to the server console.
  2. Saved to the notifications MongoDB collection.

The HTTP response is returned immediately — the user never waits for the notification process.


Environment Variables

Variable Default Description
MONGO_URI mongodb://localhost:27017 MongoDB connection URI
DATABASE_NAME smart_legal_db Target database name

Collections Schema

documents

{
  "_id": "ObjectId",
  "title": "string",
  "created_by": "string",
  "created_at": "datetime (UTC)",
  "deleted": "bool"
}

versions

{
  "_id": "ObjectId",
  "document_id": "string (ref to documents._id)",
  "content": "string",
  "version_number": "int",
  "created_by": "string",
  "created_at": "datetime (UTC)",
  "deleted": "bool"
}

notifications

{
  "_id": "ObjectId",
  "document_id": "string",
  "message": "string",
  "created_at": "datetime (UTC)"
}

License

MIT

About

Smart Legal Document Manager built with FastAPI and MongoDB. Supports document version control, diff comparison, async notifications, and metadata management.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages