# Django Admin Cleanup - Implementation Status

## Status: COMPLETED (January 4, 2026)

---

## What Was Done

### 1. Fixed Duplicate Model Registration
**File:** `backend/igny8_core/business/billing/admin.py`

- `AccountPaymentMethod` was registered in BOTH:
  - `modules/billing/admin.py` (with AccountAdminMixin - KEPT)
  - `business/billing/admin.py` (simpler version - REMOVED)
- Commented out the duplicate registration in `business/billing/admin.py`

### 2. Simplified Admin Site Configuration
**File:** `backend/igny8_core/admin/site.py`

- Removed complex `get_app_list()` override (was 250+ lines)
- Removed `get_sidebar_list()` override
- Removed `each_context()` override with debug logging
- Kept only:
  - Custom URLs for dashboard, reports, and monitoring
  - Index redirect to dashboard
- Navigation is now handled by Unfold's built-in `SIDEBAR.navigation` setting

### 3. Added Proper Unfold Navigation Configuration
**File:** `backend/igny8_core/settings.py`

Added complete `UNFOLD["SIDEBAR"]["navigation"]` config with:
- Dashboard link
- Reports section (6 reports)
- Accounts & Users group
- Plans & Billing group
- Credits group
- Planning group
- Writing group
- Taxonomy group
- Publishing group
- Automation group
- AI Configuration group (NEW - consolidated)
- Global Settings group
- Resources group
- Logs & Monitoring group
- Django Admin group

Each item has proper Material Design icons.

### 4. Added Site Logo Configuration
**File:** `backend/igny8_core/settings.py`

```python
"SITE_ICON": {
    "light": lambda request: "/static/admin/img/logo-light.svg",
    "dark": lambda request: "/static/admin/img/logo-dark.svg",
},
```

**Note:** Logo SVG files need to be created at these paths for the logo to display.

---

## Verification Results

```bash
# Django system check
$ docker exec igny8_backend python manage.py check
System check identified no issues (0 silenced).

# Admin registry test
$ docker exec igny8_backend python manage.py shell -c "..."
Total registered models: 63
Admin site ready!

# UNFOLD config test
Navigation items: 20
```

---

## What Was NOT Done (and why)

### Models NOT Hidden from Admin

These models were originally planned for removal but are **actively used**:

| Model | Reason Kept |
|-------|-------------|
| `IntegrationSettings` | Used by AI functions, settings, integration views |
| `AIPrompt` | Used by ai/prompts.py, management commands |
| `AuthorProfile` | Used by content generation |
| `Strategy` | Used by content planning |

---

## Admin Sidebar Structure (Final)

```
Dashboard
Reports
  ├── Revenue
  ├── Usage
  ├── Content
  ├── Data Quality
  ├── Token Usage
  └── AI Cost Analysis

─── Core ───
Accounts & Users
  ├── Accounts
  ├── Users
  ├── Sites
  ├── Sectors
  └── Site Access

Plans & Billing
  ├── Plans
  ├── Subscriptions
  ├── Invoices
  ├── Payments
  ├── Credit Packages
  └── Payment Methods

Credits
  ├── Transactions
  ├── Usage Log
  └── Plan Limits

─── Content ───
Planning
  ├── Keywords
  ├── Clusters
  └── Content Ideas

Writing
  ├── Tasks
  ├── Content
  ├── Images
  └── Image Prompts

Taxonomy
  ├── Taxonomies
  ├── Relations
  ├── Attributes
  └── Cluster Maps

─── Automation ───
Publishing
  ├── Integrations
  ├── Publishing Records
  ├── Deployments
  └── Sync Events

Automation
  ├── Configs
  └── Runs

─── Configuration ───
AI Configuration
  ├── AI Models
  ├── Credit Costs
  ├── Billing Config
  └── AI Task Logs

Global Settings
  ├── Integration Settings
  ├── Module Settings
  ├── AI Prompts
  ├── Author Profiles
  └── Strategies

Resources
  ├── Industries
  ├── Industry Sectors
  └── Seed Keywords

─── System ───
Logs & Monitoring
  ├── System Health
  ├── API Monitor
  ├── Debug Console
  ├── Celery Tasks
  └── Admin Log

Django Admin
  ├── Groups
  ├── Permissions
  ├── Content Types
  └── Sessions
```

---

## Files Changed

| File | Change |
|------|--------|
| `backend/igny8_core/settings.py` | Added full UNFOLD navigation config |
| `backend/igny8_core/admin/site.py` | Simplified to ~60 lines (was ~350) |
| `backend/igny8_core/business/billing/admin.py` | Commented out duplicate AccountPaymentMethod |

---

## Phase 2: AI Models & Credits Refactor - COMPLETED

### IntegrationProvider Model Created
- New model: `IntegrationProvider` in `modules/system/models.py`
- Centralized storage for ALL external service API keys
- Supports: AI providers, payment gateways, email services, storage
- Migrated OpenAI and Runware API keys from GlobalIntegrationSettings
- Admin interface added in `modules/system/admin.py`
- Added to admin sidebar under "Global Settings"

### AIModelConfig Enhanced
- Added `tokens_per_credit` - for text models (e.g., 1000 tokens = 1 credit)
- Added `credits_per_image` - for image models (e.g., 1, 5, 15 credits)
- Added `quality_tier` - for frontend UI (basic/quality/premium)
- Migration `0025_add_aimodel_credit_fields` adds fields
- Migration `0026_populate_aimodel_credits` sets initial values

### ModelRegistry Updated
- Removed fallback to `constants.py` - database is now authoritative
- Added `get_provider()`, `get_api_key()`, `get_api_secret()`, `get_webhook_secret()`
- Provider caching with TTL

### CreditService Updated
- Added `calculate_credits_for_image(model_name, num_images)` - uses AIModelConfig.credits_per_image
- Added `calculate_credits_from_tokens_by_model(model_name, total_tokens)` - uses AIModelConfig.tokens_per_credit
- Added `deduct_credits_for_image()` - convenience method

### Files Changed (Phase 2)
| File | Change |
|------|--------|
| `modules/system/models.py` | Added IntegrationProvider model |
| `modules/system/admin.py` | Added IntegrationProviderAdmin |
| `business/billing/models.py` | Added tokens_per_credit, credits_per_image, quality_tier to AIModelConfig |
| `business/billing/services/credit_service.py` | Added image/model-based credit calculation |
| `ai/model_registry.py` | Removed constants fallback, added provider methods |
| `ai/ai_core.py` | Use ModelRegistry for API keys, removed constants fallback |
| `ai/constants.py` | Marked MODEL_RATES, IMAGE_MODEL_RATES as DEPRECATED |
| `ai/settings.py` | Use ModelRegistry for model validation |
| `ai/validators.py` | Removed constants fallback |
| `modules/system/integration_views.py` | Use ModelRegistry for cost calculation |
| `modules/billing/serializers.py` | Added new fields to AIModelConfigSerializer |

---

## Next Steps

1. ✅ **Django Admin Cleanup** - DONE
2. ⏳ **Simplify AI Settings** - Merge content + image settings into AccountSettings
3. ✅ **Create IntegrationProvider** - DONE (API keys now in dedicated model)
4. ✅ **AIModelConfig Enhancement** - DONE (tokens_per_credit, credits_per_image, quality_tier added)
