Browse Source

Api para ayuda

politica
Celestino Rey 9 months ago
parent
commit
5d054027bb
7 changed files with 63 additions and 6 deletions
  1. +1
    -1
      JugarAlPadel/K8S/Makefile
  2. +0
    -0
      JugarAlPadel/gestion_reservas/gestion_reservas/management/__init__.py
  3. +0
    -0
      JugarAlPadel/gestion_reservas/gestion_reservas/management/commands/__init__.py
  4. +41
    -0
      JugarAlPadel/gestion_reservas/gestion_reservas/management/commands/importar_ayuda.py
  5. +4
    -5
      JugarAlPadel/gestion_reservas/gestion_reservas/serializers.py
  6. +3
    -0
      JugarAlPadel/gestion_reservas/gestion_reservas/urls.py
  7. +14
    -0
      JugarAlPadel/gestion_reservas/gestion_reservas/views.py

+ 1
- 1
JugarAlPadel/K8S/Makefile View File

@ -2,7 +2,7 @@ export ARQUITECTURA := $(shell lscpu |grep itectur | tr -d ' '| cut -f2 -d':')
#export REGISTRY=localhost:5000
export REGISTRY=registry.reymota.es
export IMG_VERSION = 0.58
export IMG_VERSION = 0.59
export IMG_NGINX_VERSION = 2.3
# limpia todo


+ 0
- 0
JugarAlPadel/gestion_reservas/gestion_reservas/management/__init__.py View File


+ 0
- 0
JugarAlPadel/gestion_reservas/gestion_reservas/management/commands/__init__.py View File


+ 41
- 0
JugarAlPadel/gestion_reservas/gestion_reservas/management/commands/importar_ayuda.py View File

@ -0,0 +1,41 @@
import json
from django.core.management.base import BaseCommand
from gestion_reservas.models import Ayuda
class Command(BaseCommand):
help = "Importa la ayuda desde un archivo JSON"
def add_arguments(self, parser):
parser.add_argument('archivo_json', type=str, help="Ruta del archivo JSON")
def handle(self, *args, **kwargs):
archivo_json = kwargs['archivo_json']
try:
with open(archivo_json, 'r', encoding='utf-8') as file:
datos = json.load(file)
self.stdout.write(self.style.WARNING(f"\nSe encontraron {len(datos)} elementos de ayuda en el archivo '{archivo_json}'."))
confirmar = input("¿Deseas continuar con la importación? (s/n): ").strip().lower()
if confirmar != 's':
self.stdout.write(self.style.ERROR("Importación cancelada."))
return
ayuda_creados = 0
for ayuda_data in datos:
creado = Ayuda.objects.create(
titulo=ayuda_data['titulo'],
descripcion=ayuda_data['descripcion'],
apartado=ayuda_data['apartado'],
)
if creado:
ayuda_creados += 1
self.stdout.write(self.style.SUCCESS(f'Se importaron {ayuda_creados} entradas de ayuda correctamente.'))
except FileNotFoundError:
self.stderr.write(self.style.ERROR(f"El archivo {archivo_json} no se encontró."))
except json.JSONDecodeError:
self.stderr.write(self.style.ERROR("Error al leer el archivo JSON. Asegúrate de que el formato sea correcto."))

+ 4
- 5
JugarAlPadel/gestion_reservas/gestion_reservas/serializers.py View File

@ -1,9 +1,8 @@
from rest_framework import serializers
from eventos.models import Evento
from .models import Ayuda
class EventoSerializer(serializers.ModelSerializer):
class AyudaSerializer(serializers.ModelSerializer):
class Meta:
model = Evento
fields = ['id', 'nombre', 'descripcion', 'fecha', 'hora', 'plazas_restantes', 'publicado']
model = Ayuda
fields = '__all__' # Incluir todos los campos del modelo

+ 3
- 0
JugarAlPadel/gestion_reservas/gestion_reservas/urls.py View File

@ -21,6 +21,7 @@ from django.conf import settings
from django.views.generic.base import TemplateView # new
from . import views
from .views import api_lista_ayuda
urlpatterns = [
@ -36,5 +37,7 @@ urlpatterns = [
path('entorno/', views.ver_variables_entorno, name='ver_variables_entorno'),
path('ayuda/', views.ayuda, name='ayuda'),
path('api/ayuda/', api_lista_ayuda, name='api_lista_ayuda'),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

+ 14
- 0
JugarAlPadel/gestion_reservas/gestion_reservas/views.py View File

@ -8,6 +8,8 @@ from django.contrib.auth.decorators import user_passes_test
from django.shortcuts import render, get_object_or_404
from django.http import HttpResponseForbidden
from rest_framework.decorators import api_view
from eventos.models import Noticia, Evento
from .serializers import EventoSerializer
from .models import Ayuda
@ -60,3 +62,15 @@ def ayuda(request):
item.descripcion = markdown.markdown(item.descripcion)
return render(request, 'ayuda.html', {'elementos': elementos_ayuda, 'apartados': apartados})
#
# API
#
@api_view(['GET'])
def api_lista_ayuda(request):
"""Devuelve la lista de toda la ayuda."""
ayuda = Ayuda.objects.all()
serializer = EventoSerializer(ayuda, many=True)
return Response(serializer.data)

Loading…
Cancel
Save