Commit ea670b84 authored by Claude Paroz's avatar Claude Paroz

Add a method to delete obsolete accounts

parent 4c8daebb
......@@ -55,6 +55,23 @@ class Person(User):
for account in accounts:
account.delete()
@classmethod
def clean_obsolete_accounts(cls):
""" Remove accounts that:
- last login is more than 2 years
- is not coordinator
- is not module maintainer
- has no reserved module
"""
accounts = cls.objects.annotate(num_modules=models.Count('maintains_modules')
).annotate(num_states=models.Count('state')
).filter(last_login__lt=(datetime.datetime.now()-datetime.timedelta(days=730))
).exclude(role__role='coordinator'
).exclude(num_modules__gt=0
).exclude(num_states__gt=0)
for account in accounts:
account.delete()
@classmethod
def get_by_user(cls, user):
if user.is_anonymous():
......
# -*- coding: utf-8 -*-
#
# Copyright (c) 2010 Claude Paroz <claude@2xlibre.net>
# Copyright (c) 2010-2011 Claude Paroz <claude@2xlibre.net>
#
# This file is part of Damned Lies.
#
......@@ -18,6 +18,8 @@
# along with Damned Lies; if not, write to the Free Software Foundation, Inc.,
# 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
import datetime
from django.test import TestCase
from django.test.client import Client
from django.core.urlresolvers import reverse
......@@ -28,9 +30,11 @@ from people import forms
class PeopleTestCase(TestCase):
def _create_person(self):
def _create_person(self, seq='', **kwargs):
pn = Person(first_name='John', last_name='Nothing',
email='jn@devnull.com', username= 'jn')
email='jn%s@devnull.com' % seq, username= 'jn%s' % seq)
for key, arg in kwargs.items():
setattr(pn, key, arg)
pn.set_password('password')
pn.save()
return pn
......@@ -63,3 +67,32 @@ class PeopleTestCase(TestCase):
form = forms.DetailForm(post_data, instance=self.pn)
self.assertFalse(form.is_valid())
self.assertTrue('image' in form.errors)
def test_obsolete_accounts(self):
from teams.models import Team, Role
from stats.models import Module, Branch, Domain
from languages.models import Language
from vertimus.models import StateTranslating
module = Module.objects.create(name="gnome-hello")
Branch.checkout_on_creation = False
branch = Branch(name='gnome-2-24', module=module)
branch.save(update_statistics=False)
domain = Domain.objects.create(module=module, name='po', directory='po')
team = Team.objects.create(name='fr', description='French', mailing_list='french_ml@example.org')
lang = Language.objects.create(name='French', locale='fr', team=team)
# Create Users
p1 = self._create_person(seq='1', last_login=datetime.datetime.now()-datetime.timedelta(days=700))
p2 = self._create_person(seq='2', last_login=datetime.datetime.now()-datetime.timedelta(days=800))
role = Role.objects.create(team=team, person=p2, role='coordinator')
p3 = self._create_person(seq='3', last_login=datetime.datetime.now()-datetime.timedelta(days=800))
module.maintainers.add(p3)
p4 = self._create_person(seq='4', last_login=datetime.datetime.now()-datetime.timedelta(days=800))
state = StateTranslating.objects.create(branch=branch, domain=domain, language=lang, person=p4)
p5 = self._create_person(seq='5', last_login=datetime.datetime.now()-datetime.timedelta(days=800))
# Test only p5 should be deleted
self.assertEqual(Person.objects.all().count(), 5)
Person.clean_obsolete_accounts()
import pdb; pdb.set_trace()
self.assertEqual(Person.objects.all().count(), 4)
self.assertEqual(set(Person.objects.all()), set([p1, p2, p3, p4]))
......@@ -10,6 +10,7 @@ class Command(BaseCommand):
def handle(self, *args, **options):
Person.clean_unactivated_accounts()
Person.clean_obsolete_accounts()
Role.inactivate_unused_roles()
ActionArchived.clean_old_actions(365)
clean_tar_files()
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment