feat: add basic subscriptions + emit notification for comment
This commit is contained in:
parent
24b4b1fad0
commit
198fb88bfd
46
src/fellchensammlung/migrations/0003_subscriptions.py
Normal file
46
src/fellchensammlung/migrations/0003_subscriptions.py
Normal 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",
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
]
|
@ -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)
|
||||
created_at = models.DateTimeField(auto_now_add=True)
|
||||
|
@ -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:
|
||||
|
Loading…
Reference in New Issue
Block a user