Compare commits
11 Commits
f526330f9e
...
feature/1-
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
539629076f | ||
|
|
7112197ad2 | ||
| 7160d64e86 | |||
| bb5ef7fed8 | |||
| 77761ea8cc | |||
| ff67720cea | |||
| 5e811c802a | |||
| d4a61b8340 | |||
| 52ff61354e | |||
| 8196137c4c | |||
| 47e87e4204 |
361
REFACTORING_SUMMARY.md
Normal file
361
REFACTORING_SUMMARY.md
Normal file
@@ -0,0 +1,361 @@
|
||||
# Refactorización a Domain-Driven Design - Resumen
|
||||
|
||||
## ✅ Refactorización Completada
|
||||
|
||||
**Fecha**: 29 de Mayo 2026
|
||||
**Commit**: `47e87e42048e2394a6461f78d5c8e6a4386aa2f9`
|
||||
**Tests**: 46 tests pasando ✓
|
||||
|
||||
---
|
||||
|
||||
## 📊 Estadísticas
|
||||
|
||||
- **Archivos creados**: 17
|
||||
- **Archivos eliminados**: 2
|
||||
- **Archivos modificados**: 1
|
||||
- **Líneas agregadas**: +816
|
||||
- **Líneas eliminadas**: -621
|
||||
- **Balance neto**: +195 líneas (mejor organización, más documentación)
|
||||
|
||||
---
|
||||
|
||||
## 🏗️ Nueva Estructura
|
||||
|
||||
```
|
||||
don_confiao/
|
||||
├── models/ # ✅ Ya estaba organizado por dominio
|
||||
│ ├── products.py
|
||||
│ ├── customers.py
|
||||
│ ├── sales.py
|
||||
│ ├── payments.py
|
||||
│ └── admin.py
|
||||
│
|
||||
├── serializers/ # 🆕 Nuevo - Organizado por dominio
|
||||
│ ├── __init__.py
|
||||
│ ├── products.py # ProductSerializer, ListProductSerializer
|
||||
│ ├── customers.py # CustomerSerializer, ListCustomerSerializer
|
||||
│ ├── sales.py # Sale, SaleLine, CatalogSale serializers
|
||||
│ └── payments.py # ReconciliationJar, PaymentMethod serializers
|
||||
│
|
||||
├── api/ # 🆕 Nuevo - API Views por dominio
|
||||
│ ├── __init__.py
|
||||
│ ├── products.py # ProductView, ProductsFromTrytonView
|
||||
│ ├── customers.py # CustomerView, CustomersFromTrytonView
|
||||
│ ├── sales.py # SaleView, CatalogSaleView, Sales*TrytonView
|
||||
│ ├── payments.py # ReconciliateJarView, PaymentMethodView
|
||||
│ └── admin.py # AdminCodeValidateView
|
||||
│
|
||||
└── services/ # 🆕 Nuevo - Capa de servicios
|
||||
└── tryton/
|
||||
├── __init__.py
|
||||
├── client.py # Factory + TrytonSale/LineSale
|
||||
├── products.py # ProductTrytonService
|
||||
├── customers.py # CustomerTrytonService
|
||||
└── sales.py # SaleTrytonService
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 📦 Detalles por Dominio
|
||||
|
||||
### 1. Products (Productos)
|
||||
|
||||
**Serializers** (`serializers/products.py`):
|
||||
- `ProductSerializer` - Serializer completo de producto
|
||||
- `ListProductSerializer` - Versión simplificada para listados
|
||||
|
||||
**API Views** (`api/products.py`):
|
||||
- `ProductView` - CRUD de productos con filtrado por `active` status
|
||||
- `ProductsFromTrytonView` - Importación desde Tryton
|
||||
|
||||
**Services** (`services/tryton/products.py`):
|
||||
- `ProductTrytonService` - Lógica de sincronización con Tryton
|
||||
- `import_from_tryton()` - Importa productos
|
||||
- `_create_product()` - Crea nuevo producto
|
||||
- `_update_product()` - Actualiza producto existente
|
||||
- `_need_update()` - Determina si necesita actualización
|
||||
|
||||
---
|
||||
|
||||
### 2. Customers (Clientes)
|
||||
|
||||
**Serializers** (`serializers/customers.py`):
|
||||
- `CustomerSerializer` - Serializer completo de cliente
|
||||
- `ListCustomerSerializer` - Versión simplificada para listados
|
||||
|
||||
**API Views** (`api/customers.py`):
|
||||
- `CustomerView` - CRUD de clientes
|
||||
- `CustomersFromTrytonView` - Importación desde Tryton
|
||||
|
||||
**Services** (`services/tryton/customers.py`):
|
||||
- `CustomerTrytonService` - Lógica de sincronización con Tryton
|
||||
- `import_from_tryton()` - Importa clientes
|
||||
- `_create_customer()` - Crea nuevo cliente
|
||||
- `_update_customer()` - Actualiza cliente existente
|
||||
- `_need_update()` - Determina si necesita actualización
|
||||
|
||||
---
|
||||
|
||||
### 3. Sales (Ventas)
|
||||
|
||||
**Serializers** (`serializers/sales.py`):
|
||||
- `SaleSerializer` - Serializer de venta
|
||||
- `SaleLineSerializer` - Serializer de línea de venta
|
||||
- `CatalogSaleSerializer` - Serializer de venta por catálogo
|
||||
- `CatalogSaleLineSerializer` - Línea de venta por catálogo
|
||||
- `SummarySaleLineSerializer` - Resumen de línea (con detalles)
|
||||
- `SaleSummarySerializer` - Resumen completo de venta
|
||||
- `SaleForRenconciliationSerializer` - Ventas para reconciliación
|
||||
|
||||
**API Views** (`api/sales.py`):
|
||||
- `SaleView` - CRUD de ventas
|
||||
- `CatalogSaleView` - CRUD de ventas por catálogo
|
||||
- `SaleSummary` - Resumen de venta por ID
|
||||
- `SalesForTrytonView` - Exportar ventas a CSV para Tryton
|
||||
- `SalesToTrytonView` - Enviar ventas a Tryton
|
||||
|
||||
**Services** (`services/tryton/sales.py`):
|
||||
- `SaleTrytonService` - Lógica de sincronización con Tryton
|
||||
- `send_to_tryton()` - Envía ventas a Tryton
|
||||
- `_to_tryton_params()` - Convierte venta a formato Tryton
|
||||
|
||||
---
|
||||
|
||||
### 4. Payments (Pagos y Reconciliación)
|
||||
|
||||
**Serializers** (`serializers/payments.py`):
|
||||
- `ReconciliationJarSerializer` - Serializer de arqueo de caja
|
||||
- `PaymentMethodSerializer` - Serializer de métodos de pago
|
||||
|
||||
**API Views** (`api/payments.py`):
|
||||
- `ReconciliateJarView` - Vista de reconciliación (POST/GET)
|
||||
- `ReconciliateJarModelView` - ViewSet para arqueos
|
||||
- `PaymentMethodView` - Listado de métodos de pago
|
||||
- `SalesForReconciliationView` - Ventas pendientes de reconciliación
|
||||
- `Pagination` - Paginación personalizada
|
||||
|
||||
---
|
||||
|
||||
### 5. Admin
|
||||
|
||||
**API Views** (`api/admin.py`):
|
||||
- `AdminCodeValidateView` - Validación de códigos de administrador
|
||||
|
||||
---
|
||||
|
||||
### 6. Services - Tryton Integration
|
||||
|
||||
**Client** (`services/tryton/client.py`):
|
||||
- `get_tryton_client()` - Factory para crear cliente conectado
|
||||
- `TrytonSale` - Representa venta para exportación
|
||||
- `TrytonLineSale` - Representa línea de venta para exportación
|
||||
- Constantes de configuración (HOST, DATABASE, CURRENCY, etc.)
|
||||
|
||||
**Características**:
|
||||
- ✅ Configuración centralizada
|
||||
- ✅ Factory pattern para cliente
|
||||
- ✅ DTOs para transformación de datos
|
||||
- ✅ Reutilizable desde cualquier vista
|
||||
|
||||
---
|
||||
|
||||
## 🔄 Migración de Código
|
||||
|
||||
### Archivos Eliminados
|
||||
|
||||
❌ `serializers.py` (170 líneas) → ✅ `serializers/` (4 archivos)
|
||||
❌ `api_views.py` (526 líneas) → ✅ `api/` (5 archivos)
|
||||
|
||||
### Backwards Compatibility
|
||||
|
||||
Todas las importaciones existentes siguen funcionando gracias a `__init__.py`:
|
||||
|
||||
```python
|
||||
# Antes y Después - Mismas importaciones funcionan
|
||||
from don_confiao.serializers import ProductSerializer
|
||||
from don_confiao.api import ProductView
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 🎯 Beneficios Obtenidos
|
||||
|
||||
### 1. Cohesión
|
||||
- Cada módulo agrupa código relacionado a un solo dominio
|
||||
- Fácil encontrar todo lo relacionado a un concepto (ej: productos)
|
||||
|
||||
### 2. Separación de Responsabilidades
|
||||
- **Models**: Definición de datos
|
||||
- **Serializers**: Transformación API ↔ Modelos
|
||||
- **API Views**: Lógica de endpoints
|
||||
- **Services**: Lógica de negocio (Tryton)
|
||||
|
||||
### 3. Mantenibilidad
|
||||
- Archivos más pequeños y enfocados
|
||||
- Menos de 150 líneas por archivo
|
||||
- Más fácil de leer y entender
|
||||
|
||||
### 4. Escalabilidad
|
||||
- Agregar nuevo dominio = crear nuevos archivos en cada capa
|
||||
- No modifica código existente
|
||||
- Open/Closed Principle
|
||||
|
||||
### 5. Testabilidad
|
||||
- Tests pueden organizarse por dominio
|
||||
- Servicios fáciles de mockear
|
||||
- Cada componente testeable independientemente
|
||||
|
||||
### 6. Reutilización
|
||||
- Servicios Tryton usables desde cualquier vista
|
||||
- Serializers compartibles entre vistas
|
||||
- DRY (Don't Repeat Yourself)
|
||||
|
||||
---
|
||||
|
||||
## 📝 Próximos Pasos Sugeridos
|
||||
|
||||
### 1. Organizar Tests por Dominio (Opcional)
|
||||
|
||||
```
|
||||
tests/
|
||||
├── __init__.py
|
||||
├── products/
|
||||
│ ├── test_products_api.py
|
||||
│ ├── test_products_serializers.py
|
||||
│ └── test_products_tryton.py
|
||||
├── customers/
|
||||
│ └── ...
|
||||
└── sales/
|
||||
└── ...
|
||||
```
|
||||
|
||||
### 2. Documentación
|
||||
|
||||
Crear documentación por dominio:
|
||||
```
|
||||
docs/
|
||||
├── products.md
|
||||
├── customers.md
|
||||
├── sales.md
|
||||
└── tryton_integration.md
|
||||
```
|
||||
|
||||
### 3. Mejorar Services Layer
|
||||
|
||||
Agregar más lógica de negocio a services:
|
||||
- Validaciones complejas
|
||||
- Cálculos de negocio
|
||||
- Transformaciones de datos
|
||||
|
||||
### 4. Agregar Type Hints
|
||||
|
||||
Mejorar type hints en services para mejor IDE support:
|
||||
```python
|
||||
def import_from_tryton(self) -> dict[str, list[int]]:
|
||||
"""Importa productos desde Tryton
|
||||
|
||||
Returns:
|
||||
Dict con listas de IDs: checked, failed, updated, created, untouched
|
||||
"""
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 🧪 Tests
|
||||
|
||||
### Estado Actual
|
||||
✅ **46 tests pasando**
|
||||
|
||||
### Cobertura por Dominio
|
||||
- ✅ Products: 13 tests (incluye filtrado por active status)
|
||||
- ✅ Sales: 8 tests
|
||||
- ✅ Customers: Tests de integración Tryton
|
||||
- ✅ Payments: Tests de reconciliación
|
||||
- ✅ Admin: Tests de códigos
|
||||
|
||||
### Ejecución
|
||||
```bash
|
||||
# Todos los tests
|
||||
docker compose -f docker-compose.dev.yml run --rm django python manage.py test don_confiao.tests
|
||||
|
||||
# Tests específicos
|
||||
docker compose -f docker-compose.dev.yml run --rm django python manage.py test don_confiao.tests.test_products
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 📚 Ejemplos de Uso
|
||||
|
||||
### Importar Serializers
|
||||
```python
|
||||
# Forma explícita
|
||||
from don_confiao.serializers.products import ProductSerializer
|
||||
|
||||
# Forma simplificada (recomendada)
|
||||
from don_confiao.serializers import ProductSerializer
|
||||
```
|
||||
|
||||
### Importar API Views
|
||||
```python
|
||||
# Forma explícita
|
||||
from don_confiao.api.products import ProductView
|
||||
|
||||
# Forma simplificada (recomendada)
|
||||
from don_confiao.api import ProductView
|
||||
```
|
||||
|
||||
### Usar Services
|
||||
```python
|
||||
from don_confiao.services import get_tryton_client, ProductTrytonService
|
||||
|
||||
# En una vista
|
||||
def my_view(request):
|
||||
client = get_tryton_client()
|
||||
service = ProductTrytonService(client)
|
||||
result = service.import_from_tryton()
|
||||
return Response(result)
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 🔍 Verificación de Calidad
|
||||
|
||||
### Checks Realizados
|
||||
- ✅ Todos los tests pasan
|
||||
- ✅ Sin imports circulares
|
||||
- ✅ Todas las URLs funcionando
|
||||
- ✅ Backwards compatibility mantenida
|
||||
- ✅ Código limpio y documentado
|
||||
|
||||
### Métricas
|
||||
- **Complejidad ciclomática**: Reducida (archivos más pequeños)
|
||||
- **Acoplamiento**: Reducido (separación de capas)
|
||||
- **Cohesión**: Incrementada (código por dominio)
|
||||
|
||||
---
|
||||
|
||||
## 🎓 Principios Aplicados
|
||||
|
||||
1. **Domain-Driven Design (DDD)**: Organización por dominios de negocio
|
||||
2. **Single Responsibility Principle (SRP)**: Cada módulo una responsabilidad
|
||||
3. **Open/Closed Principle**: Abierto a extensión, cerrado a modificación
|
||||
4. **Dependency Inversion**: Dependencias hacia abstracciones (services)
|
||||
5. **DRY**: Servicios reutilizables, no repetir lógica
|
||||
6. **Separation of Concerns**: Capas bien definidas
|
||||
|
||||
---
|
||||
|
||||
## 📞 Soporte
|
||||
|
||||
Si tienes preguntas sobre la nueva estructura:
|
||||
|
||||
1. **Serializers**: Ver `serializers/__init__.py` para exportaciones
|
||||
2. **API Views**: Ver `api/__init__.py` para exportaciones
|
||||
3. **Services**: Ver `services/tryton/` para lógica Tryton
|
||||
4. **URLs**: Ver `urls.py` para rutas disponibles
|
||||
|
||||
---
|
||||
|
||||
**¡Refactorización completada exitosamente! 🎉**
|
||||
|
||||
La estructura está lista para escalar y mantener de forma eficiente.
|
||||
@@ -15,3 +15,4 @@ python-decouple # Manage environment variables and settings
|
||||
|
||||
# Static files serving in production/staging
|
||||
whitenoise==6.6.0 # Serve static files efficiently with compression
|
||||
Pillow # Image processing for catalogue image resizing
|
||||
|
||||
@@ -138,6 +138,22 @@ USE_TZ = True
|
||||
STATIC_URL = "static/"
|
||||
STATIC_ROOT = BASE_DIR / "staticfiles"
|
||||
|
||||
# Media files
|
||||
MEDIA_ROOT = BASE_DIR / "media"
|
||||
MEDIA_URL = "/media/"
|
||||
|
||||
# Catalogue image settings
|
||||
CATALOGUE_IMAGE_WIDTH = int(os.environ.get("CATALOGUE_IMAGE_WIDTH", "600"))
|
||||
CATALOGUE_IMAGE_HEIGHT = int(os.environ.get("CATALOGUE_IMAGE_HEIGHT", "600"))
|
||||
CATALOGUE_STRICT_DIMENSION = os.environ.get("CATALOGUE_STRICT_DIMENSION", "False").lower() in ("true", "1", "yes")
|
||||
CATALOGUE_BACKGROUND_IMAGES_RGBA = os.environ.get(
|
||||
"CATALOGUE_BACKGROUND_IMAGES_RGBA", "0,0,0,0"
|
||||
)
|
||||
# Parse RGBA string to tuple
|
||||
_rgba_parts = CATALOGUE_BACKGROUND_IMAGES_RGBA.split(",")
|
||||
CATALOGUE_BACKGROUND_IMAGES_RGBA = tuple(int(p.strip()) for p in _rgba_parts)
|
||||
CATALOGUE_MAX_UPLOAD_SIZE = int(os.environ.get("CATALOGUE_MAX_UPLOAD_SIZE", str(5 * 1024 * 1024)))
|
||||
|
||||
# Default primary key field type
|
||||
# https://docs.djangoproject.com/en/5.0/ref/settings/#default-auto-field
|
||||
|
||||
|
||||
@@ -48,6 +48,10 @@ EMAIL_BACKEND = "django.core.mail.backends.console.EmailBackend"
|
||||
STATIC_ROOT = BASE_DIR / "staticfiles"
|
||||
STATIC_URL = "/static/"
|
||||
|
||||
# Media files configuration
|
||||
MEDIA_ROOT = BASE_DIR / "media"
|
||||
MEDIA_URL = "/media/"
|
||||
|
||||
# WhiteNoise configuration for development (optional, for consistency)
|
||||
# In development with DEBUG=True, Django serves static files automatically
|
||||
# But this ensures consistent behavior across all environments
|
||||
|
||||
@@ -14,6 +14,8 @@ Including another URLconf
|
||||
1. Import the include() function: from django.urls import include, path
|
||||
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
|
||||
"""
|
||||
from django.conf import settings
|
||||
from django.conf.urls.static import static
|
||||
from django.contrib import admin
|
||||
from django.urls import include, path
|
||||
from rest_framework_simplejwt.views import (
|
||||
@@ -32,3 +34,8 @@ urlpatterns = [
|
||||
name='token_refresh'),
|
||||
path('api/users/', include('users.urls')),
|
||||
]
|
||||
|
||||
# Serve media files through Django only in development.
|
||||
# In production/staging, media is served by the web server (nginx).
|
||||
if settings.DEBUG:
|
||||
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
|
||||
|
||||
@@ -11,7 +11,19 @@ from .models.sales import (
|
||||
from .models.products import Product, ProductCategory
|
||||
from .models.payments import ReconciliationJar
|
||||
|
||||
admin.site.register(Customer)
|
||||
|
||||
@admin.register(Customer)
|
||||
class CustomerAdmin(admin.ModelAdmin):
|
||||
list_display = (
|
||||
"name",
|
||||
"email",
|
||||
"phone",
|
||||
"external_id",
|
||||
"address_external_id",
|
||||
)
|
||||
search_fields = ("name", "email", "phone")
|
||||
|
||||
|
||||
admin.site.register(Sale)
|
||||
admin.site.register(SaleLine)
|
||||
admin.site.register(CatalogSale)
|
||||
|
||||
47
tienda_ilusion/don_confiao/api/__init__.py
Normal file
47
tienda_ilusion/don_confiao/api/__init__.py
Normal file
@@ -0,0 +1,47 @@
|
||||
from .catalogue_images import CatalogueImageViewSet
|
||||
from .products import ProductView, ProductsFromTrytonView
|
||||
from .customers import CustomerView, CustomersFromTrytonView
|
||||
from .sales import (
|
||||
SaleView,
|
||||
CatalogSaleView,
|
||||
SaleSummary,
|
||||
CatalogSaleSummary,
|
||||
SalesForTrytonView,
|
||||
SalesToTrytonView,
|
||||
CatalogSalesToTrytonView,
|
||||
)
|
||||
from .payments import (
|
||||
ReconciliateJarView,
|
||||
ReconciliateJarModelView,
|
||||
PaymentMethodView,
|
||||
SalesForReconciliationView,
|
||||
Pagination,
|
||||
)
|
||||
from .admin import AdminCodeValidateView
|
||||
|
||||
__all__ = [
|
||||
# Catalogue Images
|
||||
"CatalogueImageViewSet",
|
||||
# Products
|
||||
"ProductView",
|
||||
"ProductsFromTrytonView",
|
||||
# Customers
|
||||
"CustomerView",
|
||||
"CustomersFromTrytonView",
|
||||
# Sales
|
||||
"SaleView",
|
||||
"CatalogSaleView",
|
||||
"SaleSummary",
|
||||
"CatalogSaleSummary",
|
||||
"SalesForTrytonView",
|
||||
"SalesToTrytonView",
|
||||
"CatalogSalesToTrytonView",
|
||||
# Payments
|
||||
"ReconciliateJarView",
|
||||
"ReconciliateJarModelView",
|
||||
"PaymentMethodView",
|
||||
"SalesForReconciliationView",
|
||||
"Pagination",
|
||||
# Admin
|
||||
"AdminCodeValidateView",
|
||||
]
|
||||
14
tienda_ilusion/don_confiao/api/admin.py
Normal file
14
tienda_ilusion/don_confiao/api/admin.py
Normal file
@@ -0,0 +1,14 @@
|
||||
from rest_framework.views import APIView
|
||||
from rest_framework.response import Response
|
||||
from rest_framework.permissions import IsAuthenticated
|
||||
|
||||
from ..models.admin import AdminCode
|
||||
from ..permissions import IsAdministrator
|
||||
|
||||
|
||||
class AdminCodeValidateView(APIView):
|
||||
permission_classes = [IsAuthenticated, IsAdministrator]
|
||||
|
||||
def get(self, request, code):
|
||||
codes = AdminCode.objects.filter(value=code)
|
||||
return Response({"validCode": bool(codes)})
|
||||
25
tienda_ilusion/don_confiao/api/catalogue_images.py
Normal file
25
tienda_ilusion/don_confiao/api/catalogue_images.py
Normal file
@@ -0,0 +1,25 @@
|
||||
from rest_framework import viewsets
|
||||
from rest_framework.permissions import IsAuthenticated
|
||||
|
||||
from ..image_service import resize_catalogue_image
|
||||
from ..models.catalogue_images import CatalogueImage
|
||||
from ..permissions import IsAdministrator
|
||||
from ..serializers.catalogue_images import CatalogueImageSerializer
|
||||
|
||||
|
||||
class CatalogueImageViewSet(viewsets.ModelViewSet):
|
||||
queryset = CatalogueImage.objects.all()
|
||||
serializer_class = CatalogueImageSerializer
|
||||
|
||||
def get_permissions(self):
|
||||
if self.action in ("create", "update", "partial_update", "destroy"):
|
||||
return [IsAuthenticated(), IsAdministrator()]
|
||||
return [IsAuthenticated()]
|
||||
|
||||
def perform_create(self, serializer):
|
||||
instance = serializer.save()
|
||||
resize_catalogue_image(instance.image)
|
||||
|
||||
def perform_update(self, serializer):
|
||||
instance = serializer.save()
|
||||
resize_catalogue_image(instance.image)
|
||||
25
tienda_ilusion/don_confiao/api/customers.py
Normal file
25
tienda_ilusion/don_confiao/api/customers.py
Normal file
@@ -0,0 +1,25 @@
|
||||
from rest_framework import viewsets
|
||||
from rest_framework.views import APIView
|
||||
from rest_framework.response import Response
|
||||
from rest_framework.permissions import IsAuthenticated
|
||||
|
||||
from ..models.customers import Customer
|
||||
from ..serializers import CustomerSerializer
|
||||
from ..permissions import IsAdministrator
|
||||
from ..services.tryton.customers import CustomerTrytonService
|
||||
from ..services.tryton.client import get_tryton_client
|
||||
|
||||
|
||||
class CustomerView(viewsets.ModelViewSet):
|
||||
queryset = Customer.objects.all()
|
||||
serializer_class = CustomerSerializer
|
||||
|
||||
|
||||
class CustomersFromTrytonView(APIView):
|
||||
permission_classes = [IsAuthenticated, IsAdministrator]
|
||||
|
||||
def post(self, request):
|
||||
tryton_client = get_tryton_client()
|
||||
service = CustomerTrytonService(tryton_client)
|
||||
result = service.import_from_tryton()
|
||||
return Response(result, status=200)
|
||||
108
tienda_ilusion/don_confiao/api/payments.py
Normal file
108
tienda_ilusion/don_confiao/api/payments.py
Normal file
@@ -0,0 +1,108 @@
|
||||
from rest_framework import viewsets
|
||||
from rest_framework.views import APIView
|
||||
from rest_framework.response import Response
|
||||
from rest_framework.status import HTTP_400_BAD_REQUEST
|
||||
from rest_framework.pagination import PageNumberPagination
|
||||
from rest_framework.permissions import IsAuthenticated
|
||||
from decimal import Decimal
|
||||
|
||||
from ..models.sales import Sale
|
||||
from ..models.payments import ReconciliationJar, PaymentMethods
|
||||
from ..serializers import (
|
||||
ReconciliationJarSerializer,
|
||||
PaymentMethodSerializer,
|
||||
SaleForRenconciliationSerializer,
|
||||
)
|
||||
from ..permissions import IsAdministrator
|
||||
|
||||
|
||||
class Pagination(PageNumberPagination):
|
||||
page_size = 10
|
||||
page_size_query_param = "page_size"
|
||||
|
||||
|
||||
class ReconciliateJarView(APIView):
|
||||
permission_classes = [IsAuthenticated, IsAdministrator]
|
||||
|
||||
def post(self, request):
|
||||
data = request.data
|
||||
cash_purchases_id = data.get("cash_purchases")
|
||||
serializer = ReconciliationJarSerializer(data=data)
|
||||
if serializer.is_valid():
|
||||
cash_purchases = Sale.objects.filter(pk__in=cash_purchases_id)
|
||||
if not self._is_valid_total(
|
||||
cash_purchases, data.get("total_cash_purchases")
|
||||
):
|
||||
return Response(
|
||||
{
|
||||
"error": "total_cash_purchases not equal to sum of all purchases."
|
||||
},
|
||||
status=HTTP_400_BAD_REQUEST,
|
||||
)
|
||||
reconciliation = serializer.save()
|
||||
other_purchases = self._get_other_purchases(data.get("other_totals"))
|
||||
|
||||
self._link_purchases(reconciliation, cash_purchases, other_purchases)
|
||||
return Response({"id": reconciliation.id})
|
||||
return Response(serializer.errors, status=HTTP_400_BAD_REQUEST)
|
||||
|
||||
def get(self, request):
|
||||
reconciliations = ReconciliationJar.objects.all()
|
||||
serializer = ReconciliationJarSerializer(reconciliations, many=True)
|
||||
return Response(serializer.data)
|
||||
|
||||
def _is_valid_total(self, purchases, total):
|
||||
calculated_total = sum(p.get_total() for p in purchases)
|
||||
return Decimal(calculated_total).quantize(Decimal(".0001")) == (
|
||||
Decimal(total).quantize(Decimal(".0001"))
|
||||
)
|
||||
|
||||
def _get_other_purchases(self, other_totals):
|
||||
if not other_totals:
|
||||
return []
|
||||
purchases = []
|
||||
for method in other_totals:
|
||||
purchases.extend(other_totals[method]["purchases"])
|
||||
if purchases:
|
||||
return Sale.objects.filter(pk__in=purchases)
|
||||
return []
|
||||
|
||||
def _link_purchases(self, reconciliation, cash_purchases, other_purchases):
|
||||
for purchase in cash_purchases:
|
||||
purchase.reconciliation = reconciliation
|
||||
purchase.clean()
|
||||
purchase.save()
|
||||
|
||||
for purchase in other_purchases:
|
||||
purchase.reconciliation = reconciliation
|
||||
purchase.clean()
|
||||
purchase.save()
|
||||
|
||||
|
||||
class ReconciliateJarModelView(viewsets.ModelViewSet):
|
||||
queryset = ReconciliationJar.objects.all().order_by("-date_time")
|
||||
pagination_class = Pagination
|
||||
serializer_class = ReconciliationJarSerializer
|
||||
permission_classes = [IsAuthenticated, IsAdministrator]
|
||||
|
||||
|
||||
class PaymentMethodView(APIView):
|
||||
def get(self, request):
|
||||
serializer = PaymentMethodSerializer(PaymentMethods.choices, many=True)
|
||||
return Response(serializer.data)
|
||||
|
||||
|
||||
class SalesForReconciliationView(APIView):
|
||||
permission_classes = [IsAuthenticated, IsAdministrator]
|
||||
|
||||
def get(self, request):
|
||||
sales = Sale.objects.filter(reconciliation=None)
|
||||
grouped_sales = {}
|
||||
|
||||
for sale in sales:
|
||||
if sale.payment_method not in grouped_sales.keys():
|
||||
grouped_sales[sale.payment_method] = []
|
||||
serializer = SaleForRenconciliationSerializer(sale)
|
||||
grouped_sales[sale.payment_method].append(serializer.data)
|
||||
|
||||
return Response(grouped_sales)
|
||||
53
tienda_ilusion/don_confiao/api/products.py
Normal file
53
tienda_ilusion/don_confiao/api/products.py
Normal file
@@ -0,0 +1,53 @@
|
||||
from rest_framework import viewsets
|
||||
from rest_framework.views import APIView
|
||||
from rest_framework.response import Response
|
||||
from rest_framework.permissions import IsAuthenticated
|
||||
|
||||
from ..models.products import Product
|
||||
from ..serializers import ProductSerializer
|
||||
from ..permissions import IsAdministrator
|
||||
from ..services.tryton.products import ProductTrytonService
|
||||
from ..services.tryton.client import get_tryton_client
|
||||
|
||||
|
||||
class ProductView(viewsets.ModelViewSet):
|
||||
queryset = Product.objects.all()
|
||||
serializer_class = ProductSerializer
|
||||
|
||||
def get_queryset(self):
|
||||
"""
|
||||
Filters products by active status for list operations.
|
||||
Detail operations (retrieve, update, destroy) return all products.
|
||||
|
||||
Query params for list:
|
||||
- active=true (default): Only active products
|
||||
- active=false: Only inactive products
|
||||
- active=all: All products regardless of status
|
||||
"""
|
||||
queryset = Product.objects.all()
|
||||
|
||||
# Only filter for list action, not for detail operations
|
||||
if self.action != "list":
|
||||
return queryset
|
||||
|
||||
active_param = self.request.query_params.get("active", "true")
|
||||
|
||||
if active_param.lower() == "all":
|
||||
return queryset
|
||||
elif active_param.lower() in ["true", "1", "yes"]:
|
||||
return queryset.filter(active=True)
|
||||
elif active_param.lower() in ["false", "0", "no"]:
|
||||
return queryset.filter(active=False)
|
||||
else:
|
||||
# Default behavior: return only active products
|
||||
return queryset.filter(active=True)
|
||||
|
||||
|
||||
class ProductsFromTrytonView(APIView):
|
||||
permission_classes = [IsAuthenticated, IsAdministrator]
|
||||
|
||||
def post(self, request):
|
||||
tryton_client = get_tryton_client()
|
||||
service = ProductTrytonService(tryton_client)
|
||||
result = service.import_from_tryton()
|
||||
return Response(result, status=200)
|
||||
112
tienda_ilusion/don_confiao/api/sales.py
Normal file
112
tienda_ilusion/don_confiao/api/sales.py
Normal file
@@ -0,0 +1,112 @@
|
||||
from rest_framework import viewsets
|
||||
from rest_framework.views import APIView
|
||||
from rest_framework.response import Response
|
||||
from rest_framework.permissions import IsAuthenticated
|
||||
import io
|
||||
import csv
|
||||
|
||||
from ..models.sales import Sale, SaleLine, CatalogSale
|
||||
from ..models.customers import Customer
|
||||
from ..models.products import Product
|
||||
from ..serializers import (
|
||||
SaleSerializer,
|
||||
CatalogSaleSerializer,
|
||||
SaleSummarySerializer,
|
||||
CatalogSaleSummarySerializer,
|
||||
)
|
||||
from ..permissions import IsAdministrator
|
||||
from ..services.tryton.sales import SaleTrytonService
|
||||
from ..services.tryton.client import get_tryton_client
|
||||
from ..views import sales_to_tryton_csv
|
||||
|
||||
|
||||
class SaleView(viewsets.ModelViewSet):
|
||||
queryset = Sale.objects.all()
|
||||
serializer_class = SaleSerializer
|
||||
|
||||
def create(self, request):
|
||||
data = request.data
|
||||
customer = Customer.objects.get(pk=data["customer"])
|
||||
date = data["date"]
|
||||
lines = data["saleline_set"]
|
||||
payment_method = data["payment_method"]
|
||||
description = data.get("notes", "")
|
||||
sale = Sale.objects.create(
|
||||
customer=customer,
|
||||
date=date,
|
||||
payment_method=payment_method,
|
||||
description=description,
|
||||
)
|
||||
|
||||
for line in lines:
|
||||
product = Product.objects.get(pk=line["product"])
|
||||
quantity = line["quantity"]
|
||||
unit_price = line["unit_price"]
|
||||
SaleLine.objects.create(
|
||||
sale=sale,
|
||||
product=product,
|
||||
quantity=quantity,
|
||||
unit_price=unit_price,
|
||||
)
|
||||
|
||||
return Response(
|
||||
{"id": sale.id, "message": "Venta creada con exito"},
|
||||
status=201,
|
||||
)
|
||||
|
||||
|
||||
class CatalogSaleView(viewsets.ModelViewSet):
|
||||
queryset = CatalogSale.objects.all()
|
||||
serializer_class = CatalogSaleSerializer
|
||||
|
||||
|
||||
class SaleSummary(APIView):
|
||||
def get(self, request, id):
|
||||
sale = Sale.objects.get(pk=id)
|
||||
serializer = SaleSummarySerializer(sale)
|
||||
return Response(serializer.data)
|
||||
|
||||
|
||||
class CatalogSaleSummary(APIView):
|
||||
def get(self, request, id):
|
||||
catalog_sale = CatalogSale.objects.get(pk=id)
|
||||
serializer = CatalogSaleSummarySerializer(catalog_sale)
|
||||
return Response(serializer.data)
|
||||
|
||||
|
||||
class SalesForTrytonView(APIView):
|
||||
permission_classes = [IsAuthenticated, IsAdministrator]
|
||||
|
||||
def get(self, request):
|
||||
sales = Sale.objects.all()
|
||||
csv_data = self._generate_sales_CSV(sales)
|
||||
return Response({"csv": csv_data})
|
||||
|
||||
def _generate_sales_CSV(self, sales):
|
||||
output = io.StringIO()
|
||||
writer = csv.writer(output)
|
||||
csv_data = sales_to_tryton_csv(sales)
|
||||
|
||||
for row in csv_data:
|
||||
writer.writerow(row)
|
||||
return output.getvalue()
|
||||
|
||||
|
||||
class SalesToTrytonView(APIView):
|
||||
permission_classes = [IsAuthenticated, IsAdministrator]
|
||||
|
||||
def post(self, request):
|
||||
tryton_client = get_tryton_client()
|
||||
service = SaleTrytonService(tryton_client)
|
||||
result = service.send_to_tryton()
|
||||
return Response(result, status=200)
|
||||
|
||||
|
||||
class CatalogSalesToTrytonView(APIView):
|
||||
permission_classes = [IsAuthenticated, IsAdministrator]
|
||||
|
||||
def post(self, request):
|
||||
tryton_client = get_tryton_client()
|
||||
service = SaleTrytonService(tryton_client)
|
||||
result = service.send_catalog_sales_to_tryton()
|
||||
return Response(result, status=200)
|
||||
@@ -1,525 +0,0 @@
|
||||
from rest_framework import viewsets
|
||||
from rest_framework.response import Response
|
||||
from rest_framework.status import HTTP_400_BAD_REQUEST
|
||||
from rest_framework.views import APIView
|
||||
from rest_framework.pagination import PageNumberPagination
|
||||
from rest_framework.permissions import IsAuthenticated
|
||||
|
||||
from .models.sales import Sale, SaleLine
|
||||
from .models.customers import Customer
|
||||
from .models.sales import (
|
||||
Sale,
|
||||
SaleLine,
|
||||
CatalogSale,
|
||||
CatalogSaleLine,
|
||||
Payment,
|
||||
)
|
||||
from .models.products import Product, ProductCategory
|
||||
from .models.payments import PaymentMethods, ReconciliationJar
|
||||
from .models.admin import AdminCode
|
||||
|
||||
from .serializers import (
|
||||
SaleSerializer,
|
||||
CatalogSaleSerializer,
|
||||
ProductSerializer,
|
||||
CustomerSerializer,
|
||||
ReconciliationJarSerializer,
|
||||
PaymentMethodSerializer,
|
||||
SaleForRenconciliationSerializer,
|
||||
SaleSummarySerializer,
|
||||
)
|
||||
from .views import sales_to_tryton_csv
|
||||
from .permissions import IsAdministrator
|
||||
|
||||
from decimal import Decimal
|
||||
from sabatron_tryton_rpc_client.client import Client
|
||||
import io
|
||||
import csv
|
||||
import os
|
||||
|
||||
TRYTON_HOST = os.environ.get("TRYTON_HOST", "localhost")
|
||||
TRYTON_DATABASE = os.environ.get("TRYTON_DATABASE", "tryton")
|
||||
TRYTON_USERNAME = os.environ.get("TRYTON_USERNAME", "admin")
|
||||
TRYTON_PASSWORD = os.environ.get("TRYTON_PASSWORD", "admin")
|
||||
TRYTON_COP_CURRENCY = 31
|
||||
TRYTON_COMPANY_ID = 1
|
||||
TRYTON_SHOPS = [1]
|
||||
|
||||
|
||||
class Pagination(PageNumberPagination):
|
||||
page_size = 10
|
||||
page_size_query_param = "page_size"
|
||||
|
||||
|
||||
class SaleView(viewsets.ModelViewSet):
|
||||
queryset = Sale.objects.all()
|
||||
serializer_class = SaleSerializer
|
||||
|
||||
def create(self, request):
|
||||
data = request.data
|
||||
customer = Customer.objects.get(pk=data["customer"])
|
||||
date = data["date"]
|
||||
lines = data["saleline_set"]
|
||||
payment_method = data["payment_method"]
|
||||
description = data.get("notes", "")
|
||||
sale = Sale.objects.create(
|
||||
customer=customer,
|
||||
date=date,
|
||||
payment_method=payment_method,
|
||||
description=description,
|
||||
)
|
||||
|
||||
for line in lines:
|
||||
product = Product.objects.get(pk=line["product"])
|
||||
quantity = line["quantity"]
|
||||
unit_price = line["unit_price"]
|
||||
SaleLine.objects.create(
|
||||
sale=sale,
|
||||
product=product,
|
||||
quantity=quantity,
|
||||
unit_price=unit_price,
|
||||
)
|
||||
|
||||
return Response(
|
||||
{"id": sale.id, "message": "Venta creada con exito"},
|
||||
status=201,
|
||||
)
|
||||
|
||||
|
||||
class CatalogSaleView(viewsets.ModelViewSet):
|
||||
queryset = CatalogSale.objects.all()
|
||||
serializer_class = CatalogSaleSerializer
|
||||
|
||||
|
||||
class ProductView(viewsets.ModelViewSet):
|
||||
queryset = Product.objects.all()
|
||||
serializer_class = ProductSerializer
|
||||
|
||||
def get_queryset(self):
|
||||
"""
|
||||
Filters products by active status for list operations.
|
||||
Detail operations (retrieve, update, destroy) return all products.
|
||||
|
||||
Query params for list:
|
||||
- active=true (default): Only active products
|
||||
- active=false: Only inactive products
|
||||
- active=all: All products regardless of status
|
||||
"""
|
||||
queryset = Product.objects.all()
|
||||
|
||||
# Only filter for list action, not for detail operations
|
||||
if self.action != "list":
|
||||
return queryset
|
||||
|
||||
active_param = self.request.query_params.get("active", "true")
|
||||
|
||||
if active_param.lower() == "all":
|
||||
return queryset
|
||||
elif active_param.lower() in ["true", "1", "yes"]:
|
||||
return queryset.filter(active=True)
|
||||
elif active_param.lower() in ["false", "0", "no"]:
|
||||
return queryset.filter(active=False)
|
||||
else:
|
||||
# Default behavior: return only active products
|
||||
return queryset.filter(active=True)
|
||||
|
||||
|
||||
class CustomerView(viewsets.ModelViewSet):
|
||||
queryset = Customer.objects.all()
|
||||
serializer_class = CustomerSerializer
|
||||
|
||||
|
||||
class ReconciliateJarView(APIView):
|
||||
permission_classes = [IsAuthenticated, IsAdministrator]
|
||||
|
||||
def post(self, request):
|
||||
data = request.data
|
||||
cash_purchases_id = data.get("cash_purchases")
|
||||
serializer = ReconciliationJarSerializer(data=data)
|
||||
if serializer.is_valid():
|
||||
cash_purchases = Sale.objects.filter(pk__in=cash_purchases_id)
|
||||
if not self._is_valid_total(
|
||||
cash_purchases, data.get("total_cash_purchases")
|
||||
):
|
||||
return Response(
|
||||
{
|
||||
"error": "total_cash_purchases not equal to sum of all purchases."
|
||||
},
|
||||
status=HTTP_400_BAD_REQUEST,
|
||||
)
|
||||
reconciliation = serializer.save()
|
||||
other_purchases = self._get_other_purchases(data.get("other_totals"))
|
||||
|
||||
self._link_purchases(reconciliation, cash_purchases, other_purchases)
|
||||
return Response({"id": reconciliation.id})
|
||||
return Response(serializer.errors, status=HTTP_400_BAD_REQUEST)
|
||||
|
||||
def get(self, request):
|
||||
reconciliations = ReconciliationJar.objects.all()
|
||||
serializer = ReconciliationJarSerializer(reconciliations, many=True)
|
||||
return Response(serializer.data)
|
||||
|
||||
def _is_valid_total(self, purchases, total):
|
||||
calculated_total = sum(p.get_total() for p in purchases)
|
||||
return Decimal(calculated_total).quantize(Decimal(".0001")) == (
|
||||
Decimal(total).quantize(Decimal(".0001"))
|
||||
)
|
||||
|
||||
def _get_other_purchases(self, other_totals):
|
||||
if not other_totals:
|
||||
return []
|
||||
purchases = []
|
||||
for method in other_totals:
|
||||
purchases.extend(other_totals[method]["purchases"])
|
||||
if purchases:
|
||||
return Sale.objects.filter(pk__in=purchases)
|
||||
return []
|
||||
|
||||
def _link_purchases(self, reconciliation, cash_purchases, other_purchases):
|
||||
for purchase in cash_purchases:
|
||||
purchase.reconciliation = reconciliation
|
||||
purchase.clean()
|
||||
purchase.save()
|
||||
|
||||
for purchase in other_purchases:
|
||||
purchase.reconciliation = reconciliation
|
||||
purchase.clean()
|
||||
purchase.save()
|
||||
|
||||
|
||||
class PaymentMethodView(APIView):
|
||||
def get(self, request):
|
||||
serializer = PaymentMethodSerializer(PaymentMethods.choices, many=True)
|
||||
return Response(serializer.data)
|
||||
|
||||
|
||||
class SalesForReconciliationView(APIView):
|
||||
permission_classes = [IsAuthenticated, IsAdministrator]
|
||||
|
||||
def get(self, request):
|
||||
sales = Sale.objects.filter(reconciliation=None)
|
||||
grouped_sales = {}
|
||||
|
||||
for sale in sales:
|
||||
if sale.payment_method not in grouped_sales.keys():
|
||||
grouped_sales[sale.payment_method] = []
|
||||
serializer = SaleForRenconciliationSerializer(sale)
|
||||
grouped_sales[sale.payment_method].append(serializer.data)
|
||||
|
||||
return Response(grouped_sales)
|
||||
|
||||
|
||||
class SaleSummary(APIView):
|
||||
def get(self, request, id):
|
||||
sale = Sale.objects.get(pk=id)
|
||||
serializer = SaleSummarySerializer(sale)
|
||||
return Response(serializer.data)
|
||||
|
||||
|
||||
class AdminCodeValidateView(APIView):
|
||||
permission_classes = [IsAuthenticated, IsAdministrator]
|
||||
|
||||
def get(self, request, code):
|
||||
codes = AdminCode.objects.filter(value=code)
|
||||
return Response({"validCode": bool(codes)})
|
||||
|
||||
|
||||
class ReconciliateJarModelView(viewsets.ModelViewSet):
|
||||
queryset = ReconciliationJar.objects.all().order_by("-date_time")
|
||||
pagination_class = Pagination
|
||||
serializer_class = ReconciliationJarSerializer
|
||||
permission_classes = [IsAuthenticated, IsAdministrator]
|
||||
|
||||
|
||||
class SalesForTrytonView(APIView):
|
||||
permission_classes = [IsAuthenticated, IsAdministrator]
|
||||
|
||||
def get(self, request):
|
||||
sales = Sale.objects.all()
|
||||
csv = self._generate_sales_CSV(sales)
|
||||
return Response({"csv": csv})
|
||||
|
||||
def _generate_sales_CSV(self, sales):
|
||||
output = io.StringIO()
|
||||
writer = csv.writer(output)
|
||||
csv_data = sales_to_tryton_csv(sales)
|
||||
|
||||
for row in csv_data:
|
||||
writer.writerow(row)
|
||||
return output.getvalue()
|
||||
|
||||
|
||||
class SalesToTrytonView(APIView):
|
||||
permission_classes = [IsAuthenticated, IsAdministrator]
|
||||
|
||||
def post(self, request):
|
||||
tryton_client = Client(
|
||||
hostname=TRYTON_HOST,
|
||||
database=TRYTON_DATABASE,
|
||||
username=TRYTON_USERNAME,
|
||||
password=TRYTON_PASSWORD,
|
||||
)
|
||||
tryton_client.connect()
|
||||
method = "model.sale.sale.create"
|
||||
tryton_context = {
|
||||
"company": TRYTON_COMPANY_ID,
|
||||
"shops": TRYTON_SHOPS,
|
||||
}
|
||||
|
||||
successful = []
|
||||
failed = []
|
||||
|
||||
sales = Sale.objects.filter(external_id=None)
|
||||
for sale in sales:
|
||||
try:
|
||||
lines = SaleLine.objects.filter(sale=sale.id)
|
||||
tryton_params = self.__to_tryton_params(sale, lines, tryton_context)
|
||||
external_ids = tryton_client.call(method, tryton_params)
|
||||
sale.external_id = external_ids[0]
|
||||
sale.save()
|
||||
successful.append(sale.id)
|
||||
except Exception as e:
|
||||
print(f"Error al enviar la venta: {e}venta_id: {sale.id}")
|
||||
failed.append(sale.id)
|
||||
continue
|
||||
|
||||
return Response({"successful": successful, "failed": failed}, status=200)
|
||||
|
||||
def __to_tryton_params(self, sale, lines, tryton_context):
|
||||
sale_tryton = TrytonSale(sale, lines)
|
||||
return [[sale_tryton.to_tryton()], tryton_context]
|
||||
|
||||
|
||||
class TrytonSale:
|
||||
def __init__(self, sale, lines):
|
||||
self.sale = sale
|
||||
self.lines = lines
|
||||
|
||||
def _format_date(self, _date):
|
||||
return {
|
||||
"__class__": "date",
|
||||
"year": _date.year,
|
||||
"month": _date.month,
|
||||
"day": _date.day,
|
||||
}
|
||||
|
||||
def to_tryton(self):
|
||||
return {
|
||||
"company": TRYTON_COMPANY_ID,
|
||||
"shipment_address": self.sale.customer.address_external_id,
|
||||
"invoice_address": self.sale.customer.address_external_id,
|
||||
"currency": TRYTON_COP_CURRENCY,
|
||||
"comment": self.sale.description or "",
|
||||
"description": "Metodo pago: " + str(self.sale.payment_method or ""),
|
||||
"party": self.sale.customer.external_id,
|
||||
"reference": "don_confiao " + str(self.sale.id),
|
||||
"sale_date": self._format_date(self.sale.date),
|
||||
"lines": [
|
||||
[
|
||||
"create",
|
||||
[TrytonLineSale(line).to_tryton() for line in self.lines],
|
||||
]
|
||||
],
|
||||
"self_pick_up": True,
|
||||
}
|
||||
|
||||
|
||||
class TrytonLineSale:
|
||||
def __init__(self, sale_line):
|
||||
self.sale_line = sale_line
|
||||
|
||||
def _format_decimal(self, number):
|
||||
return {"__class__": "Decimal", "decimal": str(number)}
|
||||
|
||||
def to_tryton(self):
|
||||
return {
|
||||
"product": self.sale_line.product.external_id,
|
||||
"quantity": self._format_decimal(self.sale_line.quantity),
|
||||
"type": "line",
|
||||
"unit": self.sale_line.product.unit_external_id,
|
||||
"unit_price": self._format_decimal(self.sale_line.unit_price),
|
||||
}
|
||||
|
||||
|
||||
class ProductsFromTrytonView(APIView):
|
||||
permission_classes = [IsAuthenticated, IsAdministrator]
|
||||
|
||||
def post(self, request):
|
||||
tryton_client = Client(
|
||||
hostname=TRYTON_HOST,
|
||||
database=TRYTON_DATABASE,
|
||||
username=TRYTON_USERNAME,
|
||||
password=TRYTON_PASSWORD,
|
||||
)
|
||||
tryton_client.connect()
|
||||
method = "model.product.product.search"
|
||||
context = {"company": 1}
|
||||
params = [
|
||||
[["salable", "=", True]],
|
||||
0,
|
||||
1000,
|
||||
[["rec_name", "ASC"], ["id", None]],
|
||||
context,
|
||||
]
|
||||
product_ids = tryton_client.call(method, params)
|
||||
tryton_products = self.__get_product_datails_from_tryton(
|
||||
product_ids, tryton_client, context
|
||||
)
|
||||
checked_tryton_products = product_ids
|
||||
failed_products = []
|
||||
updated_products = []
|
||||
created_products = []
|
||||
untouched_products = []
|
||||
|
||||
for tryton_product in tryton_products:
|
||||
try:
|
||||
product = Product.objects.get(external_id=tryton_product.get("id"))
|
||||
except Product.DoesNotExist:
|
||||
try:
|
||||
product = self.__create_product(tryton_product)
|
||||
created_products.append(product.id)
|
||||
continue
|
||||
except Exception as e:
|
||||
print(
|
||||
f"Error al importar productos: {e}El producto: {tryton_product}"
|
||||
)
|
||||
failed_products.append(tryton_product.get("id"))
|
||||
continue
|
||||
|
||||
if self.__need_update(product, tryton_product):
|
||||
self.__update_product(product, tryton_product)
|
||||
updated_products.append(product.id)
|
||||
else:
|
||||
untouched_products.append(product.id)
|
||||
|
||||
return Response(
|
||||
{
|
||||
"checked_tryton_products": checked_tryton_products,
|
||||
"failed_products": failed_products,
|
||||
"updated_products": updated_products,
|
||||
"created_products": created_products,
|
||||
"untouched_products": untouched_products,
|
||||
},
|
||||
status=200,
|
||||
)
|
||||
|
||||
def __get_product_datails_from_tryton(self, product_ids, tryton_client, context):
|
||||
tryton_fields = [
|
||||
"id",
|
||||
"name",
|
||||
"default_uom.id",
|
||||
"default_uom.rec_name",
|
||||
"list_price",
|
||||
]
|
||||
method = "model.product.product.read"
|
||||
params = (product_ids, tryton_fields, context)
|
||||
response = tryton_client.call(method, params)
|
||||
return response
|
||||
|
||||
def __need_update(self, product, tryton_product):
|
||||
if not product.name == tryton_product.get("name"):
|
||||
return True
|
||||
if not product.price == tryton_product.get("list_price"):
|
||||
return True
|
||||
unit = tryton_product.get("default_uom.")
|
||||
if not product.measuring_unit == unit.get("rec_name"):
|
||||
return True
|
||||
|
||||
def __create_product(self, tryton_product):
|
||||
product = Product()
|
||||
product.name = tryton_product.get("name")
|
||||
product.price = tryton_product.get("list_price")
|
||||
product.external_id = tryton_product.get("id")
|
||||
unit = tryton_product.get("default_uom.")
|
||||
product.measuring_unit = unit.get("rec_name")
|
||||
product.unit_external_id = unit.get("id")
|
||||
product.save()
|
||||
return product
|
||||
|
||||
def __update_product(self, product, tryton_product):
|
||||
product.name = tryton_product.get("name")
|
||||
product.price = tryton_product.get("list_price")
|
||||
product.external_id = tryton_product.get("id")
|
||||
unit = tryton_product.get("default_uom.")
|
||||
product.measuring_unit = unit.get("rec_name")
|
||||
product.unit_external_id = unit.get("id")
|
||||
product.save()
|
||||
|
||||
|
||||
class CustomersFromTrytonView(APIView):
|
||||
permission_classes = [IsAuthenticated, IsAdministrator]
|
||||
|
||||
def post(self, request):
|
||||
tryton_client = Client(
|
||||
hostname=TRYTON_HOST,
|
||||
database=TRYTON_DATABASE,
|
||||
username=TRYTON_USERNAME,
|
||||
password=TRYTON_PASSWORD,
|
||||
)
|
||||
tryton_client.connect()
|
||||
method = "model.party.party.search"
|
||||
context = {"company": 1}
|
||||
params = [[], 0, 1000, [["name", "ASC"], ["id", None]], context]
|
||||
party_ids = tryton_client.call(method, params)
|
||||
tryton_parties = self.__get_party_datails(party_ids, tryton_client, context)
|
||||
checked_tryton_parties = party_ids
|
||||
failed_parties = []
|
||||
updated_customers = []
|
||||
created_customers = []
|
||||
untouched_customers = []
|
||||
|
||||
for tryton_party in tryton_parties:
|
||||
try:
|
||||
customer = Customer.objects.get(external_id=tryton_party.get("id"))
|
||||
except Customer.DoesNotExist:
|
||||
customer = self.__create_customer(tryton_party)
|
||||
created_customers.append(customer.id)
|
||||
continue
|
||||
if self.__need_update(customer, tryton_party):
|
||||
self.__update_customer(customer, tryton_party)
|
||||
updated_customers.append(customer.id)
|
||||
else:
|
||||
untouched_customers.append(customer.id)
|
||||
|
||||
return Response(
|
||||
{
|
||||
"checked_tryton_parties": checked_tryton_parties,
|
||||
"failed_parties": failed_parties,
|
||||
"updated_customers": updated_customers,
|
||||
"created_customers": created_customers,
|
||||
"untouched_customers": untouched_customers,
|
||||
},
|
||||
status=200,
|
||||
)
|
||||
|
||||
def __get_party_datails(self, party_ids, tryton_client, context):
|
||||
tryton_fields = ["id", "name", "addresses"]
|
||||
method = "model.party.party.read"
|
||||
params = (party_ids, tryton_fields, context)
|
||||
response = tryton_client.call(method, params)
|
||||
return response
|
||||
|
||||
def __need_update(self, customer, tryton_party):
|
||||
if not customer.name == tryton_party.get("name"):
|
||||
return True
|
||||
if tryton_party.get("addresses") and tryton_party.get("addresses")[0]:
|
||||
if not customer.address_external_id == str(
|
||||
tryton_party.get("addresses")[0]
|
||||
):
|
||||
return True
|
||||
|
||||
def __create_customer(self, tryton_party):
|
||||
customer = Customer()
|
||||
customer.name = tryton_party.get("name")
|
||||
customer.external_id = tryton_party.get("id")
|
||||
if tryton_party.get("addresses") and tryton_party.get("addresses")[0]:
|
||||
customer.address_external_id = tryton_party.get("addresses")[0]
|
||||
customer.save()
|
||||
return customer
|
||||
|
||||
def __update_customer(self, customer, tryton_party):
|
||||
customer.name = tryton_party.get("name")
|
||||
customer.external_id = tryton_party.get("id")
|
||||
if tryton_party.get("addresses") and tryton_party.get("addresses")[0]:
|
||||
customer.address_external_id = tryton_party.get("addresses")[0]
|
||||
customer.save()
|
||||
@@ -1 +0,0 @@
|
||||
#!/usr/bin/env python3
|
||||
33
tienda_ilusion/don_confiao/image_service.py
Normal file
33
tienda_ilusion/don_confiao/image_service.py
Normal file
@@ -0,0 +1,33 @@
|
||||
from django.conf import settings
|
||||
from PIL import Image
|
||||
|
||||
|
||||
def resize_catalogue_image(image_field):
|
||||
width = settings.CATALOGUE_IMAGE_WIDTH
|
||||
height = settings.CATALOGUE_IMAGE_HEIGHT
|
||||
strict = settings.CATALOGUE_STRICT_DIMENSION
|
||||
bg_color = settings.CATALOGUE_BACKGROUND_IMAGES_RGBA
|
||||
|
||||
img = Image.open(image_field.path)
|
||||
img = img.convert("RGBA")
|
||||
|
||||
if strict:
|
||||
img = resize_to_fit(img, width, height)
|
||||
canvas = Image.new("RGBA", (width, height), bg_color)
|
||||
x = (width - img.width) // 2
|
||||
y = (height - img.height) // 2
|
||||
canvas.paste(img, (x, y), img)
|
||||
img = canvas
|
||||
else:
|
||||
if img.width > width:
|
||||
new_height = int(img.height * width / img.width)
|
||||
img = img.resize((width, new_height), Image.LANCZOS)
|
||||
|
||||
img.save(image_field.path, format="PNG")
|
||||
|
||||
|
||||
def resize_to_fit(img, max_width, max_height):
|
||||
ratio = min(max_width / img.width, max_height / img.height)
|
||||
new_width = int(img.width * ratio)
|
||||
new_height = int(img.height * ratio)
|
||||
return img.resize((new_width, new_height), Image.LANCZOS)
|
||||
@@ -0,0 +1,18 @@
|
||||
# Generated by Django 5.0.6 on 2026-05-31 01:46
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('don_confiao', '0046_product_active'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AddField(
|
||||
model_name='catalogsale',
|
||||
name='external_id',
|
||||
field=models.CharField(blank=True, max_length=100, null=True),
|
||||
),
|
||||
]
|
||||
@@ -0,0 +1,33 @@
|
||||
# Generated by Django 5.0.6 on 2026-06-05 14:02
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('don_confiao', '0047_catalogsale_external_id'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AddField(
|
||||
model_name='catalogsale',
|
||||
name='customer_address',
|
||||
field=models.CharField(blank=True, max_length=255, null=True),
|
||||
),
|
||||
migrations.AddField(
|
||||
model_name='catalogsale',
|
||||
name='customer_name',
|
||||
field=models.CharField(blank=True, max_length=255, null=True),
|
||||
),
|
||||
migrations.AddField(
|
||||
model_name='catalogsale',
|
||||
name='customer_phone',
|
||||
field=models.CharField(blank=True, max_length=13, null=True),
|
||||
),
|
||||
migrations.AddField(
|
||||
model_name='catalogsale',
|
||||
name='pickup_method',
|
||||
field=models.CharField(blank=True, max_length=30, null=True),
|
||||
),
|
||||
]
|
||||
24
tienda_ilusion/don_confiao/migrations/0049_catalogueimage.py
Normal file
24
tienda_ilusion/don_confiao/migrations/0049_catalogueimage.py
Normal file
@@ -0,0 +1,24 @@
|
||||
# Generated by Django 5.0.6 on 2026-06-13 18:31
|
||||
|
||||
import django.db.models.deletion
|
||||
import don_confiao.models.catalogue_images
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('don_confiao', '0048_catalogsale_customer_address_and_more'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.CreateModel(
|
||||
name='CatalogueImage',
|
||||
fields=[
|
||||
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
||||
('image', models.ImageField(upload_to=don_confiao.models.catalogue_images._catalogue_image_upload_path)),
|
||||
('uploaded_at', models.DateTimeField(auto_now_add=True)),
|
||||
('product', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='catalogue_images', to='don_confiao.product')),
|
||||
],
|
||||
),
|
||||
]
|
||||
20
tienda_ilusion/don_confiao/models/catalogue_images.py
Normal file
20
tienda_ilusion/don_confiao/models/catalogue_images.py
Normal file
@@ -0,0 +1,20 @@
|
||||
from django.db import models
|
||||
|
||||
from .products import Product
|
||||
|
||||
|
||||
def _catalogue_image_upload_path(instance, filename):
|
||||
return f"catalogue_images/{instance.product_id}/{filename}"
|
||||
|
||||
|
||||
class CatalogueImage(models.Model):
|
||||
product = models.ForeignKey(
|
||||
Product,
|
||||
on_delete=models.CASCADE,
|
||||
related_name="catalogue_images",
|
||||
)
|
||||
image = models.ImageField(upload_to=_catalogue_image_upload_path)
|
||||
uploaded_at = models.DateTimeField(auto_now_add=True)
|
||||
|
||||
def __str__(self):
|
||||
return f"CatalogueImage for {self.product.name} ({self.id})"
|
||||
@@ -67,7 +67,6 @@ class Sale(SaleAbstractModel):
|
||||
|
||||
|
||||
class SaleLine(SaleLineAbstractModel):
|
||||
|
||||
sale = models.ForeignKey(Sale, on_delete=models.CASCADE)
|
||||
|
||||
def __str__(self):
|
||||
@@ -75,6 +74,13 @@ class SaleLine(SaleLineAbstractModel):
|
||||
|
||||
|
||||
class CatalogSale(SaleAbstractModel):
|
||||
external_id = models.CharField(max_length=100, null=True, blank=True)
|
||||
customer_name = models.CharField(max_length=255, null=True, blank=True)
|
||||
customer_phone = models.CharField(max_length=13, null=True, blank=True)
|
||||
customer_address = models.CharField(
|
||||
max_length=255, null=True, blank=True
|
||||
)
|
||||
pickup_method = models.CharField(max_length=30, null=True, blank=True)
|
||||
|
||||
def __str__(self):
|
||||
return f"{self.date} {self.customer}"
|
||||
|
||||
42
tienda_ilusion/don_confiao/serializers/__init__.py
Normal file
42
tienda_ilusion/don_confiao/serializers/__init__.py
Normal file
@@ -0,0 +1,42 @@
|
||||
from .catalogue_images import CatalogueImageSerializer
|
||||
from .products import ProductSerializer, ListProductSerializer
|
||||
from .customers import CustomerSerializer, ListCustomerSerializer
|
||||
from .sales import (
|
||||
SaleSerializer,
|
||||
SaleLineSerializer,
|
||||
CatalogSaleSerializer,
|
||||
CatalogSaleLineSerializer,
|
||||
SummarySaleLineSerializer,
|
||||
SaleSummarySerializer,
|
||||
CatalogSummarySaleLineSerializer,
|
||||
CatalogSaleSummarySerializer,
|
||||
SaleForRenconciliationSerializer,
|
||||
)
|
||||
from .payments import (
|
||||
ReconciliationJarSerializer,
|
||||
PaymentMethodSerializer,
|
||||
)
|
||||
|
||||
__all__ = [
|
||||
# Catalogue Images
|
||||
"CatalogueImageSerializer",
|
||||
# Products
|
||||
"ProductSerializer",
|
||||
"ListProductSerializer",
|
||||
# Customers
|
||||
"CustomerSerializer",
|
||||
"ListCustomerSerializer",
|
||||
# Sales
|
||||
"SaleSerializer",
|
||||
"SaleLineSerializer",
|
||||
"CatalogSaleSerializer",
|
||||
"CatalogSaleLineSerializer",
|
||||
"SummarySaleLineSerializer",
|
||||
"SaleSummarySerializer",
|
||||
"CatalogSummarySaleLineSerializer",
|
||||
"CatalogSaleSummarySerializer",
|
||||
"SaleForRenconciliationSerializer",
|
||||
# Payments
|
||||
"ReconciliationJarSerializer",
|
||||
"PaymentMethodSerializer",
|
||||
]
|
||||
20
tienda_ilusion/don_confiao/serializers/catalogue_images.py
Normal file
20
tienda_ilusion/don_confiao/serializers/catalogue_images.py
Normal file
@@ -0,0 +1,20 @@
|
||||
from django.conf import settings
|
||||
from rest_framework import serializers
|
||||
|
||||
from ..models.catalogue_images import CatalogueImage
|
||||
|
||||
|
||||
class CatalogueImageSerializer(serializers.ModelSerializer):
|
||||
class Meta:
|
||||
model = CatalogueImage
|
||||
fields = ["id", "product", "image", "uploaded_at"]
|
||||
read_only_fields = ["uploaded_at"]
|
||||
|
||||
def validate_image(self, value):
|
||||
max_size = settings.CATALOGUE_MAX_UPLOAD_SIZE
|
||||
if value.size > max_size:
|
||||
raise serializers.ValidationError(
|
||||
f"Image size exceeds the maximum allowed size of "
|
||||
f"{max_size // (1024 * 1024)}MB."
|
||||
)
|
||||
return value
|
||||
15
tienda_ilusion/don_confiao/serializers/customers.py
Normal file
15
tienda_ilusion/don_confiao/serializers/customers.py
Normal file
@@ -0,0 +1,15 @@
|
||||
from rest_framework import serializers
|
||||
|
||||
from ..models.customers import Customer
|
||||
|
||||
|
||||
class CustomerSerializer(serializers.ModelSerializer):
|
||||
class Meta:
|
||||
model = Customer
|
||||
fields = ["id", "name", "address", "email", "phone", "external_id"]
|
||||
|
||||
|
||||
class ListCustomerSerializer(serializers.ModelSerializer):
|
||||
class Meta:
|
||||
model = Customer
|
||||
fields = ["id", "name"]
|
||||
31
tienda_ilusion/don_confiao/serializers/payments.py
Normal file
31
tienda_ilusion/don_confiao/serializers/payments.py
Normal file
@@ -0,0 +1,31 @@
|
||||
from rest_framework import serializers
|
||||
|
||||
from ..models.payments import ReconciliationJar, PaymentMethods
|
||||
from .sales import SaleSerializer
|
||||
|
||||
|
||||
class ReconciliationJarSerializer(serializers.ModelSerializer):
|
||||
Sales = SaleSerializer(many=True, read_only=True)
|
||||
|
||||
class Meta:
|
||||
model = ReconciliationJar
|
||||
fields = [
|
||||
"id",
|
||||
"date_time",
|
||||
"reconcilier",
|
||||
"cash_taken",
|
||||
"cash_discrepancy",
|
||||
"total_cash_purchases",
|
||||
"Sales",
|
||||
]
|
||||
|
||||
|
||||
class PaymentMethodSerializer(serializers.Serializer):
|
||||
text = serializers.CharField()
|
||||
value = serializers.CharField()
|
||||
|
||||
def to_representation(self, instance):
|
||||
return {
|
||||
"text": instance[1],
|
||||
"value": instance[0],
|
||||
}
|
||||
46
tienda_ilusion/don_confiao/serializers/products.py
Normal file
46
tienda_ilusion/don_confiao/serializers/products.py
Normal file
@@ -0,0 +1,46 @@
|
||||
from rest_framework import serializers
|
||||
|
||||
from ..models.products import Product, ProductCategory
|
||||
|
||||
|
||||
class ProductSerializer(serializers.ModelSerializer):
|
||||
catalogue_images = serializers.SerializerMethodField()
|
||||
|
||||
class Meta:
|
||||
model = Product
|
||||
fields = [
|
||||
"id",
|
||||
"name",
|
||||
"active",
|
||||
"price",
|
||||
"measuring_unit",
|
||||
"categories",
|
||||
"external_id",
|
||||
"catalogue_images",
|
||||
]
|
||||
|
||||
def get_catalogue_images(self, obj):
|
||||
request = self.context.get("request")
|
||||
if not request:
|
||||
return [img.image.url for img in obj.catalogue_images.all()]
|
||||
return [
|
||||
request.build_absolute_uri(img.image.url)
|
||||
for img in obj.catalogue_images.all()
|
||||
]
|
||||
|
||||
|
||||
class ListProductSerializer(serializers.ModelSerializer):
|
||||
catalogue_images = serializers.SerializerMethodField()
|
||||
|
||||
class Meta:
|
||||
model = Product
|
||||
fields = ["id", "name", "catalogue_images"]
|
||||
|
||||
def get_catalogue_images(self, obj):
|
||||
request = self.context.get("request")
|
||||
if not request:
|
||||
return [img.image.url for img in obj.catalogue_images.all()]
|
||||
return [
|
||||
request.build_absolute_uri(img.image.url)
|
||||
for img in obj.catalogue_images.all()
|
||||
]
|
||||
@@ -1,16 +1,14 @@
|
||||
from rest_framework import serializers
|
||||
|
||||
from .models.sales import Sale, SaleLine
|
||||
from .models.customers import Customer
|
||||
from .models.sales import (
|
||||
from ..models.sales import (
|
||||
Sale,
|
||||
SaleLine,
|
||||
CatalogSale,
|
||||
CatalogSaleLine,
|
||||
Payment,
|
||||
)
|
||||
from .models.products import Product, ProductCategory
|
||||
from .models.payments import ReconciliationJar
|
||||
from .products import ListProductSerializer
|
||||
from .customers import ListCustomerSerializer
|
||||
|
||||
|
||||
class SaleLineSerializer(serializers.ModelSerializer):
|
||||
@@ -62,6 +60,11 @@ class CatalogSaleSerializer(serializers.ModelSerializer):
|
||||
"date",
|
||||
"catalogsaleline_set",
|
||||
"total",
|
||||
"external_id",
|
||||
"customer_name",
|
||||
"customer_phone",
|
||||
"customer_address",
|
||||
"pickup_method",
|
||||
]
|
||||
|
||||
def create(self, validated_data):
|
||||
@@ -76,82 +79,6 @@ class CatalogSaleSerializer(serializers.ModelSerializer):
|
||||
return catalog_sale
|
||||
|
||||
|
||||
class ProductSerializer(serializers.ModelSerializer):
|
||||
class Meta:
|
||||
model = Product
|
||||
fields = [
|
||||
"id",
|
||||
"name",
|
||||
"active",
|
||||
"price",
|
||||
"measuring_unit",
|
||||
"categories",
|
||||
"external_id",
|
||||
]
|
||||
|
||||
|
||||
class CustomerSerializer(serializers.ModelSerializer):
|
||||
class Meta:
|
||||
model = Customer
|
||||
fields = ["id", "name", "address", "email", "phone", "external_id"]
|
||||
|
||||
|
||||
class ReconciliationJarSerializer(serializers.ModelSerializer):
|
||||
Sales = SaleSerializer(many=True, read_only=True)
|
||||
|
||||
class Meta:
|
||||
model = ReconciliationJar
|
||||
fields = [
|
||||
"id",
|
||||
"date_time",
|
||||
"reconcilier",
|
||||
"cash_taken",
|
||||
"cash_discrepancy",
|
||||
"total_cash_purchases",
|
||||
"Sales",
|
||||
]
|
||||
|
||||
|
||||
class PaymentMethodSerializer(serializers.Serializer):
|
||||
text = serializers.CharField()
|
||||
value = serializers.CharField()
|
||||
|
||||
def to_representation(self, instance):
|
||||
return {
|
||||
"text": instance[1],
|
||||
"value": instance[0],
|
||||
}
|
||||
|
||||
|
||||
class SaleForRenconciliationSerializer(serializers.Serializer):
|
||||
id = serializers.IntegerField()
|
||||
date = serializers.DateTimeField()
|
||||
payment_method = serializers.CharField()
|
||||
customer = serializers.SerializerMethodField()
|
||||
total = serializers.SerializerMethodField()
|
||||
|
||||
def get_customer(self, sale):
|
||||
return {
|
||||
"id": sale.customer.id,
|
||||
"name": sale.customer.name,
|
||||
}
|
||||
|
||||
def get_total(self, sale):
|
||||
return sale.get_total()
|
||||
|
||||
|
||||
class ListCustomerSerializer(serializers.ModelSerializer):
|
||||
class Meta:
|
||||
model = Customer
|
||||
fields = ["id", "name"]
|
||||
|
||||
|
||||
class ListProductSerializer(serializers.ModelSerializer):
|
||||
class Meta:
|
||||
model = Product
|
||||
fields = ["id", "name"]
|
||||
|
||||
|
||||
class SummarySaleLineSerializer(serializers.ModelSerializer):
|
||||
product = ListProductSerializer()
|
||||
|
||||
@@ -167,3 +94,39 @@ class SaleSummarySerializer(serializers.ModelSerializer):
|
||||
class Meta:
|
||||
model = Sale
|
||||
fields = ["id", "date", "customer", "payment_method", "lines"]
|
||||
|
||||
|
||||
class CatalogSummarySaleLineSerializer(serializers.ModelSerializer):
|
||||
product = ListProductSerializer()
|
||||
|
||||
class Meta:
|
||||
model = CatalogSaleLine
|
||||
fields = ["product", "quantity", "unit_price", "description"]
|
||||
|
||||
|
||||
class CatalogSaleSummarySerializer(serializers.ModelSerializer):
|
||||
customer = ListCustomerSerializer()
|
||||
lines = CatalogSummarySaleLineSerializer(
|
||||
many=True, source="catalogsaleline_set"
|
||||
)
|
||||
|
||||
class Meta:
|
||||
model = CatalogSale
|
||||
fields = ["id", "date", "customer", "lines"]
|
||||
|
||||
|
||||
class SaleForRenconciliationSerializer(serializers.Serializer):
|
||||
id = serializers.IntegerField()
|
||||
date = serializers.DateTimeField()
|
||||
payment_method = serializers.CharField()
|
||||
customer = serializers.SerializerMethodField()
|
||||
total = serializers.SerializerMethodField()
|
||||
|
||||
def get_customer(self, sale):
|
||||
return {
|
||||
"id": sale.customer.id,
|
||||
"name": sale.customer.name,
|
||||
}
|
||||
|
||||
def get_total(self, sale):
|
||||
return sale.get_total()
|
||||
13
tienda_ilusion/don_confiao/services/__init__.py
Normal file
13
tienda_ilusion/don_confiao/services/__init__.py
Normal file
@@ -0,0 +1,13 @@
|
||||
from .tryton import (
|
||||
get_tryton_client,
|
||||
ProductTrytonService,
|
||||
CustomerTrytonService,
|
||||
SaleTrytonService,
|
||||
)
|
||||
|
||||
__all__ = [
|
||||
"get_tryton_client",
|
||||
"ProductTrytonService",
|
||||
"CustomerTrytonService",
|
||||
"SaleTrytonService",
|
||||
]
|
||||
13
tienda_ilusion/don_confiao/services/tryton/__init__.py
Normal file
13
tienda_ilusion/don_confiao/services/tryton/__init__.py
Normal file
@@ -0,0 +1,13 @@
|
||||
from .client import get_tryton_client, TrytonSale, TrytonLineSale
|
||||
from .products import ProductTrytonService
|
||||
from .customers import CustomerTrytonService
|
||||
from .sales import SaleTrytonService
|
||||
|
||||
__all__ = [
|
||||
"get_tryton_client",
|
||||
"TrytonSale",
|
||||
"TrytonLineSale",
|
||||
"ProductTrytonService",
|
||||
"CustomerTrytonService",
|
||||
"SaleTrytonService",
|
||||
]
|
||||
132
tienda_ilusion/don_confiao/services/tryton/client.py
Normal file
132
tienda_ilusion/don_confiao/services/tryton/client.py
Normal file
@@ -0,0 +1,132 @@
|
||||
import os
|
||||
from sabatron_tryton_rpc_client.client import Client
|
||||
|
||||
TRYTON_HOST = os.environ.get("TRYTON_HOST", "localhost")
|
||||
TRYTON_DATABASE = os.environ.get("TRYTON_DATABASE", "tryton")
|
||||
TRYTON_USERNAME = os.environ.get("TRYTON_USERNAME", "admin")
|
||||
TRYTON_PASSWORD = os.environ.get("TRYTON_PASSWORD", "admin")
|
||||
TRYTON_COP_CURRENCY = 31
|
||||
TRYTON_COMPANY_ID = 1
|
||||
TRYTON_SHOPS = [1]
|
||||
|
||||
|
||||
def get_tryton_client():
|
||||
"""Factory para crear cliente Tryton conectado"""
|
||||
client = Client(
|
||||
hostname=TRYTON_HOST,
|
||||
database=TRYTON_DATABASE,
|
||||
username=TRYTON_USERNAME,
|
||||
password=TRYTON_PASSWORD,
|
||||
)
|
||||
client.connect()
|
||||
return client
|
||||
|
||||
|
||||
class TrytonSale:
|
||||
"""Representa una venta para exportación a Tryton"""
|
||||
|
||||
def __init__(self, sale, lines):
|
||||
self.sale = sale
|
||||
self.lines = lines
|
||||
|
||||
def _format_date(self, _date):
|
||||
return {
|
||||
"__class__": "date",
|
||||
"year": _date.year,
|
||||
"month": _date.month,
|
||||
"day": _date.day,
|
||||
}
|
||||
|
||||
def to_tryton(self):
|
||||
return {
|
||||
"company": TRYTON_COMPANY_ID,
|
||||
"shipment_address": self.sale.customer.address_external_id,
|
||||
"invoice_address": self.sale.customer.address_external_id,
|
||||
"currency": TRYTON_COP_CURRENCY,
|
||||
"comment": self.sale.description or "",
|
||||
"description": "Metodo pago: " + str(self.sale.payment_method or ""),
|
||||
"party": self.sale.customer.external_id,
|
||||
"reference": "don_confiao " + str(self.sale.id),
|
||||
"sale_date": self._format_date(self.sale.date),
|
||||
"lines": [
|
||||
[
|
||||
"create",
|
||||
[TrytonLineSale(line).to_tryton() for line in self.lines],
|
||||
]
|
||||
],
|
||||
"self_pick_up": True,
|
||||
}
|
||||
|
||||
|
||||
class TrytonLineSale:
|
||||
"""Representa una línea de venta para exportación a Tryton"""
|
||||
|
||||
def __init__(self, sale_line):
|
||||
self.sale_line = sale_line
|
||||
|
||||
def _format_decimal(self, number):
|
||||
return {"__class__": "Decimal", "decimal": str(number)}
|
||||
|
||||
def to_tryton(self):
|
||||
return {
|
||||
"product": self.sale_line.product.external_id,
|
||||
"quantity": self._format_decimal(self.sale_line.quantity),
|
||||
"type": "line",
|
||||
"unit": self.sale_line.product.unit_external_id,
|
||||
"unit_price": self._format_decimal(self.sale_line.unit_price),
|
||||
}
|
||||
|
||||
|
||||
class TrytonCatalogSale:
|
||||
"""Representa una catalog sale para exportación a Tryton"""
|
||||
|
||||
def __init__(self, catalog_sale, lines):
|
||||
self.catalog_sale = catalog_sale
|
||||
self.lines = lines
|
||||
|
||||
def _format_date(self, _date):
|
||||
return {
|
||||
"__class__": "date",
|
||||
"year": _date.year,
|
||||
"month": _date.month,
|
||||
"day": _date.day,
|
||||
}
|
||||
|
||||
def to_tryton(self):
|
||||
return {
|
||||
"company": TRYTON_COMPANY_ID,
|
||||
"shipment_address": self.catalog_sale.customer.address_external_id,
|
||||
"invoice_address": self.catalog_sale.customer.address_external_id,
|
||||
"currency": TRYTON_COP_CURRENCY,
|
||||
"comment": self.catalog_sale.description or "",
|
||||
"description": "Venta de catálogo",
|
||||
"party": self.catalog_sale.customer.external_id,
|
||||
"reference": "don_confiao_catalog " + str(self.catalog_sale.id),
|
||||
"sale_date": self._format_date(self.catalog_sale.date),
|
||||
"lines": [
|
||||
[
|
||||
"create",
|
||||
[TrytonCatalogSaleLine(line).to_tryton() for line in self.lines],
|
||||
]
|
||||
],
|
||||
"self_pick_up": True,
|
||||
}
|
||||
|
||||
|
||||
class TrytonCatalogSaleLine:
|
||||
"""Representa una línea de catalog sale para exportación a Tryton"""
|
||||
|
||||
def __init__(self, catalog_sale_line):
|
||||
self.catalog_sale_line = catalog_sale_line
|
||||
|
||||
def _format_decimal(self, number):
|
||||
return {"__class__": "Decimal", "decimal": str(number)}
|
||||
|
||||
def to_tryton(self):
|
||||
return {
|
||||
"product": self.catalog_sale_line.product.external_id,
|
||||
"quantity": self._format_decimal(self.catalog_sale_line.quantity),
|
||||
"type": "line",
|
||||
"unit": self.catalog_sale_line.product.unit_external_id,
|
||||
"unit_price": self._format_decimal(self.catalog_sale_line.unit_price),
|
||||
}
|
||||
81
tienda_ilusion/don_confiao/services/tryton/customers.py
Normal file
81
tienda_ilusion/don_confiao/services/tryton/customers.py
Normal file
@@ -0,0 +1,81 @@
|
||||
from ...models.customers import Customer
|
||||
|
||||
|
||||
class CustomerTrytonService:
|
||||
"""Servicio para sincronización de clientes con Tryton"""
|
||||
|
||||
def __init__(self, tryton_client):
|
||||
self.client = tryton_client
|
||||
|
||||
def import_from_tryton(self):
|
||||
"""Importa clientes desde Tryton"""
|
||||
method = "model.party.party.search"
|
||||
context = {"company": 1}
|
||||
params = [[], 0, 1000, [["name", "ASC"], ["id", None]], context]
|
||||
party_ids = self.client.call(method, params)
|
||||
tryton_parties = self._get_party_details(party_ids, context)
|
||||
|
||||
checked_tryton_parties = party_ids
|
||||
failed_parties = []
|
||||
updated_customers = []
|
||||
created_customers = []
|
||||
untouched_customers = []
|
||||
|
||||
for tryton_party in tryton_parties:
|
||||
try:
|
||||
customer = Customer.objects.get(external_id=tryton_party.get("id"))
|
||||
except Customer.DoesNotExist:
|
||||
customer = self._create_customer(tryton_party)
|
||||
created_customers.append(customer.id)
|
||||
continue
|
||||
|
||||
if self._need_update(customer, tryton_party):
|
||||
self._update_customer(customer, tryton_party)
|
||||
updated_customers.append(customer.id)
|
||||
else:
|
||||
untouched_customers.append(customer.id)
|
||||
|
||||
return {
|
||||
"checked_tryton_parties": checked_tryton_parties,
|
||||
"failed_parties": failed_parties,
|
||||
"updated_customers": updated_customers,
|
||||
"created_customers": created_customers,
|
||||
"untouched_customers": untouched_customers,
|
||||
}
|
||||
|
||||
def _get_party_details(self, party_ids, context):
|
||||
"""Obtiene detalles de clientes desde Tryton"""
|
||||
tryton_fields = ["id", "name", "addresses"]
|
||||
method = "model.party.party.read"
|
||||
params = (party_ids, tryton_fields, context)
|
||||
response = self.client.call(method, params)
|
||||
return response
|
||||
|
||||
def _need_update(self, customer, tryton_party):
|
||||
"""Verifica si el cliente necesita actualización"""
|
||||
if not customer.name == tryton_party.get("name"):
|
||||
return True
|
||||
if tryton_party.get("addresses") and tryton_party.get("addresses")[0]:
|
||||
if not customer.address_external_id == str(
|
||||
tryton_party.get("addresses")[0]
|
||||
):
|
||||
return True
|
||||
return False
|
||||
|
||||
def _create_customer(self, tryton_party):
|
||||
"""Crea un nuevo cliente desde datos de Tryton"""
|
||||
customer = Customer()
|
||||
customer.name = tryton_party.get("name")
|
||||
customer.external_id = tryton_party.get("id")
|
||||
if tryton_party.get("addresses") and tryton_party.get("addresses")[0]:
|
||||
customer.address_external_id = tryton_party.get("addresses")[0]
|
||||
customer.save()
|
||||
return customer
|
||||
|
||||
def _update_customer(self, customer, tryton_party):
|
||||
"""Actualiza un cliente existente con datos de Tryton"""
|
||||
customer.name = tryton_party.get("name")
|
||||
customer.external_id = tryton_party.get("id")
|
||||
if tryton_party.get("addresses") and tryton_party.get("addresses")[0]:
|
||||
customer.address_external_id = tryton_party.get("addresses")[0]
|
||||
customer.save()
|
||||
104
tienda_ilusion/don_confiao/services/tryton/products.py
Normal file
104
tienda_ilusion/don_confiao/services/tryton/products.py
Normal file
@@ -0,0 +1,104 @@
|
||||
from ...models.products import Product
|
||||
|
||||
|
||||
class ProductTrytonService:
|
||||
"""Servicio para sincronización de productos con Tryton"""
|
||||
|
||||
def __init__(self, tryton_client):
|
||||
self.client = tryton_client
|
||||
|
||||
def import_from_tryton(self):
|
||||
"""Importa productos desde Tryton"""
|
||||
method = "model.product.product.search"
|
||||
context = {"company": 1}
|
||||
params = [
|
||||
[["salable", "=", True]],
|
||||
0,
|
||||
1000,
|
||||
[["rec_name", "ASC"], ["id", None]],
|
||||
context,
|
||||
]
|
||||
product_ids = self.client.call(method, params)
|
||||
tryton_products = self._get_product_details(product_ids, context)
|
||||
|
||||
checked_tryton_products = product_ids
|
||||
failed_products = []
|
||||
updated_products = []
|
||||
created_products = []
|
||||
untouched_products = []
|
||||
|
||||
for tryton_product in tryton_products:
|
||||
try:
|
||||
product = Product.objects.get(external_id=tryton_product.get("id"))
|
||||
except Product.DoesNotExist:
|
||||
try:
|
||||
product = self._create_product(tryton_product)
|
||||
created_products.append(product.id)
|
||||
continue
|
||||
except Exception as e:
|
||||
print(
|
||||
f"Error al importar productos: {e}El producto: {tryton_product}"
|
||||
)
|
||||
failed_products.append(tryton_product.get("id"))
|
||||
continue
|
||||
|
||||
if self._need_update(product, tryton_product):
|
||||
self._update_product(product, tryton_product)
|
||||
updated_products.append(product.id)
|
||||
else:
|
||||
untouched_products.append(product.id)
|
||||
|
||||
return {
|
||||
"checked_tryton_products": checked_tryton_products,
|
||||
"failed_products": failed_products,
|
||||
"updated_products": updated_products,
|
||||
"created_products": created_products,
|
||||
"untouched_products": untouched_products,
|
||||
}
|
||||
|
||||
def _get_product_details(self, product_ids, context):
|
||||
"""Obtiene detalles de productos desde Tryton"""
|
||||
tryton_fields = [
|
||||
"id",
|
||||
"name",
|
||||
"default_uom.id",
|
||||
"default_uom.rec_name",
|
||||
"list_price",
|
||||
]
|
||||
method = "model.product.product.read"
|
||||
params = (product_ids, tryton_fields, context)
|
||||
response = self.client.call(method, params)
|
||||
return response
|
||||
|
||||
def _need_update(self, product, tryton_product):
|
||||
"""Verifica si el producto necesita actualización"""
|
||||
if not product.name == tryton_product.get("name"):
|
||||
return True
|
||||
if not product.price == tryton_product.get("list_price"):
|
||||
return True
|
||||
unit = tryton_product.get("default_uom.")
|
||||
if not product.measuring_unit == unit.get("rec_name"):
|
||||
return True
|
||||
return False
|
||||
|
||||
def _create_product(self, tryton_product):
|
||||
"""Crea un nuevo producto desde datos de Tryton"""
|
||||
product = Product()
|
||||
product.name = tryton_product.get("name")
|
||||
product.price = tryton_product.get("list_price")
|
||||
product.external_id = tryton_product.get("id")
|
||||
unit = tryton_product.get("default_uom.")
|
||||
product.measuring_unit = unit.get("rec_name")
|
||||
product.unit_external_id = unit.get("id")
|
||||
product.save()
|
||||
return product
|
||||
|
||||
def _update_product(self, product, tryton_product):
|
||||
"""Actualiza un producto existente con datos de Tryton"""
|
||||
product.name = tryton_product.get("name")
|
||||
product.price = tryton_product.get("list_price")
|
||||
product.external_id = tryton_product.get("id")
|
||||
unit = tryton_product.get("default_uom.")
|
||||
product.measuring_unit = unit.get("rec_name")
|
||||
product.unit_external_id = unit.get("id")
|
||||
product.save()
|
||||
77
tienda_ilusion/don_confiao/services/tryton/sales.py
Normal file
77
tienda_ilusion/don_confiao/services/tryton/sales.py
Normal file
@@ -0,0 +1,77 @@
|
||||
from ...models.sales import Sale, SaleLine, CatalogSale, CatalogSaleLine
|
||||
from .client import TrytonSale, TrytonCatalogSale, TRYTON_COMPANY_ID, TRYTON_SHOPS
|
||||
|
||||
|
||||
class SaleTrytonService:
|
||||
"""Servicio para sincronización de ventas con Tryton"""
|
||||
|
||||
def __init__(self, tryton_client):
|
||||
self.client = tryton_client
|
||||
|
||||
def send_to_tryton(self):
|
||||
"""Envía ventas sin external_id a Tryton"""
|
||||
method = "model.sale.sale.create"
|
||||
tryton_context = {
|
||||
"company": TRYTON_COMPANY_ID,
|
||||
"shops": TRYTON_SHOPS,
|
||||
}
|
||||
|
||||
successful = []
|
||||
failed = []
|
||||
|
||||
sales = Sale.objects.filter(external_id=None)
|
||||
for sale in sales:
|
||||
try:
|
||||
lines = SaleLine.objects.filter(sale=sale.id)
|
||||
tryton_params = self._to_tryton_params(sale, lines, tryton_context)
|
||||
external_ids = self.client.call(method, tryton_params)
|
||||
sale.external_id = external_ids[0]
|
||||
sale.save()
|
||||
successful.append(sale.id)
|
||||
except Exception as e:
|
||||
print(f"Error al enviar la venta: {e}venta_id: {sale.id}")
|
||||
failed.append(sale.id)
|
||||
continue
|
||||
|
||||
return {"successful": successful, "failed": failed}
|
||||
|
||||
def _to_tryton_params(self, sale, lines, tryton_context):
|
||||
"""Convierte venta a parámetros para Tryton"""
|
||||
sale_tryton = TrytonSale(sale, lines)
|
||||
return [[sale_tryton.to_tryton()], tryton_context]
|
||||
|
||||
def send_catalog_sales_to_tryton(self):
|
||||
"""Envía catalog sales sin external_id a Tryton"""
|
||||
method = "model.sale.sale.create"
|
||||
tryton_context = {
|
||||
"company": TRYTON_COMPANY_ID,
|
||||
"shops": TRYTON_SHOPS,
|
||||
}
|
||||
|
||||
successful = []
|
||||
failed = []
|
||||
|
||||
catalog_sales = CatalogSale.objects.filter(external_id=None)
|
||||
for catalog_sale in catalog_sales:
|
||||
try:
|
||||
lines = CatalogSaleLine.objects.filter(catalog_sale=catalog_sale.id)
|
||||
tryton_params = self._catalog_sale_to_tryton_params(
|
||||
catalog_sale, lines, tryton_context
|
||||
)
|
||||
external_ids = self.client.call(method, tryton_params)
|
||||
catalog_sale.external_id = external_ids[0]
|
||||
catalog_sale.save()
|
||||
successful.append(catalog_sale.id)
|
||||
except Exception as e:
|
||||
print(
|
||||
f"Error al enviar catalog sale: {e}, catalog_sale_id: {catalog_sale.id}"
|
||||
)
|
||||
failed.append(catalog_sale.id)
|
||||
continue
|
||||
|
||||
return {"successful": successful, "failed": failed}
|
||||
|
||||
def _catalog_sale_to_tryton_params(self, catalog_sale, lines, tryton_context):
|
||||
"""Convierte catalog sale a parámetros para Tryton"""
|
||||
sale_tryton = TrytonCatalogSale(catalog_sale, lines)
|
||||
return [[sale_tryton.to_tryton()], tryton_context]
|
||||
@@ -86,6 +86,67 @@ class TestAPI(APITestCase, LoginMixin):
|
||||
self.assertIn("csv", json_response)
|
||||
self.assertGreater(len(json_response["csv"]), 0)
|
||||
|
||||
def test_catalog_sale_summary(self):
|
||||
# Create a catalog sale
|
||||
response = self._create_catalog_sale()
|
||||
content = json.loads(response.content.decode("utf-8"))
|
||||
catalog_sale_id = content["id"]
|
||||
|
||||
# Get the summary
|
||||
url = f"/don_confiao/resumen_compra_catalogo_json/{catalog_sale_id}"
|
||||
response = self.client.get(url)
|
||||
|
||||
# Verify response
|
||||
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
||||
json_response = json.loads(response.content.decode("utf-8"))
|
||||
|
||||
# Verify structure
|
||||
self.assertIn("id", json_response)
|
||||
self.assertIn("date", json_response)
|
||||
self.assertIn("customer", json_response)
|
||||
self.assertIn("lines", json_response)
|
||||
|
||||
# Verify customer details
|
||||
self.assertEqual(json_response["customer"]["id"], self.customer.id)
|
||||
self.assertEqual(json_response["customer"]["name"], self.customer.name)
|
||||
|
||||
# Verify lines
|
||||
self.assertEqual(len(json_response["lines"]), 2)
|
||||
|
||||
# Verify first line
|
||||
line1 = json_response["lines"][0]
|
||||
self.assertIn("product", line1)
|
||||
self.assertIn("quantity", line1)
|
||||
self.assertIn("unit_price", line1)
|
||||
self.assertEqual(line1["product"]["id"], self.product.id)
|
||||
self.assertEqual(line1["product"]["name"], self.product.name)
|
||||
self.assertEqual(line1["quantity"], "2.00")
|
||||
self.assertEqual(line1["unit_price"], "3000.00")
|
||||
|
||||
# Verify second line
|
||||
line2 = json_response["lines"][1]
|
||||
self.assertEqual(line2["product"]["id"], self.product.id)
|
||||
self.assertEqual(line2["quantity"], "3.00")
|
||||
self.assertEqual(line2["unit_price"], "5000.00")
|
||||
|
||||
def test_catalog_sale_has_external_id_field(self):
|
||||
"""Verifica que CatalogSale tiene el campo external_id"""
|
||||
response = self._create_catalog_sale()
|
||||
content = json.loads(response.content.decode("utf-8"))
|
||||
catalog_sale_id = content["id"]
|
||||
|
||||
catalog_sale = CatalogSale.objects.get(pk=catalog_sale_id)
|
||||
# Debe tener el campo external_id
|
||||
self.assertIsNone(catalog_sale.external_id)
|
||||
|
||||
# Se puede asignar un valor
|
||||
catalog_sale.external_id = "123"
|
||||
catalog_sale.save()
|
||||
|
||||
# Verificar que se guardó
|
||||
catalog_sale.refresh_from_db()
|
||||
self.assertEqual(catalog_sale.external_id, "123")
|
||||
|
||||
def test_csv_structure_in_sales_for_tryton(self):
|
||||
url = "/don_confiao/api/sales/for_tryton"
|
||||
self._create_sale()
|
||||
|
||||
408
tienda_ilusion/don_confiao/tests/test_catalogue_images.py
Normal file
408
tienda_ilusion/don_confiao/tests/test_catalogue_images.py
Normal file
@@ -0,0 +1,408 @@
|
||||
import io
|
||||
|
||||
from django.conf import settings
|
||||
from django.contrib.auth.models import User
|
||||
from django.core.files.uploadedfile import SimpleUploadedFile
|
||||
from django.test import override_settings
|
||||
from PIL import Image
|
||||
from rest_framework import status
|
||||
from rest_framework.test import APIClient, APITestCase
|
||||
from rest_framework_simplejwt.tokens import RefreshToken
|
||||
|
||||
from ..models.catalogue_images import CatalogueImage
|
||||
from ..models.products import Product
|
||||
from .Mixins import LoginMixin
|
||||
|
||||
|
||||
def _create_test_image(width=600, height=400, name="test.png"):
|
||||
"""Create a SimpleUploadedFile from an in-memory image using PIL."""
|
||||
img = Image.new("RGBA", (width, height), (255, 0, 0, 255))
|
||||
output = io.BytesIO()
|
||||
img.save(output, format="PNG")
|
||||
return SimpleUploadedFile(
|
||||
name=name,
|
||||
content=output.getvalue(),
|
||||
content_type="image/png",
|
||||
)
|
||||
|
||||
|
||||
class TestCatalogueImageModel(APITestCase, LoginMixin):
|
||||
def setUp(self):
|
||||
self.login()
|
||||
self.product = Product.objects.create(
|
||||
name="Test Product", price=100.00
|
||||
)
|
||||
|
||||
def test_create_catalogue_image(self):
|
||||
image_file = _create_test_image()
|
||||
catalogue_image = CatalogueImage.objects.create(
|
||||
product=self.product, image=image_file
|
||||
)
|
||||
self.assertIsInstance(catalogue_image, CatalogueImage)
|
||||
self.assertEqual(catalogue_image.product, self.product)
|
||||
self.assertIsNotNone(catalogue_image.image)
|
||||
self.assertIsNotNone(catalogue_image.uploaded_at)
|
||||
|
||||
def test_catalogue_image_product_relation(self):
|
||||
image_file = _create_test_image()
|
||||
ci = CatalogueImage.objects.create(
|
||||
product=self.product, image=image_file
|
||||
)
|
||||
self.assertIn(ci, self.product.catalogue_images.all())
|
||||
|
||||
def test_catalogue_image_str(self):
|
||||
image_file = _create_test_image()
|
||||
ci = CatalogueImage.objects.create(
|
||||
product=self.product, image=image_file
|
||||
)
|
||||
expected = f"CatalogueImage for {self.product.name} ({ci.id})"
|
||||
self.assertEqual(str(ci), expected)
|
||||
|
||||
def test_cascade_delete_with_product(self):
|
||||
image_file = _create_test_image()
|
||||
CatalogueImage.objects.create(
|
||||
product=self.product, image=image_file
|
||||
)
|
||||
self.assertEqual(CatalogueImage.objects.count(), 1)
|
||||
self.product.delete()
|
||||
self.assertEqual(CatalogueImage.objects.count(), 0)
|
||||
|
||||
|
||||
class TestCatalogueImageAPIPermissions(APITestCase, LoginMixin):
|
||||
def setUp(self):
|
||||
self.product = Product.objects.create(
|
||||
name="Perm Test Product", price=100.00
|
||||
)
|
||||
|
||||
def test_create_catalogue_image_unauthenticated(self):
|
||||
url = "/don_confiao/api/catalogue_images/"
|
||||
image_file = _create_test_image()
|
||||
data = {"product": self.product.id, "image": image_file}
|
||||
response = self.client.post(url, data, format="multipart")
|
||||
self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED)
|
||||
|
||||
def test_create_catalogue_image_non_admin(self):
|
||||
self.user = User.objects.create_user(
|
||||
username="regularuser",
|
||||
email="regular@example.com",
|
||||
password="regularpass",
|
||||
)
|
||||
refresh = RefreshToken.for_user(self.user)
|
||||
self.client = APIClient()
|
||||
self.client.credentials(
|
||||
HTTP_AUTHORIZATION=f"Bearer {str(refresh.access_token)}"
|
||||
)
|
||||
|
||||
url = "/don_confiao/api/catalogue_images/"
|
||||
image_file = _create_test_image()
|
||||
data = {"product": self.product.id, "image": image_file}
|
||||
response = self.client.post(url, data, format="multipart")
|
||||
self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
|
||||
|
||||
def test_get_catalogue_images_authenticated_non_admin(self):
|
||||
self.user = User.objects.create_user(
|
||||
username="regularuser2",
|
||||
email="regular2@example.com",
|
||||
password="regularpass",
|
||||
)
|
||||
refresh = RefreshToken.for_user(self.user)
|
||||
self.client = APIClient()
|
||||
self.client.credentials(
|
||||
HTTP_AUTHORIZATION=f"Bearer {str(refresh.access_token)}"
|
||||
)
|
||||
|
||||
url = "/don_confiao/api/catalogue_images/"
|
||||
response = self.client.get(url)
|
||||
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
||||
|
||||
def test_delete_catalogue_image_non_admin(self):
|
||||
admin_user = User.objects.create_superuser(
|
||||
username="admin2", email="admin2@example.com", password="adminpass"
|
||||
)
|
||||
admin_refresh = RefreshToken.for_user(admin_user)
|
||||
admin_client = APIClient()
|
||||
admin_client.credentials(
|
||||
HTTP_AUTHORIZATION=f"Bearer {str(admin_refresh.access_token)}"
|
||||
)
|
||||
|
||||
image_file = _create_test_image()
|
||||
create_data = {
|
||||
"product": self.product.id, "image": image_file
|
||||
}
|
||||
create_response = admin_client.post(
|
||||
"/don_confiao/api/catalogue_images/", create_data, format="multipart"
|
||||
)
|
||||
ci_id = create_response.json()["id"]
|
||||
|
||||
regular_user = User.objects.create_user(
|
||||
username="regularuser3",
|
||||
email="regular3@example.com",
|
||||
password="regularpass",
|
||||
)
|
||||
refresh = RefreshToken.for_user(regular_user)
|
||||
self.client = APIClient()
|
||||
self.client.credentials(
|
||||
HTTP_AUTHORIZATION=f"Bearer {str(refresh.access_token)}"
|
||||
)
|
||||
|
||||
response = self.client.delete(
|
||||
f"/don_confiao/api/catalogue_images/{ci_id}/"
|
||||
)
|
||||
self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
|
||||
|
||||
self.assertEqual(CatalogueImage.objects.count(), 1)
|
||||
|
||||
|
||||
class TestCatalogueImageAPI(APITestCase, LoginMixin):
|
||||
def setUp(self):
|
||||
self.login()
|
||||
self.product = Product.objects.create(
|
||||
name="API Test Product", price=100.00
|
||||
)
|
||||
self.product2 = Product.objects.create(
|
||||
name="API Test Product 2", price=200.00
|
||||
)
|
||||
|
||||
def _create_image(self, product=None, width=600, height=400):
|
||||
if product is None:
|
||||
product = self.product
|
||||
image_file = _create_test_image(width=width, height=height)
|
||||
url = "/don_confiao/api/catalogue_images/"
|
||||
data = {"product": product.id, "image": image_file}
|
||||
return self.client.post(url, data, format="multipart")
|
||||
|
||||
def test_list_catalogue_images_empty(self):
|
||||
url = "/don_confiao/api/catalogue_images/"
|
||||
response = self.client.get(url)
|
||||
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
||||
self.assertEqual(response.json(), [])
|
||||
|
||||
def test_create_catalogue_image(self):
|
||||
response = self._create_image()
|
||||
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
|
||||
data = response.json()
|
||||
self.assertIn("id", data)
|
||||
self.assertEqual(data["product"], self.product.id)
|
||||
self.assertIn("image", data)
|
||||
self.assertIn("uploaded_at", data)
|
||||
|
||||
def test_list_catalogue_images(self):
|
||||
self._create_image()
|
||||
self._create_image(product=self.product2)
|
||||
|
||||
url = "/don_confiao/api/catalogue_images/"
|
||||
response = self.client.get(url)
|
||||
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
||||
self.assertEqual(len(response.json()), 2)
|
||||
|
||||
def test_retrieve_catalogue_image(self):
|
||||
create_response = self._create_image()
|
||||
ci_id = create_response.json()["id"]
|
||||
|
||||
url = f"/don_confiao/api/catalogue_images/{ci_id}/"
|
||||
response = self.client.get(url)
|
||||
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
||||
self.assertEqual(response.json()["id"], ci_id)
|
||||
self.assertEqual(response.json()["product"], self.product.id)
|
||||
|
||||
def test_update_catalogue_image(self):
|
||||
create_response = self._create_image()
|
||||
ci_id = create_response.json()["id"]
|
||||
|
||||
new_image = _create_test_image(width=200, height=200, name="updated.png")
|
||||
url = f"/don_confiao/api/catalogue_images/{ci_id}/"
|
||||
response = self.client.put(
|
||||
url,
|
||||
{"product": self.product.id, "image": new_image},
|
||||
format="multipart",
|
||||
)
|
||||
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
||||
|
||||
def test_delete_catalogue_image(self):
|
||||
create_response = self._create_image()
|
||||
ci_id = create_response.json()["id"]
|
||||
|
||||
url = f"/don_confiao/api/catalogue_images/{ci_id}/"
|
||||
response = self.client.delete(url)
|
||||
self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT)
|
||||
|
||||
self.assertEqual(CatalogueImage.objects.count(), 0)
|
||||
|
||||
def test_create_catalogue_image_invalid_product(self):
|
||||
image_file = _create_test_image()
|
||||
url = "/don_confiao/api/catalogue_images/"
|
||||
data = {"product": 99999, "image": image_file}
|
||||
response = self.client.post(url, data, format="multipart")
|
||||
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
|
||||
|
||||
def test_create_catalogue_image_missing_file(self):
|
||||
url = "/don_confiao/api/catalogue_images/"
|
||||
data = {"product": self.product.id}
|
||||
response = self.client.post(url, data, format="multipart")
|
||||
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
|
||||
|
||||
|
||||
class TestCatalogueImageResize(APITestCase, LoginMixin):
|
||||
def setUp(self):
|
||||
self.login()
|
||||
self.product = Product.objects.create(
|
||||
name="Resize Test Product", price=100.00
|
||||
)
|
||||
|
||||
def _create_and_get_image_obj(self, width, height, strict=False,
|
||||
bg_color=(0, 0, 0, 0)):
|
||||
settings_overrides = {
|
||||
"CATALOGUE_IMAGE_WIDTH": 600,
|
||||
"CATALOGUE_IMAGE_HEIGHT": 600,
|
||||
"CATALOGUE_STRICT_DIMENSION": strict,
|
||||
"CATALOGUE_BACKGROUND_IMAGES_RGBA": bg_color,
|
||||
}
|
||||
|
||||
with override_settings(**settings_overrides):
|
||||
image_file = _create_test_image(width=width, height=height)
|
||||
url = "/don_confiao/api/catalogue_images/"
|
||||
data = {"product": self.product.id, "image": image_file}
|
||||
response = self.client.post(url, data, format="multipart")
|
||||
|
||||
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
|
||||
ci_id = response.json()["id"]
|
||||
return CatalogueImage.objects.get(id=ci_id)
|
||||
|
||||
def test_resize_non_strict_mode_landscape(self):
|
||||
ci = self._create_and_get_image_obj(
|
||||
width=800, height=600, strict=False
|
||||
)
|
||||
with Image.open(ci.image.path) as img:
|
||||
self.assertEqual(img.width, 600)
|
||||
expected_height = int(600 * 600 / 800)
|
||||
self.assertEqual(img.height, expected_height)
|
||||
|
||||
def test_resize_non_strict_mode_portrait(self):
|
||||
ci = self._create_and_get_image_obj(
|
||||
width=600, height=800, strict=False
|
||||
)
|
||||
with Image.open(ci.image.path) as img:
|
||||
self.assertEqual(img.width, 600)
|
||||
expected_height = int(800 * 600 / 600)
|
||||
self.assertEqual(img.height, expected_height)
|
||||
|
||||
def test_resize_non_strict_mode_square(self):
|
||||
ci = self._create_and_get_image_obj(
|
||||
width=600, height=600, strict=False
|
||||
)
|
||||
with Image.open(ci.image.path) as img:
|
||||
self.assertEqual(img.width, 600)
|
||||
self.assertEqual(img.height, 600)
|
||||
|
||||
def test_resize_non_strict_no_upscale(self):
|
||||
ci = self._create_and_get_image_obj(
|
||||
width=400, height=300, strict=False
|
||||
)
|
||||
with Image.open(ci.image.path) as img:
|
||||
self.assertEqual(img.width, 400)
|
||||
self.assertEqual(img.height, 300)
|
||||
|
||||
def test_resize_strict_mode_landscape(self):
|
||||
ci = self._create_and_get_image_obj(
|
||||
width=800, height=600, strict=True
|
||||
)
|
||||
with Image.open(ci.image.path) as img:
|
||||
self.assertEqual(img.width, 600)
|
||||
self.assertEqual(img.height, 600)
|
||||
|
||||
def test_resize_strict_mode_portrait(self):
|
||||
ci = self._create_and_get_image_obj(
|
||||
width=600, height=800, strict=True
|
||||
)
|
||||
with Image.open(ci.image.path) as img:
|
||||
self.assertEqual(img.width, 600)
|
||||
self.assertEqual(img.height, 600)
|
||||
|
||||
def test_resize_strict_mode_with_color_background(self):
|
||||
ci = self._create_and_get_image_obj(
|
||||
width=800, height=600, strict=True, bg_color=(255, 255, 255, 255)
|
||||
)
|
||||
with Image.open(ci.image.path) as img:
|
||||
self.assertEqual(img.width, 600)
|
||||
self.assertEqual(img.height, 600)
|
||||
|
||||
@override_settings(CATALOGUE_MAX_UPLOAD_SIZE=1)
|
||||
def test_resize_large_file_rejected(self):
|
||||
image_file = _create_test_image(width=10, height=10)
|
||||
url = "/don_confiao/api/catalogue_images/"
|
||||
data = {"product": self.product.id, "image": image_file}
|
||||
response = self.client.post(url, data, format="multipart")
|
||||
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
|
||||
|
||||
|
||||
class TestProductListingWithImages(APITestCase, LoginMixin):
|
||||
def setUp(self):
|
||||
self.login()
|
||||
self.product = Product.objects.create(
|
||||
name="Product With Images", price=100.00
|
||||
)
|
||||
self.product_no_images = Product.objects.create(
|
||||
name="Product Without Images", price=200.00
|
||||
)
|
||||
|
||||
def test_product_list_includes_catalogue_images_field(self):
|
||||
url = "/don_confiao/api/products/"
|
||||
response = self.client.get(url)
|
||||
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
||||
for product in response.json():
|
||||
self.assertIn("catalogue_images", product)
|
||||
|
||||
def test_product_list_catalogue_images_urls(self):
|
||||
image_file = _create_test_image()
|
||||
CatalogueImage.objects.create(
|
||||
product=self.product, image=image_file
|
||||
)
|
||||
|
||||
url = "/don_confiao/api/products/"
|
||||
response = self.client.get(url)
|
||||
data = response.json()
|
||||
|
||||
product_data = next(
|
||||
p for p in data if p["id"] == self.product.id
|
||||
)
|
||||
self.assertEqual(len(product_data["catalogue_images"]), 1)
|
||||
self.assertTrue(
|
||||
product_data["catalogue_images"][0].endswith(".png")
|
||||
)
|
||||
|
||||
def test_product_list_catalogue_images_multiple(self):
|
||||
img1 = _create_test_image(name="img1.png")
|
||||
img2 = _create_test_image(name="img2.png")
|
||||
CatalogueImage.objects.create(product=self.product, image=img1)
|
||||
CatalogueImage.objects.create(product=self.product, image=img2)
|
||||
|
||||
url = "/don_confiao/api/products/"
|
||||
response = self.client.get(url)
|
||||
data = response.json()
|
||||
|
||||
product_data = next(
|
||||
p for p in data if p["id"] == self.product.id
|
||||
)
|
||||
self.assertEqual(len(product_data["catalogue_images"]), 2)
|
||||
|
||||
def test_product_list_catalogue_images_empty(self):
|
||||
url = "/don_confiao/api/products/"
|
||||
response = self.client.get(url)
|
||||
data = response.json()
|
||||
|
||||
product_data = next(
|
||||
p for p in data if p["id"] == self.product_no_images.id
|
||||
)
|
||||
self.assertEqual(product_data["catalogue_images"], [])
|
||||
|
||||
def test_product_detail_includes_catalogue_images(self):
|
||||
image_file = _create_test_image()
|
||||
CatalogueImage.objects.create(
|
||||
product=self.product, image=image_file
|
||||
)
|
||||
|
||||
url = f"/don_confiao/api/products/{self.product.id}/"
|
||||
response = self.client.get(url)
|
||||
data = response.json()
|
||||
self.assertIn("catalogue_images", data)
|
||||
self.assertEqual(len(data["catalogue_images"]), 1)
|
||||
@@ -2,20 +2,47 @@ from django.urls import path, include
|
||||
from rest_framework.routers import DefaultRouter
|
||||
|
||||
from . import views
|
||||
from . import api_views
|
||||
from .api import (
|
||||
# Catalogue Images
|
||||
CatalogueImageViewSet,
|
||||
# Products
|
||||
ProductView,
|
||||
ProductsFromTrytonView,
|
||||
# Customers
|
||||
CustomerView,
|
||||
CustomersFromTrytonView,
|
||||
# Sales
|
||||
SaleView,
|
||||
CatalogSaleView,
|
||||
SaleSummary,
|
||||
CatalogSaleSummary,
|
||||
SalesForTrytonView,
|
||||
SalesToTrytonView,
|
||||
CatalogSalesToTrytonView,
|
||||
# Payments
|
||||
ReconciliateJarView,
|
||||
ReconciliateJarModelView,
|
||||
PaymentMethodView,
|
||||
SalesForReconciliationView,
|
||||
# Admin
|
||||
AdminCodeValidateView,
|
||||
)
|
||||
|
||||
app_name = "don_confiao"
|
||||
|
||||
router = DefaultRouter()
|
||||
router.register(r"sales", api_views.SaleView, basename="sale")
|
||||
router.register(r"sales", SaleView, basename="sale")
|
||||
router.register(r"catalog_sales", CatalogSaleView, basename="catalog_sale")
|
||||
router.register(r"customers", CustomerView, basename="customer")
|
||||
router.register(r"products", ProductView, basename="product")
|
||||
router.register(
|
||||
r"catalog_sales", api_views.CatalogSaleView, basename="catalog_sale"
|
||||
r"catalogue_images",
|
||||
CatalogueImageViewSet,
|
||||
basename="catalogue_image",
|
||||
)
|
||||
router.register(r"customers", api_views.CustomerView, basename="customer")
|
||||
router.register(r"products", api_views.ProductView, basename="product")
|
||||
router.register(
|
||||
r"reconciliate_jar",
|
||||
api_views.ReconciliateJarModelView,
|
||||
ReconciliateJarModelView,
|
||||
basename="reconciliate_jar",
|
||||
)
|
||||
|
||||
@@ -23,39 +50,49 @@ urlpatterns = [
|
||||
path("productos", views.products, name="products"),
|
||||
path(
|
||||
"resumen_compra_json/<int:id>",
|
||||
api_views.SaleSummary.as_view(),
|
||||
SaleSummary.as_view(),
|
||||
name="purchase_json_summary",
|
||||
),
|
||||
path(
|
||||
"resumen_compra_catalogo_json/<int:id>",
|
||||
CatalogSaleSummary.as_view(),
|
||||
name="catalog_purchase_json_summary",
|
||||
),
|
||||
path(
|
||||
"payment_methods/all/select_format",
|
||||
api_views.PaymentMethodView.as_view(),
|
||||
PaymentMethodView.as_view(),
|
||||
name="payment_methods_to_select",
|
||||
),
|
||||
path(
|
||||
"purchases/for_reconciliation",
|
||||
api_views.SalesForReconciliationView.as_view(),
|
||||
SalesForReconciliationView.as_view(),
|
||||
name="sales_for_reconciliation",
|
||||
),
|
||||
path("reconciliate_jar", api_views.ReconciliateJarView.as_view()),
|
||||
path("reconciliate_jar", ReconciliateJarView.as_view()),
|
||||
path("api/", include(router.urls)),
|
||||
path(
|
||||
"api/importar_productos_de_tryton",
|
||||
api_views.ProductsFromTrytonView.as_view(),
|
||||
ProductsFromTrytonView.as_view(),
|
||||
name="products_from_tryton",
|
||||
),
|
||||
path(
|
||||
"api/importar_clientes_de_tryton",
|
||||
api_views.CustomersFromTrytonView.as_view(),
|
||||
CustomersFromTrytonView.as_view(),
|
||||
name="customers_from_tryton",
|
||||
),
|
||||
path(
|
||||
"api/enviar_ventas_a_tryton",
|
||||
api_views.SalesToTrytonView.as_view(),
|
||||
SalesToTrytonView.as_view(),
|
||||
name="send_tryton",
|
||||
),
|
||||
path(
|
||||
"api/admin_code/validate/<code>",
|
||||
api_views.AdminCodeValidateView.as_view(),
|
||||
"api/enviar_catalog_sales_a_tryton",
|
||||
CatalogSalesToTrytonView.as_view(),
|
||||
name="send_catalog_sales_tryton",
|
||||
),
|
||||
path("api/sales/for_tryton", api_views.SalesForTrytonView.as_view()),
|
||||
path(
|
||||
"api/admin_code/validate/<code>",
|
||||
AdminCodeValidateView.as_view(),
|
||||
),
|
||||
path("api/sales/for_tryton", SalesForTrytonView.as_view()),
|
||||
]
|
||||
|
||||
Reference in New Issue
Block a user