diff --git a/shiftregister/core/templates/pagination.html b/shiftregister/core/templates/pagination.html new file mode 100644 index 0000000..5cbd26a --- /dev/null +++ b/shiftregister/core/templates/pagination.html @@ -0,0 +1,27 @@ +{% if page.has_other_pages %} + +{% endif %} diff --git a/shiftregister/settings.py b/shiftregister/settings.py index b86b60b..20523aa 100644 --- a/shiftregister/settings.py +++ b/shiftregister/settings.py @@ -50,6 +50,7 @@ INSTALLED_APPS = [ "django.contrib.admin", "django.contrib.auth", "django.contrib.contenttypes", + "django.contrib.humanize", "django.contrib.sessions", "django.contrib.messages", "django.contrib.staticfiles", diff --git a/shiftregister/team/admin.py b/shiftregister/team/admin.py index 83e7dd6..1c8ba9d 100644 --- a/shiftregister/team/admin.py +++ b/shiftregister/team/admin.py @@ -7,3 +7,4 @@ from .models import IncomingMessage @admin.register(IncomingMessage) class IncomingMessageAdmin(admin.ModelAdmin): list_display = ("sender", "created_at") + readonly_fields = ("id", "sender", "content", "created_at", "read") diff --git a/shiftregister/team/migrations/0003_incomingmessage_read_and_more.py b/shiftregister/team/migrations/0003_incomingmessage_read_and_more.py new file mode 100644 index 0000000..2272cd2 --- /dev/null +++ b/shiftregister/team/migrations/0003_incomingmessage_read_and_more.py @@ -0,0 +1,21 @@ +# Generated by Django 4.0.4 on 2023-05-07 11:52 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + dependencies = [ + ("team", "0002_alter_incomingmessage_id"), + ] + + operations = [ + migrations.AddField( + model_name="incomingmessage", + name="read", + field=models.BooleanField(default=False), + ), + migrations.AddIndex( + model_name="incomingmessage", + index=models.Index(fields=["sender"], name="team_incomi_sender_799f7d_idx"), + ), + ] diff --git a/shiftregister/team/models.py b/shiftregister/team/models.py index 76fcbc5..7005c1e 100644 --- a/shiftregister/team/models.py +++ b/shiftregister/team/models.py @@ -8,8 +8,20 @@ from shiftregister.app.models import * class IncomingMessage(models.Model): class Meta: get_latest_by = "created_at" + indexes = (models.Index(fields=("sender",)),) id = models.BigIntegerField(primary_key=True) sender = PhoneNumberField() content = models.TextField() created_at = models.DateTimeField() + read = models.BooleanField(default=False) + + def from_helper(self): + try: + return Helper.objects.get(phone=self.sender) + except Helper.DoesNotExist: + return None + + def display_sender(self): + helper = self.from_helper() + return helper.name if helper else self.sender diff --git a/shiftregister/team/signals.py b/shiftregister/team/signals.py index f387a30..d9a308b 100644 --- a/shiftregister/team/signals.py +++ b/shiftregister/team/signals.py @@ -1,6 +1,7 @@ from django.dispatch import receiver from django.shortcuts import reverse from shiftregister.core.signals import populate_nav +from .models import IncomingMessage @receiver(populate_nav, dispatch_uid="populate_team_nav") @@ -20,4 +21,14 @@ def populate_team_nav(sender, **kwargs): {"link": reverse("team:bulk_message"), "text": "Massen-Nachricht"} ) + num_unread = IncomingMessage.objects.filter(read=False).count() + nav_items.append( + { + "link": reverse("team:incoming_messages"), + "text": "Eingehende Nachrichten" + f" ({num_unread})" + if num_unread + else "", + } + ) + return nav_items diff --git a/shiftregister/team/templates/incoming_messages.html b/shiftregister/team/templates/incoming_messages.html new file mode 100644 index 0000000..496d1cc --- /dev/null +++ b/shiftregister/team/templates/incoming_messages.html @@ -0,0 +1,21 @@ +{% extends "base.html" %} + +{% load humanize %} + +{% block title %}Sie haben {{ num_unread | apnumber }} neue Nachricht{{ num_unread | pluralize:"en" }}{% endblock %} + +{% block content %} +{% for message in page %} + +
+
+ {{ message.display_sender }} {{ message.created_at | naturaltime }} +
+
+ {{ message.content }} +
+
+
+{% endfor %} +{% include "pagination.html" %} +{% endblock %} diff --git a/shiftregister/team/urls.py b/shiftregister/team/urls.py index 8f56b70..f4b1558 100644 --- a/shiftregister/team/urls.py +++ b/shiftregister/team/urls.py @@ -13,4 +13,6 @@ urlpatterns = [ path("message/", views.bulk_message, name="bulk_message"), path("checkin/", views.checkin, name="checkin"), path("remove_helper/", views.delete_shiftregistration, name="unregister"), + path("incoming/", views.incoming_messages, name="incoming_messages"), + path("incoming/", views.conversation, name="conversation"), ] diff --git a/shiftregister/team/views.py b/shiftregister/team/views.py index 2e900ca..8b8a0ba 100644 --- a/shiftregister/team/views.py +++ b/shiftregister/team/views.py @@ -3,11 +3,12 @@ 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, Case, When -from .models import ShiftRegistration, Room, Shift, Helper, Message +from .models import ShiftRegistration, Room, Shift, Helper, Message, IncomingMessage 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 django.core.paginator import Paginator from .forms import BulkMessage, HelperShift @@ -192,3 +193,24 @@ def delete_shiftregistration(request, pk): spk = reg.shift.pk reg.delete() return redirect("team:shift", pk=spk) + + +@login_required +def incoming_messages(request): + p = Paginator(IncomingMessage.objects.order_by("read", "-created_at"), 10) + page = p.get_page(request.GET.get("page")) + + return render( + request, + "incoming_messages.html", + { + "num_unread": IncomingMessage.objects.filter(read=False).count(), + "page": page, + "page_range": p.get_elided_page_range(page.number), + }, + ) + + +@login_required +def conversation(request, sender): + pass