diff --git a/src/fellchensammlung/api/views.py b/src/fellchensammlung/api/views.py index 0798d2d..02e101a 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 add_adoption_notice_location +from fellchensammlung.tasks import post_adoption_notice_save from rest_framework import status from rest_framework.permissions import IsAuthenticated from .serializers import ( @@ -47,7 +47,7 @@ class AdoptionNoticeApiView(APIView): adoption_notice = serializer.save(owner=request.user) # Add the location - add_adoption_notice_location.delay_on_commit(adoption_notice.pk) + post_adoption_notice_save.delay_on_commit(adoption_notice.pk) # Only set active when user has trust level moderator or higher if request.user.trust_level >= TrustLevel.MODERATOR: diff --git a/src/fellchensammlung/tasks.py b/src/fellchensammlung/tasks.py index 9a495ea..d40e8ef 100644 --- a/src/fellchensammlung/tasks.py +++ b/src/fellchensammlung/tasks.py @@ -37,17 +37,14 @@ def task_deactivate_unchecked(): set_timestamp("task_deactivate_404_adoption_notices") -@celery_app.task(name="commit.add_location") -def add_adoption_notice_location(pk): +@celery_app.task(name="commit.post_an_save") +def post_adoption_notice_save(pk): instance = AdoptionNotice.objects.get(pk=pk) Location.add_location_to_object(instance) set_timestamp("add_adoption_notice_location") + logging.info(f"Location was added to Adoption notice {pk}") -@celery_app.task(name="commit.notify_subscribers") -def notify_subscribers(pk): - instance = AdoptionNotice.objects.get(pk=pk) - notify_search_subscribers(instance) - logging.info(f"Subscribers for AN {pk} have been notified") + notify_search_subscribers(instance, only_if_active=True) @celery_app.task(name="tools.healthcheck") def task_healthcheck(): diff --git a/src/fellchensammlung/views.py b/src/fellchensammlung/views.py index 9b393ad..430b838 100644 --- a/src/fellchensammlung/views.py +++ b/src/fellchensammlung/views.py @@ -25,7 +25,7 @@ from .tools.geo import GeoAPI from .tools.metrics import gather_metrics_data from .tools.admin import clean_locations, get_unchecked_adoption_notices, deactivate_unchecked_adoption_notices, \ deactivate_404_adoption_notices -from .tasks import add_adoption_notice_location, notify_subscribers +from .tasks import post_adoption_notice_save from rest_framework.authtoken.models import Token from .tools.search import Search @@ -212,15 +212,9 @@ def add_adoption_notice(request): if form.is_valid(): an_instance = form.save(commit=False) an_instance.owner = request.user - an_instance.save() - """Spin up a task that adds the location""" - add_adoption_notice_location.delay_on_commit(an_instance.pk) - - # Set correct status if request.user.trust_level >= TrustLevel.MODERATOR: an_instance.set_active() - notify_subscribers.delay_on_commit(an_instance.pk) else: an_instance.set_unchecked() @@ -238,6 +232,9 @@ def add_adoption_notice(request): Log.objects.create(user=request.user, action="add_adoption_notice", text=f"{request.user} hat Vermittlung {an_instance.pk} hinzugefĆ¼gt") + """Spin up a task that adds the location""" + post_adoption_notice_save.delay(an_instance.id) + """Subscriptions""" # Automatically subscribe user that created AN to AN Subscriptions.objects.create(owner=request.user, adoption_notice=an_instance)