From 6557e9f9eb299bb4749dde4282140d9158436166 Mon Sep 17 00:00:00 2001 From: moanos Date: Thu, 20 Mar 2025 23:26:16 +0100 Subject: [PATCH] feat: Auto-add location to rescue org --- src/fellchensammlung/api/views.py | 6 +++++- src/fellchensammlung/receivers.py | 9 ++++++++- src/fellchensammlung/tasks.py | 9 ++++++++- 3 files changed, 21 insertions(+), 3 deletions(-) diff --git a/src/fellchensammlung/api/views.py b/src/fellchensammlung/api/views.py index bec2a77..a816bc8 100644 --- a/src/fellchensammlung/api/views.py +++ b/src/fellchensammlung/api/views.py @@ -2,7 +2,7 @@ from rest_framework.views import APIView from rest_framework.response import Response from django.db import transaction from fellchensammlung.models import AdoptionNotice, Animal, Log, TrustLevel -from fellchensammlung.tasks import post_adoption_notice_save +from fellchensammlung.tasks import post_adoption_notice_save, post_rescue_org_save from rest_framework import status from rest_framework.permissions import IsAuthenticated from .serializers import ( @@ -161,10 +161,14 @@ class RescueOrganizationApiView(APIView): serializer = RescueOrgSerializer(data=request.data, context={"request": request}) if serializer.is_valid(): rescue_org = serializer.save() + # Add the location + post_rescue_org_save.delay_on_commit(rescue_org.pk) return Response( {"message": "Rescue organization created/updated successfully!", "id": rescue_org.id}, status=status.HTTP_201_CREATED, ) + + return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) diff --git a/src/fellchensammlung/receivers.py b/src/fellchensammlung/receivers.py index 1512d84..ba61a73 100644 --- a/src/fellchensammlung/receivers.py +++ b/src/fellchensammlung/receivers.py @@ -1,6 +1,6 @@ from django.db.models.signals import post_save from django.dispatch import receiver -from fellchensammlung.models import BaseNotification, CommentNotification, User, TrustLevel +from fellchensammlung.models import BaseNotification, CommentNotification, User, TrustLevel, RescueOrganization from .tasks import task_send_notification_email from notfellchen.settings import host from django.utils.translation import gettext_lazy as _ @@ -18,6 +18,13 @@ def base_notification_receiver(sender, instance: BaseNotification, created: bool else: task_send_notification_email.delay(instance.pk) +@receiver(post_save, sender=RescueOrganization) +def rescue_org_receiver(sender, instance: RescueOrganization, created: bool, **kwargs): + if instance.location: + return + else: + task_send_notification_email.delay(instance.pk) + @receiver(post_save, sender=User) def notification_new_user(sender, instance: User, created: bool, **kwargs): diff --git a/src/fellchensammlung/tasks.py b/src/fellchensammlung/tasks.py index a238560..016abc0 100644 --- a/src/fellchensammlung/tasks.py +++ b/src/fellchensammlung/tasks.py @@ -6,7 +6,7 @@ from notfellchen.celery import app as celery_app from .mail import send_notification_email from .tools.admin import clean_locations, deactivate_unchecked_adoption_notices, deactivate_404_adoption_notices from .tools.misc import healthcheck_ok -from .models import Location, AdoptionNotice, Timestamp +from .models import Location, AdoptionNotice, Timestamp, RescueOrganization from .tools.notifications import notify_of_AN_to_be_checked from .tools.search import notify_search_subscribers @@ -57,3 +57,10 @@ def task_healthcheck(): @shared_task def task_send_notification_email(notification_pk): send_notification_email(notification_pk) + +@celery_app.task(name="commit.post_rescue_org_save") +def post_rescue_org_save(pk): + instance = RescueOrganization.objects.get(pk=pk) + Location.add_location_to_object(instance) + set_timestamp("add_rescue_org_location") + logging.info(f"Location was added to Rescue Organization {pk}") \ No newline at end of file