[damned-lies] Add a method to delete obsolete accounts



commit ea670b8448f0baec02b01698febf35c5c36c5b45
Author: Claude Paroz <claude 2xlibre net>
Date:   Sat Aug 6 14:33:30 2011 +0200

    Add a method to delete obsolete accounts

 people/models.py                             |   17 +++++++++++
 people/tests/__init__.py                     |   39 ++++++++++++++++++++++++--
 stats/management/commands/run-maintenance.py |    1 +
 3 files changed, 54 insertions(+), 3 deletions(-)
---
diff --git a/people/models.py b/people/models.py
index 10abc26..a518d87 100644
--- a/people/models.py
+++ b/people/models.py
@@ -56,6 +56,23 @@ class Person(User):
             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():
             return None
diff --git a/people/tests/__init__.py b/people/tests/__init__.py
index bde4b00..b11b560 100644
--- a/people/tests/__init__.py
+++ b/people/tests/__init__.py
@@ -1,6 +1,6 @@
 # -*- 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]))
diff --git a/stats/management/commands/run-maintenance.py b/stats/management/commands/run-maintenance.py
index fc5cd70..994a47b 100644
--- a/stats/management/commands/run-maintenance.py
+++ b/stats/management/commands/run-maintenance.py
@@ -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()



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]