# reza-ns/blog-api-drf architecture

> Architecture diagram of the GitHub repository reza-ns/blog-api-drf, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-04-08). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/reza-ns/blog-api-drf
- Repository: https://github.com/reza-ns/blog-api-drf

## Overview

The Blog API project is a backend service built with Django and Django Rest Framework (DRF) that provides a blog platform inspired by Medium.com. It handles both free and paid articles, user authentication (via phone/OTP or email/password), user profiles, subscriptions (with various plans and expiration logic using Celery), and payment processing (integrated with the Zarinpal gateway).

##### Main Components
- Authentication & Accounts Module:
- Contains user management logic, custom authentication (JWT, OTP), and user profile management.
- Key files: Blog-API-drf/accounts/ (views, serializers, models, utils for JWT, OTP, and Redis integrations).
- Blog Module:
- Handles article creation (both free and paid), comments, and other blog-related features.
- Key files: Blog-API-drf/blog/ (APIs, models, views, serializers, and tests).
- Payment Module:
- Manages subscription payments using the Zarinpal gateway.
- Key files: Blog-API-drf/payment/ (APIs, models, signals, and utility integration with zarinpal).
- Subscription Module:
- Manages subscription plans, creation (leveraging Django signals), and expiration logic (using Celery tasks).
- Key files: Blog-API-drf/subscription/ (models, signals/receivers, tasks, views, serializers).
- Configuration & Application Bootstrapping:
- Bootstrapping code for Django (wsgi/asgi), settings, URL routing, and Celery integration.
- Key files: Blog-API-drf/config/ (asgi.py, wsgi.py, settings.py, urls.py) and manage.py.
- Infrastructure & Deployment:
- Docker configuration for containerized deployment.
- Key file: docker/docker-compose.yml.
- External Services and Database:
- Postgres is used for the database, while Celery and Redis are used for asynchronous tasks and caching respectively.

##### Out Relationships and Interactions
- That the API endpoints (in accounts/api, blog/api, payment/api, and subscription/api) interact with their respective models and serializers to handle client requests.
- From client/API gateway → Django controllers (views), then to business logic & data layer (models).
- Between modules: for example, the accounts module interconnects with blog (to link authors to articles) and with subscription/payment (to validate user subscriptions for accessing paid content).
- Decoupled components with Django signals (e.g., creating subscriptions when certain events occur) and Celery tasks handling background processes (subscription expiration).

##### Architectural Patterns and Design Principles
- MVC/MVT Framework: Emphasize the Django Model-View-Template (or DRF’s serializer-viewset approach) that structures the project.
- Modularization: Each functionality (accounts, blog, payment, subscription) exists as separate Django apps, promoting modular design.
- Asynchronous Processing: Use of Celery for background tasks (subscription management).
- Dependency Management: Integration with external payment provider (Zarinpal) and caching mechanisms (via Redis).

##### Details on Technologies and Frameworks
- Python, Django, and DRF drive the backend logic.
- Celery (with Django integration via Blog-API-drf/config/celery.py) handles asynchronous execution.
- Postgres serves as the relational database.
- Docker ensures containerized deployment.
- External integrations such as Zarinpal (for payments) and potential use of Redis in authentication.

##### Elements
- Labels:
- Tag each component (e.g., “Accounts Service”, “Blog Service”, “Payment Service”, “Subscription Service”, “Configuration”, “External Services”).
- Interactions between modules (e.g., Accounts → Blog for user content association, Payment → Subscription for plan activation).
- Visual Distinction:
- Notes/Annotations:
- Briefly label communication protocols (such as HTTP/HTTPS for API calls, or asynchronous messaging for Celery tasks).

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Global Entities
    Client("Client"):::external

    %% Configuration & Application Bootstrapping
    subgraph "Configuration & Bootstrapping"
        config_main["Configuration (URLs, Settings, WSGI, ASGI)"]:::config
        config_asgi["asgi.py"]:::config
        config_celery["celery.py"]:::config
        config_settings["settings.py"]:::config
        config_urls["urls.py"]:::config
        config_wsgi["wsgi.py"]:::config
        manage_py["manage.py"]:::config
    end

    %% Backend Services
    subgraph "Backend Services"
        subgraph "Accounts Service"
            accounts_main["Accounts Service"]:::accounts
            accounts_api["accounts/api"]:::accounts
            accounts_jwt["JWT Util"]:::accounts
            accounts_otp["OTP Util"]:::accounts
            accounts_redis["Redis Util"]:::accounts
        end

        subgraph "Blog Service"
            blog_main["Blog Service"]:::blog
            blog_api["blog/api"]:::blog
            blog_models["blog/models.py"]:::blog
            blog_tests["blog/tests"]:::blog
        end

        subgraph "Payment Service"
            payment_main["Payment Service"]:::payment
            payment_api["payment/api"]:::payment
            payment_models["payment/models.py"]:::payment
            payment_signals["payment/signals.py"]:::payment
            payment_zarinpal["Zarinpal Util"]:::payment
        end

        subgraph "Subscription Service"
            subscription_main["Subscription Service"]:::subscription
            subscription_api["subscription/api"]:::subscription
            subscription_models["subscription/models.py"]:::subscription
            subscription_receivers["subscription/receivers.py"]:::subscription
            subscription_tasks["subscription/tasks.py"]:::subscription
        end
    end

    %% External Services & Infrastructure
    postgres("Postgres DB"):::postgres
    celery("Celery Worker"):::celery
    redis("Redis Cache"):::redis
    zarinpal("Zarinpal Gateway"):::zarinpal
    docker("Docker Deployment"):::docker

    %% Connections - Client and Configuration
    Client -->|"HTTP/HTTPS"| config_main
    docker -->|"Deployment"| config_main

    %% Configuration to Backend Services
    config_main -->|"API Calls"| accounts_main
    config_main -->|"API Calls"| blog_main
    config_main -->|"API Calls"| payment_main
    config_main -->|"API Calls"| subscription_main

    %% Internal Module Details (vertical grouping)
    accounts_main --> accounts_api
    accounts_main --> accounts_jwt
    accounts_main --> accounts_otp
    accounts_main --> accounts_redis

    blog_main --> blog_api
    blog_main --> blog_models
    blog_main --> blog_tests

    payment_main --> payment_api
    payment_main --> payment_models
    payment_main --> payment_signals
    payment_main --> payment_zarinpal

    subscription_main --> subscription_api
    subscription_main --> subscription_models
    subscription_main --> subscription_receivers
    subscription_main --> subscription_tasks

    %% Cross-Module Interactions
    accounts_main -->|"author_association"| blog_main
    payment_main -->|"activate_subscription"| subscription_main
    payment_main -->|"process_payment"| zarinpal
    subscription_main -->|"schedule_expiration"| celery

    %% Database Connections
    config_main -->|"DB_config"| postgres
    accounts_main -->|"CRUD"| postgres
    blog_main -->|"CRUD"| postgres
    payment_main -->|"CRUD"| postgres
    subscription_main -->|"CRUD"| postgres

    %% Redis Usage in Accounts
    accounts_main -->|"cache"| redis

    %% Styles
    classDef accounts fill:#ffcccc,stroke:#cc0000,stroke-width:2px;
    classDef blog fill:#ccffcc,stroke:#339933,stroke-width:2px;
    classDef payment fill:#ccccff,stroke:#3333cc,stroke-width:2px;
    classDef subscription fill:#ffffcc,stroke:#cccc00,stroke-width:2px;
    classDef config fill:#e6e6e6,stroke:#666666,stroke-width:2px;
    classDef external fill:#ffcc99,stroke:#ff9900,stroke-width:2px;
    classDef docker fill:#d9d9d9,stroke:#808080,stroke-dasharray:5,5;
    classDef celery fill:#99ccff,stroke:#3399ff,stroke-dasharray:3,3;
    classDef postgres fill:#f2d7d5,stroke:#c0392b,stroke-width:2px;
    classDef redis fill:#f9e79f,stroke:#f1c40f,stroke-width:2px;
    classDef zarinpal fill:#f6ddcc,stroke:#d35400,stroke-width:2px;

    %% Click Events for Configuration & Bootstrapping
    click config_main "https://github.com/reza-ns/blog-api-drf/tree/main/Blog-API-drf/config"
    click config_asgi "https://github.com/reza-ns/blog-api-drf/blob/main/Blog-API-drf/config/asgi.py"
    click config_celery "https://github.com/reza-ns/blog-api-drf/blob/main/Blog-API-drf/config/celery.py"
    click config_settings "https://github.com/reza-ns/blog-api-drf/blob/main/Blog-API-drf/config/settings.py"
    click config_urls "https://github.com/reza-ns/blog-api-drf/blob/main/Blog-API-drf/config/urls.py"
    click config_wsgi "https://github.com/reza-ns/blog-api-drf/blob/main/Blog-API-drf/config/wsgi.py"
    click manage_py "https://github.com/reza-ns/blog-api-drf/blob/main/Blog-API-drf/manage.py"

    %% Click Events for Accounts Module
    click accounts_main "https://github.com/reza-ns/blog-api-drf/tree/main/Blog-API-drf/accounts"
    click accounts_api "https://github.com/reza-ns/blog-api-drf/tree/main/Blog-API-drf/accounts/api"
    click accounts_jwt "https://github.com/reza-ns/blog-api-drf/blob/main/Blog-API-drf/accounts/utils/JWT.py"
    click accounts_otp "https://github.com/reza-ns/blog-api-drf/blob/main/Blog-API-drf/accounts/utils/OTP.py"
    click accounts_redis "https://github.com/reza-ns/blog-api-drf/blob/main/Blog-API-drf/accounts/utils/Redis.py"

    %% Click Events for Blog Module
    click blog_main "https://github.com/reza-ns/blog-api-drf/tree/main/Blog-API-drf/blog"
    click blog_api "https://github.com/reza-ns/blog-api-drf/tree/main/Blog-API-drf/blog/api"
    click blog_models "https://github.com/reza-ns/blog-api-drf/blob/main/Blog-API-drf/blog/models.py"
    click blog_tests "https://github.com/reza-ns/blog-api-drf/tree/main/Blog-API-drf/blog/tests"

    %% Click Events for Payment Module
    click payment_main "https://github.com/reza-ns/blog-api-drf/tree/main/Blog-API-drf/payment"
    click payment_api "https://github.com/reza-ns/blog-api-drf/tree/main/Blog-API-drf/payment/api"
    click payment_models "https://github.com/reza-ns/blog-api-drf/blob/main/Blog-API-drf/payment/models.py"
    click payment_signals "https://github.com/reza-ns/blog-api-drf/blob/main/Blog-API-drf/payment/signals.py"
    click payment_zarinpal "https://github.com/reza-ns/blog-api-drf/blob/main/Blog-API-drf/payment/utils/zarinpal.py"

    %% Click Events for Subscription Module
    click subscription_main "https://github.com/reza-ns/blog-api-drf/tree/main/Blog-API-drf/subscription"
    click subscription_api "https://github.com/reza-ns/blog-api-drf/tree/main/Blog-API-drf/subscription/api"
    click subscription_models "https://github.com/reza-ns/blog-api-drf/blob/main/Blog-API-drf/subscription/models.py"
    click subscription_receivers "https://github.com/reza-ns/blog-api-drf/blob/main/Blog-API-drf/subscription/receivers.py"
    click subscription_tasks "https://github.com/reza-ns/blog-api-drf/blob/main/Blog-API-drf/subscription/tasks.py"

    %% Click Event for Infrastructure
    click docker "https://github.com/reza-ns/blog-api-drf/blob/main/docker/docker-compose.yml"
```
