Spaces:
Sleeping
Sleeping
Guardado de datos de grupos en sesion con Sort
Browse files
tecnicas/admin.py
CHANGED
|
@@ -14,7 +14,7 @@ from .models import Producto, Participacion
|
|
| 14 |
|
| 15 |
from .models import Orden, Posicion
|
| 16 |
|
| 17 |
-
from .models import Dato, ValorDecimal, ValorBooleano, Calificacion, ListaPalabras
|
| 18 |
|
| 19 |
# Register your models here.
|
| 20 |
admin.site.register(CategoriaTecnica)
|
|
@@ -47,3 +47,4 @@ admin.site.register(ValorDecimal)
|
|
| 47 |
admin.site.register(ValorBooleano)
|
| 48 |
admin.site.register(Calificacion)
|
| 49 |
admin.site.register(ListaPalabras)
|
|
|
|
|
|
| 14 |
|
| 15 |
from .models import Orden, Posicion
|
| 16 |
|
| 17 |
+
from .models import Dato, ValorDecimal, ValorBooleano, Calificacion, ListaPalabras, GrupoProducto
|
| 18 |
|
| 19 |
# Register your models here.
|
| 20 |
admin.site.register(CategoriaTecnica)
|
|
|
|
| 47 |
admin.site.register(ValorBooleano)
|
| 48 |
admin.site.register(Calificacion)
|
| 49 |
admin.site.register(ListaPalabras)
|
| 50 |
+
admin.site.register(GrupoProducto)
|
tecnicas/controllers/__init__.py
CHANGED
|
@@ -57,3 +57,4 @@ from .api_controller.rating_sacales_controller import RatingScalesController
|
|
| 57 |
from .api_controller.rating_cata_controller import RatingCataController
|
| 58 |
from .api_controller.rating_pf_list_controller import RatingPFListController
|
| 59 |
from .views_controller.tester_list_controller import TesterListController
|
|
|
|
|
|
| 57 |
from .api_controller.rating_cata_controller import RatingCataController
|
| 58 |
from .api_controller.rating_pf_list_controller import RatingPFListController
|
| 59 |
from .views_controller.tester_list_controller import TesterListController
|
| 60 |
+
from .api_controller.rating_sort_controller import RatingSortController
|
tecnicas/controllers/api_controller/rating_sort_controller.py
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from django.http import JsonResponse, HttpRequest
|
| 2 |
+
from django.db import transaction
|
| 3 |
+
from tecnicas.models import Participacion, Palabra, GrupoProducto, Producto
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
class RatingSortController():
|
| 7 |
+
def __init__(self):
|
| 8 |
+
pass
|
| 9 |
+
|
| 10 |
+
@staticmethod
|
| 11 |
+
def saveRating(request: HttpRequest, data: list[dict]):
|
| 12 |
+
try:
|
| 13 |
+
with transaction.atomic():
|
| 14 |
+
participation = Participacion.objects.get(
|
| 15 |
+
id=request.session["id_participation"])
|
| 16 |
+
|
| 17 |
+
technique = participation.tecnica
|
| 18 |
+
catador = participation.catador
|
| 19 |
+
|
| 20 |
+
# Obtener productos de la técnica
|
| 21 |
+
technique_products = Producto.objects.filter(
|
| 22 |
+
id_tecnica=technique)
|
| 23 |
+
technique_product_ids = set(p.id for p in technique_products)
|
| 24 |
+
|
| 25 |
+
# Recolectar IDs de productos enviados
|
| 26 |
+
sent_product_ids = set()
|
| 27 |
+
for group in data:
|
| 28 |
+
for product in group["products"]:
|
| 29 |
+
sent_product_ids.add(int(product["id"]))
|
| 30 |
+
|
| 31 |
+
# Validar que los productos enviados existan en la técnica
|
| 32 |
+
if not sent_product_ids.issubset(technique_product_ids):
|
| 33 |
+
return JsonResponse({"error": "Productos enviados no pertenecen a la técnica"})
|
| 34 |
+
|
| 35 |
+
# Validar que todos los productos de la técnica estén presentes
|
| 36 |
+
if sent_product_ids != technique_product_ids:
|
| 37 |
+
return JsonResponse({"error": "Faltan productos por clasificar"})
|
| 38 |
+
|
| 39 |
+
for group in data:
|
| 40 |
+
words_data = group["words"]
|
| 41 |
+
products_data = group["products"]
|
| 42 |
+
|
| 43 |
+
# Crear u obtener palabras
|
| 44 |
+
words_objs = []
|
| 45 |
+
for word_name in words_data:
|
| 46 |
+
word, created = Palabra.objects.get_or_create(nombre_palabra=word_name)
|
| 47 |
+
words_objs.append(word)
|
| 48 |
+
|
| 49 |
+
# Crear GrupoProducto
|
| 50 |
+
group_product = GrupoProducto.objects.create(
|
| 51 |
+
tecnica=technique,
|
| 52 |
+
catador=catador
|
| 53 |
+
)
|
| 54 |
+
|
| 55 |
+
# Asignar palabras
|
| 56 |
+
group_product.palabras.set(words_objs)
|
| 57 |
+
|
| 58 |
+
# Asignar productos
|
| 59 |
+
product_ids = [p["id"] for p in products_data]
|
| 60 |
+
group_product.productos.set(product_ids)
|
| 61 |
+
|
| 62 |
+
return JsonResponse({"message": "Valores guardados"})
|
| 63 |
+
except Participacion.DoesNotExist:
|
| 64 |
+
return JsonResponse({"error": "Participación no encontrada"})
|
| 65 |
+
except Exception as e:
|
| 66 |
+
print(f"Error de calificacion: {e}")
|
| 67 |
+
return JsonResponse({"error": "Error al guardar los datos"})
|
tecnicas/static/js/test-sort.js
CHANGED
|
@@ -397,4 +397,112 @@ function getItemWord(wordName, code) {
|
|
| 397 |
//////// Save data
|
| 398 |
//////
|
| 399 |
////
|
| 400 |
-
*/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 397 |
//////// Save data
|
| 398 |
//////
|
| 399 |
////
|
| 400 |
+
*/
|
| 401 |
+
|
| 402 |
+
async function saveData() {
|
| 403 |
+
const keysDataGrups = Object.keys(DATA_GRUPS);
|
| 404 |
+
const allCodesProducts = new Set()
|
| 405 |
+
|
| 406 |
+
products.forEach((productElement) => {
|
| 407 |
+
allCodesProducts.add(productElement.getAttribute("data-code"))
|
| 408 |
+
})
|
| 409 |
+
|
| 410 |
+
if (keysDataGrups.length === 0) {
|
| 411 |
+
spanNotifaction("No hay grupos para guardar")
|
| 412 |
+
return false;
|
| 413 |
+
}
|
| 414 |
+
|
| 415 |
+
const data = []
|
| 416 |
+
let thereError = false;
|
| 417 |
+
const codesProducts = []
|
| 418 |
+
|
| 419 |
+
keysDataGrups.forEach((key) => {
|
| 420 |
+
if (thereError) return
|
| 421 |
+
|
| 422 |
+
const dataGrup = DATA_GRUPS[key];
|
| 423 |
+
if (!dataGrup) {
|
| 424 |
+
spanNotifaction("No hay datos para guardar")
|
| 425 |
+
thereError = true;
|
| 426 |
+
return
|
| 427 |
+
}
|
| 428 |
+
|
| 429 |
+
if (dataGrup.products.length === 0) {
|
| 430 |
+
spanNotifaction("Los grupos deben tener por lo menos un producto para guardar")
|
| 431 |
+
thereError = true;
|
| 432 |
+
return
|
| 433 |
+
}
|
| 434 |
+
|
| 435 |
+
if (dataGrup.words.length === 0) {
|
| 436 |
+
spanNotifaction("Los grupos deben tener por lo menos una palabra para guardar")
|
| 437 |
+
thereError = true;
|
| 438 |
+
return
|
| 439 |
+
}
|
| 440 |
+
|
| 441 |
+
const words = dataGrup.words;
|
| 442 |
+
const products = dataGrup.products;
|
| 443 |
+
|
| 444 |
+
codesProducts.push(...products.map((product) => product.code))
|
| 445 |
+
|
| 446 |
+
data.push({
|
| 447 |
+
words,
|
| 448 |
+
products
|
| 449 |
+
})
|
| 450 |
+
})
|
| 451 |
+
|
| 452 |
+
if (thereError) return false;
|
| 453 |
+
|
| 454 |
+
const currentCodesProducts = new Set(codesProducts)
|
| 455 |
+
const difference = symmetricDifference(currentCodesProducts, allCodesProducts)
|
| 456 |
+
|
| 457 |
+
if (difference.size > 0) {
|
| 458 |
+
spanNotifaction("Falta un producto que debe ser ordenado")
|
| 459 |
+
return false
|
| 460 |
+
}
|
| 461 |
+
|
| 462 |
+
const URL = "/cata/testers/api/rating-sort"
|
| 463 |
+
const csrfToken = document.querySelector('[name=csrfmiddlewaretoken]').value;
|
| 464 |
+
|
| 465 |
+
try {
|
| 466 |
+
const response = await fetch(URL, {
|
| 467 |
+
method: "POST",
|
| 468 |
+
headers: {
|
| 469 |
+
"Content-Type": "application/json",
|
| 470 |
+
"X-CSRFToken": csrfToken,
|
| 471 |
+
},
|
| 472 |
+
body: JSON.stringify(data),
|
| 473 |
+
})
|
| 474 |
+
|
| 475 |
+
if (!response.ok) {
|
| 476 |
+
spanNotifaction("Error en la respuesta del servidor")
|
| 477 |
+
return false;
|
| 478 |
+
}
|
| 479 |
+
|
| 480 |
+
const result = await response.json()
|
| 481 |
+
|
| 482 |
+
if (result.error) {
|
| 483 |
+
spanNotifaction(result.error)
|
| 484 |
+
return false
|
| 485 |
+
} else {
|
| 486 |
+
spanNotifaction(result.message, false)
|
| 487 |
+
return true
|
| 488 |
+
}
|
| 489 |
+
} catch (error) {
|
| 490 |
+
spanNotifaction("Error en proceso de guardar los datos")
|
| 491 |
+
return false
|
| 492 |
+
}
|
| 493 |
+
}
|
| 494 |
+
|
| 495 |
+
const buttonSaveData = document.getElementById("save-progress")
|
| 496 |
+
buttonSaveData.addEventListener("click", saveData)
|
| 497 |
+
|
| 498 |
+
function symmetricDifference(setA, setB) {
|
| 499 |
+
let _difference = new Set(setA);
|
| 500 |
+
for (let elem of setB) {
|
| 501 |
+
if (_difference.has(elem)) {
|
| 502 |
+
_difference.delete(elem);
|
| 503 |
+
} else {
|
| 504 |
+
_difference.add(elem);
|
| 505 |
+
}
|
| 506 |
+
}
|
| 507 |
+
return _difference;
|
| 508 |
+
}
|
tecnicas/urls.py
CHANGED
|
@@ -147,4 +147,8 @@ urlpatterns = [
|
|
| 147 |
path("testers/api/ratingword/pf/list",
|
| 148 |
views.apiListWordsPF,
|
| 149 |
name="api_rating_word_pf_list"),
|
|
|
|
|
|
|
|
|
|
|
|
|
| 150 |
]
|
|
|
|
| 147 |
path("testers/api/ratingword/pf/list",
|
| 148 |
views.apiListWordsPF,
|
| 149 |
name="api_rating_word_pf_list"),
|
| 150 |
+
|
| 151 |
+
path("testers/api/rating-sort",
|
| 152 |
+
views.ratingSort,
|
| 153 |
+
name="api_rating_sort"),
|
| 154 |
]
|
tecnicas/views/__init__.py
CHANGED
|
@@ -28,6 +28,7 @@ from .apis.api_words import wordsVocabulary
|
|
| 28 |
from .apis.api_list_words_pf import apiListWordsPF
|
| 29 |
from .apis.rating_word_scales import ratingWordScales
|
| 30 |
from .apis.rating_word_cata import ratingWordCata
|
|
|
|
| 31 |
|
| 32 |
from .tester_forms.init_tester_form import initTesterForm
|
| 33 |
from .tester_forms.panel_main_tester import mainPanelTester
|
|
|
|
| 28 |
from .apis.api_list_words_pf import apiListWordsPF
|
| 29 |
from .apis.rating_word_scales import ratingWordScales
|
| 30 |
from .apis.rating_word_cata import ratingWordCata
|
| 31 |
+
from .apis.rating_sort import ratingSort
|
| 32 |
|
| 33 |
from .tester_forms.init_tester_form import initTesterForm
|
| 34 |
from .tester_forms.panel_main_tester import mainPanelTester
|
tecnicas/views/apis/rating_sort.py
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from django.http import HttpRequest, JsonResponse
|
| 2 |
+
from tecnicas.controllers import RatingSortController
|
| 3 |
+
import json
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
def ratingSort(req: HttpRequest):
|
| 7 |
+
if req.method == "POST":
|
| 8 |
+
try:
|
| 9 |
+
data = json.loads(req.body.decode("utf-8"))
|
| 10 |
+
response = RatingSortController.saveRating(
|
| 11 |
+
request=req, data=data)
|
| 12 |
+
return response
|
| 13 |
+
except Exception as e:
|
| 14 |
+
return JsonResponse({"error": "Error al procesar datos"})
|
| 15 |
+
else:
|
| 16 |
+
return JsonResponse({"error": "Método no permitido"})
|