Skip to content

Commit

Permalink
feat: purge_personal_api_key_events() task (#7485)
Browse files Browse the repository at this point in the history
* feat: purge_personal_api_key_events() task

* feat: log number of events purged

* test: test new task

* fix: name task properly

* chore: create daily PeriodicTask

* chore: remove old management command

* chore: remove tests of old command

* test: finish removing now-empty tests.py
  • Loading branch information
jennifer-richards authored May 30, 2024
1 parent 607a5c8 commit 020bdeb
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 191 deletions.
3 changes: 0 additions & 3 deletions bin/daily
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,3 @@ $DTDIR/ietf/manage.py populate_yang_model_dirs -v0

# Re-run yang checks on active documents
$DTDIR/ietf/manage.py run_yang_model_checks -v0

# Purge older PersonApiKeyEvents
$DTDIR/ietf/manage.py purge_old_personal_api_key_events 14

This file was deleted.

122 changes: 0 additions & 122 deletions ietf/person/management/commands/tests.py

This file was deleted.

20 changes: 20 additions & 0 deletions ietf/person/tasks.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Copyright The IETF Trust 2024, All Rights Reserved
#
# Celery task definitions
#
import datetime

from celery import shared_task
from django.utils import timezone

from ietf.utils import log
from .models import PersonApiKeyEvent


@shared_task
def purge_personal_api_key_events_task(keep_days):
keep_since = timezone.now() - datetime.timedelta(days=keep_days)
old_events = PersonApiKeyEvent.objects.filter(time__lt=keep_since)
count = len(old_events)
old_events.delete()
log.log(f"Deleted {count} PersonApiKeyEvents older than {keep_since}")
19 changes: 17 additions & 2 deletions ietf/person/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import datetime
import json
import mock

from io import StringIO, BytesIO
from PIL import Image
Expand All @@ -25,8 +26,9 @@
from ietf.nomcom.models import NomCom
from ietf.nomcom.test_data import nomcom_test_data
from ietf.nomcom.factories import NomComFactory, NomineeFactory, NominationFactory, FeedbackFactory, PositionFactory
from ietf.person.factories import EmailFactory, PersonFactory
from ietf.person.models import Person, Alias
from ietf.person.factories import EmailFactory, PersonFactory, PersonApiKeyEventFactory
from ietf.person.models import Person, Alias, PersonApiKeyEvent
from ietf.person.tasks import purge_personal_api_key_events_task
from ietf.person.utils import (merge_persons, determine_merge_order, send_merge_notification,
handle_users, get_extra_primary, dedupe_aliases, move_related_objects, merge_nominees,
handle_reviewer_settings, get_dots)
Expand Down Expand Up @@ -450,3 +452,16 @@ def test_dots(self):
self.assertEqual(get_dots(ncmember),['nomcom'])
ncchair = RoleFactory(group__acronym='nomcom2020',group__type_id='nomcom',name_id='chair').person
self.assertEqual(get_dots(ncchair),['nomcom'])


class TaskTests(TestCase):
@mock.patch("ietf.person.tasks.log.log")
def test_purge_personal_api_key_events_task(self, mock_log):
now = timezone.now()
old_event = PersonApiKeyEventFactory(time=now - datetime.timedelta(days=1, minutes=1))
young_event = PersonApiKeyEventFactory(time=now - datetime.timedelta(days=1, minutes=-1))
purge_personal_api_key_events_task(keep_days=1)
self.assertFalse(PersonApiKeyEvent.objects.filter(pk=old_event.pk).exists())
self.assertTrue(PersonApiKeyEvent.objects.filter(pk=young_event.pk).exists())
self.assertTrue(mock_log.called)
self.assertIn("Deleted 1", mock_log.call_args[0][0])
11 changes: 11 additions & 0 deletions ietf/utils/management/commands/periodic_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,17 @@ def create_default_tasks(self):
),
)

PeriodicTask.objects.get_or_create(
name="Purge old personal API key events",
task="ietf.person.tasks.purge_personal_api_key_events_task",
kwargs=json.dumps(dict(keep_days=14)),
defaults=dict(
enabled=False,
crontab=self.crontabs["daily"],
description="Purge PersonApiKeyEvent instances older than 14 days",
),
)

def show_tasks(self):
for label, crontab in self.crontabs.items():
tasks = PeriodicTask.objects.filter(crontab=crontab).order_by(
Expand Down

0 comments on commit 020bdeb

Please sign in to comment.