2
0
Fork 0

Implement bulk message sending

This commit is contained in:
Luca 2022-05-16 16:48:32 +02:00
parent 48cb7d98bd
commit c38b194f9c
8 changed files with 81 additions and 4 deletions

View File

@ -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),
),
]

View File

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

View File

@ -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"})
)

View File

@ -0,0 +1,21 @@
{% extends "team_base.html" %}
{% block title %}Massen-Nachricht{% endblock %}
{% block content %}
<h3 class="title">Massen-Nachricht versenden</h3>
<form action="" method="post">
{% csrf_token %}
{% for field in form %}
<div class="field">
<label class="label" for="{{ field.id_for_label }}">{{ field.label }}</label>
<div class="control">
{{ field }}
</div>
{% for error in field.errors %}
<p class="help is-danger">{{ error }}</p>
{% endfor %}
</div>
{% endfor %}
<button class="button is-primary" type="submit">Versenden</button>
</form>
{% endblock %}

View File

@ -7,6 +7,7 @@
<a class="navbar-item" href="{% url 'team:shift_overview' %}">Schichtübersicht</a>
<a class="navbar-item" href="{% url 'team:shift_free' %}">Freie Schichten</a>
<a class="navbar-item" href="{% url 'team:shift_all' %}">Alle Schichten</a>
<a class="navbar-item" href="{% url 'team:bulk_message' %}">Massen-Nachricht</a>
</div>
<div class="navbar-end"></div>
{% endblock %}

View File

@ -10,4 +10,5 @@ urlpatterns = [
path("free_shifts/", views.FreeShiftList.as_view(), name="shift_free"),
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"),
]

View File

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