feat: add basic subscriptions + emit notification for comment

This commit is contained in:
moanos [he/him] 2024-08-02 20:03:25 +02:00
parent 24b4b1fad0
commit 198fb88bfd
3 changed files with 61 additions and 4 deletions

View File

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

View File

@ -185,6 +185,9 @@ class AdoptionNotice(models.Model):
def get_report_url(self): def get_report_url(self):
return reverse('report-adoption-notice', args=[str(self.id)]) return reverse('report-adoption-notice', args=[str(self.id)])
def get_subscriptions(self):
return Subscriptions.objects.filter(adoption_notice=self)
def get_photos(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 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): class Subscriptions(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE, verbose_name=_('Nutzer*in')) user = models.ForeignKey(User, on_delete=models.CASCADE, verbose_name=_('Nutzer*in'))
adoption_notice = models.ForeignKey(AdoptionNotice, on_delete=models.CASCADE, verbose_name=_('AdoptionNotice')) adoption_notice = models.ForeignKey(AdoptionNotice, on_delete=models.CASCADE, verbose_name=_('AdoptionNotice'))
created_at = models.DateTimeField(auto_now_add=True) created_at = models.DateTimeField(auto_now_add=True)

View File

@ -12,7 +12,7 @@ from notfellchen import settings
from fellchensammlung import logger from fellchensammlung import logger
from .models import AdoptionNotice, Text, Animal, Rule, Image, Report, ModerationAction, \ 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, \ from .forms import AdoptionNoticeForm, AdoptionNoticeFormWithDateWidget, ImageForm, ReportAdoptionNoticeForm, \
CommentForm, ReportCommentForm, AnimalForm, \ CommentForm, ReportCommentForm, AnimalForm, \
AdoptionNoticeSearchForm, AnimalFormWithDateWidget AdoptionNoticeSearchForm, AnimalFormWithDateWidget
@ -59,12 +59,20 @@ def adoption_notice_detail(request, adoption_notice_id):
if comment_form.is_valid(): if comment_form.is_valid():
comment_instance = comment_form.save(commit=False) comment_instance = comment_form.save(commit=False)
comment_instance.adoption_notice_id = adoption_notice_id comment_instance.adoption_notice_id = adoption_notice_id
comment_instance.user = request.user.member comment_instance.user = request.user
comment_instance.save() comment_instance.save()
# Auto-subscribe user to adoption notice # 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() 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: else:
raise PermissionDenied raise PermissionDenied
else: else: