From 198fb88bfdc60d57a93ef271518e1cd9203b679c Mon Sep 17 00:00:00 2001 From: moanos Date: Fri, 2 Aug 2024 20:03:25 +0200 Subject: [PATCH] feat: add basic subscriptions + emit notification for comment --- .../migrations/0003_subscriptions.py | 46 +++++++++++++++++++ src/fellchensammlung/models.py | 5 +- src/fellchensammlung/views.py | 14 ++++-- 3 files changed, 61 insertions(+), 4 deletions(-) create mode 100644 src/fellchensammlung/migrations/0003_subscriptions.py diff --git a/src/fellchensammlung/migrations/0003_subscriptions.py b/src/fellchensammlung/migrations/0003_subscriptions.py new file mode 100644 index 0000000..98a8c30 --- /dev/null +++ b/src/fellchensammlung/migrations/0003_subscriptions.py @@ -0,0 +1,46 @@ +# Generated by Django 5.0.6 on 2024-08-02 17:31 + +import django.db.models.deletion +from django.conf import settings +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ("fellchensammlung", "0002_basenotification_commentnotification"), + ] + + operations = [ + migrations.CreateModel( + name="Subscriptions", + fields=[ + ( + "id", + models.BigAutoField( + auto_created=True, + primary_key=True, + serialize=False, + verbose_name="ID", + ), + ), + ("created_at", models.DateTimeField(auto_now_add=True)), + ( + "adoption_notice", + models.ForeignKey( + on_delete=django.db.models.deletion.CASCADE, + to="fellchensammlung.adoptionnotice", + verbose_name="AdoptionNotice", + ), + ), + ( + "user", + models.ForeignKey( + on_delete=django.db.models.deletion.CASCADE, + to=settings.AUTH_USER_MODEL, + verbose_name="Nutzer*in", + ), + ), + ], + ), + ] diff --git a/src/fellchensammlung/models.py b/src/fellchensammlung/models.py index 1c90f4b..f67f7f8 100644 --- a/src/fellchensammlung/models.py +++ b/src/fellchensammlung/models.py @@ -185,6 +185,9 @@ class AdoptionNotice(models.Model): def get_report_url(self): return reverse('report-adoption-notice', args=[str(self.id)]) + def get_subscriptions(self): + return Subscriptions.objects.filter(adoption_notice=self) + def get_photos(self): """ First trys to get group photos that are attached to the adoption notice if there is none it trys to fetch @@ -557,4 +560,4 @@ class CommentNotification(BaseNotification): class Subscriptions(models.Model): user = models.ForeignKey(User, on_delete=models.CASCADE, verbose_name=_('Nutzer*in')) adoption_notice = models.ForeignKey(AdoptionNotice, on_delete=models.CASCADE, verbose_name=_('AdoptionNotice')) - created_at = models.DateTimeField(auto_now_add=True) \ No newline at end of file + created_at = models.DateTimeField(auto_now_add=True) diff --git a/src/fellchensammlung/views.py b/src/fellchensammlung/views.py index e4cd7de..b9ce936 100644 --- a/src/fellchensammlung/views.py +++ b/src/fellchensammlung/views.py @@ -12,7 +12,7 @@ from notfellchen import settings from fellchensammlung import logger from .models import AdoptionNotice, Text, Animal, Rule, Image, Report, ModerationAction, \ - User, Location, AdoptionNoticeStatus, Subscriptions + User, Location, AdoptionNoticeStatus, Subscriptions, CommentNotification from .forms import AdoptionNoticeForm, AdoptionNoticeFormWithDateWidget, ImageForm, ReportAdoptionNoticeForm, \ CommentForm, ReportCommentForm, AnimalForm, \ AdoptionNoticeSearchForm, AnimalFormWithDateWidget @@ -59,12 +59,20 @@ def adoption_notice_detail(request, adoption_notice_id): if comment_form.is_valid(): comment_instance = comment_form.save(commit=False) comment_instance.adoption_notice_id = adoption_notice_id - comment_instance.user = request.user.member + comment_instance.user = request.user comment_instance.save() # Auto-subscribe user to adoption notice - subscription = Subscriptions(adoption_notice=adoption_notice, user=request.user) + subscription, created = Subscriptions.objects.get_or_create(adoption_notice=adoption_notice, user=request.user) subscription.save() + + # Notify users that a comment was added + for subscription in adoption_notice.get_subscriptions(): + notification = CommentNotification(user=subscription.user, + title=f"{adoption_notice.name} - Neuer Kommentar", + text=f"{request.user}: {comment_instance.text}", + comment=comment_instance) + notification.save() else: raise PermissionDenied else: