Add lulStudentLevel filter for students
authorDan Scott <dan@coffeecode.net>
Fri, 2 Sep 2011 01:34:54 +0000 (21:34 -0400)
committerDan Scott <dscott@laurentian.ca>
Wed, 8 May 2013 13:46:37 +0000 (09:46 -0400)
Prefer lulStudentLevel to distinguish between undergrad and grad
students.

Left-pad, with zeroes, Colleague IDs that are shorter than 7 digits.

Don't update names from LDAP as they appear to lack Unicode support.

Sundry small pylint fixes.

Signed-off-by: Dan Scott <dscott@laurentian.ca>
tools/patron-load/ldap_sync

index 3a68390..0b7e896 100644 (file)
@@ -76,7 +76,7 @@ def barcode_to_datatel(barcode):
     """
 
     if len(barcode) != 14:
-        return false
+        return False
 
     return barcode[6:13]
 
@@ -254,6 +254,8 @@ def update_existing_users():
         WHEN LOWER(email) LIKE '%huntingtonu.ca' THEN 104
       END,
       profile = CASE
+        WHEN LOWER(affiliation) = 'gr' THEN 12
+        WHEN LOWER(affiliation) = 'ug' THEN 13
         WHEN LOWER(affiliation) = 'faculty' THEN 11
         WHEN LOWER(affiliation) = 'student' THEN 13
         WHEN LOWER(affiliation) = 'staff' THEN 15
@@ -261,19 +263,17 @@ def update_existing_users():
       END
     ;
 
-    UPDATE actor.usr SET
-      home_ou = su.home_ou,
-      usrname = LOWER(su.usrname),
-      profile = su.profile,
-      email = LOWER(su.email),
-      first_given_name = su.first_given_name,
-      family_name = su.family_name
-        FROM scratchpad.usr_staging su
-        WHERE su.ident_value= actor.usr.ident_value
-          AND actor.usr.home_ou IN (
-            SELECT id FROM actor.org_unit WHERE parent_ou = 105
-          )
-          AND su.profile IS NOT NULL
+    UPDATE actor.usr 
+      SET home_ou = su.home_ou,
+        usrname = LOWER(su.usrname),
+        profile = su.profile,
+        email = LOWER(su.email)
+      FROM scratchpad.usr_staging su
+      WHERE su.ident_value= actor.usr.ident_value
+        AND su.profile IS NOT NULL
+        AND actor.usr.home_ou IN (
+          SELECT id FROM actor.org_unit WHERE parent_ou = 105
+        )
      ;
 
     UPDATE scratchpad.usr_staging
@@ -302,8 +302,8 @@ def insert_new_users():
       SET profile = 13
       WHERE profile IS NULL;
 
-    INSERT INTO actor.usr (usrname, passwd, email, first_given_name, family_name, home_ou, ident_type, ident_value, profile)
-      SELECT LOWER(usrname), MD5(RANDOM()::TEXT), LOWER(email), first_given_name, family_name, home_ou, 2, ident_value, profile
+    INSERT INTO actor.usr (usrname, passwd, email, first_given_name, family_name, home_ou, ident_type, ident_value, profile, expire_date)
+      SELECT LOWER(usrname), MD5(RANDOM()::TEXT), LOWER(email), first_given_name, family_name, home_ou, 2, ident_value, profile, '2012-09-30'::date
         FROM scratchpad.usr_staging WHERE ident_value IN (
           SELECT ident_value
             FROM scratchpad.usr_staging
@@ -331,14 +331,17 @@ def insert_new_users():
 """
 
 def search_for_students(con, attributes, create_date):
+    """
+    Retrieve personnel accounts from LDAP directory and process'em
+    """
     base_dn = 'o=lul'
     search_scope = ldap.SCOPE_SUBTREE
-    filter = '(&(objectclass=lulEduPerson))'
-    filter = '(&(objectclass=lulEduPerson)(lulPrimaryAffiliation=*)(createTimestamp>=%s000000Z))' % create_date
+    ldap_filter = '(&(objectclass=lulEduPerson))'
+    ldap_filter = '(&(objectclass=lulEduPerson)(lulPrimaryAffiliation=*)(createTimestamp>=%s000000Z))' % create_date
+    ldap_filter = '(&(lulStudentLevel=*)(createTimestamp>=%s000000Z))' % create_date
 
     try:
-        result_id = con.search(base_dn, search_scope, filter, attributes)
-        result_set = []
+        result_id = con.search(base_dn, search_scope, ldap_filter, attributes)
         while 1:
             result_type, result_data = con.result(result_id, 0)
             if result_data == []:
@@ -368,14 +371,19 @@ def insert_into_staging(result_data):
 
     if len(datatel) != 7:
         print >> sys.stderr, 'Datatel number not 7 chars for %s (%s)' % (usrname, datatel)
-        return
+        if len(datatel) == 6:
+            datatel = '0%s' % datatel
+        elif len(datatel) == 5:
+            datatel = '00%s' % datatel
 
     if 'preferredLanguage' in result_data:
         lang = result_data['preferredLanguage'][0].strip()
     else:
         lang = r'\N'
 
-    if 'lulPrimaryAffiliation' in result_data:
+    if 'lulStudentLevel' in result_data:
+        affiliation = result_data['lulStudentLevel'][0].strip()
+    elif 'lulPrimaryAffiliation' in result_data:
         affiliation = result_data['lulPrimaryAffiliation'][0].strip()
     else:
         affiliation = r'\N'
@@ -427,7 +435,7 @@ def generate_ldap_sql(create_date):
     print(create_staging_table())
 
     try:
-        attributes = ['lulPrimaryAffiliation', 'cn', 'mail', 'givenName', 'sn', 'lulColleagueId', 'preferredLanguage']
+        attributes = ['lulStudentLevel', 'lulPrimaryAffiliation', 'cn', 'mail', 'givenName', 'sn', 'lulColleagueId', 'preferredLanguage']
         con.simple_bind_s(luauth.dn, luauth.pw)
         search_for_students(con, attributes, create_date)
     except ldap.LDAPError, e: