A comprehensive Django REST Framework backend for a cyber security learning platform with JWT authentication, OTP verification, interactive modules, quizzes, and certificate generation.
- JWT Authentication with access and refresh tokens
- OTP Verification for user registration and password reset
- Auto-login after successful OTP verification
- Password Management (change, reset, forgot password)
- User Profile Management with verification status
- Interactive Modules with content and progress tracking
- Module Quizzes for knowledge assessment
- Final Comprehensive Quiz with scoring system
- Progress Tracking with completion percentages
- User Module Progress monitoring
- Automatic Certificate Generation upon passing final quiz
- PDF Certificate Download with professional formatting
- Certificate Validation and verification
- Unique Certificate IDs with timestamp
- Comprehensive Test Suite with pytest
- Authentication Tests covering all auth flows
- API Endpoint Tests for all views
- Error Handling Tests with proper validation
- Framework: Django 5.2.4
- API: Django REST Framework 3.16.0
- Authentication: JWT (djangorestframework-simplejwt 5.5.0)
- Database: PostgreSQL
- Email: SendGrid
- PDF Generation: ReportLab
- Testing: pytest & pytest-django
- Documentation: drf-spectacular (OpenAPI/Swagger)
- Python 3.10+
- PostgreSQL
- SendGrid account (for email functionality)
-
Clone the repository
git clone <repository-url> cd backend-django
-
Create virtual environment
python -m venv .cyber-backend source .cyber-backend/bin/activate # On Windows: .cyber-backend\Scripts\activate
-
Install dependencies
pip install -r requirements.txt
-
Environment Setup Create a
.envfile in the root directory:SECRET_KEY=your-secret-key DEBUG=True DATABASE_URL=postgresql://user:password@localhost:5432/dbname SENDGRID_API_KEY=your-sendgrid-api-key SENDGRID_FROM_EMAIL=your-email@domain.com
-
Database Setup
python manage.py makemigrations python manage.py migrate
-
Create superuser
python manage.py createsuperuser
-
Run the server
python manage.py runserver
http://localhost:8000/api/
POST /api/register/
Content-Type: application/json
{
"email": "user@example.com",
"password": "securepassword123",
"first_name": "John",
"last_name": "Doe"
}POST /api/verify-otp/
Content-Type: application/json
{
"email": "user@example.com",
"code": "123456"
}Response includes JWT tokens for automatic login:
{
"status": "success",
"message": "OTP verified successfully. You are now logged in!",
"data": {
"email": "user@example.com",
"first_name": "John",
"verified": true,
"access": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9...",
"refresh": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9...",
"first_login": true
}
}POST /api/login/
Content-Type: application/json
{
"email": "user@example.com",
"password": "securepassword123"
}POST /api/token/refresh/
Content-Type: application/json
{
"refresh": "your-refresh-token"
}GET /api/dashboard/
Authorization: Bearer <access_token>GET /api/modules/{module_id}/
Authorization: Bearer <access_token>POST /api/modules/{module_id}/complete/
Authorization: Bearer <access_token>GET /api/module-progress/
Authorization: Bearer <access_token>GET /api/quiz/
Authorization: Bearer <access_token>POST /api/quiz/
Authorization: Bearer <access_token>
Content-Type: application/json
[
{
"question": "What is cybersecurity?",
"selected_option": "Protection of digital systems"
}
]GET /api/certificates/
Authorization: Bearer <access_token>GET /api/certificates/{certificate_id}/download/
Authorization: Bearer <access_token>pytest# Authentication tests
pytest app/tests/test_auth.py -v
# API endpoint tests
pytest app/tests/test_views.py -v
# Run with coverage
pytest --cov=app --cov-report=html- Authentication Tests: Registration, OTP verification, login, password management
- API Tests: All endpoints with proper authentication and error handling
- Integration Tests: Full user flows from registration to certificate generation
# settings.py
SIMPLE_JWT = {
'ACCESS_TOKEN_LIFETIME': timedelta(minutes=60),
'REFRESH_TOKEN_LIFETIME': timedelta(days=1),
'ROTATE_REFRESH_TOKENS': True,
}backend-django/
βββ app/
β βββ models.py # Database models
β βββ views.py # API views and endpoints
β βββ serializers.py # Data serialization
β βββ admin.py # Django admin configuration
β βββ tests/
β βββ test_auth.py # Authentication tests
β βββ test_views.py # API endpoint tests
βββ core/
β βββ settings.py # Django settings
β βββ urls.py # Main URL configuration
β βββ wsgi.py # WSGI configuration
βββ users/ # Custom user app
βββ utils/
β βββ response.py # Response mixin for consistent API responses
β βββ email.py # Email utilities
β βββ certificate_generator.py # PDF certificate generation
βββ requirements.txt # Python dependencies
βββ manage.py # Django management script
βββ README.md # This file
- JWT Authentication with secure token handling
- OTP Verification for account security
- Password Validation with Django's built-in validators
- CORS Configuration for frontend integration
- Rate Limiting on sensitive endpoints
- Input Validation and sanitization
The platform uses SendGrid for:
- Registration OTP emails
- Password Reset OTP emails
- Certificate Notifications (future feature)
- Automatic Generation when user passes final quiz (80%+ score)
- Professional PDF Format with ReportLab
- Unique Certificate IDs with timestamp
- Download Functionality with proper headers
- Certificate Validation and verification
- User name and email
- Score and pass status
- Issue date
- Unique certificate ID
- Professional formatting
- Set
DEBUG=False - Configure production database
- Set up proper CORS settings
- Configure static files
- Set up SSL/HTTPS
- Configure email settings
- Set up monitoring and logging
# Dockerfile example for containerization
FROM python:3.10-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install -r requirements.txt
COPY . .
EXPOSE 8000
CMD ["python", "manage.py", "runserver", "0.0.0.0:8000"]- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.
For support and questions:
- Create an issue in the repository
- Contact the development team
- Check the API documentation at
/api/schema/
The API follows semantic versioning. Current version: v1
- Database Optimization with select_related and prefetch_related
- Bulk Operations for quiz answer processing
- Efficient Token Handling with proper refresh mechanisms
- Caching Ready for future optimization
Built with β€οΈ using Django REST Framework