|
| 1 | +# Backend Development Guide |
| 2 | + |
| 3 | +Backend guidance for Relay python Django codebase. |
| 4 | + |
| 5 | +See [agents.md](agents.md) for project overview and global principles. |
| 6 | + |
| 7 | +## Technology Stack |
| 8 | + |
| 9 | +- **Backend**: Python 3, Django, Django REST Framework (DRF) |
| 10 | +- **Database**: PostgreSQL via `psycopg[c]` and Django ORM models |
| 11 | +- **API**: REST API served via DRF |
| 12 | +- **Caching**: Redis via django-redis |
| 13 | +- **Email**: AWS SES/SNS/SQS via boto3 |
| 14 | +- **Phone**: Twilio |
| 15 | +- **Auth**: Mozilla Accounts OAuth via django-allauth |
| 16 | +- **Testing**: pytest-django, model-bakery, responses library |
| 17 | +- **Code Quality**: Ruff, mypy (strict mode) |
| 18 | + |
| 19 | +## Common Commands |
| 20 | + |
| 21 | +```bash |
| 22 | +# Code quality |
| 23 | +ruff check . # Lint |
| 24 | +ruff format . # Format |
| 25 | +mypy . # Type check |
| 26 | + |
| 27 | +# Testing |
| 28 | +pytest # All tests |
| 29 | +pytest api/ # Test specific app |
| 30 | +pytest -k test_name # Run specific test |
| 31 | +``` |
| 32 | + |
| 33 | +## Django Apps Structure |
| 34 | + |
| 35 | +The backend is organized into Django apps. |
| 36 | + |
| 37 | +Check for an `.agents.md` file in an app directory for detailed guidance for that app. |
| 38 | + |
| 39 | +## Runtime data for front-end |
| 40 | + |
| 41 | +1. **Django serves static files** via Whitenoise |
| 42 | +2. **Frontend fetches runtime config** from `/api/runtime_data/` endpoint |
| 43 | + |
| 44 | +## Runtime operations |
| 45 | + |
| 46 | +1. **Email forwarding** happens via background process (NOT in web request cycle) |
| 47 | +2. **Phone masking** happens synchronously in web requests |
| 48 | + |
| 49 | +## Django Migrations |
| 50 | + |
| 51 | +### Adding New Fields |
| 52 | + |
| 53 | +New columns need database default or allow `NULL` to prevent errors when old code runs against new database. |
| 54 | + |
| 55 | +**Why:** During rollout, old code runs against new database. Omitted columns in INSERT statements must have defaults. |
| 56 | + |
| 57 | +**Example:** |
| 58 | + |
| 59 | +```python |
| 60 | +# Good |
| 61 | +new_field = models.CharField(max_length=100, default="") |
| 62 | +# or |
| 63 | +new_field = models.CharField(max_length=100, null=True, blank=True) |
| 64 | + |
| 65 | +# Bad (will fail during rollout) |
| 66 | +new_field = models.CharField(max_length=100) |
| 67 | +``` |
| 68 | + |
| 69 | +### Deleting Fields/Models |
| 70 | + |
| 71 | +**Rollout process:** |
| 72 | + |
| 73 | +1. Remove all references in code |
| 74 | +2. Deploy code changes to production |
| 75 | +3. Then remove from `models.py` and create migration |
| 76 | +4. Deploy model changes and migration |
| 77 | + |
| 78 | +**Why:** Prevents errors when code references deleted columns during rollout. |
| 79 | + |
| 80 | +## Management Commands |
| 81 | + |
| 82 | +Django apps have management commands in their `management/commands/` directory. See app-specific .agents.md files for detailed guidance on each app's management commands. |
| 83 | + |
| 84 | +## Backend Testing |
| 85 | + |
| 86 | +See [agents.testing.md](agents.testing.md) for full testing guidance. |
| 87 | + |
| 88 | +**Quick reference:** |
| 89 | + |
| 90 | +```bash |
| 91 | +pytest # Run all tests |
| 92 | +pytest api/ # Test specific app |
| 93 | +pytest -k test_name # Run specific test |
| 94 | +``` |
| 95 | + |
| 96 | +**Framework:** pytest with pytest-django |
| 97 | +**Fixtures:** model-bakery for model factories |
| 98 | +**Mocking:** responses library for HTTP mocks |
| 99 | +**Coverage:** coverage.py with HTML reports |
| 100 | + |
| 101 | +## Code Quality Guidelines |
| 102 | + |
| 103 | +See [.agents.md](.agents.md) for global code quality rules. |
| 104 | + |
| 105 | +**Backend-specific:** |
| 106 | + |
| 107 | +- Type hints required (mypy strict mode) |
| 108 | +- Use Django ORM over raw SQL |
| 109 | +- Prefer built-in functions over custom implementations |
| 110 | +- Extract functions when indented too many levels (Python-specific) |
| 111 | + |
| 112 | +## Further Reading |
| 113 | + |
| 114 | +### App-Specific Guidance |
| 115 | + |
| 116 | +- [privaterelay/.agents.md](privaterelay/.agents.md) - Core Django, settings, middleware, management commands |
| 117 | +- [api/.agents.md](api/.agents.md) - REST API, authentication, serializers |
| 118 | +- [emails/.agents.md](emails/.agents.md) - Email masking, AWS integration, metrics |
| 119 | +- [phones/.agents.md](phones/.agents.md) - Phone masking, Twilio integration |
| 120 | + |
| 121 | +### Cross-Cutting Guidance |
| 122 | + |
| 123 | +- [.agents.md](.agents.md) - Project overview and global principles |
| 124 | +- [.agents/agents.frontend.md](.agents/agents.frontend.md) - Frontend development |
| 125 | +- [.agents/agents.testing.md](.agents/agents.testing.md) - Testing guidance |
| 126 | + |
| 127 | +### Additional Resources |
| 128 | + |
| 129 | +- `docs/` - Comprehensive architecture documentation |
| 130 | +- `README.md` - Full setup instructions |
0 commit comments