CodeFinal Shared Backend Utilities
Shared Django utilities, models, serializers, and validation that mirror the TypeScript shared types. This package ensures consistency between your Django backend and React frontend for the CodeFinal fire inspection system.
Installation
# From your Django project directory
pip install -e /path/to/packages/backend-utils
Or add to your requirements.txt:
-e /path/to/packages/backend-utils
Quick Start
1. Update Your Django User Model
Replace your existing Django user model imports with shared utilities:
# accounts/models.py
from codefinal_shared import (
UUIDMixin, TimestampMixin, AccountFieldsMixin,
SharedUserManager, ROLE_CHOICES, STATUS_CHOICES
)
from django.contrib.auth.models import AbstractUser
class CustomUser(AbstractUser, UUIDMixin, TimestampMixin, AccountFieldsMixin):
objects = SharedUserManager()
class Meta:
ordering = ["-created_at"]
2. Update Your Serializers
Use shared serializers for consistent API responses:
# accounts/serializers.py
from codefinal_shared import (
AccountSerializer, LoginRequestSerializer, LoginResponseSerializer,
create_api_response, create_error_response
)
class UserSerializer(AccountSerializer):
class Meta(AccountSerializer.Meta):
model = CustomUser
# Add any additional fields specific to your implementation
3. Update Your Views
Use shared authentication and validation utilities:
# accounts/views.py
from codefinal_shared import (
authenticate_user, AuthTokenManager,
validate_login_data, create_api_response
)
@api_view(['POST'])
def login_view(request):
email = request.data.get('email')
password = request.data.get('password')
# Use shared authentication
result = authenticate_user(email, password)
return Response(result, status=200 if result['success'] else 400)
Core Components
Constants
from codefinal_shared import AccountRole, AccountStatus, SubscriptionLevel
# Use enum values that match TypeScript
user.role = AccountRole.ADMINISTRATOR.value
user.status = AccountStatus.ACTIVE.value
Model Mixins
from codefinal_shared import UUIDMixin, TimestampMixin, AccountFieldsMixin
class YourModel(UUIDMixin, TimestampMixin):
# Automatically gets UUID primary key and created_at/updated_at fields
pass
Serializers
from codefinal_shared import AccountSerializer, create_api_response
# Consistent API responses
return create_api_response(
data=user_data,
message="Operation successful"
)
Validation
from codefinal_shared import validate_login_data, validate_email_format
# Validation that matches frontend Zod schemas
validation_result = validate_login_data(email, password)
if not validation_result['valid']:
return create_error_response("Validation failed", validation_result['errors'])
Authentication
from codefinal_shared import AuthTokenManager, TwoFactorAuthMixin
# JWT token management
tokens = AuthTokenManager.create_tokens_for_user(user)
# Two-factor authentication
class CustomUser(AbstractUser, TwoFactorAuthMixin):
def setup_2fa(self):
return self.enable_two_factor()
Features
✅ Type Consistency
All utilities match the shared TypeScript types exactly, ensuring frontend and backend stay in sync.
✅ Validation Matching
Python validation functions mirror the Zod schemas from the frontend, providing identical validation logic.
✅ API Response Format
Consistent API response structure matching TypeScript ApiResponse and ApiError interfaces.
✅ Authentication Utilities
Complete JWT token management and two-factor authentication support.
✅ Django Integration
Drop-in replacements for common Django patterns with enhanced type safety.
Available Utilities
Models
UUIDMixin- UUID primary keyTimestampMixin- created_at/updated_at fieldsAccountFieldsMixin- Complete user account fieldsTenantAwareMixin- Subscription level supportSharedUserManager- Consistent user creation
Serializers
AccountSerializer- User account serializationLoginRequestSerializer- Login request validationLoginResponseSerializer- Login response formatPaginatedResponseSerializer- Paginated API responses
Validation
validate_email_format()- Email validation matching frontendvalidate_password_strength()- Password strength checkingvalidate_login_data()- Complete login validationDjangoFormValidator- Django form integration
Authentication
AuthTokenManager- JWT token creation/managementTwoFactorAuthMixin- TOTP two-factor authenticationauthenticate_user()- Complete authentication flowcreate_user_permissions_list()- Permission management
Constants
AccountRole- User roles enumAccountStatus- Account status enumSubscriptionLevel- Subscription level enumROLE_CHOICES,STATUS_CHOICES- Django choices
Migration Guide
From Existing Django Project
-
Install the package:
pip install -e /path/to/packages/backend-utils
-
Update your models to use shared mixins:
# Before class CustomUser(AbstractUser): id = models.UUIDField(primary_key=True, default=uuid.uuid4) role = models.CharField(max_length=100, choices=ROLE_CHOICES) # After from codefinal_shared import UUIDMixin, AccountFieldsMixin class CustomUser(AbstractUser, UUIDMixin, AccountFieldsMixin): pass # Fields are provided by mixins
-
Update serializers to use shared formats:
# Before class UserSerializer(serializers.ModelSerializer): # Custom serialization logic # After from codefinal_shared import AccountSerializer class UserSerializer(AccountSerializer): # Inherits consistent serialization
-
Update views to use shared utilities:
# Before def login_view(request): # Custom authentication logic # After from codefinal_shared import authenticate_user def login_view(request): return authenticate_user(email, password)
Benefits
🎯 Type Safety
Ensures your Django models exactly match the TypeScript interfaces used in the frontend.
🔄 Consistency
Identical validation, serialization, and response formats between frontend and backend.
🚀 Productivity
Drop-in utilities eliminate boilerplate code and reduce development time.
🛡️ Reliability
Shared validation logic prevents frontend/backend mismatches that cause bugs.
📈 Scalability
Easily extend utilities as your application grows, with changes automatically reflected across projects.
License
MIT License - matches the main CodeFinal project licensing.
Metadata
Release files for finstack-backend 1.0.5
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| finstack_backend-1.0.5.tar.gz | 18.3 kB | Without provenance | Details for finstack_backend-1.0.5.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| finstack_backend-1.0.5-py3-none-any.whl | Python 3 | none | any | Details for finstack_backend-1.0.5-py3-none-any.whl |
Total release size: 37.0 kB
Release files / finstack_backend-1.0.5.tar.gz
| Download URL | finstack_backend-1.0.5.tar.gz |
|---|---|
| Size | 18.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
ec9b932b71280994b821e8ad0f78439e9735b4352d82c832d0b2b1e753d7940c
|
|
BLAKE2b-256 checksum How to use checksums |
08bf3bf03342c42c335c1100d859b761bdb43eeee353dfc86f66fc3ec3d5906b
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.0.1 CPython/3.11.12
|
Release files / finstack_backend-1.0.5-py3-none-any.whl
| Download URL | finstack_backend-1.0.5-py3-none-any.whl |
|---|---|
| Size | 18.6 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
57f9db3e51c1b3f8b348a8d079a3e3eac63e96f4661129206e0f21b5f8f3ca01
|
|
BLAKE2b-256 checksum How to use checksums |
1989d7670196a03714b6671914c9297202431e0df4ad0eaf5471f8ebb5f335b8
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.0.1 CPython/3.11.12
|
Security for finstack-backend 1.0.5
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.