5.5 KiB
5.5 KiB
Data Flow Diagrams (Narrative)
Purpose
Describe end-to-end data movement through the system based on current routing, middleware, and model conventions. No diagrams are embedded; flows are explained textually.
Code Locations (exact paths)
- Request routing:
backend/igny8_core/urls.py - Middleware:
backend/igny8_core/middleware/request_id.py,backend/igny8_core/auth/middleware.py,backend/igny8_core/middleware/resource_tracker.py - DRF base viewsets:
backend/igny8_core/api/base.py - Authentication classes:
backend/igny8_core/api/authentication.py - Tenancy models:
backend/igny8_core/auth/models.py - Celery configuration:
backend/igny8_core/settings.py
High-Level Responsibilities
- Trace how HTTP requests are processed, tenant-scoped, authorized, and persisted.
- Show where async processing departs to Celery and where responses are shaped.
Detailed Behavior
- Incoming request → Django middleware stack:
- Security/WhiteNoise/CORS/session/common/CSRF/Django auth.
RequestIDMiddlewareassignsrequest.request_idand returns it inX-Request-ID.AccountContextMiddlewareresolves user/account (session or JWT) and enforces active plan; setsrequest.account.ResourceTrackingMiddlewareoptionally tracks resource usage for admin/developer users when theX-Debug-Resource-Trackingheader is true; addsX-Resource-Tracking-ID.
- URL dispatch via
urls.pyroutes to module routers (auth, account, planner, writer, system, billing, automation, linker, optimizer, publisher, integration) under/api/v1/*. - DRF viewset pipeline:
- Authentication classes (API key → JWT → session → basic) establish
request.user(and optionallyrequest.account/request.site). - Base viewsets (
AccountModelViewSet,SiteSectorModelViewSet) filter querysets byaccount/site/sectorand attachaccounton create. - Serializers handle validation; responses are wrapped by unified helpers to standardize success/error payloads and pagination.
- Authentication classes (API key → JWT → session → basic) establish
- Persistence:
- Tenant-scoped models inherit
AccountBaseModelorSiteSectorBaseModel; save hooks enforce account/site/sector alignment. - Soft deletion is used where models implement
soft_delete, respecting retention windows from account settings.
- Tenant-scoped models inherit
- Async/Background:
- Celery uses Redis broker/backend; tasks inherit JSON payloads and time limits from
settings.py. - Automation, AI, publishing, and billing tasks enqueue via Celery; results return through database/state updates, not synchronous responses.
- Celery uses Redis broker/backend; tasks inherit JSON payloads and time limits from
- Response:
- Unified response wrappers ensure
success,data/error, and request ID are present; paginated responses includecount/next/previous/results. - Throttling headers apply per-scope (as configured in
REST_FRAMEWORKthrottles).
- Unified response wrappers ensure
Data Structures / Models Involved (no code)
- Tenancy bases:
AccountBaseModel,SiteSectorBaseModel. - Core entities:
Account,Plan,Site,Sector,User,SiteUserAccess. - Module-specific models follow the same tenancy bases (documented in module-specific files).
Execution Flow
- HTTP request hits middleware; IDs and tenant context are set.
- DRF authentication authenticates and sets user/account/site.
- Viewset filters data by tenant/site/sector and runs serializer validation.
- DB operations persist data with enforced tenant alignment.
- Optional Celery tasks are queued for long-running work.
- Response returns unified JSON with request IDs and optional throttling/pagination headers.
Cross-Module Interactions
- Auth context set in middleware is consumed by all module viewsets for scoping.
- API key auth provides site context for integration/publisher flows.
- Celery configuration is shared by automation/AI/publishing/billing task modules.
State Transitions (if applicable)
- Entity lifecycle changes (create/update/delete/soft-delete) flow through base viewsets and tenancy bases, ensuring account/site/sector consistency.
- Request lifecycle includes request ID creation, optional resource tracking, and unified response wrapping.
Error Handling
- Middleware can short-circuit with JSON errors for missing account/plan.
- Viewset overrides wrap validation and server errors into unified responses; missing objects return 404 payloads.
- Throttling (scope-based) returns standard DRF throttle responses with headers.
Tenancy Rules
- All tenant-bound data flows require
request.account; filtering and save hooks prevent cross-tenant access. - Admin/developer/system-account users may bypass tenant filtering; system accounts are guarded against deletion.
- Site/sector alignment is validated on save for models inheriting
SiteSectorBaseModel.
Billing Rules (if applicable)
- Plan activation is validated in middleware. Credit debits and billing workflows occur in billing modules (covered elsewhere) after tenant resolution.
Background Tasks / Schedulers (if applicable)
- Celery broker/backend configuration in
settings.pygoverns async flow; tasks should include account/site identifiers to maintain scoping.
Key Design Considerations
- Request ID and resource tracking enable traceability and performance debugging.
- Middleware ordering ensures tenant context precedes view logic.
- Unified response format keeps clients consistent across modules.
How Developers Should Work With This Module
- Preserve middleware order; new middleware must not break request ID or tenant context.
- Ensure new viewsets inherit the base classes to pick up scoping and unified responses.
- When adding async tasks, include tenant/site identifiers and respect Celery limits from settings.