# UML Sequence Diagram: Import and Allocation

Package flow: `api.views.admin.import_table`, `api.views.admin.allocation`, `api.utils`, `api.models`, `backend.kafka`.

## Import Flow Description

1. The frontend uploads `.csv`, `.xlsx`, or `.xls` to `import/table`.
2. `ImportTable` iterates sheets/rows, resolves headers, normalizes boolean and pattern values, and resolves referenced ids by codes.
3. Rows are grouped into creates and updates for Django models.
4. Redis is synced for scheduler-facing tables.
5. Kafka mirror messages are sent to the timetabling microservice where needed.

## Allocation Flow Description

1. The frontend calls `allocation` with `type=pathway`, pathway ids, and allocation method.
2. `Allocation` loads pathway/module/activity-template data, calculates remaining pathway size, generates student sets, attaches modules and activities, initializes resource maps, and syncs Redis.
3. The view writes audit records and can prepare Kafka payloads for generated student-set data.

```mermaid
sequenceDiagram
autonumber
participant FE as Frontend
participant Import as ImportTable
participant Parser as CSV/XLS/XLSX parser
participant Resolver as Code and pattern resolver
participant DB as Django models
participant Redis as bulk_sync_to_redis
participant Kafka as send_request
participant Alloc as Allocation
participant Audit as AuditTrailDetails

FE->>Import: POST import/table with uploaded file
Import->>Parser: iter_sheets_from_upload(file)
Parser-->>Import: sheet headers and row generator
Import->>Resolver: resolve codes, ids, booleans, patterns
Resolver-->>Import: normalized row payloads or row errors
Import->>DB: bulk create/update model records
Import->>Redis: sync changed scheduler-facing records
Import->>Kafka: send create/update mirror messages
Import-->>FE: import result and errors

FE->>Alloc: POST allocation type=pathway, ids, allocation_method
Alloc->>DB: load pathways, modules, templates, activities, weeks
Alloc->>Alloc: calculate remaining sizes and generated student sets
Alloc->>DB: create student sets, relation rows, resource maps
Alloc->>Redis: insert/update student_set and activity cache data
Alloc->>Kafka: prepare or send student-set mirror data
Alloc->>Audit: write allocation audit details
Alloc-->>FE: allocation result and failed rows
```

