Spaces:
Sleeping
Sleeping
Creada controlador Orden y Producto para la vista create session
Browse files
.gitignore
CHANGED
|
@@ -167,6 +167,7 @@ cython_debug/
|
|
| 167 |
|
| 168 |
# Others files
|
| 169 |
some_test
|
|
|
|
| 170 |
|
| 171 |
# PyCharm
|
| 172 |
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
|
|
|
|
| 167 |
|
| 168 |
# Others files
|
| 169 |
some_test
|
| 170 |
+
others_files
|
| 171 |
|
| 172 |
# PyCharm
|
| 173 |
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
|
tecnicas/controllers/__init__.py
CHANGED
|
@@ -1,2 +1,4 @@
|
|
| 1 |
from .tecnica_controller import TecnicaController
|
| 2 |
-
from .escala_controller import EscalaController
|
|
|
|
|
|
|
|
|
| 1 |
from .tecnica_controller import TecnicaController
|
| 2 |
+
from .escala_controller import EscalaController
|
| 3 |
+
from .productos_controller import ProductosController
|
| 4 |
+
from .ordenes_controller import OrdenesController
|
tecnicas/controllers/ordenes_controller.py
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from ..models import Orden, Posicion, Producto, Tecnica
|
| 2 |
+
from ..utils import controller_error
|
| 3 |
+
from django.db import DatabaseError
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
class OrdenesController():
|
| 7 |
+
products: list[Producto]
|
| 8 |
+
technique: Tecnica
|
| 9 |
+
raw_list_orders: list[list[dict]]
|
| 10 |
+
orders: list[Orden]
|
| 11 |
+
orders_save: list[Orden]
|
| 12 |
+
positions: list[Posicion]
|
| 13 |
+
|
| 14 |
+
def __init__(self, raw_orders: list[list[dict]], list_products: list[Producto], technique: Tecnica):
|
| 15 |
+
self.products = list_products
|
| 16 |
+
self.technique = technique
|
| 17 |
+
self.raw_list_orders = raw_orders
|
| 18 |
+
|
| 19 |
+
def setOrders(self, new_raw_orders: list[list[dict]] = None):
|
| 20 |
+
self.orders = new_raw_orders or []
|
| 21 |
+
|
| 22 |
+
for i in range(len(self.raw_list_orders)):
|
| 23 |
+
self.orders.append(Orden(id_tecnica=self.technique))
|
| 24 |
+
|
| 25 |
+
def saveOrders(self):
|
| 26 |
+
if not self.orders:
|
| 27 |
+
return controller_error("no se han establecido las ordenes para guardar")
|
| 28 |
+
try:
|
| 29 |
+
self.orders_save = Orden.objects.bulk_create(self.orders)
|
| 30 |
+
return self.orders_save
|
| 31 |
+
except DatabaseError as error:
|
| 32 |
+
return controller_error(error)
|
| 33 |
+
|
| 34 |
+
def setPositions(self):
|
| 35 |
+
codes_ids = [(product.codigoProducto, product.id)
|
| 36 |
+
for product in self.products]
|
| 37 |
+
|
| 38 |
+
codes_expect = [pair[0] for pair in codes_ids]
|
| 39 |
+
|
| 40 |
+
if len(self.orders_save) != len(self.raw_list_orders):
|
| 41 |
+
return controller_error("el numero de ordenes guardados no coinciden con los recibidos")
|
| 42 |
+
|
| 43 |
+
self.positions = []
|
| 44 |
+
|
| 45 |
+
for index, raw_order in enumerate(self.raw_list_orders):
|
| 46 |
+
codes_raw_orders = [next(iter(position.values()))
|
| 47 |
+
for position in raw_order]
|
| 48 |
+
|
| 49 |
+
if not set(codes_raw_orders) == set(codes_expect):
|
| 50 |
+
self.positions = []
|
| 51 |
+
return controller_error("las ordenes mandadas no contienen los productos esperados")
|
| 52 |
+
|
| 53 |
+
for data_position in raw_order:
|
| 54 |
+
code = data_position["code"]
|
| 55 |
+
position = data_position["position"]
|
| 56 |
+
|
| 57 |
+
locate_code_id = [
|
| 58 |
+
code_id for code_id in codes_ids if code in code_id]
|
| 59 |
+
|
| 60 |
+
if not len(locate_code_id) == 1:
|
| 61 |
+
return controller_error("no es posible asociar mas de un producto a un modelo posicion")
|
| 62 |
+
else:
|
| 63 |
+
locate_code_id = locate_code_id[0]
|
| 64 |
+
|
| 65 |
+
new_position = Posicion(
|
| 66 |
+
id_producto=Producto.objects.get(id=locate_code_id[1]),
|
| 67 |
+
id_orden=self.saveOrders[index],
|
| 68 |
+
posicion=position,
|
| 69 |
+
)
|
| 70 |
+
|
| 71 |
+
self.positions.append(new_position)
|
| 72 |
+
|
| 73 |
+
return self.positions
|
| 74 |
+
|
| 75 |
+
def savePostions(self):
|
| 76 |
+
if not self.positions:
|
| 77 |
+
return controller_error("no se han establecido posiciones para guargar")
|
| 78 |
+
try:
|
| 79 |
+
self.positions_save = Posicion.objects.bulk_create(self.positions)
|
| 80 |
+
return self.positions_save
|
| 81 |
+
except DatabaseError as error:
|
| 82 |
+
return controller_error(error)
|
tecnicas/controllers/productos_controller.py
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from django.db import DatabaseError
|
| 2 |
+
from ..utils import controller_error
|
| 3 |
+
from ..models import Producto, Tecnica
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
class ProductosController():
|
| 7 |
+
list_codes: list[str]
|
| 8 |
+
list_product: list[Producto]
|
| 9 |
+
technique: Tecnica
|
| 10 |
+
|
| 11 |
+
def __init__(self, codes: list[str], technique: Tecnica):
|
| 12 |
+
for code in codes:
|
| 13 |
+
self.list_codes.append(code)
|
| 14 |
+
|
| 15 |
+
self.technique = technique
|
| 16 |
+
|
| 17 |
+
def setListCodes(self, codes: list[str]):
|
| 18 |
+
for code in codes:
|
| 19 |
+
self.list_codes.append(code)
|
| 20 |
+
|
| 21 |
+
def setTechnique(self, technique: Tecnica):
|
| 22 |
+
self.technique = technique
|
| 23 |
+
|
| 24 |
+
def setProductsNoSave(self):
|
| 25 |
+
self.list_product = []
|
| 26 |
+
for code in self.list_codes:
|
| 27 |
+
product = Producto(
|
| 28 |
+
codigoProducto=code,
|
| 29 |
+
id_tecnica=self.technique
|
| 30 |
+
)
|
| 31 |
+
self.list_product.append(product)
|
| 32 |
+
|
| 33 |
+
def saveProducts(self):
|
| 34 |
+
if not self.list_product:
|
| 35 |
+
return controller_error("no se han establecido los productos para guardar")
|
| 36 |
+
|
| 37 |
+
try:
|
| 38 |
+
self.products_save = Producto.objects.bulk_create(self.list_product)
|
| 39 |
+
return self.products_save
|
| 40 |
+
except DatabaseError as error:
|
| 41 |
+
return None
|
tecnicas/utils/__init__.py
CHANGED
|
@@ -1,3 +1,3 @@
|
|
| 1 |
from .code_generate import generarCodigo
|
| 2 |
from .code_generate import generarCodigos
|
| 3 |
-
from .
|
|
|
|
| 1 |
from .code_generate import generarCodigo
|
| 2 |
from .code_generate import generarCodigos
|
| 3 |
+
from .personal_errors import general_error, controller_error
|
tecnicas/utils/{general_error.py → personal_errors.py}
RENAMED
|
@@ -1,5 +1,11 @@
|
|
| 1 |
from django.http import JsonResponse
|
| 2 |
|
|
|
|
| 3 |
def general_error(message="informacion incorrecta"):
|
| 4 |
-
|
| 5 |
-
return JsonResponse(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
from django.http import JsonResponse
|
| 2 |
|
| 3 |
+
|
| 4 |
def general_error(message="informacion incorrecta"):
|
| 5 |
+
result = {"error": message}
|
| 6 |
+
return JsonResponse(result)
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
def controller_error(message="ha ocurrido un error inesperado"):
|
| 10 |
+
result = {"error": message}
|
| 11 |
+
return result
|
tecnicas/views/create_session.py
CHANGED
|
@@ -2,7 +2,7 @@ from django.http import HttpRequest, JsonResponse
|
|
| 2 |
from django.shortcuts import render, redirect
|
| 3 |
from django.urls import reverse
|
| 4 |
from ..utils import general_error
|
| 5 |
-
from ..controllers import TecnicaController, EscalaController
|
| 6 |
|
| 7 |
|
| 8 |
def createSession(req: HttpRequest):
|
|
@@ -46,5 +46,24 @@ def createSession(req: HttpRequest):
|
|
| 46 |
if not saved_tags:
|
| 47 |
return general_error("error al guardar asociar escalas, datos agregados previeamante borrados")
|
| 48 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
return JsonResponse({"message": "sesion creada", "data": {"session_id": "asd548ad4a"}})
|
| 50 |
-
return general_error("ha orcurrido un error inesperado")
|
|
|
|
| 2 |
from django.shortcuts import render, redirect
|
| 3 |
from django.urls import reverse
|
| 4 |
from ..utils import general_error
|
| 5 |
+
from ..controllers import TecnicaController, EscalaController, ProductosController
|
| 6 |
|
| 7 |
|
| 8 |
def createSession(req: HttpRequest):
|
|
|
|
| 46 |
if not saved_tags:
|
| 47 |
return general_error("error al guardar asociar escalas, datos agregados previeamante borrados")
|
| 48 |
|
| 49 |
+
# ////////////////////////////////////////////////////////// #
|
| 50 |
+
#
|
| 51 |
+
# Second step: Create orders, productos and set the position #
|
| 52 |
+
#
|
| 53 |
+
# ////////////////////////////////////////////////////////// #
|
| 54 |
+
data_codes = req.session["form_codes"]
|
| 55 |
+
|
| 56 |
+
codes = []
|
| 57 |
+
for product in data_codes["product_codes"]:
|
| 58 |
+
code = next(iter(product.values()))
|
| 59 |
+
codes.append(code)
|
| 60 |
+
|
| 61 |
+
controllerProducts = ProductosController(
|
| 62 |
+
codes=codes,
|
| 63 |
+
technique=technique
|
| 64 |
+
)
|
| 65 |
+
|
| 66 |
+
raw_sort_codes = data_codes["sort_codes"]
|
| 67 |
+
|
| 68 |
return JsonResponse({"message": "sesion creada", "data": {"session_id": "asd548ad4a"}})
|
| 69 |
+
return general_error("ha orcurrido un error inesperado")
|