Model Architecture
All models inherit fromJSONModel, which provides common functionality for working with JSON data from the API.
Base Classes
JSONModel (app/models/__init__.py)
The base class for all models:
- Automatic attribute assignment from JSON data
- Type coercion based on type hints
- Sortable by designated attribute
- Hashable by ID
- Comparison operators
ModelList
Container for collections of models:PaginatedModelList
Supports paginated API responses:Core Models
The application includes 20 model files representing different domain objects.Service Model
File:app/models/service.py
Represents a notification service with permissions, limits, and branding.
from_id(service_id)- Load service by IDhas_permission(permission)- Check if service has permission- Various cached properties for relationships (users, templates, etc.)
User Model
File:app/models/user.py
Represents a user with authentication and permissions.
is_gov_user- Whether user has government emailis_authenticated- Flask-Login requirementlocked- Whether account is lockedplatform_admin- Platform admin status
Organisation Model
File:app/models/organisation.py
Represents a government organisation.
Notification Model
File:app/models/notification.py
Represents a sent notification (email, SMS, or letter).
Notifications(ModelList)- Collection of notificationsInboundSMSMessages(ModelList)- Received SMS messages
Job Model
File:app/models/job.py
Represents a batch sending job.
PaginatedJobs(PaginatedModelList)- Paginated job listImmediateJobs(ModelList)- Non-scheduled jobsScheduledJobs(ModelList)- Scheduled jobsPaginatedUploads(PaginatedModelList)- Letter uploads
Supporting Models
The following models provide additional functionality:Template Models
File:app/models/template_list.py
- Template collections
- Template folders
- Template versions
Branding Models
File:app/models/branding.py
EmailBranding- Email branding configurationLetterBranding- Letter branding configurationEmailBrandingPool- Available email brandingsLetterBrandingPool- Available letter brandings
API Key Model
File:app/models/api_key.py
Other Models
ContactList(app/models/contact_list.py) - Saved contact listsEvent(app/models/event.py) - Audit eventsFeedback(app/models/feedback.py) - User feedbackLetterRates(app/models/letter_rates.py) - Postage ratesReportRequest(app/models/report_request.py) - Data export requestsSmsRate(app/models/sms_rate.py) - SMS pricingSpreadsheet(app/models/spreadsheet.py) - CSV handlingTemplateEmailFile(app/models/template_email_file.py) - Email attachmentsToken(app/models/token.py) - Authentication tokensUnsubscribeRequestsReport(app/models/unsubscribe_requests_report.py) - Unsubscribe dataWebAuthnCredential(app/models/webauthn_credential.py) - Hardware keys
Model Usage
In Views
Models are typically loaded in view functions:In Templates
Models are accessed in Jinja2 templates:Current Service and Organisation
The application provides proxies for the current context:Model Collections
Many models have associated collection classes:Model Caching
API client methods that load models often use caching:Next Steps
- API Clients - Learn how models fetch data
- Flask Blueprints - See how models are used in views
- Application Structure - Understand the overall architecture