# naughtycomputer/naughty-be-django architecture

> Architecture diagram of the GitHub repository naughtycomputer/naughty-be-django, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-05-30). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/naughtycomputer/naughty-be-django
- Repository: https://github.com/naughtycomputer/naughty-be-django

## Overview

##### Project type and purpose
- From the file tree and README, this is a Django-based backend service (“Naughty-BE-Djaongo”) for a 2025 graduation project.
- Its primary function is to power a chat system with GPT integration and Retrieval-Augmented Generation (RAG) modules for financial/annuity information.

##### File structure
- Top-level:
- Pipfile / Pipfile.lock + requirements.txt – Python dependencies
- .github/workflows/django-ci.yml – CI pipeline for testing
- naughtyDjango/manage.py – Django CLI entrypoint
- Project package “naughtyDjango/” contains two main parts:
a) chat app under “naughtyDjango/chat/”
– models.py, views.py, urls.py, admin.py, tests.py
– gpt_service.py – interfaces with OpenAI or another LLM
– rag/ folder with annuity_rag.py and financial_product_rag.py
b) core Django config under “naughtyDjango/naughtyDjango/”
– settings.py, urls.py, wsgi.py, asgi.py
– db_routers.py – multi-DB routing logic?
– utils/ (custom exception handler, response wrapper)
– constants/ (error and success codes)
- Migrations folder for database schema.

##### Key insights from README
- Very brief, confirming it’s strictly the Django backend layer.
- No explicit mention of frontend or additional microservices.

##### Main components
a. API Layer (Django REST endpoints)
– HTTP(S) Ingress → django.urls routing → chat.views
b. Business Logic Layer
– ChatView / ChatService
– gpt_service.py – orchestrates prompt construction and calls to external LLM API
– RAG modules (annuity_rag.py, financial_product_rag.py) – retrieval + augmentation
c. Data Access Layer
– Django ORM models in chat/models.py and core models.py
– Database(s) behind db_routers.py (could be one or more Postgres/MySQL instances)
d. Utilities
– custom_exception_handler & custom_response for uniform error/success wrappers
– constants for standardizing response codes
e. External Services
– OpenAI API (or another GPT provider)
– Potential document store for RAG (if referenced) or internal tables
f. CI Pipeline
– .github/workflows/django-ci.yml runs tests and linting on push/PR

##### Relationships & interactions
- Client → Django API endpoints
- Endpoint handler → invokes business logic (views → gpt_service + RAG)
- Business logic → reads/writes from Database via ORM
- Business logic → external GPT API over HTTPS
- custom_exception_handler applied globally to catch and format errors
- db_routers route certain models to specific databases if configured

##### Architectural patterns & principles
- Layered architecture: Presentation (URLs/Views), Service (GPT/RAG), Data (ORM)
- Separation of concerns: RAG modules decoupled from core chat logic
- Config-driven DB routing for sharding/multi-tenant if needed
- Utility modules for cross-cutting concerns (errors, responses)

##### Final recommendations
- Detail external dependencies (GPT API, CI pipeline, DB instances).

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% API Layer
    subgraph "API Layer"
        direction TB
        API_Project_URLs["Project URLs<br/>(naughtyDjango/naughtyDjango/urls.py)"]:::api
        API_Chat_URLs["Chat App URLs<br/>(naughtyDjango/chat/urls.py)"]:::api
        API_Chat_Views["Chat Views<br/>(naughtyDjango/chat/views.py)"]:::api
    end

    %% Business Logic Layer
    subgraph "Business Logic Layer"
        direction TB
        Service_GPT["GPT Service<br/>(gpt_service.py)"]:::service
        RAG_Annuity["Annuity RAG<br/>(rag/annuity_rag.py)"]:::service
        RAG_Financial["Financial Product RAG<br/>(rag/financial_product_rag.py)"]:::service
    end

    %% Data Access Layer
    subgraph "Data Access Layer"
        direction TB
        Data_Chat_Models["Chat Models<br/>(chat/models.py)"]:::data
        Data_Core_Models["Core Models<br/>(naughtyDjango/models.py)"]:::data
        Data_DB_Routers["DB Routers<br/>(db_routers.py)"]:::data
        Data_Migrations["Migrations<br/>(chat/migrations/)"]:::data
        DB[(Databases)]:::data
    end

    %% Utilities
    subgraph "Utilities"
        direction TB
        Util_Exception["Exception Handler<br/>(utils/custom_exception_handler.py)"]:::utils
        Util_Response["Response Wrapper<br/>(utils/custom_response.py)"]:::utils
        Util_ErrorCodes["Error Codes<br/>(constants/error_codes.py)"]:::utils
        Util_SuccessCodes["Success Codes<br/>(constants/success_codes.py)"]:::utils
    end

    %% Core Config
    subgraph "Core Config"
        direction TB
        Core_Settings["Settings<br/>(settings.py)"]:::core
        Core_ASGI["ASGI<br/>(asgi.py)"]:::core
        Core_WSGI["WSGI<br/>(wsgi.py)"]:::core
        Core_Manage["Manage CLI<br/>(manage.py)"]:::core
    end

    %% External Services
    subgraph "External Services"
        direction TB
        External_GPT["OpenAI GPT API"]:::external
    end

    %% CI Pipeline
    subgraph "CI Pipeline"
        direction TB
        CI_Workflow["GitHub Actions CI<br/>(.github/workflows/django-ci.yml)"]:::ci
    end

    %% Client
    Client["Client"]:::external

    %% Connections
    Client -->|HTTPS| API_Project_URLs
    API_Project_URLs --> API_Chat_URLs
    API_Chat_URLs --> API_Chat_Views
    API_Chat_Views -->|calls| Service_GPT
    API_Chat_Views -->|calls| RAG_Annuity
    API_Chat_Views -->|calls| RAG_Financial
    API_Chat_Views -->|handles ORM| Data_Chat_Models
    API_Chat_Views -->|uses| Util_Exception
    API_Chat_Views -->|wraps via| Util_Response

    Service_GPT -->|HTTP| External_GPT
    Service_GPT -->|reads/writes| Data_Chat_Models
    RAG_Annuity -->|reads| Data_Chat_Models
    RAG_Financial -->|reads| Data_Chat_Models

    Data_Chat_Models --> Data_DB_Routers
    Data_Core_Models --> Data_DB_Routers
    Data_DB_Routers --> DB
    Data_Migrations --> DB

    Core_Settings --> Data_DB_Routers
    Core_ASGI --> API_Project_URLs
    Core_WSGI --> API_Project_URLs
    Core_Manage --> API_Project_URLs

    CI_Workflow --> Core_Settings

    %% Click Events
    click API_Project_URLs "https://github.com/naughtycomputer/naughty-be-django/blob/main/naughtyDjango/naughtyDjango/urls.py"
    click API_Chat_URLs "https://github.com/naughtycomputer/naughty-be-django/blob/main/naughtyDjango/chat/urls.py"
    click API_Chat_Views "https://github.com/naughtycomputer/naughty-be-django/blob/main/naughtyDjango/chat/views.py"
    click Service_GPT "https://github.com/naughtycomputer/naughty-be-django/blob/main/naughtyDjango/chat/gpt_service.py"
    click RAG_Annuity "https://github.com/naughtycomputer/naughty-be-django/blob/main/naughtyDjango/chat/rag/annuity_rag.py"
    click RAG_Financial "https://github.com/naughtycomputer/naughty-be-django/blob/main/naughtyDjango/chat/rag/financial_product_rag.py"
    click Data_Chat_Models "https://github.com/naughtycomputer/naughty-be-django/blob/main/naughtyDjango/chat/models.py"
    click Data_Core_Models "https://github.com/naughtycomputer/naughty-be-django/blob/main/naughtyDjango/naughtyDjango/models.py"
    click Data_DB_Routers "https://github.com/naughtycomputer/naughty-be-django/blob/main/naughtyDjango/naughtyDjango/db_routers.py"
    click Data_Migrations "https://github.com/naughtycomputer/naughty-be-django/tree/main/naughtyDjango/chat/migrations/"
    click Util_Exception "https://github.com/naughtycomputer/naughty-be-django/blob/main/naughtyDjango/naughtyDjango/utils/custom_exception_handler.py"
    click Util_Response "https://github.com/naughtycomputer/naughty-be-django/blob/main/naughtyDjango/naughtyDjango/utils/custom_response.py"
    click Util_ErrorCodes "https://github.com/naughtycomputer/naughty-be-django/blob/main/naughtyDjango/naughtyDjango/constants/error_codes.py"
    click Util_SuccessCodes "https://github.com/naughtycomputer/naughty-be-django/blob/main/naughtyDjango/naughtyDjango/constants/success_codes.py"
    click Core_Settings "https://github.com/naughtycomputer/naughty-be-django/blob/main/naughtyDjango/naughtyDjango/settings.py"
    click Core_ASGI "https://github.com/naughtycomputer/naughty-be-django/blob/main/naughtyDjango/naughtyDjango/asgi.py"
    click Core_WSGI "https://github.com/naughtycomputer/naughty-be-django/blob/main/naughtyDjango/naughtyDjango/wsgi.py"
    click Core_Manage "https://github.com/naughtycomputer/naughty-be-django/blob/main/naughtyDjango/manage.py"
    click CI_Workflow "https://github.com/naughtycomputer/naughty-be-django/blob/main/.github/workflows/django-ci.yml"

    %% Styles
    classDef api fill:#cce5ff,stroke:#004085,color:#004085;
    classDef service fill:#d4edda,stroke:#155724,color:#155724;
    classDef data fill:#fff3cd,stroke:#856404,color:#856404;
    classDef utils fill:#e2e3e5,stroke:#383d41,color:#383d41;
    classDef core fill:#f8f9fa,stroke:#6c757d,color:#6c757d;
    classDef external fill:#f5c6cb,stroke:#721c24,color:#721c24;
    classDef ci fill:#ffeeba,stroke:#856404,color:#856404;
```
