A comprehensive RESTful API for user management and friendship features built with Django and Django REST Framework.
- User Management: Complete CRUD operations for user profiles
- Authentication: Token-based authentication with secure password handling
- Friendship System: Follow/following relationships with status management
- Location Services: Geographic coordinate support with distance calculations
- Search & Filtering: Advanced search and filtering capabilities
- Nearby Friends: Find friends within a specified radius using geographic coordinates
- Comprehensive Logging: Request/response logging with performance monitoring
- API Documentation: Auto-generated Swagger/OpenAPI documentation
- Unit Testing: Comprehensive test coverage for all components
- Admin Interface: Django admin integration for data management
- Python 3.12+
- Django 5.2+
- Django REST Framework 3.14+
- PostgreSQL (Database)
- Redis (Caching & Task Queue)
- Celery (Background Tasks)
- Geopy (Geographic Calculations)
challenge/
├── challenge/ # Main project settings
│ ├── settings.py # Django settings
│ ├── urls.py # Main URL configuration
│ └── wsgi.py # WSGI configuration
├── users/ # User management app
│ ├── models.py # Custom User model
│ ├── serializers.py # User serializers
│ ├── views.py # User API views
│ ├── permissions.py # Custom permissions
│ ├── utils.py # Utility functions
│ ├── middleware.py # Request logging middleware
│ ├── admin.py # Admin interface
│ └── tests.py # User tests
├── friendships/ # Friendship management app
│ ├── models.py # Friendship model
│ ├── serializers.py # Friendship serializers
│ ├── views.py # Friendship API views
│ ├── admin.py # Admin interface
│ └── tests.py # Friendship tests
├── manage.py # Django management script
├── pyproject.toml # Project dependencies
└── README.md # This file
- Python 3.12+
- PostgreSQL
- Redis (optional, for caching and Celery)
git clone <repository-url>
cd challenge# Using Poetry (recommended)
poetry install
# Or using pip
pip install -r requirements.txtCreate a .env file in the project root:
# Django Settings
SECRET_KEY=your-secret-key-here
DEBUG=True
ALLOWED_HOSTS=localhost,127.0.0.1
# Database Settings
DB_NAME=ryde_db
DB_USER=postgres
DB_PASSWORD=postgres
DB_HOST=localhost
DB_PORT=5432
# Redis Settings (optional)
REDIS_URL=redis://localhost:6379/0# Create PostgreSQL database
createdb ryde_db
# Run migrations
python manage.py makemigrations
python manage.py migrate
# Create superuser
python manage.py createsuperuserpython manage.py runserverThe API will be available at http://localhost:8000/
http://localhost:8000/api/
The API uses Token Authentication. Include the token in the Authorization header:
Authorization: Token <your-token>
POST /api/users/
Content-Type: application/json
{
"email": "[email protected]",
"name": "John Doe",
"password": "securepassword123",
"password_confirm": "securepassword123",
"dob": "1990-01-01",
"address": "123 Main St, City",
"description": "User description",
"latitude": 40.7128,
"longitude": -74.0060
}POST /api/users/login/
Content-Type: application/json
{
"email": "[email protected]",
"password": "securepassword123"
}GET /api/users/me/
Authorization: Token <your-token>PATCH /api/users/{user_id}/
Authorization: Token <your-token>
Content-Type: application/json
{
"name": "Updated Name",
"address": "Updated Address"
}GET /api/users/search_by_name/?q=john
Authorization: Token <your-token>GET /api/users/nearby_friends/?radius=10
Authorization: Token <your-token>POST /api/friendships/
Authorization: Token <your-token>
Content-Type: application/json
{
"to_user": "user-uuid-here"
}GET /api/friendships/friends/
Authorization: Token <your-token>GET /api/friendships/pending_requests/
Authorization: Token <your-token>POST /api/friendships/{friendship_id}/action/
Authorization: Token <your-token>
Content-Type: application/json
{
"action": "accept" // or "decline", "block", "unblock"
}GET /api/friendships/nearby_friends/?radius=10
Authorization: Token <your-token>{
"id": "uuid",
"email": "[email protected]",
"name": "John Doe",
"dob": "1990-01-01",
"address": "123 Main St, City",
"description": "User description",
"latitude": 40.7128,
"longitude": -74.0060,
"created_at": "2024-01-01T00:00:00Z",
"updated_at": "2024-01-01T00:00:00Z",
"age": 34,
"has_location": true
}Run the test suite:
# Run all tests
python manage.py test
# Run specific app tests
python manage.py test users
python manage.py test friendships
# Run with coverage
coverage run --source='.' manage.py test
coverage report
coverage htmlAccess the interactive API documentation:
- Swagger UI: http://localhost:8000/swagger/
- ReDoc: http://localhost:8000/redoc/
Access the Django admin interface at http://localhost:8000/admin/ using your superuser credentials.
The application includes comprehensive logging:
- File Logs:
logs/django.log - Console Logs: Development server output
- Request Logging: All API requests and responses
- Performance Monitoring: Slow request detection
- Database Indexing: Optimized queries with proper indexes
- Caching: Redis-based caching for frequently accessed data
- Pagination: API responses are paginated for better performance
- Select Related: Optimized database queries to reduce N+1 problems
- Token Authentication: Secure API access
- Password Validation: Strong password requirements
- CORS Configuration: Cross-origin request handling
- Input Validation: Comprehensive data validation
- Permission System: Role-based access control
Update settings.py for production:
DEBUG = False
ALLOWED_HOSTS = ['your-domain.com']
SECRET_KEY = 'your-production-secret-key'
# Use environment variables for sensitive data
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': os.environ.get('DB_NAME'),
'USER': os.environ.get('DB_USER'),
'PASSWORD': os.environ.get('DB_PASSWORD'),
'HOST': os.environ.get('DB_HOST'),
'PORT': os.environ.get('DB_PORT'),
}
}Create a Dockerfile:
FROM python:3.12-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install -r requirements.txt
COPY . .
RUN python manage.py collectstatic --noinput
EXPOSE 8000
CMD ["gunicorn", "challenge.wsgi:application", "--bind", "0.0.0.0:8000"]- Fork the repository
- Create a feature branch
- Make your changes
- Add tests for new functionality
- Run the test suite
- Submit a pull request
This project is licensed under the MIT License - see the LICENSE file for details.
For support and questions, please contact:
- Email: [email protected]
- Documentation: http://localhost:8000/swagger/ or http://localhost:8000/redoc/