-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[#1715] Added management command to delete expired contact invitations
- Loading branch information
Showing
5 changed files
with
99 additions
and
4 deletions.
There are no files selected for viewing
38 changes: 38 additions & 0 deletions
38
src/open_inwoner/accounts/management/commands/delete_invitations.py
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,38 @@ | ||
from collections import defaultdict | ||
from datetime import timedelta | ||
|
||
from django.conf import settings | ||
from django.core.management.base import BaseCommand | ||
from django.utils import timezone | ||
|
||
from open_inwoner.utils.logentry import system_action | ||
|
||
from ...models import Invite | ||
|
||
|
||
class Command(BaseCommand): | ||
help = ( | ||
"Delete contact invitations which are older than a predefined period " | ||
"(the default is defined in `settings.INVITE_EXPIRY_DAYS`)" | ||
) | ||
|
||
def handle(self, *args, **kwargs): | ||
now = timezone.now() | ||
expired_invitations = Invite.objects.select_related("inviter").filter( | ||
created_on__lt=now - timedelta(days=settings.INVITE_EXPIRY_DAYS) | ||
) | ||
|
||
grouped = defaultdict(list) | ||
|
||
for invite in expired_invitations: | ||
grouped[invite.inviter].append(invite) | ||
|
||
for inviter, invites in grouped.items(): | ||
invites_info = [ | ||
f"{invite.invitee_email} (invited on {invite.created_on.strftime('%Y-%m-%d')})" | ||
for invite in invites | ||
] | ||
message = f"{len(invites)} expired contact invitations from {inviter.get_full_name()} deleted" | ||
system_action(message, user=inviter, deleted_invitations=invites_info) | ||
|
||
expired_invitations.delete() |
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,52 @@ | ||
from django.core.management import call_command | ||
from django.test import TestCase | ||
|
||
from freezegun import freeze_time | ||
from timeline_logger.models import TimelineLog | ||
|
||
from ..models import Invite | ||
from .factories import InviteFactory, UserFactory | ||
|
||
|
||
class DeleteContactInvitationsTest(TestCase): | ||
@freeze_time("2023-09-26", as_arg=True) | ||
def test_delete_expired_invitations(frozen_time, self): | ||
user = UserFactory( | ||
first_name="Johann Maria Salvadore", | ||
infix="van de", | ||
last_name="Eenzaameiland", | ||
) | ||
|
||
invite1 = InviteFactory.create(inviter=user) | ||
invite2 = InviteFactory.create(inviter=user) | ||
invite3 = InviteFactory.create() | ||
|
||
frozen_time.move_to("2023-10-01") | ||
|
||
invite4 = InviteFactory.create() | ||
|
||
frozen_time.move_to("2023-10-27") | ||
|
||
call_command("delete_invitations") | ||
|
||
# check remaining invites | ||
invitations = Invite.objects.all() | ||
self.assertEqual(len(invitations), 1) | ||
self.assertEqual(invitations[0].invitee_email, invite4.invitee_email) | ||
|
||
# check logs | ||
logs = TimelineLog.objects.all() | ||
self.assertEqual(len(logs), 2) | ||
|
||
self.assertEqual( | ||
logs[0].extra_data["deleted_invitations"][0], | ||
f"{invite1.invitee_email} (invited on {invite1.created_on.strftime('%Y-%m-%d')})", | ||
) | ||
self.assertEqual( | ||
logs[0].extra_data["deleted_invitations"][1], | ||
f"{invite2.invitee_email} (invited on {invite2.created_on.strftime('%Y-%m-%d')})", | ||
) | ||
self.assertEqual( | ||
logs[1].extra_data["deleted_invitations"][0], | ||
f"{invite3.invitee_email} (invited on {invite3.created_on.strftime('%Y-%m-%d')})", | ||
) |
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