Project Structure¶
This document outlines the organization and structure of the NSGG Backend project.
Directory Structure¶
nsgg_backend/
├── config/ # Project configuration
│ ├── __init__.py
│ ├── settings/ # Settings modules
│ │ ├── base.py # Base settings
│ │ ├── local.py # Local development settings
│ │ └── production.py # Production settings
│ ├── urls.py # Main URL configuration
│ └── wsgi.py # WSGI configuration
│
├── products/ # Products app
│ ├── api/ # API-related code
│ │ ├── serializers.py
│ │ ├── views.py
│ │ └── urls.py
│ ├── migrations/
│ ├── models.py # Product-related models
│ ├── services.py # Business logic
│ └── tests/ # Test files
│
├── users/ # Users app
│ ├── api/
│ ├── migrations/
│ ├── models.py
│ └── tests/
│
├── orders/ # Orders app
│ ├── api/
│ ├── migrations/
│ ├── models.py
│ └── tests/
│
├── common/ # Shared utilities
│ ├── exceptions.py
│ ├── middleware.py
│ └── utils.py
│
├── docs/ # Documentation
│ ├── api/
│ ├── architecture/
│ └── development/
│
├── requirements/ # Dependencies
│ ├── base.txt
│ ├── local.txt
│ ├── production.txt
│ └── testing.txt
│
├── scripts/ # Utility scripts
│ ├── setup.sh
│ └── deploy.sh
│
├── static/ # Static files
│ ├── admin/
│ └── rest_framework/
│
├── media/ # User-uploaded files
│
├── .env # Environment variables
├── .gitignore
├── docker-compose.yml
├── Dockerfile
├── manage.py
├── pytest.ini
└── README.md
Key Components¶
1. Configuration (config/)¶
The config directory contains project-wide settings and configurations:
settings/: Split settings for different environmentsurls.py: Main URL routing configurationwsgi.py: WSGI application entry point
2. Applications¶
Products App (products/)¶
Handles product management:
# products/models.py
class Product(models.Model):
name = models.CharField(max_length=255)
price = models.DecimalField(max_digits=10, decimal_places=2)
category = models.ForeignKey('Category', on_delete=models.PROTECT)
Users App (users/)¶
Manages user accounts and authentication:
# users/models.py
class User(AbstractUser):
email = models.EmailField(unique=True)
phone = models.CharField(max_length=15, blank=True)
Orders App (orders/)¶
Handles order processing and cart management:
# orders/models.py
class Order(models.Model):
user = models.ForeignKey('users.User', on_delete=models.PROTECT)
status = models.CharField(max_length=20, choices=ORDER_STATUS_CHOICES)
total = models.DecimalField(max_digits=10, decimal_places=2)
3. Common Utilities (common/)¶
Shared code and utilities:
# common/exceptions.py
class ApplicationError(Exception):
def __init__(self, message, code=None):
self.message = message
self.code = code
# common/middleware.py
class RequestLoggingMiddleware:
def __init__(self, get_response):
self.get_response = get_response
def __call__(self, request):
# Log request
response = self.get_response(request)
# Log response
return response
4. API Structure¶
Each app follows a consistent API structure:
# products/api/views.py
class ProductViewSet(viewsets.ModelViewSet):
queryset = Product.objects.all()
serializer_class = ProductSerializer
permission_classes = [IsAuthenticated]
# products/api/serializers.py
class ProductSerializer(serializers.ModelSerializer):
class Meta:
model = Product
fields = ['id', 'name', 'price', 'category']
# products/api/urls.py
router = DefaultRouter()
router.register('products', ProductViewSet)
5. Testing Structure¶
Each app has its own test directory:
# products/tests/test_models.py
class TestProduct:
def test_create_product(self):
product = ProductFactory()
assert product.pk is not None
# products/tests/test_views.py
class TestProductAPI:
def test_list_products(self, client):
response = client.get('/api/v1/products/')
assert response.status_code == 200
Code Organization Principles¶
- Separation of Concerns
- Models define data structure
- Views handle request/response
- Services contain business logic
-
Serializers handle data transformation
-
DRY (Don't Repeat Yourself)
- Common utilities in
common/ - Shared mixins and base classes
-
Reusable test fixtures
-
Configuration Management
- Environment-specific settings
- Environment variables for secrets
-
Separate requirement files
-
Testing Organization
- Tests mirror application structure
- Factories for test data
- Fixtures for common setup
Development Guidelines¶
-
Creating New Apps
-
Adding Models
-
API Endpoints
-
URL Configuration
Deployment Structure¶
- Docker Configuration
Dockerfilefor building imagesdocker-compose.ymlfor local development-
docker-compose.prod.ymlfor production -
Static Files
- Collected to
static/ - Served through Nginx
-
CDN in production
-
Media Files
- Uploaded to
media/ - S3 storage in production
- Local storage in development