chartManD commited on
Commit
472349b
·
1 Parent(s): 0defa4d

Actualizacion de Vocabulario

Browse files
tecnicas/controllers/views_controller/vocabulary_manage/create_vocabulary_controller.py CHANGED
@@ -16,17 +16,15 @@ class CreateVocabularyController():
16
 
17
  def controllGet(self, request: HttpRequest):
18
  self.context = {
19
- "form_word": self.context["form_word"],
20
- "words": self.context["words"],
21
  }
22
 
23
  if "name_voca" in request.GET:
24
- current = Vocabulario.objects.get(nombre_vocabulario=request.GET["name_voca"])
25
-
26
- self.context = {
27
- "name_vacabulary": current.nombre_vocabulario,
28
- "words": current.palabras.all()
29
- }
30
 
31
  return render(request, self.current_url, self.context)
32
 
@@ -40,29 +38,73 @@ class CreateVocabularyController():
40
  self.context["error"] = "Nombre de vocabulario requerido"
41
  return render(request, self.current_url, self.context)
42
 
43
- vocabulary_name = request.POST.get("nombre_vocabulario").strip()
 
44
 
45
- try:
46
- new_vocababulary = Vocabulario.objects.create(
47
- nomre_vocabulario=vocabulary_name)
48
- except IntegrityError:
49
- self.context["error"] = "Ya existe un vocabulario con ese nombre"
50
- return render(request, self.current_url, self.context)
 
 
 
 
 
 
 
51
 
52
- words_json = request.POST.get("words", "")
53
- if words_json:
54
  try:
55
- words_list = json.loads(words_json)
 
 
 
 
 
 
 
 
 
56
 
57
- ids = [int(w.get("id", 0))
58
- for w in words_list if str(w.get("id", "")).isdigit()]
59
 
60
- words = Palabra.objects.filter(id__in=ids)
61
 
62
- new_vocababulary.palabras.add(*words)
63
- except (json.JSONDecodeError, ValueError):
64
- self.context["error"] = 'Ocurrió un error al revisar las palabras, revise “Ver vocabularios”, para reasignar las palabras'
 
 
 
 
 
 
 
 
 
 
 
 
65
  return render(request, self.current_url, self.context)
66
 
67
- self.context["message"] = 'Vocabulario creado con éxito, puedes revisarlo en "Ver vocabularios"'
68
- return render(request, self.current_url, self.context)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
16
 
17
  def controllGet(self, request: HttpRequest):
18
  self.context = {
19
+ "form_word": WordForm(),
20
+ "words": []
21
  }
22
 
23
  if "name_voca" in request.GET:
24
+ current = Vocabulario.objects.get(
25
+ nombre_vocabulario=request.GET["name_voca"])
26
+ self.context["name_vacabulary"] = current.nombre_vocabulario
27
+ self.context["words"] = current.palabras.all()
 
 
28
 
29
  return render(request, self.current_url, self.context)
30
 
 
38
  self.context["error"] = "Nombre de vocabulario requerido"
39
  return render(request, self.current_url, self.context)
40
 
41
+ new_vocabulary_name = request.POST.get("nombre_vocabulario").strip()
42
+ is_update = request.POST.get("is_update")
43
 
44
+ print(is_update)
45
+
46
+ if is_update:
47
+ if "original_name" not in request.POST:
48
+ self.context["error"] = "Nombre original de vocabulario requerido"
49
+ return render(request, self.current_url, self.context)
50
+
51
+ original_name = request.POST["original_name"].strip()
52
+
53
+ if original_name != new_vocabulary_name:
54
+ if Vocabulario.objects.filter(nombre_vocabulario=new_vocabulary_name).exists():
55
+ self.context["error"] = "Ya existe un vocabulario con el nombre pasado"
56
+ return render(request, self.current_url, self.context)
57
 
 
 
58
  try:
59
+ current_vocababulary = Vocabulario.objects.get(
60
+ nombre_vocabulario=original_name)
61
+ except Vocabulario.DoesNotExist:
62
+ self.context["error"] = "No existe un vocabulario con ese nombre"
63
+ return render(request, self.current_url, self.context)
64
+
65
+ words_json = request.POST.get("words", "")
66
+ if words_json:
67
+ try:
68
+ words_list = json.loads(words_json)
69
 
70
+ ids = [int(w.get("id", 0))
71
+ for w in words_list if str(w.get("id", "")).isdigit()]
72
 
73
+ words = Palabra.objects.filter(id__in=ids)
74
 
75
+ current_vocababulary.palabras.set(words)
76
+ current_vocababulary.nombre_vocabulario = new_vocabulary_name
77
+ current_vocababulary.save()
78
+ except (json.JSONDecodeError, ValueError):
79
+ self.context["error"] = 'Ocurrió un error al revisar las palabras, revise “Ver vocabularios”, para reasignar las palabras'
80
+ return render(request, self.current_url, self.context)
81
+
82
+ self.context["message"] = 'Vocabulario creado con éxito, puedes revisarlo en "Ver vocabularios"'
83
+ return render(request, self.current_url, self.context)
84
+ elif not is_update:
85
+ try:
86
+ new_vocababulary = Vocabulario.objects.create(
87
+ nombre_vocabulario=new_vocabulary_name)
88
+ except IntegrityError:
89
+ self.context["error"] = "Ya existe un vocabulario con ese nombre"
90
  return render(request, self.current_url, self.context)
91
 
92
+ words_json = request.POST.get("words", "")
93
+ if words_json:
94
+ try:
95
+ words_list = json.loads(words_json)
96
+
97
+ ids = [int(w.get("id", 0))
98
+ for w in words_list if str(w.get("id", "")).isdigit()]
99
+
100
+ words = Palabra.objects.filter(id__in=ids)
101
+
102
+ new_vocababulary.palabras.add(*words)
103
+ except (json.JSONDecodeError, ValueError):
104
+ self.context["error"] = 'Ocurrió un error al revisar las palabras, revise “Ver vocabularios”, para reasignar las palabras'
105
+ return render(request, self.current_url, self.context)
106
+
107
+ self.context["message"] = 'Vocabulario actualziado con éxito, puedes revisarlo en "Ver vocabularios"'
108
+ return render(request, self.current_url, self.context)
109
+ else:
110
+ pass
tecnicas/static/js/created-vocabulary.js CHANGED
@@ -1,7 +1,7 @@
1
  // **************************************
2
  // Create Vocabulary
3
  // **************************************
4
- async function submitSelectWords(classNanmeForm) {
5
  const form = document.querySelector(`.${classNanmeForm}`);
6
 
7
  const name_vocabulary = form.querySelector(".cts-name-voca").value;
@@ -20,7 +20,29 @@ async function submitSelectWords(classNanmeForm) {
20
  wordsInput.name = "words";
21
  wordsInput.value = JSON.stringify(listWordsSelect);
22
 
 
 
23
  form.appendChild(wordsInput);
 
 
24
 
25
  form.submit();
26
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  // **************************************
2
  // Create Vocabulary
3
  // **************************************
4
+ async function submitSelectWords(classNanmeForm, update = false) {
5
  const form = document.querySelector(`.${classNanmeForm}`);
6
 
7
  const name_vocabulary = form.querySelector(".cts-name-voca").value;
 
20
  wordsInput.name = "words";
21
  wordsInput.value = JSON.stringify(listWordsSelect);
22
 
23
+ const [isUpdata, orinalName] = inputIsUpdateVocabulary(update);
24
+
25
  form.appendChild(wordsInput);
26
+ form.appendChild(isUpdata);
27
+ if (orinalName) form.appendChild(orinalName);
28
 
29
  form.submit();
30
  }
31
+
32
+ function inputIsUpdateVocabulary(is_update = false) {
33
+ const isUpdata = document.createElement("input");
34
+ isUpdata.type = "hidden";
35
+ isUpdata.name = "is_update";
36
+ isUpdata.value = is_update;
37
+
38
+ if (is_update) {
39
+ const orinalName = document.querySelector(".cts-original-name").textContent;
40
+ const inputName = document.createElement("input");
41
+ inputName.type = "hidden";
42
+ inputName.name = "original_name";
43
+ inputName.value = orinalName;
44
+ return [isUpdata, inputName];
45
+ }
46
+
47
+ return [isUpdata, is_update];
48
+ }
tecnicas/templates/tecnicas/manage_vocabulary/create-vocabulary.html CHANGED
@@ -105,15 +105,22 @@
105
  </p>
106
  </form>
107
 
 
 
 
 
108
  <article class="flex justify-center gap-4 mt-4">
 
109
  <button type="button" name="start" class="cts-btn-general cts-btn-primary btn-push flex-1 w-full"
110
- onclick="submitSelectWords('cts-craed-vocabulary')">
111
- {% if name_vacabulary %}
112
  Actualizar vocabulario
113
- {% else %}
 
 
 
114
  Crear vocabulario
115
- {% endif %}
116
  </button>
 
117
  <a href="{% url 'cata_system:panel_vocabulario' %}" class="flex-1 w-full">
118
  <button type="button" class="cts-btn-general cts-btn-error btn-push w-full">
119
  Cancelar
 
105
  </p>
106
  </form>
107
 
108
+ {% if name_vacabulary %}
109
+ <p class="hidden cts-original-name">{{ name_vacabulary }}</p>
110
+ {% endif %}
111
+
112
  <article class="flex justify-center gap-4 mt-4">
113
+ {% if name_vacabulary %}
114
  <button type="button" name="start" class="cts-btn-general cts-btn-primary btn-push flex-1 w-full"
115
+ onclick="submitSelectWords('cts-craed-vocabulary', true)">
 
116
  Actualizar vocabulario
117
+ </button>
118
+ {% else %}
119
+ <button type="button" name="start" class="cts-btn-general cts-btn-primary btn-push flex-1 w-full"
120
+ onclick="submitSelectWords('cts-craed-vocabulary')">
121
  Crear vocabulario
 
122
  </button>
123
+ {% endif %}
124
  <a href="{% url 'cata_system:panel_vocabulario' %}" class="flex-1 w-full">
125
  <button type="button" class="cts-btn-general cts-btn-error btn-push w-full">
126
  Cancelar