# Django Admin - CART

Django admin interface for managing product↔tag mappings and authentication tokens.

## Overview

This Django application provides:
- **Products app** - CRUD for products and their RFID tag mappings
- **Tokens app** - Authentication token management for ingest-api
- **Internal API** - `GET /internal/tokens/` endpoint for ingest-api to poll active tokens

See [../README.md](../README.md) for full project context.

## Quick Start

```bash
# From admin/ directory

# 1. Copy environment file
cp .env.example .env

# 2. Install dependencies
uv sync

# 3. Run migrations
make migrate

# 4. Create superuser
make createsuperuser

# 5. Start development server
make runserver
```

Django admin will be available at http://localhost:8000/admin/

## Makefile Commands

| Command | Description |
| --- | --- |
| `make help` | Show available commands |
| `make migrate` | Apply database migrations |
| `make makemigrations` | Create new migrations from model changes |
| `make runserver` | Start development server (port 8000) |
| `make createsuperuser` | Create admin superuser account |
| `make shell` | Open Django shell |
| `make check` | Run Django system checks |
| `make test` | Run Django test suite |

## Environment Variables

| Variable | Description | Required |
| --- | --- | --- |
| `DATABASE_URL` | Database connection (SQLite or MySQL) | Yes |
| `DJANGO_SECRET_KEY` | Secret key for Django | Yes |
| `DJANGO_DEBUG` | Enable debug mode | No (true) |
| `DJANGO_ALLOWED_HOSTS` | Comma-separated allowed hosts | No |
| `DJANGO_LOG_LEVEL` | Logging level (DEBUG, INFO, WARNING) | No (INFO) |

Example `.env`:
```env
DATABASE_URL=sqlite:///../cart.db
DJANGO_SECRET_KEY=change-me-in-production
DJANGO_DEBUG=true
DJANGO_ALLOWED_HOSTS=localhost,127.0.0.1
DJANGO_LOG_LEVEL=INFO
DJANGO_SETTINGS_MODULE=cart.settings
```

## Database

By default uses SQLite at `../cart.db` (shared with ingest-api). For MySQL:

1. Set `DATABASE_URL` to your database URL
2. Run `make migrate` to create tables
3. Both Django and ingest-api can share the same database

## Apps

### products
- Product model with name, SKU, description
- Tag model with UID, notes
- Many-to-many relationship (one product ↔ many tags)
- Admin interface for Paweł

### tokens
- AuthToken model with token, note (location), active flag
- Admin interface for managing ingest-api tokens
- GET /internal/tokens/ endpoint returns active tokens

## Administration

Access admin at http://localhost:8000/admin/ (requires superuser account).

## Testing

```bash
make test
```

## Notes

- Django reads from the same database that ingest-api writes to
- Admin interface is accessible only behind VPN (Tailscale)
- Token-based authentication managed here, used by ingest-api
