Django REST Framework Common Utilities - Modular apps for enhanced DRF functionality
Project description
DRF Commons
⚠️ UNDER DEVELOPMENT: This library is currently under active development. APIs may change, and some features may not be fully stable yet.
A comprehensive collection of reusable Django REST Framework utilities and components designed to accelerate API development.
Features
Core Modules
- Current User: Middleware and utilities for accessing the current user context
- Debug: Advanced logging and debugging tools with categorized loggers
- Decorators: Performance monitoring, caching, database optimization, and logging decorators
- Filters: Enhanced filtering and ordering capabilities including computed fields
- Middlewares: Current user tracking and debug middleware
- Models: Base models, mixins, and custom fields for common patterns
- Pagination: Flexible pagination classes
- Response: Standardized API response utilities
- Serializers: Enhanced serializers with custom fields and mixins
- Views: ViewSet mixins for CRUD, bulk operations, and import/export
Services
- Export File: Export data to CSV, XLSX, and PDF formats
- Import from File: Robust file import with validation and bulk operations
Installation
Install the base package:
pip install drf-commons
Install with specific features:
# For export functionality (XLSX and PDF)
pip install drf-commons[export]
# For import functionality
pip install drf-commons[import]
# For dynamic configuration
pip install drf-commons[config]
# For all features
pip install drf-commons[all]
# For development
pip install drf-commons[dev,test]
Quick Start
1. Add to INSTALLED_APPS
INSTALLED_APPS = [
# ... your apps
'drf_commons.current_user',
'drf_commons.debug',
'drf_commons.filters',
'drf_commons.pagination',
'drf_commons.response',
'drf_commons.serializers',
'drf_commons.views',
]
2. Add Middleware (Optional)
MIDDLEWARE = [
# ... other middleware
'drf_commons.middlewares.current_user.CurrentUserMiddleware',
'drf_commons.middlewares.debug.DebugMiddleware',
]
3. Use in Your Code
from drf_commons.views.base import BaseViewSet
from drf_commons.serializers.base import BaseSerializer
from drf_commons.models.base import BaseModel
from drf_commons.pagination.base import CustomPageNumberPagination
class MyModel(BaseModel):
# Your model fields
pass
class MySerializer(BaseSerializer):
class Meta:
model = MyModel
fields = '__all__'
class MyViewSet(BaseViewSet):
queryset = MyModel.objects.all()
serializer_class = MySerializer
pagination_class = CustomPageNumberPagination
Usage Examples
Export Data
from drf_commons.services import ExportService
service = ExportService()
# Export to CSV (no optional dependencies required)
response = service.export_csv(
data_rows=data,
includes=['id', 'name', 'created_at'],
column_config={},
filename='export.csv',
export_headers=[],
document_titles=[]
)
# Export to XLSX (requires openpyxl)
response = service.export_xlsx(...)
# Export to PDF (requires weasyprint)
response = service.export_pdf(...)
Import Data
from drf_commons.services import FileImportService
config = {
'model': MyModel,
'fields': {
'name': {'required': True},
'email': {'required': True, 'unique': True},
}
}
service = FileImportService(config)
result = service.import_file(file_path)
Use Decorators
from drf_commons.decorators.performance import measure_performance
from drf_commons.decorators.cache import cached_method
class MyViewSet(BaseViewSet):
@measure_performance
@cached_method(timeout=300)
def expensive_operation(self):
# Your expensive operation
pass
Requirements
- Python >= 3.8
- Django >= 3.2
- djangorestframework >= 3.12
Optional Dependencies
Different features require different dependencies:
Core Package (No optional dependencies)
The base installation includes:
- Models, Serializers, Views, Pagination, Response utilities
- CSV export (no additional dependencies required)
- Filters, Decorators, Middleware
File Export ([export])
For XLSX and PDF export functionality:
openpyxl >= 3.0- Excel (XLSX) exportweasyprint >= 60.0- PDF export
File Import ([import])
For importing data from CSV and Excel files:
pandas >= 1.3- Data processing and file parsingopenpyxl >= 3.0- Excel file reading
Dynamic Configuration ([config])
For runtime configuration management:
django-constance >= 2.9- Dynamic Django settings
Development
Setup Development Environment
# Clone the repository
git clone https://github.com/yourusername/drf-common.git
cd drf-common
# Create virtual environment
python -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate
# Install in development mode with all dependencies
pip install -e .[dev,test,all]
Run Tests
pytest
Code Quality
# Format code
black .
# Sort imports
isort .
# Lint
flake8
# Type checking
mypy drf_commons
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
License
This project is licensed under the MIT License.
Author
Victoire HABAMUNGU
Support
For issues and questions, please use the GitHub issue tracker.
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
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 drf_commons-1.0.0.tar.gz.
File metadata
- Download URL: drf_commons-1.0.0.tar.gz
- Upload date:
- Size: 130.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b97042139971530d8ecbe4814cff80ca07d6f91ddb6056b33be958faec02adbc
|
|
| MD5 |
c90903155343da30b8657b2dacbfb8bf
|
|
| BLAKE2b-256 |
a7adbda3311730f1ad4d47eefc42aa1f194011675fb2c9b2ad4f0820474d23ac
|
File details
Details for the file drf_commons-1.0.0-py3-none-any.whl.
File metadata
- Download URL: drf_commons-1.0.0-py3-none-any.whl
- Upload date:
- Size: 189.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
deed0085a54eb590d1b11c39b1dfc5b561255842a1a952b9f76f3face073ab2b
|
|
| MD5 |
7cb7eb9d99680c9f4e99d44be0d1c2d1
|
|
| BLAKE2b-256 |
74261e22d4efdc30205f745bcae20bd7f29ef68165a47848f43e368c125d4833
|