Introduction
The Maths Society Platform is built using Flask, a lightweight Python web framework, following a modular blueprint architecture. The system is designed to support mathematical challenge competitions, user management, content publishing, and real-time leaderboards.Architecture Diagram
Application Structure
Project Layout
Core Components
Application Factory Pattern
The application uses the factory pattern for flexible configuration and testing:app/__init__.py
- Environment-specific configurations
- Simplified testing with isolated app instances
- Clean dependency injection
Blueprint Architecture
The application is divided into 5 blueprints, each handling a specific domain:Main Blueprint
Public-facing routes including challenges, articles, and leaderboards
Auth Blueprint
User authentication: login, registration, and logout
Admin Blueprint
Administrative functions for content and user management
Profile Blueprint
User profile management and account settings
Math API Blueprint
RESTful API for mathematical expression validation
Extension Layer
The platform leverages several Flask extensions:| Extension | Purpose | Configuration |
|---|---|---|
| Flask-Login | User session management | login_manager |
| Flask-Migrate | Database migrations | migrate |
| Flask-CKEditor | Rich text editing | ckeditor |
| Flask-Limiter | Rate limiting | limiter (200/day, 50/hour) |
| Flask-Talisman | Security headers (CSP, HTTPS) | Talisman |
| SQLAlchemy | ORM for database operations | db |
Database Layer
The system uses SQLAlchemy ORM with support for:- PostgreSQL (production)
- SQLite (development/testing)
app/models.py with proper relationships, indexes, and cascading behaviors.
See Database Models for complete schema documentation.
Design Patterns
Model-View-Controller (MVC)
- Models: SQLAlchemy models in
app/models.py - Views: Jinja2 templates in
app/templates/ - Controllers: Blueprint routes in
app/*/routes.py
Repository Pattern
Database operations are encapsulated within model methods:Service Layer Pattern
Complex business logic is extracted to utility modules:- Math Engine (
app/utils/math_engine.py): Expression normalization and comparison - Authentication Helpers: Password hashing, session management
- File Upload Handlers: Secure file processing
Security Architecture
Content Security Policy (CSP)
Strict CSP headers configured via Flask-Talisman:Rate Limiting
API endpoints are protected with rate limits:- Default: 200 requests/day, 50 requests/hour
- Custom limits: Per-endpoint overrides for sensitive operations
Input Validation
See Math Engine for security details.Data Flow
Challenge Submission Flow
User Authentication Flow
Configuration Management
Environment-specific configurations inconfig.py:
Health and Monitoring
The application exposes health check endpoints:Error Handling
Centralized error handlers for common HTTP errors:- 404 Not Found: Custom template rendering
- 403 Forbidden: Access denied page
- 500 Internal Server Error: Automatic database rollback + error page
Performance Considerations
Database Optimization
- Indexes: Strategic indexes on frequently queried fields (see
app/models.py:403) - Lazy Loading: Dynamic relationships for efficient query patterns
- Composite Indexes: Multi-column indexes for complex queries
Caching Strategy
Future enhancement: Implement Redis caching for:
- Leaderboard queries
- Challenge content
- Frequently accessed articles
Deployment Architecture
Production Stack
- Application Server: Gunicorn/uWSGI
- Reverse Proxy: Nginx (handles SSL, static files)
- Database: PostgreSQL with connection pooling
- File Storage: Local filesystem or S3-compatible storage
- Process Management: systemd or Supervisor
Proxy Configuration
The application handles proxy headers correctly:Next Steps
Database Models
Explore the complete database schema and relationships
Blueprint Structure
Learn about routes and URL structure
Math Engine
Deep dive into expression evaluation
API Reference
API endpoints and integration guides