2
0
Fork 0

Merge separate bulk message views

This commit is contained in:
Luca 2022-05-27 15:25:06 +02:00
parent 7db3e68260
commit 440e128bcc
4 changed files with 21 additions and 46 deletions

View File

@ -19,3 +19,6 @@ class BulkMessage(forms.Form):
message = forms.CharField( message = forms.CharField(
label="Nachricht", widget=forms.Textarea(attrs={"class": "textarea"}) 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
)

View File

@ -7,10 +7,16 @@
{% csrf_token %} {% csrf_token %}
{% for field in form %} {% for field in form %}
<div class="field"> <div class="field">
{% if field.widget_type == 'checkbox' %}
<div class="control">
<label class="checkbox" for="{{ field.id_for_label }}">{{ field }} {{ field.label }}</label>
</div>
{% else %}
<label class="label" for="{{ field.id_for_label }}">{{ field.label }}</label> <label class="label" for="{{ field.id_for_label }}">{{ field.label }}</label>
<div class="control"> <div class="control">
{{ field }} {{ field }}
</div> </div>
{% endif %}
{% for error in field.errors %} {% for error in field.errors %}
<p class="help is-danger">{{ error }}</p> <p class="help is-danger">{{ error }}</p>
{% endfor %} {% endfor %}

View File

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

View File

@ -89,42 +89,8 @@ def bulk_message(request):
if request.method == "POST": if request.method == "POST":
form = BulkMessage(request.POST) form = BulkMessage(request.POST)
if form.is_valid(): if form.is_valid():
try: helpers = Helper.objects.filter(number_validated=True)
Message.objects.bulk_create( if form.cleaned_data["checked_in_only"]:
(
Message(
text=form.cleaned_data["message"].replace(
"$token",
f"https://kontakt.rocks{helper.logintoken_set.first().get_absolute_url()}",
),
to=helper,
)
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",
)
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( helpers = Helper.objects.annotate(
shift_count=Count( shift_count=Count(
Case( Case(
@ -138,6 +104,7 @@ def bulk_message_helped(request):
) )
) )
).filter(number_validated=True, shift_count__gte=1) ).filter(number_validated=True, shift_count__gte=1)
try: try:
Message.objects.bulk_create( Message.objects.bulk_create(
( (