diff --git a/src/fellchensammlung/migrations/0002_basenotification_commentnotification.py b/src/fellchensammlung/migrations/0002_basenotification_commentnotification.py new file mode 100644 index 0000000..7bf4ac9 --- /dev/null +++ b/src/fellchensammlung/migrations/0002_basenotification_commentnotification.py @@ -0,0 +1,66 @@ +# Generated by Django 5.0.6 on 2024-08-02 17:11 + +import django.db.models.deletion +from django.conf import settings +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ("fellchensammlung", "0001_initial"), + ] + + operations = [ + migrations.CreateModel( + name="BaseNotification", + fields=[ + ( + "id", + models.BigAutoField( + auto_created=True, + primary_key=True, + serialize=False, + verbose_name="ID", + ), + ), + ("created_at", models.DateTimeField(auto_now_add=True)), + ("title", models.CharField(max_length=100)), + ("text", models.TextField(verbose_name="Inhalt")), + ("read", models.BooleanField(default=False)), + ( + "user", + models.ForeignKey( + on_delete=django.db.models.deletion.CASCADE, + to=settings.AUTH_USER_MODEL, + verbose_name="Nutzer*in", + ), + ), + ], + ), + migrations.CreateModel( + name="CommentNotification", + fields=[ + ( + "basenotification_ptr", + models.OneToOneField( + auto_created=True, + on_delete=django.db.models.deletion.CASCADE, + parent_link=True, + primary_key=True, + serialize=False, + to="fellchensammlung.basenotification", + ), + ), + ( + "comment", + models.ForeignKey( + on_delete=django.db.models.deletion.CASCADE, + to="fellchensammlung.comment", + verbose_name="Antwort", + ), + ), + ], + bases=("fellchensammlung.basenotification",), + ), + ] diff --git a/src/fellchensammlung/models.py b/src/fellchensammlung/models.py index f7e766e..a5dd806 100644 --- a/src/fellchensammlung/models.py +++ b/src/fellchensammlung/models.py @@ -68,6 +68,9 @@ class User(AbstractUser): def get_absolute_url(self): return reverse("user-detail", args=[str(self.pk)]) + def get_notifications_url(self): + return self.get_absolute_url() + class Image(models.Model): image = models.ImageField(upload_to='images') @@ -531,3 +534,21 @@ class Comment(models.Model): @property def get_absolute_url(self): return self.adoption_notice.get_absolute_url() + + +class BaseNotification(models.Model): + created_at = models.DateTimeField(auto_now_add=True) + title = models.CharField(max_length=100) + text = models.TextField(verbose_name="Inhalt") + user = models.ForeignKey(User, on_delete=models.CASCADE, verbose_name=_('Nutzer*in')) + read = models.BooleanField(default=False) + + def __str__(self): + return f"[{self.user}] {self.title} ({self.created_at})" + + def get_absolute_url(self): + self.user.get_notifications_url() + + +class CommentNotification(BaseNotification): + comment = models.ForeignKey(Comment, on_delete=models.CASCADE, verbose_name=_('Antwort')) diff --git a/src/fellchensammlung/templates/fellchensammlung/header.html b/src/fellchensammlung/templates/fellchensammlung/header.html index 3f18488..4181e8a 100644 --- a/src/fellchensammlung/templates/fellchensammlung/header.html +++ b/src/fellchensammlung/templates/fellchensammlung/header.html @@ -18,6 +18,7 @@
{% include "fellchensammlung/forms/change_language.html" %} {% if user.is_authenticated %} +
{% csrf_token %}