forked from TampereHacklab/mulysa
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add mark_for_deletion action and filter to django admin for users * add setting for how long to wait untill deleting, process the signal and send email, add management command * bit of testing * black
- Loading branch information
Showing
7 changed files
with
263 additions
and
4 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
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,31 @@ | ||
import logging | ||
from django.core.management.base import BaseCommand | ||
from django.utils import timezone | ||
|
||
from users.models import CustomUser | ||
|
||
from drfx import config | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class Command(BaseCommand): | ||
help = "Delete all users that have been marked for deletion for more than the cutoff setting" | ||
|
||
def handle(self, *args, **options): | ||
# some safety margin | ||
dt = timezone.now() - timezone.timedelta(days=config.USER_DELETION_DAYS) | ||
|
||
logger.info( | ||
f" Search for users that have been marked for deletion for over {config.USER_DELETION_DAYS} days" | ||
) | ||
|
||
users = CustomUser.objects.filter( | ||
marked_for_deletion_on__isnull=False, marked_for_deletion_on__lt=dt | ||
) | ||
|
||
for user in users: | ||
logger.info( | ||
f" Deleting User {user} as it has been marked for deletion over {config.USER_DELETION_DAYS} days" | ||
) | ||
user.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
10 changes: 10 additions & 0 deletions
10
users/templates/mail/account_deactivated_and_marked_for_deletion.txt
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,10 @@ | ||
{% extends 'mail/email_base.txt' %} | ||
{% load i18n %} | ||
{% block content %} | ||
|
||
{% blocktrans %}Your account has been deactivated and marked for deletion{% endblocktrans %} | ||
|
||
{{user.first_name}} {{user.last_name}} | ||
|
||
{# TODO: tell what this means and how to recover if you want to #} | ||
{% endblock %} |
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,130 @@ | ||
from django.contrib.auth import get_user_model | ||
from django.core import mail | ||
from django.test import TestCase | ||
from django.utils import timezone | ||
from drfx import config | ||
|
||
from .. import models | ||
|
||
|
||
class TestUserDeletion(TestCase): | ||
def setUp(self): | ||
self.user = get_user_model().objects.create_customuser( | ||
first_name="FirstName", | ||
last_name="LastName", | ||
email="[email protected]", | ||
birthday=timezone.now(), | ||
municipality="City", | ||
nick="user1", | ||
phone="+358123123", | ||
) | ||
|
||
self.memberservice = models.MemberService.objects.create( | ||
name="TestService", cost=10, days_per_payment=30, days_before_warning=2 | ||
) | ||
|
||
mail.outbox = [] | ||
|
||
def test_mark_user_for_deletion(self): | ||
""" | ||
Test marking user for deletion | ||
""" | ||
self.assertEqual(self.user.is_active, True) | ||
self.assertIsNone(self.user.marked_for_deletion_on) | ||
|
||
self.user.marked_for_deletion_on = timezone.now() | ||
self.user.save() | ||
|
||
# user was marked is inactive | ||
self.assertEqual(self.user.is_active, False) | ||
|
||
# email was sent to user and admins | ||
self.assertEqual(len(mail.outbox), 1) | ||
self.assertIn(self.user.email, mail.outbox[0].to) | ||
self.assertIn(config.MEMBERSHIP_APPLICATION_NOTIFY_ADDRESS, mail.outbox[0].to) | ||
self.assertIn("Your account", mail.outbox[0].subject) | ||
self.assertIn("deletion", mail.outbox[0].body) | ||
self.assertIn(self.user.first_name, mail.outbox[0].body) | ||
|
||
# clear outbox | ||
mail.outbox = [] | ||
|
||
# mark again | ||
self.user.marked_for_deletion_on = timezone.now() | ||
self.user.save() | ||
|
||
# no emails sent | ||
self.assertEqual(len(mail.outbox), 0) | ||
|
||
# clear again | ||
mail.outbox = [] | ||
|
||
# remove mark | ||
self.user.marked_for_deletion_on = None | ||
self.user.save() | ||
|
||
# user still marked as inactive | ||
self.assertEqual(self.user.is_active, False) | ||
|
||
# no email notifications | ||
self.assertEqual(len(mail.outbox), 0) | ||
|
||
def test_user_deletion(self): | ||
""" | ||
Check that all necessary (and just the necessary) objects are cleared when user is deleted | ||
""" | ||
# add few objects pointing to this user, log entries, payment transactions, service subcsriptions | ||
self.subscription = models.ServiceSubscription.objects.create( | ||
user=self.user, | ||
service=self.memberservice, | ||
state=models.ServiceSubscription.ACTIVE, | ||
paid_until=timezone.now().date(), | ||
) | ||
|
||
# log entry for the user | ||
models.UsersLog.objects.create(user=self.user, message="test") | ||
|
||
# nfc card | ||
models.NFCCard.objects.create(user=self.user, cardid="123") | ||
|
||
# custom invoice | ||
models.CustomInvoice.objects.create( | ||
user=self.user, | ||
subscription=self.subscription, | ||
days=10, | ||
amount=10, | ||
) | ||
|
||
# bank transaction that points to the user | ||
self.transaction = models.BankTransaction.objects.create( | ||
user=self.user, date=timezone.now().date(), amount=10 | ||
) | ||
|
||
# delete the user | ||
self.user.delete() | ||
|
||
# user was deleted | ||
self.assertEqual(models.CustomUser.objects.count(), 0) | ||
|
||
# the subscription was deleted | ||
self.assertEqual(models.ServiceSubscription.objects.count(), 0) | ||
|
||
# message log was deleted | ||
self.assertEqual(models.UsersLog.objects.count(), 0) | ||
|
||
# nfc card is removed | ||
self.assertEqual(models.NFCCard.objects.count(), 0) | ||
|
||
# custom invoice is removed | ||
self.assertEqual(models.CustomInvoice.objects.count(), 0) | ||
|
||
# bank transaction was not removed | ||
self.assertEqual(models.BankTransaction.objects.count(), 1) | ||
# but the user assignment is cleared | ||
self.transaction.refresh_from_db() | ||
self.assertEqual(self.transaction.user, None) | ||
|
||
def tearDown(self): | ||
mail.outbox = [] | ||
get_user_model().objects.all().delete() | ||
models.MemberService.objects.all().delete() |