Skip to content

Commit

Permalink
Add task to delete old applications
Browse files Browse the repository at this point in the history
refs. TILA-2888
  • Loading branch information
ranta committed Nov 18, 2024
1 parent 4a29acf commit b408298
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 1 deletion.
1 change: 1 addition & 0 deletions config/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -532,6 +532,7 @@ def SEARCH_SETTINGS(cls):
PRUNE_RESERVATIONS_OLDER_THAN_MINUTES = 20
REMOVE_RESERVATION_STATS_OLDER_THAN_YEARS = 5
REMOVE_RECURRING_RESERVATIONS_OLDER_THAN_DAYS = 1
REMOVE_EXPIRED_APPLICATIONS_OLDER_THAN_DAYS = 365
TEXT_SEARCH_CACHE_TIME_DAYS = 30

APPLICATION_ROUND_RESERVATION_CREATION_TIMEOUT_MINUTES = values.IntegerValue(default=10)
Expand Down
34 changes: 34 additions & 0 deletions tests/test_tasks/test_delete_expired_applications.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import datetime

import pytest
from django.test import override_settings

from tests.factories import ApplicationFactory, ApplicationRoundFactory
from tilavarauspalvelu.models import Application
from tilavarauspalvelu.tasks import delete_expired_applications
from utils.date_utils import local_date

pytestmark = [
pytest.mark.django_db,
]


@override_settings(REMOVE_EXPIRED_APPLICATIONS_OLDER_THAN_DAYS=365)
def test_delete_expired_applications():
application_round_old = ApplicationRoundFactory.create_in_status_results_sent(
application_period_end=local_date() - datetime.timedelta(days=365)
)
application_sent = ApplicationFactory.create_in_status_results_sent(application_round=application_round_old) # Kept
ApplicationFactory.create_in_status_expired(application_round=application_round_old) # Deleted
ApplicationFactory.create_in_status_cancelled(application_round=application_round_old) # Deleted

application_round_new = ApplicationRoundFactory.create_in_status_results_sent(
application_period_end=local_date() - datetime.timedelta(days=364) # Not old enough to delete applications
)
application_new = ApplicationFactory.create_in_status_expired(application_round=application_round_new) # Kept

delete_expired_applications()

applications = Application.objects.all().order_by("application_round")
assert len(applications) == 2
assert list(applications) == [application_sent, application_new]
16 changes: 15 additions & 1 deletion tilavarauspalvelu/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,13 @@
from django.utils.translation import gettext_lazy as _
from easy_thumbnails.exceptions import InvalidImageFormatError
from elasticsearch_django.index import create_index, delete_index, update_index
from lookup_property import L

from config.celery import app
from tilavarauspalvelu.enums import (
ApplicantTypeChoice,
ApplicationRoundStatusChoice,
ApplicationStatusChoice,
CustomerTypeChoice,
HaukiResourceState,
OrderStatus,
Expand All @@ -32,6 +35,7 @@
from tilavarauspalvelu.models import (
AffectingTimeSpan,
AllocatedTimeSlot,
Application,
PaymentOrder,
PaymentProduct,
PersonalInfoViewLog,
Expand Down Expand Up @@ -66,7 +70,7 @@
from tilavarauspalvelu.utils.verkkokauppa.product.exceptions import CreateOrUpdateAccountingError
from tilavarauspalvelu.utils.verkkokauppa.product.types import CreateOrUpdateAccountingParams, CreateProductParams
from tilavarauspalvelu.utils.verkkokauppa.verkkokauppa_api_client import VerkkokauppaAPIClient
from utils.date_utils import local_datetime, local_end_of_day, local_start_of_day
from utils.date_utils import local_date, local_datetime, local_end_of_day, local_start_of_day
from utils.image_cache import purge
from utils.sentry import SentryLogger

Expand Down Expand Up @@ -602,6 +606,16 @@ def generate_reservation_series_from_allocations(application_round_id: int) -> N
create_or_update_reservation_statistics.delay(reservation_pks=list(reservation_pks))


@app.task(name="delete_expired_applications")
def delete_expired_applications() -> None:
cutoff_date = local_date() - datetime.timedelta(days=settings.REMOVE_EXPIRED_APPLICATIONS_OLDER_THAN_DAYS)
Application.objects.filter(
L(status__in=[ApplicationStatusChoice.EXPIRED, ApplicationStatusChoice.CANCELLED])
& L(application_round__status=ApplicationRoundStatusChoice.RESULTS_SENT)
& Q(application_round__application_period_end__lte=cutoff_date)
).delete()


@app.task(name="save_sql_queries_from_request")
def save_sql_queries_from_request(queries: list[QueryInfo], path: str, body: bytes, duration_ms: int) -> None:
decoded_body: str | None = None
Expand Down

0 comments on commit b408298

Please sign in to comment.