mirror of
https://github.com/eitchtee/WYGIWYH.git
synced 2026-04-24 09:38:35 +02:00
feat(transactions:tasks): add old deleted transactions cleanup task
This commit is contained in:
@@ -1,9 +1,13 @@
|
|||||||
import logging
|
import logging
|
||||||
|
from datetime import timedelta
|
||||||
|
|
||||||
|
from cachalot.api import cachalot_disabled, invalidate
|
||||||
|
from django.utils import timezone
|
||||||
|
from django.conf import settings
|
||||||
|
|
||||||
from procrastinate.contrib.django import app
|
from procrastinate.contrib.django import app
|
||||||
|
|
||||||
from apps.transactions.models import RecurringTransaction
|
from apps.transactions.models import RecurringTransaction, Transaction
|
||||||
|
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
@@ -19,3 +23,34 @@ def generate_recurring_transactions(timestamp=None):
|
|||||||
exc_info=True,
|
exc_info=True,
|
||||||
)
|
)
|
||||||
raise e
|
raise e
|
||||||
|
|
||||||
|
|
||||||
|
@app.periodic(cron="10 1 * * *")
|
||||||
|
@app.task
|
||||||
|
def cleanup_deleted_transactions():
|
||||||
|
with cachalot_disabled():
|
||||||
|
if (
|
||||||
|
settings.ENABLE_SOFT_DELETION
|
||||||
|
and settings.KEEP_DELETED_TRANSACTIONS_FOR == 0
|
||||||
|
):
|
||||||
|
return "KEEP_DELETED_TRANSACTIONS_FOR is 0, no cleanup performed."
|
||||||
|
|
||||||
|
if not settings.ENABLE_SOFT_DELETION:
|
||||||
|
# Hard delete all soft-deleted transactions
|
||||||
|
deleted_count, _ = Transaction.deleted_objects.all().hard_delete()
|
||||||
|
return (
|
||||||
|
f"Hard deleted {deleted_count} transactions (soft deletion disabled)."
|
||||||
|
)
|
||||||
|
|
||||||
|
# Calculate the cutoff date
|
||||||
|
cutoff_date = timezone.now() - timedelta(
|
||||||
|
days=settings.KEEP_DELETED_TRANSACTIONS_FOR
|
||||||
|
)
|
||||||
|
|
||||||
|
invalidate("transactions.Transaction")
|
||||||
|
|
||||||
|
# Hard delete soft-deleted transactions older than the cutoff date
|
||||||
|
old_transactions = Transaction.deleted_objects.filter(deleted_at__lt=cutoff_date)
|
||||||
|
deleted_count, _ = old_transactions.hard_delete()
|
||||||
|
|
||||||
|
return f"Hard deleted {deleted_count} objects older than {settings.KEEP_DELETED_TRANSACTIONS_FOR} days."
|
||||||
|
|||||||
Reference in New Issue
Block a user