From 27b7e47f184f51b17ac1c6903f462f450864e7c1 Mon Sep 17 00:00:00 2001 From: moanos Date: Thu, 26 Dec 2024 20:24:10 +0100 Subject: [PATCH] feat: Add SearchSubscriptions --- .../migrations/0028_searchsubscription.py | 25 +++++++++++++++++++ src/fellchensammlung/models.py | 21 +++++++++++++++- .../templates/fellchensammlung/search.html | 7 +++++- src/fellchensammlung/views.py | 2 +- 4 files changed, 52 insertions(+), 3 deletions(-) create mode 100644 src/fellchensammlung/migrations/0028_searchsubscription.py diff --git a/src/fellchensammlung/migrations/0028_searchsubscription.py b/src/fellchensammlung/migrations/0028_searchsubscription.py new file mode 100644 index 0000000..f92ae3e --- /dev/null +++ b/src/fellchensammlung/migrations/0028_searchsubscription.py @@ -0,0 +1,25 @@ +# Generated by Django 5.1.1 on 2024-12-26 15:56 + +import django.db.models.deletion +from django.conf import settings +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('fellchensammlung', '0027_alter_animal_species_andoptionnoticenotification'), + ] + + operations = [ + migrations.CreateModel( + name='SearchSubscription', + fields=[ + ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('sex', models.CharField(choices=[('F', 'Weiblich'), ('M', 'Männlich'), ('M_N', 'Männlich, kastriert'), ('F_N', 'Weiblich, kastriert'), ('I', 'Intergeschlechtlich')], max_length=20)), + ('radius', models.IntegerField(choices=[(20, '20 km'), (50, '50 km'), (100, '100 km'), (200, '200 km'), (500, '500 km')])), + ('location', models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, to='fellchensammlung.location')), + ('owner', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)), + ], + ), + ] diff --git a/src/fellchensammlung/models.py b/src/fellchensammlung/models.py index d44ca3a..8ecef26 100644 --- a/src/fellchensammlung/models.py +++ b/src/fellchensammlung/models.py @@ -546,6 +546,23 @@ class DistanceChoices(models.IntegerChoices): TWO_HUNDRED = 200, '200 km' FIVE_HUNDRED = 500, '500 km' +class SearchSubscription(models.Model): + """ + SearchSubscriptions allow a user to get a notification when a new AdoptionNotice is added that matches their Search + criteria. Search criteria are location, SexChoicesWithAll and distance + + Process: + - User performs a normal search + - User clicks Button "Subscribe to this Search" + - SearchSubscription is added to database + - On new AdoptionNotice: Check all existing SearchSubscriptions for matches + - For matches: Send notification to user of the SearchSubscription + """ + owner = models.ForeignKey(User, on_delete=models.CASCADE) + location = models.ForeignKey(Location, on_delete=models.PROTECT) + sex = models.CharField(max_length=20, choices=SexChoices.choices) + radius = models.IntegerField(choices=DistanceChoices.choices) + class Rule(models.Model): """ @@ -640,7 +657,6 @@ class ModerationAction(models.Model): return f"[{self.action}]: {self.public_comment}" - class TextTypeChoices(models.TextChoices): DEDICATED = "dedicated", _("Fest zugeordnet") MALE = "M", _("Männlich") @@ -648,6 +664,7 @@ class TextTypeChoices(models.TextChoices): FEMALE_NEUTERED = "F_N", _("Weiblich, kastriert") INTER = "I", _("Intergeschlechtlich") + class Text(models.Model): """ Base class to store markdown content @@ -769,6 +786,7 @@ class CommentNotification(BaseNotification): def url(self): return self.comment.get_absolute_url + class AndoptionNoticeNotification(BaseNotification): adoption_notice = models.ForeignKey(AdoptionNotice, on_delete=models.CASCADE, verbose_name=_('Vermittlung')) @@ -776,6 +794,7 @@ class AndoptionNoticeNotification(BaseNotification): def url(self): return self.adoption_notice.get_absolute_url + class Subscriptions(models.Model): owner = models.ForeignKey(User, on_delete=models.CASCADE, verbose_name=_('Nutzer*in')) adoption_notice = models.ForeignKey(AdoptionNotice, on_delete=models.CASCADE, verbose_name=_('AdoptionNotice')) diff --git a/src/fellchensammlung/templates/fellchensammlung/search.html b/src/fellchensammlung/templates/fellchensammlung/search.html index 7f3966d..af953a5 100644 --- a/src/fellchensammlung/templates/fellchensammlung/search.html +++ b/src/fellchensammlung/templates/fellchensammlung/search.html @@ -9,7 +9,12 @@ {{ search_form.as_p }} - + + {% if place_not_found %}

{% translate "Ort nicht gefunden" %}

diff --git a/src/fellchensammlung/views.py b/src/fellchensammlung/views.py index b210d83..e20e114 100644 --- a/src/fellchensammlung/views.py +++ b/src/fellchensammlung/views.py @@ -16,7 +16,7 @@ from notfellchen import settings from fellchensammlung import logger from .models import AdoptionNotice, Text, Animal, Rule, Image, Report, ModerationAction, \ User, Location, AdoptionNoticeStatus, Subscriptions, CommentNotification, BaseNotification, RescueOrganization, \ - Species, Log, Timestamp, TrustLevel, SexChoicesWithAll + Species, Log, Timestamp, TrustLevel, SexChoicesWithAll, SearchSubscription from .forms import AdoptionNoticeForm, AdoptionNoticeFormWithDateWidget, ImageForm, ReportAdoptionNoticeForm, \ CommentForm, ReportCommentForm, AnimalForm, \ AdoptionNoticeSearchForm, AnimalFormWithDateWidget, AdoptionNoticeFormWithDateWidgetAutoAnimal