From c38b194f9cdbb45cb646156a46f811294e40fc0b Mon Sep 17 00:00:00 2001 From: Luca Date: Mon, 16 May 2022 16:48:32 +0200 Subject: [PATCH] Implement bulk message sending --- .../migrations/0008_alter_message_sent_at.py | 18 ++++++++++ shiftregister/app/models.py | 2 +- shiftregister/app/templates/register.html | 2 +- shiftregister/team/forms.py | 6 ++++ .../team/templates/bulk_message.html | 21 ++++++++++++ shiftregister/team/templates/team_base.html | 1 + shiftregister/team/urls.py | 1 + shiftregister/team/views.py | 34 +++++++++++++++++-- 8 files changed, 81 insertions(+), 4 deletions(-) create mode 100644 shiftregister/app/migrations/0008_alter_message_sent_at.py create mode 100644 shiftregister/team/templates/bulk_message.html diff --git a/shiftregister/app/migrations/0008_alter_message_sent_at.py b/shiftregister/app/migrations/0008_alter_message_sent_at.py new file mode 100644 index 0000000..7273d2e --- /dev/null +++ b/shiftregister/app/migrations/0008_alter_message_sent_at.py @@ -0,0 +1,18 @@ +# Generated by Django 4.0.4 on 2022-05-16 14:13 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ("app", "0007_shiftregistration_state"), + ] + + operations = [ + migrations.AlterField( + model_name="message", + name="sent_at", + field=models.DateTimeField(blank=True, null=True), + ), + ] diff --git a/shiftregister/app/models.py b/shiftregister/app/models.py index 49a4383..f78cd5c 100644 --- a/shiftregister/app/models.py +++ b/shiftregister/app/models.py @@ -149,7 +149,7 @@ class Message(models.Model): text = models.CharField(max_length=160) to = models.ForeignKey(Helper, on_delete=models.CASCADE) created_at = models.DateTimeField(auto_now_add=True) - sent_at = models.DateTimeField(null=True) + sent_at = models.DateTimeField(blank=True, null=True) def __str__(self): return f"{self.to.name}({self.created_at}): {self.text}" diff --git a/shiftregister/app/templates/register.html b/shiftregister/app/templates/register.html index 7aadfed..bae4ef8 100644 --- a/shiftregister/app/templates/register.html +++ b/shiftregister/app/templates/register.html @@ -18,7 +18,7 @@ {% endif %} {% for error in field.errors %} -

{{ error }}

+

{{ error }}

{% endfor %} {% if field.help_text %}

{{ field.help_text }}

diff --git a/shiftregister/team/forms.py b/shiftregister/team/forms.py index 7a47018..2bd0a3f 100644 --- a/shiftregister/team/forms.py +++ b/shiftregister/team/forms.py @@ -10,3 +10,9 @@ class HelperShift(forms.Form): helper = forms.ModelChoiceField( label="Helfer*in", queryset=Helper.objects.order_by("name") ) + + +class BulkMessage(forms.Form): + message = forms.CharField( + label="Nachricht", widget=forms.Textarea(attrs={"class": "textarea"}) + ) diff --git a/shiftregister/team/templates/bulk_message.html b/shiftregister/team/templates/bulk_message.html new file mode 100644 index 0000000..679b7b1 --- /dev/null +++ b/shiftregister/team/templates/bulk_message.html @@ -0,0 +1,21 @@ +{% extends "team_base.html" %} + +{% block title %}Massen-Nachricht{% endblock %} +{% block content %} +

Massen-Nachricht versenden

+
+ {% csrf_token %} +{% for field in form %} +
+ +
+ {{ field }} +
+{% for error in field.errors %} +

{{ error }}

+{% endfor %} +
+{% endfor %} + +
+{% endblock %} diff --git a/shiftregister/team/templates/team_base.html b/shiftregister/team/templates/team_base.html index bae2910..b3f2acf 100644 --- a/shiftregister/team/templates/team_base.html +++ b/shiftregister/team/templates/team_base.html @@ -7,6 +7,7 @@ Schichtübersicht Freie Schichten Alle Schichten + Massen-Nachricht {% endblock %} diff --git a/shiftregister/team/urls.py b/shiftregister/team/urls.py index 6c9df3d..a2660b3 100644 --- a/shiftregister/team/urls.py +++ b/shiftregister/team/urls.py @@ -10,4 +10,5 @@ urlpatterns = [ path("free_shifts/", views.FreeShiftList.as_view(), name="shift_free"), path("shift/", views.shift_detail, name="shift"), path("helper/", views.HelperDetail.as_view(), name="helper"), + path("message/", views.bulk_message, name="bulk_message"), ] diff --git a/shiftregister/team/views.py b/shiftregister/team/views.py index 818001a..3bb27e6 100644 --- a/shiftregister/team/views.py +++ b/shiftregister/team/views.py @@ -3,11 +3,11 @@ 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 .models import ShiftRegistration, Room, Shift, Helper +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 .forms import HelperShift +from .forms import BulkMessage, HelperShift # Create your views here. def index(request): @@ -77,6 +77,36 @@ def shift_detail(request, pk): return render(request, "shift_detail.html", context) +@login_required +def bulk_message(request): + form = BulkMessage() + + if request.method == "POST": + form = BulkMessage(request.POST) + if form.is_valid(): + try: + Message.objects.bulk_create( + ( + Message(text=form.cleaned_data["message"], to=helper) + for helper in Helper.objects.all() + ) + ) + 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