2
0
Fork 0

add bulk message for helpers

This commit is contained in:
Andreas (@xAndy) Zimmermann 2022-05-26 17:41:43 +02:00
parent e65e5eb2e6
commit 7db3e68260
2 changed files with 52 additions and 1 deletions

View File

@ -11,6 +11,7 @@ urlpatterns = [
path("shift/<int:pk>", views.shift_detail, name="shift"),
path("helper/<int:pk>", 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/<int:pk>", views.checkin, name="checkin"),
path("remove_helper/<int:pk>", views.delete_shiftregistration, name="unregister"),
]

View File

@ -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