Fix expiredaccounts
authorMichael Spang <mspang@csclub.uwaterloo.ca>
Tue, 19 Jan 2010 06:10:41 +0000 (01:10 -0500)
committerMichael Spang <mspang@csclub.uwaterloo.ca>
Tue, 19 Jan 2010 06:10:41 +0000 (01:10 -0500)
Also make it only send to accounts expired within the last 3 terms (a year).

ceo/console/expiredaccounts.py

index 2609ccb..9237d44 100644 (file)
@@ -1,5 +1,11 @@
 import sys, ldap
-from ceo import members, uwldap
+from ceo import members, uwldap, terms, ldapi
+
+def max_term(term1, term2):
+    if terms.compare(term1, term2) > 0:
+        return term1
+    else:
+        return term2
 
 class ExpiredAccounts:
   help = '''
@@ -19,14 +25,26 @@ owners will be emailed. The email will go to the email listed in uwdir.
     uwl = ldap.initialize(uwldap.uri())
     mlist = members.expired_accounts()
     for member in mlist.values():
-      uid = member['uid'][0]
-      name = member['cn'][0]
-      email = None
-      if send_email:
-        members.send_account_expired_email(name, uid)
+      term = "f0000"
+      term = reduce(max_term, member.get("term", []), term)
+      term = reduce(max_term, member.get("nonMemberTerm", []), term)
+      expiredfor = terms.delta(term, terms.current())
+
+      if expiredfor <= 3:
+        uid = member['uid'][0]
+        name = member['cn'][0]
+        email = None
+        print '%s (expired for %d terms)' % (uid.ljust(12), expiredfor)
+        if send_email:
+          print "  sending mail to %s" % uid
+          members.send_account_expired_email(name, uid)
         user = uwl.search_s(uwldap.base(), ldap.SCOPE_SUBTREE,
           '(uid=%s)' % ldapi.escape(uid))
         if len(user) > 0  and 'mailLocalAddress' in user[0][1]:
           email = user[0][1]['mailLocalAddress'][0]
-          members.send_account_expired_email(name, email)
-      print '%s %s' % (uid.ljust(12), name.ljust(30))
+          if send_email:
+            print "  sending mail to %s" % email
+            members.send_account_expired_email(name, email)
+          else:
+            print "  would also mail to %s" % email
+