# ansari-devhub/farmledger architecture

> Architecture diagram of the GitHub repository ansari-devhub/farmledger, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-10-08). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/ansari-devhub/farmledger
- Repository: https://github.com/ansari-devhub/farmledger

## Overview

FarmLedger is a multi-tenant Django REST API for tracking farm activity from farm and season setup through planting, expenses, labour, harvest, sale, and payment. The principal workflow is owner-authenticated API requests routed to domain viewsets, with planting as the ledger spine and analytics aggregating costs and revenue. The graph includes the documented authentication, ownership, API documentation, and shared-model capabilities. Relationships are limited to the router excerpt, sampled source, SOURCE INDEX references, and README; unsampled wiring is not assumed.

Main entry points: [`config/api_router.py`](https://github.com/ansari-devhub/farmledger/blob/main/config/api_router.py) (API routing), [`apps/operations/models.py`](https://github.com/ansari-devhub/farmledger/blob/main/apps/operations/models.py) (Plantings and expenses), [`apps/commerce/views.py`](https://github.com/ansari-devhub/farmledger/blob/main/apps/commerce/views.py) (Farm and crop analytics), [`apps/farms/models.py`](https://github.com/ansari-devhub/farmledger/blob/main/apps/farms/models.py) (Farms and seasons)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_access["Access and API"]
  node_api["API routing<br/>[api_router.py]"]
  node_auth["JWT authentication<br/>[views.py]"]
  node_users["User accounts<br/>[models.py]"]
  node_ownership["Ownership checks<br/>[permissions.py]"]
  node_docs["OpenAPI schema<br/>[base.py]"]
  node_base["Shared model base<br/>[models.py]"]
end

subgraph group_farm["Farm Structure"]
  node_farmdata["Farms and seasons<br/>[models.py]"]
  node_crops["Crop reference<br/>[models.py]"]
  node_farmapi["Farm endpoints<br/>[views.py]"]
end

subgraph group_field["Planting Operations"]
  node_plantingdata["Plantings and expenses<br/>[models.py]"]
  node_plantingapi["Planting endpoints<br/>[views.py]"]
end

subgraph group_work["Labour"]
  node_labourdata["Workers and logs<br/>[models.py]"]
  node_labourapi["Labour endpoints<br/>[views.py]"]
end

subgraph group_market["Harvest and Sales"]
  node_marketdata["Harvests and sales<br/>[models.py]"]
  node_marketapi["Commerce endpoints<br/>[views.py]"]
  node_analytics["Farm and crop analytics<br/>[views.py]"]
end

node_farmer(("Farm owner"))
node_postgres[("PostgreSQL")]
node_redis[("Redis cache")]

node_farmer -->|"sends requests"| node_api
node_api -->|"routes auth"| node_auth
node_api -->|"routes farm APIs"| node_farmapi
node_api -->|"routes planting APIs"| node_plantingapi
node_api -->|"routes labour APIs"| node_labourapi
node_api -->|"routes commerce APIs"| node_marketapi
node_api -->|"routes summaries"| node_analytics
node_auth -.->|"registers accounts"| node_users
node_farmapi -->|"queries farms"| node_farmdata
node_farmapi -->|"checks ownership"| node_ownership
node_plantingapi -->|"queries plantings"| node_plantingdata
node_plantingapi -->|"checks ownership"| node_ownership
node_labourapi -->|"queries logs"| node_labourdata
node_labourapi -->|"scopes planting"| node_plantingdata
node_marketapi -->|"queries sales"| node_marketdata
node_marketapi -->|"scopes harvests"| node_plantingdata
node_marketapi -->|"scopes buyers"| node_farmdata
node_analytics -->|"aggregates expenses"| node_plantingdata
node_analytics -->|"aggregates labour"| node_labourdata
node_analytics -->|"aggregates revenue"| node_marketdata
node_analytics -->|"caches summaries"| node_redis
node_farmdata -->|"inherits timestamps"| node_base
node_plantingdata -->|"inherits timestamps"| node_base
node_labourdata -->|"inherits timestamps"| node_base
node_marketdata -->|"inherits timestamps"| node_base
node_farmdata -.->|"persists records"| node_postgres
node_plantingdata -.->|"persists records"| node_postgres
node_labourdata -.->|"persists records"| node_postgres
node_marketdata -.->|"persists records"| node_postgres
node_api -.->|"documents API"| node_docs

click node_api "https://github.com/ansari-devhub/farmledger/blob/main/config/api_router.py"
click node_auth "https://github.com/ansari-devhub/farmledger/blob/main/apps/users/views.py"
click node_users "https://github.com/ansari-devhub/farmledger/blob/main/apps/users/models.py"
click node_ownership "https://github.com/ansari-devhub/farmledger/blob/main/apps/core/permissions.py"
click node_docs "https://github.com/ansari-devhub/farmledger/blob/main/config/settings/base.py"
click node_farmdata "https://github.com/ansari-devhub/farmledger/blob/main/apps/farms/models.py"
click node_crops "https://github.com/ansari-devhub/farmledger/blob/main/apps/farms/models.py"
click node_farmapi "https://github.com/ansari-devhub/farmledger/blob/main/apps/farms/views.py"
click node_plantingdata "https://github.com/ansari-devhub/farmledger/blob/main/apps/operations/models.py"
click node_plantingapi "https://github.com/ansari-devhub/farmledger/blob/main/apps/operations/views.py"
click node_labourdata "https://github.com/ansari-devhub/farmledger/blob/main/apps/labour/models.py"
click node_labourapi "https://github.com/ansari-devhub/farmledger/blob/main/apps/labour/views.py"
click node_marketdata "https://github.com/ansari-devhub/farmledger/blob/main/apps/commerce/models.py"
click node_marketapi "https://github.com/ansari-devhub/farmledger/blob/main/apps/commerce/views.py"
click node_analytics "https://github.com/ansari-devhub/farmledger/blob/main/apps/commerce/views.py"
click node_base "https://github.com/ansari-devhub/farmledger/blob/main/apps/core/models.py"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_api,node_auth,node_users,node_ownership,node_docs,node_base toneBlue
class node_farmdata,node_crops,node_farmapi,node_postgres,node_redis toneAmber
class node_plantingdata,node_plantingapi toneMint
class node_labourdata,node_labourapi toneRose
class node_marketdata,node_marketapi,node_analytics,node_farmer toneIndigo
```

## Components

### Access and API

- [API routing](https://github.com/ansari-devhub/farmledger/blob/main/config/api_router.py): `config/api_router.py`
- [JWT authentication](https://github.com/ansari-devhub/farmledger/blob/main/apps/users/views.py): `apps/users/views.py`
- [User accounts](https://github.com/ansari-devhub/farmledger/blob/main/apps/users/models.py): `apps/users/models.py`
- [Ownership checks](https://github.com/ansari-devhub/farmledger/blob/main/apps/core/permissions.py): `apps/core/permissions.py`
- [OpenAPI schema](https://github.com/ansari-devhub/farmledger/blob/main/config/settings/base.py): `config/settings/base.py`
- [Shared model base](https://github.com/ansari-devhub/farmledger/blob/main/apps/core/models.py): `apps/core/models.py`

### Farm Structure

- [Farms and seasons](https://github.com/ansari-devhub/farmledger/blob/main/apps/farms/models.py): `apps/farms/models.py`
- [Crop reference](https://github.com/ansari-devhub/farmledger/blob/main/apps/farms/models.py): `apps/farms/models.py`
- [Farm endpoints](https://github.com/ansari-devhub/farmledger/blob/main/apps/farms/views.py): `apps/farms/views.py`

### Planting Operations

- [Plantings and expenses](https://github.com/ansari-devhub/farmledger/blob/main/apps/operations/models.py): `apps/operations/models.py`
- [Planting endpoints](https://github.com/ansari-devhub/farmledger/blob/main/apps/operations/views.py): `apps/operations/views.py`

### Labour

- [Workers and logs](https://github.com/ansari-devhub/farmledger/blob/main/apps/labour/models.py): `apps/labour/models.py`
- [Labour endpoints](https://github.com/ansari-devhub/farmledger/blob/main/apps/labour/views.py): `apps/labour/views.py`

### Harvest and Sales

- [Harvests and sales](https://github.com/ansari-devhub/farmledger/blob/main/apps/commerce/models.py): `apps/commerce/models.py`
- [Commerce endpoints](https://github.com/ansari-devhub/farmledger/blob/main/apps/commerce/views.py): `apps/commerce/views.py`
- [Farm and crop analytics](https://github.com/ansari-devhub/farmledger/blob/main/apps/commerce/views.py): `apps/commerce/views.py`

### Other components

- **Farm owner**
- **PostgreSQL**
- **Redis cache**

## Connections

- Farm owner → API routing: sends requests
- API routing → JWT authentication: routes auth (evidence: `config/api_router.py`)
- API routing → Farm endpoints: routes farm APIs (evidence: `config/api_router.py`)
- API routing → Planting endpoints: routes planting APIs (evidence: `config/api_router.py`)
- API routing → Labour endpoints: routes labour APIs (evidence: `config/api_router.py`)
- API routing → Commerce endpoints: routes commerce APIs (evidence: `config/api_router.py`)
- API routing → Farm and crop analytics: routes summaries (evidence: `config/api_router.py`)
- JWT authentication → User accounts: registers accounts
- Farm endpoints → Farms and seasons: queries farms (evidence: `apps/farms/views.py`)
- Farm endpoints → Ownership checks: checks ownership (evidence: `apps/farms/views.py`)
- Planting endpoints → Plantings and expenses: queries plantings (evidence: `apps/operations/views.py`)
- Planting endpoints → Ownership checks: checks ownership (evidence: `apps/operations/views.py`)
- Labour endpoints → Workers and logs: queries logs (evidence: `apps/labour/views.py`)
- Labour endpoints → Plantings and expenses: scopes planting (evidence: `apps/labour/views.py`)
- Commerce endpoints → Harvests and sales: queries sales (evidence: `apps/commerce/views.py`)
- Commerce endpoints → Plantings and expenses: scopes harvests (evidence: `apps/commerce/views.py`)
- Commerce endpoints → Farms and seasons: scopes buyers (evidence: `apps/commerce/views.py`)
- Farm and crop analytics → Plantings and expenses: aggregates expenses (evidence: `apps/commerce/views.py`)
- Farm and crop analytics → Workers and logs: aggregates labour (evidence: `apps/commerce/views.py`)
- Farm and crop analytics → Harvests and sales: aggregates revenue (evidence: `apps/commerce/views.py`)
- Farm and crop analytics → Redis cache: caches summaries (evidence: `apps/commerce/views.py`)
- Farms and seasons → Shared model base: inherits timestamps (evidence: `apps/farms/models.py`)
- Plantings and expenses → Shared model base: inherits timestamps (evidence: `apps/operations/models.py`)
- Workers and logs → Shared model base: inherits timestamps (evidence: `apps/labour/models.py`)
- Harvests and sales → Shared model base: inherits timestamps (evidence: `apps/commerce/models.py`)
- Farms and seasons → PostgreSQL: persists records
- Plantings and expenses → PostgreSQL: persists records
- Workers and logs → PostgreSQL: persists records
- Harvests and sales → PostgreSQL: persists records
- API routing → OpenAPI schema: documents API
