A comprehensive health informatics platform backend built with Django, designed specifically for healthcare management in Africa.
OpenCare-Africa is a robust, scalable backend system for managing healthcare operations, patient records, health worker management, and health facility operations. The system is built with modern Django practices and includes comprehensive API endpoints for integration with frontend applications.
- User Management: Comprehensive user roles and permissions for healthcare workers
- Patient Management: Complete patient lifecycle management with medical history
- Health Facility Management: Facility operations, services, and resource management
- Health Records: Comprehensive medical records with FHIR compliance
- Analytics & Reporting: Health metrics, disease outbreak tracking, and performance analytics
- API-First Design: RESTful API with OpenAPI/Swagger documentation
- Django 4.2+: Modern Django with best practices
- PostgreSQL: Robust database with healthcare-optimized schemas
- Redis: Caching and session management
- Celery: Background task processing
- Docker: Containerized deployment
- JWT Authentication: Secure API authentication
- Health Checks: System monitoring and diagnostics
OpenCare-Africa/
βββ apps/ # Django applications
β βββ core/ # Core models and utilities
β βββ patients/ # Patient management
β βββ health_workers/ # Healthcare personnel management
β βββ facilities/ # Health facility operations
β βββ records/ # Medical records management
β βββ analytics/ # Health analytics and reporting
β βββ api/ # API endpoints and viewsets
βββ config/ # Project configuration
β βββ settings/ # Environment-specific settings
β βββ urls.py # Main URL configuration
β βββ celery.py # Celery configuration
βββ templates/ # HTML templates
βββ static/ # Static files
βββ media/ # User-uploaded files
βββ docs/ # Documentation
βββ scripts/ # Database and deployment scripts
- Docker & Docker Compose (recommended for all environments)
- Python 3.11+ (only needed for local development without Docker)
- PostgreSQL 15+ (included in Docker setup)
- Redis 7+ (included in Docker setup)
This is the recommended way to run OpenCare-Africa as it ensures consistency across all environments.
-
Clone the repository
git clone https://github.com/bos-com/OpenCare-Africa.git cd OpenCare-Africa -
Set up environment variables
# Copy environment template cp env.example .env # The .env file is already configured for Docker # No changes needed unless you want to customize settings
-
Build and start all services
# Build Docker images docker-compose build # Start all services (database, Redis, web app, Celery) docker-compose up -d
-
Run database migrations
docker-compose exec web python manage.py migrate -
Create superuser (optional)
docker-compose exec web python manage.py createsuperuser -
Verify the installation
# Check service status docker-compose ps # Test the health endpoint curl http://localhost:8000/health/ # Test the web interface open http://localhost:8000/
-
Access the application
- Web Interface: http://localhost:8000
- Admin Panel: http://localhost:8000/admin
- API Documentation: http://localhost:8000/api/docs/
- Health Check: http://localhost:8000/health/
- Start the stack via Docker or local development, then browse to http://localhost:8000/api/docs/ for interactive OpenAPI documentation.
- Review sanitized response expectations and logging rules in
docs/error-handling.mdbefore exposing new endpoints. - Extend automated tests to cover both happy-path and error scenarios when updating API behavior; see the error-handling guide for recommendations.
The Docker setup includes the following services:
| Service | Port | Purpose |
|---|---|---|
| web | 8000 | Django web application |
| db | 5432 | PostgreSQL database |
| redis | 6379 | Redis cache and Celery broker |
| celery | - | Background task processor |
| celery-beat | - | Scheduled task scheduler |
| nginx | 80 | Reverse proxy (production) |
| metabase | 3000 | Analytics dashboard |
| superset | 8088 | Business intelligence platform |
# Start all services
docker-compose up -d
# View service status
docker-compose ps
# View logs
docker-compose logs -f web
# Stop all services
docker-compose down
# Restart a specific service
docker-compose restart web
# Execute Django commands
docker-compose exec web python manage.py migrate
docker-compose exec web python manage.py createsuperuser
docker-compose exec web python manage.py shellIf you prefer to run the application locally without Docker:
-
Clone the repository
git clone https://github.com/bos-com/OpenCare-Africa.git cd OpenCare-Africa -
Set up Python virtual environment
# Create virtual environment python3 -m venv venv # Activate virtual environment source venv/bin/activate # On Windows: venv\Scripts\activate # Upgrade pip pip install --upgrade pip
-
Set up environment variables
# Copy environment template cp env.example .env # Update .env for local development # Change DB_HOST=localhost and REDIS_HOST=localhost
-
Install dependencies
# Install production dependencies pip install -r requirements.txt # Install development dependencies (optional) pip install -r requirements-dev.txt
-
Set up the database
# For development, SQLite is used by default # No additional database setup required # Run migrations python manage.py migrate
-
Create superuser (optional)
python manage.py createsuperuser
-
Run the development server
python manage.py runserver
-
Verify the installation
# Test the API health endpoint curl http://localhost:8000/health/ # Test the web interface open http://localhost:8000/
The API is fully documented using OpenAPI/Swagger:
- Swagger UI:
/api/docs/ - ReDoc:
/api/redoc/ - OpenAPI Schema:
/api/schema/
- Authentication:
/api/v1/auth/ - Patients:
/api/v1/patients/ - Health Workers:
/api/v1/health-workers/ - Facilities:
/api/v1/facilities/ - Health Records:
/api/v1/records/ - Analytics:
/api/v1/analytics/ - Appointments:
/api/v1/appointments/
- User: Extended user model with healthcare worker profiles
- Location: Hierarchical geographic location management
- HealthFacility: Health facility information and services
- AuditTrail: Comprehensive audit logging
- Patient: Complete patient information and demographics
- PatientVisit: Patient visit tracking and scheduling
- PatientMedicalHistory: Medical history and conditions
- HealthWorkerProfile: Extended healthcare worker profiles
- ProfessionalQualification: Qualifications and certifications
- WorkSchedule: Work schedules and availability
- PerformanceEvaluation: Performance tracking and reviews
- FacilityService: Services offered by health facilities
- FacilityStaff: Staff management and assignments
- FacilityEquipment: Medical equipment tracking
- FacilityInventory: Medical supplies and inventory
- HealthRecord: Comprehensive medical records
- VitalSigns: Patient vital signs and measurements
- Medication: Prescription and medication management
- LaboratoryTest: Lab test results and interpretation
- ImagingStudy: Medical imaging results
- HealthMetrics: Health KPIs and metrics
- DiseaseOutbreak: Disease outbreak tracking
- HealthReport: Automated health reports
- PatientAnalytics: Patient-specific analytics
# Django Configuration
SECRET_KEY=your-secret-key-here
DEBUG=True
ALLOWED_HOSTS=localhost,127.0.0.1
# Database Configuration
DB_ENGINE=django.db.backends.postgresql
DB_NAME=opencare_africa
DB_USER=opencare_user
DB_PASSWORD=opencare_password
DB_HOST=localhost
DB_PORT=5432
# Redis Configuration
REDIS_HOST=localhost
REDIS_PORT=6379
REDIS_DB=0
# JWT Configuration
JWT_ACCESS_TOKEN_LIFETIME=5
JWT_REFRESH_TOKEN_LIFETIME=1config/settings/base.py: Base configurationconfig/settings/development.py: Development environmentconfig/settings/production.py: Production environmentconfig/settings/test.py: Testing environment
# Run all tests
python manage.py test
# Run tests with coverage
coverage run --source='.' manage.py test
coverage report
coverage html
# Run specific app tests
python manage.py test apps.patients
python manage.py test apps.core- Database connectivity
- Redis connectivity
- Storage availability
- System resources
- Django Debug Toolbar (development)
- Sentry integration (production)
- Custom health metrics
- Performance analytics
- Redis-based caching
- Database query optimization
- Static file caching
- API response caching
- Set
DEBUG=False - Configure production database
- Set up SSL/TLS certificates
- Configure static file serving
- Set up monitoring and logging
- Configure backup strategies
- Set up CI/CD pipelines
# Build production image
docker build -t opencare-africa:latest .
# Run with production settings
docker run -e DJANGO_SETTINGS_MODULE=config.settings.production opencare-africa:latest- JWT-based authentication
- Role-based access control
- Comprehensive audit logging
- Input validation and sanitization
- CORS configuration
- Rate limiting (configurable)
- Secure password policies
- API Documentation: Built-in Swagger/OpenAPI docs
- Code Documentation: Comprehensive docstrings
- Admin Interface: Django admin for data management
- User Guides: Available in
/docs/directory - Patient Records: See
docs/patient-records.mdfor CRUD usage and security notes - Audit Logging: See
docs/audit-logs.mdfor PHI access tracking requirements - Appointments: See
docs/appointments.mdfor scheduling API usage and safeguards
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests for new functionality
- Ensure all tests pass
- Submit a pull request
- Follow PEP 8 style guidelines
- Write comprehensive docstrings
- Include tests for new features
- Update documentation as needed
- Use meaningful commit messages
This project is licensed under the MIT License - see the LICENSE file for details.
Issue: docker-compose build fails with errors
Solution:
# Clean up Docker cache and rebuild
docker-compose down
docker system prune -f
docker-compose build --no-cache
# Check Docker logs for specific errors
docker-compose logs webIssue: Port is already allocated or Address already in use
Solution:
# Check what's using the port
sudo netstat -tulpn | grep :8000
# Stop conflicting services or change ports in docker-compose.yml
docker-compose down
# Edit docker-compose.yml to use different ports
docker-compose up -dIssue: django.db.utils.OperationalError: could not connect to server
Solution:
# Ensure database service is running
docker-compose ps
# Check database logs
docker-compose logs db
# Restart database service
docker-compose restart db
# Wait for database to be ready, then run migrations
sleep 10
docker-compose exec web python manage.py migrateIssue: redis.exceptions.ConnectionError in Docker
Solution:
# Check Redis service status
docker-compose ps redis
# Restart Redis service
docker-compose restart redis
# Check Redis logs
docker-compose logs redisIssue: Permission denied errors in Docker containers
Solution:
# Fix file permissions
sudo chown -R $USER:$USER .
# Rebuild containers
docker-compose down
docker-compose build --no-cache
docker-compose up -dIssue: Docker containers running out of memory Solution:
# Check Docker resource usage
docker stats
# Increase Docker memory limit in Docker Desktop settings
# Or add memory limits to docker-compose.ymlIssue: Changes not reflecting in containers Solution:
# Restart services to pick up volume changes
docker-compose restart web
# Or rebuild if needed
docker-compose down
docker-compose up -d --build# Start all services
docker-compose up -d
# Stop all services
docker-compose down
# View logs
docker-compose logs web
docker-compose logs db
docker-compose logs redis
# Execute commands in containers
docker-compose exec web python manage.py migrate
docker-compose exec web python manage.py createsuperuser
docker-compose exec web python manage.py shell
# Check service status
docker-compose ps
# Rebuild specific service
docker-compose build web
docker-compose up -d web
# Clean up everything
docker-compose down -v
docker system prune -fIssue: ModuleNotFoundError or package installation failures
Solution: Ensure you're using Python 3.11+ and have created a virtual environment
python3 --version # Should be 3.11+
python3 -m venv venv
source venv/bin/activateIssue: django.db.utils.OperationalError or migration failures
Solution: Ensure the database is properly configured and migrations are up to date
python manage.py showmigrations
python manage.py migrateIssue: ModuleNotFoundError for specific packages
Solution: Reinstall dependencies and ensure all requirements are met
pip install --upgrade pip
pip install -r requirements.txtIssue: Address already in use error when starting the server
Solution: Use a different port or kill the existing process
python manage.py runserver 8001
# OR
pkill -f "python manage.py runserver"Issue: staticfiles.W004 warning about missing static directory
Solution: Create the static directory (this is normal for development)
mkdir -p staticIssue: redis.exceptions.ConnectionError: Error 111 connecting to localhost:6379
Solution: Redis is not required for basic development. The health check has been configured to skip Redis checks in development mode. If you need Redis for production features, install and start it:
# Ubuntu/Debian
sudo apt install redis-server
sudo systemctl start redis-server
# macOS (with Homebrew)
brew install redis
brew services start redisIssue: Configuration errors or missing environment variables
Solution: Ensure .env file exists and contains required variables
cp env.example .env
# Edit .env if needed for your environment- Use the development settings: The project uses
config.settings.developmentby default - Check logs: Look at the console output for detailed error messages
- Database: SQLite is used by default for development (no setup required)
- API Testing: Use the built-in API documentation at
/api/docs/ - Admin Interface: Access at
/admin/after creating a superuser
- Documentation: Check the
/docs/directory - Issues: Report bugs via GitHub Issues
- Discussions: Use GitHub Discussions for questions
- Email: Contact the development team
- β Core backend infrastructure
- β Patient management system
- β Health worker management
- β Facility management
- β Basic API endpoints
- π Advanced analytics dashboard
- π Mobile API optimization
- π Integration with external systems
- π Advanced reporting features
- π AI-powered health insights
- π Telemedicine integration
- π Advanced data visualization
- π Multi-language support
- Django community for the excellent framework
- Healthcare professionals for domain expertise
- Open source contributors for various packages
- African healthcare workers for inspiration
OpenCare-Africa - Empowering healthcare in Africa through technology.