feat: Auto-add location to rescue org

This commit is contained in:
moanos [he/him] 2025-03-20 23:26:16 +01:00
parent 602cef1302
commit 6557e9f9eb
3 changed files with 21 additions and 3 deletions

View File

@ -2,7 +2,7 @@ from rest_framework.views import APIView
from rest_framework.response import Response from rest_framework.response import Response
from django.db import transaction from django.db import transaction
from fellchensammlung.models import AdoptionNotice, Animal, Log, TrustLevel 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 import status
from rest_framework.permissions import IsAuthenticated from rest_framework.permissions import IsAuthenticated
from .serializers import ( from .serializers import (
@ -161,10 +161,14 @@ class RescueOrganizationApiView(APIView):
serializer = RescueOrgSerializer(data=request.data, context={"request": request}) serializer = RescueOrgSerializer(data=request.data, context={"request": request})
if serializer.is_valid(): if serializer.is_valid():
rescue_org = serializer.save() rescue_org = serializer.save()
# Add the location
post_rescue_org_save.delay_on_commit(rescue_org.pk)
return Response( return Response(
{"message": "Rescue organization created/updated successfully!", "id": rescue_org.id}, {"message": "Rescue organization created/updated successfully!", "id": rescue_org.id},
status=status.HTTP_201_CREATED, status=status.HTTP_201_CREATED,
) )
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

View File

@ -1,6 +1,6 @@
from django.db.models.signals import post_save from django.db.models.signals import post_save
from django.dispatch import receiver 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 .tasks import task_send_notification_email
from notfellchen.settings import host from notfellchen.settings import host
from django.utils.translation import gettext_lazy as _ from django.utils.translation import gettext_lazy as _
@ -18,6 +18,13 @@ def base_notification_receiver(sender, instance: BaseNotification, created: bool
else: else:
task_send_notification_email.delay(instance.pk) 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) @receiver(post_save, sender=User)
def notification_new_user(sender, instance: User, created: bool, **kwargs): def notification_new_user(sender, instance: User, created: bool, **kwargs):

View File

@ -6,7 +6,7 @@ from notfellchen.celery import app as celery_app
from .mail import send_notification_email from .mail import send_notification_email
from .tools.admin import clean_locations, deactivate_unchecked_adoption_notices, deactivate_404_adoption_notices from .tools.admin import clean_locations, deactivate_unchecked_adoption_notices, deactivate_404_adoption_notices
from .tools.misc import healthcheck_ok 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.notifications import notify_of_AN_to_be_checked
from .tools.search import notify_search_subscribers from .tools.search import notify_search_subscribers
@ -57,3 +57,10 @@ def task_healthcheck():
@shared_task @shared_task
def task_send_notification_email(notification_pk): def task_send_notification_email(notification_pk):
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}")