Sfoglia il codice sorgente

Pongo botón para enviar correo a inscritos

Celestino Rey 1 anno fa
parent
commit
0fb2945e84
3 ha cambiato i file con 57 aggiunte e 0 eliminazioni
  1. 2 0
      src/eventos/urls.py
  2. 46 0
      src/eventos/views.py
  3. 9 0
      src/templates/eventos/detalle_evento.html

+ 2 - 0
src/eventos/urls.py

@@ -5,6 +5,7 @@ from .views import api_lista_eventos, api_detalle_evento
 from .views import api_lista_reservas, api_detalle_reserva
 from .views import api_lista_reservas, api_detalle_reserva
 from .views import api_lista_listaespera, api_detalle_listaespera
 from .views import api_lista_listaespera, api_detalle_listaespera
 from .views import api_lista_noticias, api_detalle_noticia
 from .views import api_lista_noticias, api_detalle_noticia
+from .views import enviar_correo_inscritos
 
 
 app_name = 'eventos'
 app_name = 'eventos'
 
 
@@ -15,6 +16,7 @@ urlpatterns = [
     path('eventos/ayuda/', views.ayuda, name='ayuda'),
     path('eventos/ayuda/', views.ayuda, name='ayuda'),
     path('eventos/crear/', views.crear_evento, name='crear_evento'),  # URL para crear un evento
     path('eventos/crear/', views.crear_evento, name='crear_evento'),  # URL para crear un evento
     path('eventos/editar/', views.editar_evento, name='editar_evento'),  # URL para crear un evento
     path('eventos/editar/', views.editar_evento, name='editar_evento'),  # URL para crear un evento
+    path('eventos/<int:evento_id>/enviar-correo/', enviar_correo_inscritos, name='enviar_correo_inscritos'),
 
 
     path('noticias/<int:noticia_id>/', views.detalle_noticia, name='detalle_noticia'),
     path('noticias/<int:noticia_id>/', views.detalle_noticia, name='detalle_noticia'),
 
 

+ 46 - 0
src/eventos/views.py

@@ -6,6 +6,8 @@ from django.conf import settings
 from django.template.loader import render_to_string
 from django.template.loader import render_to_string
 from django.utils import timezone
 from django.utils import timezone
 
 
+from django.core.mail import send_mail
+
 from rest_framework.response import Response
 from rest_framework.response import Response
 from rest_framework.decorators import api_view
 from rest_framework.decorators import api_view
 from .serializers import EventoSerializer, ReservaSerializer, ListaEsperaSerializer, NoticiaSerializer
 from .serializers import EventoSerializer, ReservaSerializer, ListaEsperaSerializer, NoticiaSerializer
@@ -292,3 +294,47 @@ def api_detalle_noticia(request, noticia_id):
         return Response(serializer.data)
         return Response(serializer.data)
     except Noticia.DoesNotExist:
     except Noticia.DoesNotExist:
         return Response({'error': 'Noticia no encontrado'}, status=404)
         return Response({'error': 'Noticia no encontrado'}, status=404)
+
+
+# Verifica si el usuario es administrador
+def es_admin(user):
+    return user.is_staff
+
+@user_passes_test(es_admin)  # Solo administradores pueden acceder
+def enviar_correo_inscritos(request, evento_id):
+    evento = get_object_or_404(Evento, id=evento_id)
+    inscritos = Reserva.objects.filter(evento=evento).select_related('usuario')
+
+    if not inscritos:
+        messages.warning(request, "No hay usuarios inscritos en este evento.")
+        return redirect('detalle_evento', evento_id=evento.id)
+
+    destinatarios = [reserva.usuario.email for reserva in inscritos if reserva.usuario.email]
+
+    if not destinatarios:
+        messages.warning(request, "No hay correos disponibles para los inscritos.")
+        return redirect('detalle_evento', evento_id=evento.id)
+
+    asunto = f"Información sobre el evento: {evento.titulo}"
+    mensaje = f"""
+    Hola,
+
+    Queremos recordarte que estás inscrito en el evento "{evento.titulo}".
+    Fecha y hora: {evento.fecha} - {evento.hora}
+
+    Cualquier duda, contáctanos.
+
+    Saludos,
+    El equipo de eventos
+    """
+
+    send_mail(
+        asunto,
+        mensaje,
+        settings.DEFAULT_FROM_EMAIL,
+        destinatarios,
+        fail_silently=False
+    )
+
+    messages.success(request, f"Correo enviado a {len(destinatarios)} inscritos.")
+    return redirect('detalle_evento', evento_id=evento.id)

+ 9 - 0
src/templates/eventos/detalle_evento.html

@@ -58,6 +58,15 @@
                 {% if user.is_staff and not evento.evento.publicado %}
                 {% if user.is_staff and not evento.evento.publicado %}
                         <a class="btn btn-danger" href="{% url 'eventos:publicar_evento' evento.id %}">Publicar</a>	
                         <a class="btn btn-danger" href="{% url 'eventos:publicar_evento' evento.id %}">Publicar</a>	
                 {% endif %}
                 {% endif %}
+
+                {% if user.is_staff and evento.evento.publicado %}
+                    <form action="{% url 'eventos:enviar_correo_inscritos' evento.id %}" method="post">
+                        {% csrf_token %}
+                        <button type="submit" class="btn btn-danger">
+                            📧 Enviar correo a inscritos
+                        </button>
+                    </form>
+                {% endif %}
             </div>
             </div>
 
 
         </div><!--//card-header-->
         </div><!--//card-header-->