from rest_framework.response import Response from rest_framework.decorators import api_view from django.utils import timezone from rest_framework import status import os from django.conf import settings from django.contrib.auth.decorators import user_passes_test from django.shortcuts import render, get_object_or_404 from django.http import HttpResponseForbidden from eventos.models import Noticia, Evento from .serializers import EventoSerializer @user_passes_test(lambda u: u.is_staff) def ver_variables_entorno(request): if not settings.DEBUG: return HttpResponseForbidden("Acceso prohibido") # Variables a excluir por motivos de seguridad variables_excluidas = {'SECRET_KEY', 'DATABASES', 'EMAIL_HOST_PASSWORD', 'API_KEY'} # Obtiene todas las variables de entorno entorno = {key: os.getenv(key) for key in os.environ.keys() if key not in variables_excluidas} # Obtiene todas las variables de settings excluyendo las confidenciales configuracion = { key: getattr(settings, key) for key in dir(settings) if key.isupper() and key not in variables_excluidas } # Combina ambas en un solo diccionario contexto = { 'entorno': entorno, 'configuracion': configuracion } return render(request, 'ver_entorno.html', contexto) def principal(request): # Filtra solo las noticias publicadas noticias = Noticia.objects.filter(publicado=True) for noticia in noticias: print("Principal en gestion_reservas: ", noticia.titulo) return render(request, 'index.html', {'noticias': noticias}) @api_view(['GET']) def proximo_evento(request): # Obtiene y valida el parámetro `publicado` publicado_param = request.GET.get('publicado', 'true').lower() if publicado_param not in ['true', 'false', 'all']: return Response( {'detail': 'El parámetro "publicado" debe ser "true", "false" o "all".'}, status=status.HTTP_400_BAD_REQUEST ) # Configura el filtro según el parámetro `publicado` if publicado_param == 'all': eventos = Evento.objects.filter(fecha__gte=timezone.now()).order_by('fecha') else: publicado = publicado_param == 'true' eventos = Evento.objects.filter(publicado=publicado, fecha__gte=timezone.now()).order_by('fecha') evento = eventos.first() if evento: serializer = EventoSerializer(evento) return Response(serializer.data) else: return Response({'detail': 'No hay eventos próximos.'}, status=404) @api_view(['GET']) def todos_los_eventos(request): # Obtiene y valida el parámetro `publicado` publicado_param = request.GET.get('publicado', 'true').lower() if publicado_param not in ['true', 'false', 'all']: return Response( {'detail': 'El parámetro "publicado" debe ser "true", "false" o "all".'}, status=status.HTTP_400_BAD_REQUEST ) # Configura el filtro según el parámetro `publicado` if publicado_param == 'all': eventos = Evento.objects.all().order_by('fecha') else: publicado = publicado_param == 'true' eventos = Evento.objects.filter(publicado=publicado).order_by('fecha') serializer = EventoSerializer(eventos, many=True) # `many=True` para serializar una lista de eventos return Response(serializer.data)