app/models.py and contains comprehensive models for users, services, templates, notifications, and more.
Core Models
User Model
Location:app/models.py:136
The User model manages authentication and permissions for platform users.
- Password hashing using bcrypt
- Multiple authentication types: SMS, email, WebAuthn
- Platform admin flag for elevated permissions
- Many-to-many relationships with Services and Organisations
services- Services the user belongs to (viauser_to_service)organisations- Organisations the user belongs to (viauser_to_organisation)
Service Model
Location:app/models.py:562
Represents a government service that sends notifications.
- Hybrid properties for name handling and email sender configuration
- Service state:
active,restricted(trial mode),count_as_live - Message limits per notification type
- Versioning support for audit trail
users- Users with access to this servicetemplates- Notification templatesjobs- Bulk sending jobsorganisation- Parent organisation
Template Models
Location:app/models.py:1035 (TemplateBase), app/models.py:1216 (Template)
Templates define the content and structure of notifications.
TemplateHistorymaintains version history- Foreign key constraint links to specific versions
- Immutable once a version is created
Notification Model
Location:app/models.py:1524
Tracks individual notification sends.
created- Initial statesending- In transit to providerdelivered- Successfully deliveredpermanent-failure- Failed permanentlytemporary-failure- Failed temporarilytechnical-failure- System error
ix_notifications_service_created_at- Service queriesix_notifications_notification_type_composite- Type + status + createdix_notifications_normalised_to_trgm- Recipient search (trigram)ix_notifications_client_reference_trgm- Reference search (trigram)
Job Model
Location:app/models.py:1418
Represents bulk notification sending jobs.
pending- Waiting to processin progress- Currently processingfinished- Completed successfullycancelled- Cancelled by user or systemsending limits exceeded- Hit rate limits
Supporting Models
Organisation Model
Location:app/models.py:425
API Key Model
Location:app/models.py:929
normal- Production usetest- Testing (doesn’t send)team- Team members only
Provider Details Model
Location:app/models.py:1364
Tracks SMS/Email/Letter provider configuration.
Branding Models
Email and letter branding for customization.Database Features
Versioning
Many models inherit fromVersioned mixin for audit trails:
ServiceTemplate→TemplateHistoryApiKeyServiceCallbackApi
Encryption
Sensitive data is encrypted using thesigning module:
- API key secrets
- Notification personalisation
- Service callback bearer tokens
Indexes
Extensive indexing for performance:- Composite indexes for common query patterns
- Trigram indexes (GIN) for fuzzy text search
- Partial indexes for specific conditions
Extended Statistics
PostgreSQL extended statistics for query optimization:Connection Configuration
Fromapp/config.py:174:
Database Bindings
Supports separate read replica:- Default binding: Primary database
bulkbinding: Read replica for analytics queries
Migration Strategy
Migrations managed with Alembic:- Located in
migrations/versions/ - Auto-generation with manual review
- Downgrade paths for rollbacks
- Custom logic for views in
migrations/env.py
Related Files
app/models.py- All model definitionsapp/dao/- Data access objects for each modelapp/config.py- Database configurationmigrations/- Alembic migration files