-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
57 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,13 +8,16 @@ | |
from django.test import TestCase, override_settings | ||
from django.utils import timezone | ||
|
||
import time_machine | ||
|
||
from timeline_logger.models import TimelineLog | ||
|
||
from .factories import ArticleFactory, TimelineLogFactory, UserFactory | ||
|
||
|
||
class ReportMailingTestCase(TestCase): | ||
def setUp(self): | ||
super().setUp() | ||
self.article = ArticleFactory.create() | ||
|
||
self.user = UserFactory.create(email="[email protected]") | ||
|
@@ -155,33 +158,40 @@ def test_timeline_digest_from_email_setting(self): | |
self.assertEqual(mail.outbox[0].from_email, settings.TIMELINE_DIGEST_FROM_EMAIL) | ||
|
||
|
||
@time_machine.travel(datetime(2024, 3, 5, 0, 0, 0, tzinfo=dt_timezone.utc)) | ||
class PruneTimelineLogsTestCase(TestCase): | ||
def setUp(self): | ||
super().setUp() | ||
|
||
self.log_1 = TimelineLogFactory.create() | ||
self.log_1.timestamp = datetime(2024, 3, 1, 12, 0, 0, tzinfo=dt_timezone.utc) | ||
self.log_1.timestamp = datetime(2024, 3, 1, 0, 0, 0, tzinfo=dt_timezone.utc) | ||
self.log_1.save() | ||
|
||
self.log_2 = TimelineLogFactory.create() | ||
self.log_2.timestamp = datetime(2024, 3, 1, 14, 0, 0, tzinfo=dt_timezone.utc) | ||
self.log_2.timestamp = datetime(2024, 3, 4, 0, 0, 0, tzinfo=dt_timezone.utc) | ||
self.log_2.save() | ||
|
||
def test_prune_timeline_logs_no_date(self): | ||
stdout = StringIO() | ||
|
||
call_command( | ||
"prune_timeline_logs", interactive=False, verbosity=0, stdout=stdout | ||
"prune_timeline_logs", | ||
"--all", | ||
interactive=False, | ||
verbosity=0, | ||
stdout=stdout, | ||
) | ||
|
||
self.assertEqual(TimelineLog.objects.count(), 0) | ||
stdout.seek(0) | ||
self.assertEqual(stdout.read().strip(), "Successfully deleted 2 timeline logs.") | ||
self.assertEqual( | ||
stdout.getvalue().strip(), "Successfully deleted 2 timeline logs." | ||
) | ||
|
||
def test_prune_timeline_logs_date(self): | ||
call_command( | ||
"prune_timeline_logs", | ||
"--before", | ||
"2024-03-01T13:00:00+00:00", | ||
"--keep-days", | ||
"2", | ||
interactive=False, | ||
verbosity=0, | ||
stdout=StringIO(), | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
from datetime import timedelta | ||
|
||
from django.utils import timezone | ||
|
||
from .models import TimelineLog | ||
|
||
|
||
def prune_timeline_logs(*, keep_days: int | None = None) -> int: | ||
"""Delete the timeline logs instances. | ||
:param keep_days: If specified, only delete records older than the specified number of days. | ||
:returns: The number of deleted instances. | ||
""" | ||
limit = timezone.now() | ||
if keep_days is not None: | ||
limit -= timedelta(days=keep_days) | ||
|
||
number, _ = TimelineLog.objects.filter(timestamp__lte=limit).delete() | ||
return number |