Skip to content

CyberAware-App/backend-django

Repository files navigation

πŸŽ“ Cyber Security Learning Platform - Backend API

A comprehensive Django REST Framework backend for a cyber security learning platform with JWT authentication, OTP verification, interactive modules, quizzes, and certificate generation.

πŸš€ Features

πŸ” Authentication & Security

  • 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

πŸ“š Learning Management

  • 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

πŸ† Certificate System

  • Automatic Certificate Generation upon passing final quiz
  • PDF Certificate Download with professional formatting
  • Certificate Validation and verification
  • Unique Certificate IDs with timestamp

πŸ§ͺ Testing & Quality

  • Comprehensive Test Suite with pytest
  • Authentication Tests covering all auth flows
  • API Endpoint Tests for all views
  • Error Handling Tests with proper validation

πŸ› οΈ Technology Stack

  • 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)

πŸ“‹ Prerequisites

  • Python 3.10+
  • PostgreSQL
  • SendGrid account (for email functionality)

πŸš€ Installation

  1. Clone the repository

    git clone <repository-url>
    cd backend-django
  2. Create virtual environment

    python -m venv .cyber-backend
    source .cyber-backend/bin/activate  # On Windows: .cyber-backend\Scripts\activate
  3. Install dependencies

    pip install -r requirements.txt
  4. Environment Setup Create a .env file 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
  5. Database Setup

    python manage.py makemigrations
    python manage.py migrate
  6. Create superuser

    python manage.py createsuperuser
  7. Run the server

    python manage.py runserver

πŸ“š API Documentation

Base URL

http://localhost:8000/api/

Authentication Endpoints

πŸ” User Registration

POST /api/register/
Content-Type: application/json

{
    "email": "user@example.com",
    "password": "securepassword123",
    "first_name": "John",
    "last_name": "Doe"
}

πŸ“§ OTP Verification (Auto-login)

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
    }
}

πŸ”‘ User Login

POST /api/login/
Content-Type: application/json

{
    "email": "user@example.com",
    "password": "securepassword123"
}

πŸ”„ Token Refresh

POST /api/token/refresh/
Content-Type: application/json

{
    "refresh": "your-refresh-token"
}

Learning Management Endpoints

πŸ“Š Dashboard

GET /api/dashboard/
Authorization: Bearer <access_token>

πŸ“– Get Module

GET /api/modules/{module_id}/
Authorization: Bearer <access_token>

βœ… Mark Module as Completed

POST /api/modules/{module_id}/complete/
Authorization: Bearer <access_token>

πŸ“ˆ User Progress

GET /api/module-progress/
Authorization: Bearer <access_token>

Quiz Endpoints

🎯 Get Final Quiz

GET /api/quiz/
Authorization: Bearer <access_token>

πŸ“ Submit Final Quiz

POST /api/quiz/
Authorization: Bearer <access_token>
Content-Type: application/json

[
    {
        "question": "What is cybersecurity?",
        "selected_option": "Protection of digital systems"
    }
]

Certificate Endpoints

πŸ† Get User Certificate

GET /api/certificates/
Authorization: Bearer <access_token>

πŸ“„ Download Certificate PDF

GET /api/certificates/{certificate_id}/download/
Authorization: Bearer <access_token>

πŸ§ͺ Testing

Run All Tests

pytest

Run Specific Test Categories

# 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

Test Structure

  • 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

πŸ”§ Configuration

JWT Settings

# settings.py
SIMPLE_JWT = {
    'ACCESS_TOKEN_LIFETIME': timedelta(minutes=60),
    'REFRESH_TOKEN_LIFETIME': timedelta(days=1),
    'ROTATE_REFRESH_TOKENS': True,
}

πŸ“ Project Structure

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

πŸ”’ Security Features

  • 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

πŸ“§ Email Integration

The platform uses SendGrid for:

  • Registration OTP emails
  • Password Reset OTP emails
  • Certificate Notifications (future feature)

πŸ† Certificate System

Features

  • 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

Certificate Data

  • User name and email
  • Score and pass status
  • Issue date
  • Unique certificate ID
  • Professional formatting

πŸš€ Deployment

Production Checklist

  • 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

Docker Support (Future)

# 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"]

🀝 Contributing

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

πŸ“ License

This project is licensed under the MIT License - see the LICENSE file for details.

πŸ†˜ Support

For support and questions:

  • Create an issue in the repository
  • Contact the development team
  • Check the API documentation at /api/schema/

πŸ”„ API Versioning

The API follows semantic versioning. Current version: v1

πŸ“Š Performance

  • 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

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 2

  •  
  •