1from __future__ import annotations
2
3from plain.chores import Chore, register_chore
4from plain.runtime import settings
5from plain.utils import timezone
6
7from .models import NotFoundLog, RedirectLog
8
9
10@register_chore
11class DeleteLogs(Chore):
12 """Delete logs older than REDIRECTION_LOG_RETENTION_TIMEDELTA."""
13
14 def run(self) -> str:
15 cutoff = timezone.now() - settings.REDIRECTION_LOG_RETENTION_TIMEDELTA
16
17 redirects = RedirectLog.query.filter(created_at__lt=cutoff).delete()
18 output = f"{redirects} redirect logs deleted"
19
20 not_founds = NotFoundLog.query.filter(created_at__lt=cutoff).delete()
21 output += f", {not_founds} not found logs deleted"
22
23 return output