diff --git a/shiftregister/team/forms.py b/shiftregister/team/forms.py index ba7c2ce..3e4e01d 100644 --- a/shiftregister/team/forms.py +++ b/shiftregister/team/forms.py @@ -19,3 +19,6 @@ class BulkMessage(forms.Form): message = forms.CharField( label="Nachricht", widget=forms.Textarea(attrs={"class": "textarea"}) ) + checked_in_only = forms.BooleanField( + label="Nur an Helfer*innen mit mindestens einem Check-in senden", required=False + ) diff --git a/shiftregister/team/templates/bulk_message.html b/shiftregister/team/templates/bulk_message.html index 679b7b1..f951dfc 100644 --- a/shiftregister/team/templates/bulk_message.html +++ b/shiftregister/team/templates/bulk_message.html @@ -7,10 +7,16 @@ {% csrf_token %} {% for field in form %}
+{% if field.widget_type == 'checkbox' %} +
+ +
+{% else %}
{{ field }}
+{% endif %} {% for error in field.errors %}

{{ error }}

{% endfor %} diff --git a/shiftregister/team/urls.py b/shiftregister/team/urls.py index 966f1bf..8f56b70 100644 --- a/shiftregister/team/urls.py +++ b/shiftregister/team/urls.py @@ -11,7 +11,6 @@ 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 e1fbce2..4c63006 100644 --- a/shiftregister/team/views.py +++ b/shiftregister/team/views.py @@ -89,55 +89,22 @@ def bulk_message(request): if request.method == "POST": form = BulkMessage(request.POST) if form.is_valid(): - try: - Message.objects.bulk_create( - ( - Message( - text=form.cleaned_data["message"].replace( - "$token", - f"https://kontakt.rocks{helper.logintoken_set.first().get_absolute_url()}", + helpers = Helper.objects.filter(number_validated=True) + if form.cleaned_data["checked_in_only"]: + helpers = Helper.objects.annotate( + shift_count=Count( + Case( + When( + shiftregistration__state__in=[ + ShiftRegistration.RegState.CHECKED_IN, + ], + then=1, ), - to=helper, + output_field=models.IntegerField(), ) - for helper in Helper.objects.filter(number_validated=True) ) - ) - messages.add_message( - request, messages.SUCCESS, "Massen-Nachricht erfolgreich versendet" - ) - except: - messages.add_message( - request, - messages.ERROR, - "Fehler beim Versenden der Massen-Nachricht", - ) + ).filter(number_validated=True, shift_count__gte=1) - context = { - "form": form, - } - 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( (