chartManD commited on
Commit
3f54131
·
1 Parent(s): 472349b

Se agrega opcion de elegir un vocabulario para crear sesion

Browse files
tecnicas/controllers/views_controller/create_session/panel_words_controller.py CHANGED
@@ -1,5 +1,5 @@
1
  from django.http import HttpRequest
2
- from tecnicas.forms import WordForm
3
  from django.shortcuts import render, redirect
4
  from django.urls import reverse
5
  from tecnicas.models import Palabra
@@ -7,27 +7,36 @@ import json
7
 
8
 
9
  class PanelWordsController():
 
 
 
10
  def __init__(self):
11
  pass
12
 
13
  @staticmethod
14
- def controllGetEscalas(request: HttpRequest):
15
  form = WordForm()
16
  context = {
17
  "form_word": form
18
  }
19
 
20
- return render(request, "tecnicas/create_sesion/configuracion-panel-words.html", context)
 
 
 
 
 
 
21
 
22
  @staticmethod
23
- def controllPostEscalas(request: HttpRequest):
24
  form = WordForm()
25
  context = {
26
  "form_word": form
27
  }
28
 
29
  if not request.POST.get("words"):
30
- return render(request, "tecnicas/create_sesion/configuracion-panel-words.html", context)
31
 
32
  words = json.loads(request.POST.get("words"))
33
  context["words"] = words
@@ -36,14 +45,34 @@ class PanelWordsController():
36
 
37
  if len(ids_words) != len(set(ids_words)):
38
  context["error"] = "existen palabras duplicadas"
39
- return render(request, "tecnicas/create_sesion/configuracion-panel-words.html", context)
40
 
41
  exist_words = Palabra.objects.filter(
42
  id__in=ids_words).count() == len(ids_words)
43
 
44
  if not exist_words:
45
  context["error"] = "algunas palabras no existen"
46
- return render(request, "tecnicas/create_sesion/configuracion-panel-words.html", context)
47
 
48
  request.session["form_words"] = ids_words
49
  return redirect(reverse("cata_system:creando_sesion"))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  from django.http import HttpRequest
2
+ from tecnicas.forms import WordForm, VocabularioSelectForm
3
  from django.shortcuts import render, redirect
4
  from django.urls import reverse
5
  from tecnicas.models import Palabra
 
7
 
8
 
9
  class PanelWordsController():
10
+ current_url_escalas_atribute = "tecnicas/create_sesion/configuracion-panel-words.html"
11
+ current_url_escalas_vocabulary = "tecnicas/create_sesion/conf-panel-vocabulary.html"
12
+
13
  def __init__(self):
14
  pass
15
 
16
  @staticmethod
17
+ def controllGetEscalasAtributes(request: HttpRequest):
18
  form = WordForm()
19
  context = {
20
  "form_word": form
21
  }
22
 
23
+ return render(request, PanelWordsController.current_url_escalas_atribute, context)
24
+
25
+ @staticmethod
26
+ def controllGetEscalasVocabulary(request: HttpRequest):
27
+ form = VocabularioSelectForm()
28
+ context = {"form": form}
29
+ return render(request, PanelWordsController.current_url_escalas_vocabulary, context)
30
 
31
  @staticmethod
32
+ def controllPostEscalasAtributes(request: HttpRequest):
33
  form = WordForm()
34
  context = {
35
  "form_word": form
36
  }
37
 
38
  if not request.POST.get("words"):
39
+ return render(request, PanelWordsController.current_url_escalas_atribute, context)
40
 
41
  words = json.loads(request.POST.get("words"))
42
  context["words"] = words
 
45
 
46
  if len(ids_words) != len(set(ids_words)):
47
  context["error"] = "existen palabras duplicadas"
48
+ return render(request, PanelWordsController.current_url_escalas_atribute, context)
49
 
50
  exist_words = Palabra.objects.filter(
51
  id__in=ids_words).count() == len(ids_words)
52
 
53
  if not exist_words:
54
  context["error"] = "algunas palabras no existen"
55
+ return render(request, PanelWordsController.current_url_escalas_atribute, context)
56
 
57
  request.session["form_words"] = ids_words
58
  return redirect(reverse("cata_system:creando_sesion"))
59
+
60
+ @staticmethod
61
+ def controllPostEscalasVocabulary(request: HttpRequest):
62
+ context = {}
63
+ if not request.POST.get("vocabulario"):
64
+ context["form"] = VocabularioSelectForm()
65
+ context["error"] = "No hay un vocabulario seleccionado"
66
+ return render(request, PanelWordsController.current_url_escalas_vocabulary, context)
67
+
68
+ form = VocabularioSelectForm(request.POST)
69
+ vocabulary: int
70
+ if form.is_valid():
71
+ vocabulary = form.cleaned_data["vocabulario"]
72
+ else:
73
+ context["form"] = VocabularioSelectForm()
74
+ context["error"] = "Erro al validar el vocabulario"
75
+ return render(request, PanelWordsController.current_url_escalas_vocabulary, context)
76
+
77
+ request.session["vocabulary"] = vocabulary.nombre_vocabulario
78
+ return redirect(reverse("cata_system:creando_sesion"))
tecnicas/forms/__init__.py CHANGED
@@ -3,4 +3,5 @@ from .sesion_tags_form import SesionTagsForm
3
  from .etiqueta_form import EtiquetaForm
4
  from .codes_form import CodesForm
5
  from .catador_form import CatadorForm
6
- from .word_form import WordForm
 
 
3
  from .etiqueta_form import EtiquetaForm
4
  from .codes_form import CodesForm
5
  from .catador_form import CatadorForm
6
+ from .word_form import WordForm
7
+ from .vocabulary_select import VocabularioSelectForm
tecnicas/forms/vocabulary_select.py ADDED
@@ -0,0 +1,15 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from django import forms
2
+ from tecnicas.models import Vocabulario
3
+
4
+
5
+ class VocabularioSelectForm(forms.Form):
6
+ vocabulario = forms.ModelChoiceField(
7
+ queryset=Vocabulario.objects.all(),
8
+ required=True,
9
+ label="Selecciona un vocabulario",
10
+ empty_label="-- Selecciona uno --",
11
+ widget=forms.Select(attrs={
12
+ "class": "w-full border rounded p-4 bg-surface-sweet",
13
+ "id": "vocabulario"
14
+ })
15
+ )
tecnicas/static/js/choose-vocabulary.js ADDED
@@ -0,0 +1,67 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ document.addEventListener("DOMContentLoaded", () => {
2
+ const vocabSelect = document.getElementById("vocabulario");
3
+ const wordsList = document.getElementById("palabras-lista");
4
+ const formNextStep = document.getElementById("cts-create-session");
5
+ let vocabularyId = "";
6
+
7
+ vocabSelect.addEventListener("change", async (e) => {
8
+ const vocabId = e.target.value;
9
+ vocabularyId = vocabId;
10
+ console.log(e.target);
11
+ console.log(vocabId);
12
+
13
+ wordsList.innerHTML =
14
+ "<li class='text-gray-500 text-center'>Cargando...</li>";
15
+
16
+ if (!vocabId) {
17
+ wordsList.innerHTML =
18
+ "<li class='text-gray-500 text-center'>Selecciona un vocabulario para ver sus palabras</li>";
19
+ return;
20
+ }
21
+
22
+ url_fetch = `api/vocabulario/${vocabId}/palabras`;
23
+
24
+ try {
25
+ const response = await fetch(url_fetch, { method: "GET" });
26
+ if (!response.ok) throw new Error("Error en la petición");
27
+ const json_response = await response.json();
28
+
29
+ words = json_response.data.words;
30
+
31
+ if (words === 0) {
32
+ wordsList.innerHTML =
33
+ "<li class='text-center text-lg'>No hay palabras asociadas</li>";
34
+ return;
35
+ }
36
+
37
+ wordsList.innerHTML = "";
38
+ words.forEach((p) => {
39
+ const li = document.createElement("li");
40
+ li.textContent = p.nombre_palabra;
41
+ li.className =
42
+ "bg-surface-sweet text-black rounded font-bold text-lg px-4 py-3";
43
+ wordsList.appendChild(li);
44
+ });
45
+ } catch (err) {
46
+ wordsList.innerHTML =
47
+ "<li class='text-red-500 text-center text-lg'>Error al cargar las palabras</li>";
48
+ console.error(err);
49
+ }
50
+ });
51
+
52
+ formNextStep.addEventListener("submit", (e) => {
53
+ if (!vocabularyId) {
54
+ e.preventDefault();
55
+ wordsList.innerHTML =
56
+ "<li class='text-center text-lg'>Seleccione un Vocabulario</li>";
57
+ return;
58
+ }
59
+
60
+ const useVocabulary = document.createElement("input");
61
+ useVocabulary.type = "hidden";
62
+ useVocabulary.name = "vocabulario";
63
+ useVocabulary.value = vocabularyId;
64
+
65
+ formNextStep.appendChild(useVocabulary);
66
+ });
67
+ });
tecnicas/templates/tecnicas/create_sesion/conf-panel-vocabulary.html ADDED
@@ -0,0 +1,62 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {% extends 'tecnicas/layouts/base.html' %}
2
+ {% load static %}
3
+
4
+ {% block content %}
5
+ <article class="cts-container-main">
6
+ <article class="cts-wrap-content text-black lg:w-4xl">
7
+ <h2 class="text-2xl font-bold text-center bg-surface-card p-4 rounded-lg">
8
+ Elegir vocabulario
9
+ </h2>
10
+
11
+ <form method="get" id="form-vocabulario" class="bg-surface-card rounded-lg p-6">
12
+ <label for="{{ form.vocabulario.id_for_label }}"
13
+ class="text-lg w-fit font-medium tracking-wide w-full space-y-4">
14
+ <p class="tracking-normal text-xl font-bold">Seleccione un vocabulario</p>
15
+ {{ form.vocabulario }}
16
+ </label>
17
+ </form>
18
+
19
+ {% if error %}
20
+ {% include "../components/error-message.html" with message=error %}
21
+ {% endif %}
22
+ {% if message %}
23
+ {% include "../components/error-message.html" with message=message %}
24
+ {% endif %}
25
+
26
+ <div id="palabras-container" class="rounded-lg p-3 min-h-24 max-h-64 overflow-y-auto bg-surface-card">
27
+ <h3 class="text-xl font-semibold">Palabras asociadas:</h3>
28
+ <ul id="palabras-lista" class="flex flex-wrap gap-4">
29
+ <li class="text-center text-lg">Selecciona un vocabulario para ver sus palabras</li>
30
+ </ul>
31
+ </div>
32
+
33
+ <section>
34
+ <form action="" method="post" id="cts-create-session" class="flex justify-center mt-4">
35
+ {% csrf_token %}
36
+ <article class="cs-escalas-radio w-full flex flex-col gap-4">
37
+ <section class="flex max-sm:flex-col flex-wrap items-center justify-center gap-4">
38
+ <button type="submit" name="start"
39
+ class="cts-btn-general cts-btn-primary btn-push flex-1 w-full">
40
+ Crear sesión
41
+ </button>
42
+ <a href="{% url 'cata_system:seleccion_tecnica' %}" class="flex-1 w-full">
43
+ <button type="button" class="cts-btn-general cts-btn-secondary btn-push w-full">
44
+ Paso anterior
45
+ </button>
46
+ </a>
47
+ </section>
48
+ <a href="{% url 'cata_system:seleccion_tecnica' %}" class="flex-1 w-full">
49
+ <button type="button" class="cts-btn-general cts-btn-error btn-push w-full">
50
+ Cancelar la creación
51
+ </button>
52
+ </a>
53
+ </article>
54
+ </form>
55
+ </section>
56
+ </article>
57
+ </article>
58
+ {% endblock %}
59
+
60
+ {% block extra_js %}
61
+ <script src="{% static 'js/choose-vocabulary.js' %}"></script>
62
+ {% endblock %}
tecnicas/urls.py CHANGED
@@ -120,6 +120,10 @@ urlpatterns = [
120
  views.words,
121
  name="api_palabras"),
122
 
 
 
 
 
123
  path("testers/api/ratingword",
124
  views.reatingWord,
125
  name="api_rating_word"),
 
120
  views.words,
121
  name="api_palabras"),
122
 
123
+ path("presenter/api/vocabulario/<int:vocab_id>/palabras",
124
+ views.wordsVocabulary,
125
+ name="api_palabras_vocabulary"),
126
+
127
  path("testers/api/ratingword",
128
  views.reatingWord,
129
  name="api_rating_word"),
tecnicas/views/__init__.py CHANGED
@@ -24,6 +24,7 @@ from .vocabulary_management.list_vocabulary import listVocabulary
24
 
25
  from .apis.api_tag import newTag
26
  from .apis.api_words import words
 
27
  from .apis.rating_word import reatingWord
28
 
29
  from .tester_forms.init_tester_form import initTesterForm
 
24
 
25
  from .apis.api_tag import newTag
26
  from .apis.api_words import words
27
+ from .apis.api_words import wordsVocabulary
28
  from .apis.rating_word import reatingWord
29
 
30
  from .tester_forms.init_tester_form import initTesterForm
tecnicas/views/apis/api_words.py CHANGED
@@ -1,9 +1,10 @@
1
  from django.http import HttpRequest, JsonResponse
2
  from tecnicas.decorators import required_presenter
3
- from tecnicas.models import Palabra
4
  from tecnicas.utils import general_error
5
  from tecnicas.forms.word_form import WordForm
6
 
 
7
  @required_presenter
8
  def words(req: HttpRequest):
9
  if req.method == "GET":
@@ -48,3 +49,21 @@ def words(req: HttpRequest):
48
  else:
49
  errors = form.errors.get("nombre_palabra")
50
  return general_error(errors[0])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  from django.http import HttpRequest, JsonResponse
2
  from tecnicas.decorators import required_presenter
3
+ from tecnicas.models import Palabra, Vocabulario
4
  from tecnicas.utils import general_error
5
  from tecnicas.forms.word_form import WordForm
6
 
7
+
8
  @required_presenter
9
  def words(req: HttpRequest):
10
  if req.method == "GET":
 
49
  else:
50
  errors = form.errors.get("nombre_palabra")
51
  return general_error(errors[0])
52
+
53
+
54
+ @required_presenter
55
+ def wordsVocabulary(req: HttpRequest, vocab_id: int):
56
+ if req.method == "GET":
57
+ vocabulary = Vocabulario.objects.filter(id=vocab_id).first()
58
+ if not vocabulary:
59
+ return JsonResponse([])
60
+ palabras = vocabulary.palabras.all().values("id", "nombre_palabra")
61
+ response_json = {
62
+ "ok": True,
63
+ "data": {
64
+ "words": list(palabras)
65
+ }
66
+ }
67
+ return JsonResponse(response_json)
68
+ else:
69
+ return JsonResponse({"error": "Este método no es permitido"})
tecnicas/views/sessions_config/configuration_panel_words.py CHANGED
@@ -1,6 +1,7 @@
1
  from django.http import HttpRequest, JsonResponse
2
  from django.shortcuts import redirect
3
  from django.urls import reverse
 
4
  from tecnicas.controllers import PanelWordsController
5
  from tecnicas.utils import deleteDataSession
6
 
@@ -12,18 +13,30 @@ def configurationPanelWords(req: HttpRequest):
12
  "?error=datos requeridos no encontrados")
13
 
14
  basic_data = req.session["form_basic"]
 
15
 
16
  if req.method == "GET":
17
  if basic_data["name_tecnica"] == "escalas" or basic_data["name_tecnica"] == "rata":
18
- response = PanelWordsController.controllGetEscalas(req)
 
 
 
 
 
 
19
  else:
20
  response = redirect(
21
  reverse("cata_system:seleccion_tecnica") + "?error=Técnica no valida")
22
-
23
  return response
24
  elif req.method == "POST":
25
  if basic_data["name_tecnica"] == "escalas" or basic_data["name_tecnica"] == "rata":
26
- response = PanelWordsController.controllPostEscalas(req)
 
 
 
 
 
 
27
  else:
28
  response = redirect(
29
  reverse("cata_system:seleccion_tecnica") + "?error=Técnica no valida")
 
1
  from django.http import HttpRequest, JsonResponse
2
  from django.shortcuts import redirect
3
  from django.urls import reverse
4
+ from tecnicas.models import EstiloPalabra
5
  from tecnicas.controllers import PanelWordsController
6
  from tecnicas.utils import deleteDataSession
7
 
 
13
  "?error=datos requeridos no encontrados")
14
 
15
  basic_data = req.session["form_basic"]
16
+ style_words = EstiloPalabra.objects.get(id=basic_data["estilo_palabras"])
17
 
18
  if req.method == "GET":
19
  if basic_data["name_tecnica"] == "escalas" or basic_data["name_tecnica"] == "rata":
20
+ if style_words.nombre_estilo == "atributos":
21
+ response = PanelWordsController.controllGetEscalasAtributes(req)
22
+ elif style_words.nombre_estilo == "vocabulario":
23
+ response = PanelWordsController.controllGetEscalasVocabulary(req)
24
+ else:
25
+ response = redirect(
26
+ reverse("cata_system:seleccion_tecnica") + "?error=Estilo de palabras no valida")
27
  else:
28
  response = redirect(
29
  reverse("cata_system:seleccion_tecnica") + "?error=Técnica no valida")
 
30
  return response
31
  elif req.method == "POST":
32
  if basic_data["name_tecnica"] == "escalas" or basic_data["name_tecnica"] == "rata":
33
+ if style_words.nombre_estilo == "atributos":
34
+ response = PanelWordsController.controllPostEscalasAtributes(req)
35
+ elif style_words.nombre_estilo == "vocabulario":
36
+ response = PanelWordsController.controllPostEscalasVocabulary(req)
37
+ else:
38
+ response = redirect(
39
+ reverse("cata_system:seleccion_tecnica") + "?error=Estilo de palabras no valida")
40
  else:
41
  response = redirect(
42
  reverse("cata_system:seleccion_tecnica") + "?error=Técnica no valida")