diff --git a/shiftregister/team/urls.py b/shiftregister/team/urls.py index 8f56b70..966f1bf 100644 --- a/shiftregister/team/urls.py +++ b/shiftregister/team/urls.py @@ -11,6 +11,7 @@ urlpatterns = [ path("shift/", views.shift_detail, name="shift"), path("helper/", views.HelperDetail.as_view(), name="helper"), path("message/", views.bulk_message, name="bulk_message"), + path("message2/", views.bulk_message_helped, name="bulk_message_helped"), path("checkin/", views.checkin, name="checkin"), path("remove_helper/", views.delete_shiftregistration, name="unregister"), ] diff --git a/shiftregister/team/views.py b/shiftregister/team/views.py index ea4df2f..e1fbce2 100644 --- a/shiftregister/team/views.py +++ b/shiftregister/team/views.py @@ -2,11 +2,12 @@ from django.shortcuts import render, get_object_or_404, redirect from django.contrib.auth.decorators import login_required from django.utils import timezone from django.db.models.fields import DateTimeField -from django.db.models import F, Count, Q, ExpressionWrapper +from django.db.models import F, Count, Q, ExpressionWrapper, Case, When from .models import ShiftRegistration, Room, Shift, Helper, Message from django.views.generic import DetailView, ListView from django.contrib.auth.mixins import LoginRequiredMixin from django.contrib import messages +from django.db import models from .forms import BulkMessage, HelperShift # Create your views here. @@ -117,6 +118,55 @@ def bulk_message(request): return render(request, "bulk_message.html", context) +@login_required +def bulk_message_helped(request): + form = BulkMessage() + + if request.method == "POST": + form = BulkMessage(request.POST) + if form.is_valid(): + helpers = Helper.objects.annotate( + shift_count=Count( + Case( + When( + shiftregistration__state__in=[ + ShiftRegistration.RegState.CHECKED_IN, + ], + then=1, + ), + output_field=models.IntegerField(), + ) + ) + ).filter(number_validated=True, shift_count__gte=1) + try: + Message.objects.bulk_create( + ( + Message( + text=form.cleaned_data["message"].replace( + "$token", + f"https://kontakt.rocks{helper.logintoken_set.first().get_absolute_url()}", + ), + to=helper, + ) + for helper in helpers + ) + ) + messages.add_message( + request, messages.SUCCESS, "Massen-Nachricht erfolgreich versendet" + ) + except: + messages.add_message( + request, + messages.ERROR, + "Fehler beim Versenden der Massen-Nachricht", + ) + + context = { + "form": form, + } + return render(request, "bulk_message.html", context) + + class HelperDetail(LoginRequiredMixin, DetailView): template_name = "helper_detail.html" model = Helper