Spaces:
Sleeping
Sleeping
Creada vista api_words con sus test para GET
Browse files- {cataSystem → cata_system}/__init__.py +0 -0
- {cataSystem → cata_system}/asgi.py +2 -2
- {cataSystem → cata_system}/models.py +0 -0
- {cataSystem → cata_system}/settings.py +3 -3
- {cataSystem → cata_system}/urls.py +1 -1
- {cataSystem → cata_system}/wsgi.py +2 -2
- manage.py +1 -1
- tecnicas/models/palabra.py +9 -2
- tecnicas/test/__init__.py +1 -0
- tecnicas/test/test_words.py +54 -0
- tecnicas/urls.py +1 -0
- tecnicas/views/__init__.py +2 -1
- tecnicas/views/api_words.py +44 -0
{cataSystem → cata_system}/__init__.py
RENAMED
|
File without changes
|
{cataSystem → cata_system}/asgi.py
RENAMED
|
@@ -1,5 +1,5 @@
|
|
| 1 |
"""
|
| 2 |
-
ASGI config for
|
| 3 |
|
| 4 |
It exposes the ASGI callable as a module-level variable named ``application``.
|
| 5 |
|
|
@@ -11,6 +11,6 @@ import os
|
|
| 11 |
|
| 12 |
from django.core.asgi import get_asgi_application
|
| 13 |
|
| 14 |
-
os.environ.setdefault('DJANGO_SETTINGS_MODULE', '
|
| 15 |
|
| 16 |
application = get_asgi_application()
|
|
|
|
| 1 |
"""
|
| 2 |
+
ASGI config for cata_system project.
|
| 3 |
|
| 4 |
It exposes the ASGI callable as a module-level variable named ``application``.
|
| 5 |
|
|
|
|
| 11 |
|
| 12 |
from django.core.asgi import get_asgi_application
|
| 13 |
|
| 14 |
+
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'cata_system.settings')
|
| 15 |
|
| 16 |
application = get_asgi_application()
|
{cataSystem → cata_system}/models.py
RENAMED
|
File without changes
|
{cataSystem → cata_system}/settings.py
RENAMED
|
@@ -1,5 +1,5 @@
|
|
| 1 |
"""
|
| 2 |
-
Django settings for
|
| 3 |
|
| 4 |
Generated by 'django-admin startproject' using Django 5.2.1.
|
| 5 |
|
|
@@ -67,7 +67,7 @@ MIDDLEWARE = [
|
|
| 67 |
|
| 68 |
]
|
| 69 |
|
| 70 |
-
ROOT_URLCONF = '
|
| 71 |
|
| 72 |
TEMPLATES = [
|
| 73 |
{
|
|
@@ -84,7 +84,7 @@ TEMPLATES = [
|
|
| 84 |
},
|
| 85 |
]
|
| 86 |
|
| 87 |
-
WSGI_APPLICATION = '
|
| 88 |
|
| 89 |
|
| 90 |
# Database
|
|
|
|
| 1 |
"""
|
| 2 |
+
Django settings for cata_system project.
|
| 3 |
|
| 4 |
Generated by 'django-admin startproject' using Django 5.2.1.
|
| 5 |
|
|
|
|
| 67 |
|
| 68 |
]
|
| 69 |
|
| 70 |
+
ROOT_URLCONF = 'cata_system.urls'
|
| 71 |
|
| 72 |
TEMPLATES = [
|
| 73 |
{
|
|
|
|
| 84 |
},
|
| 85 |
]
|
| 86 |
|
| 87 |
+
WSGI_APPLICATION = 'cata_system.wsgi.application'
|
| 88 |
|
| 89 |
|
| 90 |
# Database
|
{cataSystem → cata_system}/urls.py
RENAMED
|
@@ -1,5 +1,5 @@
|
|
| 1 |
"""
|
| 2 |
-
URL configuration for
|
| 3 |
|
| 4 |
The `urlpatterns` list routes URLs to views. For more information please see:
|
| 5 |
https://docs.djangoproject.com/en/5.2/topics/http/urls/
|
|
|
|
| 1 |
"""
|
| 2 |
+
URL configuration for cata_system project.
|
| 3 |
|
| 4 |
The `urlpatterns` list routes URLs to views. For more information please see:
|
| 5 |
https://docs.djangoproject.com/en/5.2/topics/http/urls/
|
{cataSystem → cata_system}/wsgi.py
RENAMED
|
@@ -1,5 +1,5 @@
|
|
| 1 |
"""
|
| 2 |
-
WSGI config for
|
| 3 |
|
| 4 |
It exposes the WSGI callable as a module-level variable named ``application``.
|
| 5 |
|
|
@@ -11,6 +11,6 @@ import os
|
|
| 11 |
|
| 12 |
from django.core.wsgi import get_wsgi_application
|
| 13 |
|
| 14 |
-
os.environ.setdefault('DJANGO_SETTINGS_MODULE', '
|
| 15 |
|
| 16 |
application = get_wsgi_application()
|
|
|
|
| 1 |
"""
|
| 2 |
+
WSGI config for cata_system project.
|
| 3 |
|
| 4 |
It exposes the WSGI callable as a module-level variable named ``application``.
|
| 5 |
|
|
|
|
| 11 |
|
| 12 |
from django.core.wsgi import get_wsgi_application
|
| 13 |
|
| 14 |
+
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'cata_system.settings')
|
| 15 |
|
| 16 |
application = get_wsgi_application()
|
manage.py
CHANGED
|
@@ -6,7 +6,7 @@ import sys
|
|
| 6 |
|
| 7 |
def main():
|
| 8 |
"""Run administrative tasks."""
|
| 9 |
-
os.environ.setdefault('DJANGO_SETTINGS_MODULE', '
|
| 10 |
try:
|
| 11 |
from django.core.management import execute_from_command_line
|
| 12 |
except ImportError as exc:
|
|
|
|
| 6 |
|
| 7 |
def main():
|
| 8 |
"""Run administrative tasks."""
|
| 9 |
+
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'cata_system.settings')
|
| 10 |
try:
|
| 11 |
from django.core.management import execute_from_command_line
|
| 12 |
except ImportError as exc:
|
tecnicas/models/palabra.py
CHANGED
|
@@ -1,7 +1,14 @@
|
|
| 1 |
from django.db import models
|
| 2 |
|
|
|
|
| 3 |
class Palabra(models.Model):
|
| 4 |
nombre_palabra = models.CharField(max_length=255, unique=True)
|
| 5 |
-
|
| 6 |
def __str__(self):
|
| 7 |
-
return self.nombre_palabra
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
from django.db import models
|
| 2 |
|
| 3 |
+
|
| 4 |
class Palabra(models.Model):
|
| 5 |
nombre_palabra = models.CharField(max_length=255, unique=True)
|
| 6 |
+
|
| 7 |
def __str__(self):
|
| 8 |
+
return self.nombre_palabra
|
| 9 |
+
|
| 10 |
+
def to_dict(self):
|
| 11 |
+
return {
|
| 12 |
+
"id": self.id,
|
| 13 |
+
"nombre_palabra": self.nombre_palabra
|
| 14 |
+
}
|
tecnicas/test/__init__.py
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
from .test_words import TestsApiWords
|
tecnicas/test/test_words.py
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from django.test import TestCase
|
| 2 |
+
from django.urls import reverse
|
| 3 |
+
from ..models import Palabra
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
class TestsApiWords(TestCase):
|
| 7 |
+
def setUp(self):
|
| 8 |
+
Palabra.objects.create(nombre_palabra="salado")
|
| 9 |
+
|
| 10 |
+
def test_get_word_fail(self):
|
| 11 |
+
word_name = "dulce"
|
| 12 |
+
expected_error = "palabra no encontrada"
|
| 13 |
+
|
| 14 |
+
response = self.client.get(
|
| 15 |
+
reverse("cata_system:api_palabras")+f"?palabra={word_name}"
|
| 16 |
+
)
|
| 17 |
+
|
| 18 |
+
self.assertEqual(response.status_code, 200)
|
| 19 |
+
|
| 20 |
+
res_json = response.json()
|
| 21 |
+
self.assertIn("error", res_json)
|
| 22 |
+
self.assertEqual(res_json["error"], expected_error)
|
| 23 |
+
|
| 24 |
+
def test_get_word_no_parameter(self):
|
| 25 |
+
expected_error = "parametros no encontrados"
|
| 26 |
+
|
| 27 |
+
response = self.client.get(reverse("cata_system:api_palabras"))
|
| 28 |
+
|
| 29 |
+
self.assertEqual(response.status_code, 200)
|
| 30 |
+
|
| 31 |
+
res_json = response.json()
|
| 32 |
+
self.assertIn("error", res_json)
|
| 33 |
+
self.assertEqual(res_json["error"], expected_error)
|
| 34 |
+
|
| 35 |
+
def test_get_word_success(self):
|
| 36 |
+
word_name = "salado"
|
| 37 |
+
expected_message = "dato localizado"
|
| 38 |
+
|
| 39 |
+
expected_data = Palabra.objects.get(nombre_palabra=word_name).to_dict()
|
| 40 |
+
|
| 41 |
+
response = self.client.get(
|
| 42 |
+
reverse("cata_system:api_palabras")+f"?palabra={word_name}"
|
| 43 |
+
)
|
| 44 |
+
|
| 45 |
+
self.assertEqual(response.status_code, 200)
|
| 46 |
+
|
| 47 |
+
res_json = response.json()
|
| 48 |
+
|
| 49 |
+
self.assertIn("message", res_json)
|
| 50 |
+
self.assertEqual(res_json["message"], expected_message)
|
| 51 |
+
|
| 52 |
+
data = res_json["data"]
|
| 53 |
+
self.assertEqual(data["id"], expected_data["id"])
|
| 54 |
+
self.assertEqual(data["name"], expected_data["nombre_palabra"])
|
tecnicas/urls.py
CHANGED
|
@@ -17,4 +17,5 @@ urlpatterns = [
|
|
| 17 |
path("nueva-etiqueta", views.newTag, name="nueva_etiqueta"),
|
| 18 |
path("crear-catador", views.crearCatador, name="crear_catador"),
|
| 19 |
path("buscar-catador", views.searchCatador, name="buscar_catador"),
|
|
|
|
| 20 |
]
|
|
|
|
| 17 |
path("nueva-etiqueta", views.newTag, name="nueva_etiqueta"),
|
| 18 |
path("crear-catador", views.crearCatador, name="crear_catador"),
|
| 19 |
path("buscar-catador", views.searchCatador, name="buscar_catador"),
|
| 20 |
+
path("api/palabras", views.words, name="api_palabras"),
|
| 21 |
]
|
tecnicas/views/__init__.py
CHANGED
|
@@ -9,4 +9,5 @@ from .configuration_panel_tags import configuracionPanelTags
|
|
| 9 |
from .configuration_panel_codes import configurationsPanelCodes
|
| 10 |
from .api_tag import newTag
|
| 11 |
from .catador_create import crearCatador
|
| 12 |
-
from .catador_search import searchCatador
|
|
|
|
|
|
| 9 |
from .configuration_panel_codes import configurationsPanelCodes
|
| 10 |
from .api_tag import newTag
|
| 11 |
from .catador_create import crearCatador
|
| 12 |
+
from .catador_search import searchCatador
|
| 13 |
+
from .api_words import words
|
tecnicas/views/api_words.py
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from django.http import HttpRequest, JsonResponse
|
| 2 |
+
from django.db import IntegrityError
|
| 3 |
+
from ..models import Palabra
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
def words(req: HttpRequest):
|
| 7 |
+
if req.method == "GET":
|
| 8 |
+
try:
|
| 9 |
+
word_name = req.GET["palabra"]
|
| 10 |
+
fund_word = Palabra.objects.get(nombre_palabra=word_name)
|
| 11 |
+
except KeyError:
|
| 12 |
+
return error()
|
| 13 |
+
except Palabra.DoesNotExist:
|
| 14 |
+
return error("palabra no encontrada")
|
| 15 |
+
|
| 16 |
+
response = {
|
| 17 |
+
"message": "dato localizado",
|
| 18 |
+
"data": {
|
| 19 |
+
"id": fund_word.id,
|
| 20 |
+
"name": fund_word.nombre_palabra,
|
| 21 |
+
}
|
| 22 |
+
}
|
| 23 |
+
|
| 24 |
+
return JsonResponse(response)
|
| 25 |
+
elif req.method == "POST":
|
| 26 |
+
try:
|
| 27 |
+
word_name = req.POST["nombre_palabra"]
|
| 28 |
+
new_word = Palabra.objects.create(nombre_palabra=word_name)
|
| 29 |
+
except KeyError:
|
| 30 |
+
return error()
|
| 31 |
+
except IntegrityError:
|
| 32 |
+
return error("palabra repetida")
|
| 33 |
+
|
| 34 |
+
response = {
|
| 35 |
+
"message": "palabra creada",
|
| 36 |
+
"data": new_word.to_dict()
|
| 37 |
+
}
|
| 38 |
+
|
| 39 |
+
return JsonResponse(response)
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
def error(message="parametros no encontrados"):
|
| 43 |
+
respuesta = {"error": message}
|
| 44 |
+
return JsonResponse(respuesta)
|