How med-mostapha/gestion-de-stock-intelligent-django works
What this project is (type + purpose)
This repository is a backend-only Django REST API (“Gestion de Stock Intelligent API”) for inventory management with:
• Authentication (Token and/or JWT per README)
• User-isolated data model: Users own Categories; Categories contain Products
• Business endpoints: CRUD for categories/products + computed endpoints for alerts (low stock, expired) and a dashboard inventory summary.
What the file tree says about architecture
Top-level structure:
• config/ = Django project (global settings/urls/asgi/wsgi)
• settings.py: environment, installed apps, middleware, DB, auth configuration
• urls.py: global routing entrypoint (likely mounts /api/)
• asgi.py/wsgi.py: deployment entrypoints
• accounts/ = single Django app containing almost all domain logic
• models.py: domain entities (Category, Product, and likely User usage)
• serializers.py: DRF serialization + validation + computed fields
• views.py: DRF views/viewsets + custom endpoints (alerts, dashboard, auth)
• urls.py: app-level routes (register/login/categories/products/alerts/dashboard)
• admin.py: admin registration (not runtime critical, but useful for ops/admin)
• migrations/: schema evolution proving the DB-backed nature
• manage.py: local management entrypoint
• .env.example: environment variables (DB creds, secret key, JWT settings, etc.)
• requirements.txt: dependency set (Django, djangorestframework, auth libs, etc.)
Architecturally, this is a monolithic Django service with a clear DRF layering:
URLs → Views → Serializers → Models → Database
and cross-cutting concerns:
Authentication/Permissions + Queryset filtering for tenant isolation.
What the README adds (runtime responsibilities)
The README is very explicit about:
• Tenant isolation rule: queries filtered by category__owner=request.user
• Endpoints:
• /api/register/, /api/login/
• /api/categories/ (CRUD)
• /api/products/ (CRUD)
• /api/products/alerts/ (computed)
• /api/dashboard/ (aggregated analytics)
• Alert logic:
• low stock: quantity <= min_threshold (DB-level via F() expressions)
• expired: expiration_date <= today with timezone awareness
• Core invariants (user scoping / ownership enforcement)
• Data flows from client → API endpoints → DB queries/aggregations → response.
Context (C4 L1): who/what talks to the system
3) Component (C4 L3): Django internals (routing, views, serializers, models) and key endpoints.
This project is small enough that showing all three keeps it “detailed” without becoming noisy.
Main components
• Client(s): “Web/Mobile Frontend Dashboard” (external)
• Gestion de Stock API (Django/DRF): this repo
Relationships
• Client → API over HTTPS/JSON REST
• API → DB via Django ORM
Important notes to annotate
• Auth header modes: Authorization: Token … or Bearer …
• “User-isolated data access enforced server-side”
API Server
• Django + Django REST Framework
• Runs via gunicorn (WSGI) or ASGI server (if used later); repo includes both entrypoints.
• Exposes /api/*
Database
• Persistent relational store holding auth_user, Category, Product tables (plus migrations)
• Reverse proxy (Nginx) in front of API (common production)
• Email provider (future “Email alerts”)
• Scheduler/Worker (Celery/cron) if alerts become push-based (currently alerts are computed on GET)
Interactions
• Client → Reverse Proxy → Django API
• Django API ↔ DB (read/write for CRUD; read/aggregate for alerts/dashboard)
Key constraints to label
• Stateless API (token/JWT auth)
• Multi-tenant-by-filtering (per-request scoping)
Routing
• config/urls.py (project router)
• mounts accounts/urls.py under /api/ (likely)
• accounts/urls.py
• maps to views: register, login, categories, products, alerts, dashboard
API Layer
• accounts/views.py
• Auth endpoints: Register/Login
• Category endpoints: List/Create/Update/Delete
• Product endpoints: List/Retrieve/Create/Update/Delete
• Alerts endpoint: /products/alerts/
• Dashboard endpoint: /dashboard/
Serialization/Validation
• accounts/serializers.py
• Transforms Category/Product to JSON
• Adds computed fields (is_low_stock, has_expiry)
• Validates payload (price, quantity, category ownership, etc.)
Domain/Data Model
• accounts/models.py
• Category(owner FK User, name, created_at, …)
• Product(category FK Category, name, price, quantity, min_threshold, expiration_date, created_at, …)
• DB tables derived from migrations: accounts/migrations/000*_*.py
Cross-cutting
• Authentication/permissions configured in config/settings.py
• Ownership enforcement occurs in views/querysets:
• Category.objects.filter(owner=request.user)
• Product.objects.filter(category__owner=request.user) (explicitly called out in README)
• Request → config/urls.py → accounts/urls.py → accounts/views.py
• View → Serializer (validate/serialize)
• View/Serializer → Model/ORM → DB
• DB → ORM → View → Response JSON
Highlight “computed endpoints”
• Alerts: ORM queries with F() comparisons + date filtering
• Dashboard: ORM aggregations (counts, sums, group-by category)
External
• Frontend Dashboard (Web/Mobile)
• (Optional) Admin user via Django Admin
API
• Django REST API (manage.py runtime; deployed via WSGI/ASGI)
• Auth module (register/login)
• Category API
• Product API
• Alerts API
• Dashboard API
Internal layers
• config/urls.py + accounts/urls.py
• accounts/views.py
• accounts/serializers.py
• accounts/models.py
Data store
• RDBMS storing Users/Categories/Products
Key annotations
• /api/* base path
• Auth required except register/login
• Isolation rule: category__owner=request.user
• Alert rules: low stock (quantity <= min_threshold), expired (expiration_date <= today)
Container: Reverse proxy (optional) → Django API (WSGI/ASGI) → DB
3. Component: urls → views → serializers → models → DB plus endpoint groupings (Auth/Categories/Products/Alerts/Dashboard) and the ER relationship.