From: Dan Scott Date: Wed, 14 Sep 2011 19:00:18 +0000 (-0400) Subject: More defensive error handling X-Git-Url: https://old-git.evergreen-ils.org/?a=commitdiff_plain;h=fdbe2e8a20b9c0adcc6cd85c131f6ad8f3d6c62a;p=contrib%2FConifer.git More defensive error handling When users aren't opted-in, we can't search for them. Fiddlesticks! This was breaking the script rather annoyingly; now we print out an error and move on to the next user. Signed-off-by: Dan Scott --- diff --git a/tools/patron-load/ldap_osrf_sync b/tools/patron-load/ldap_osrf_sync index 883fa675cf..8722e33f6b 100755 --- a/tools/patron-load/ldap_osrf_sync +++ b/tools/patron-load/ldap_osrf_sync @@ -87,24 +87,34 @@ class User: # Strip leading/ending whitespace; LDAP data can be dirty # Using email for username deliberately here - self.usrname = self.ldap_atts['mail'][0].strip().lower() - self.email = self.ldap_atts['mail'][0].strip().lower() - self.family_name = self.ldap_atts['sn'][0].strip() + self.usrname = self._simple_map('mail').lower() + self.email = self._simple_map('mail').lower() + self.family_name = self._simple_map('sn') self.ident_type, self.ident_value = self.get_identity() self.home_ou = self.get_home_ou() + self.first_given_name = self._simple_map('givenName') + if not self.first_given_name: + self.first_given_name('LDAP_NULL') + print >> sys.stderr, 'No givenName for %s' % (self.usrname) + self.lang_pref = self._simple_map('preferredLanguage') # Randomized password, assuming user will use "Forgot password" # function to set their own password self.passwd = oils.utils.utils.md5sum(os.urandom(10)) - if 'givenName' in self.ldap_atts: - self.first_given_name = self.ldap_atts['givenName'][0].strip() - else: - self.first_given_name('LDAP_NULL') - print >> sys.stderr, 'No givenName for %s' % (self.usrname) - self.profile = self.get_profile() self.expire_date = self.get_expiry_date() + self.barcode = self._simple_map('lulLibraryBarcode') + + def _simple_map(self, ldap_attribute): + """ + Convenience method for mapping a given LDAP attribute + """ + + if ldap_attribute in self.ldap_atts: + return self.ldap_atts[ldap_attribute][0].strip() + + return None def get_expiry_date(self): """ @@ -141,25 +151,28 @@ class User: """ if 'lulStudentLevel' in self.ldap_atts: - affiliation = self.ldap_atts['lulStudentLevel'][0].strip().lower() + affiliation = self._simple_map('lulStudentLevel').lower() elif 'lulPrimaryAffiliation' in self.ldap_atts: - affiliation = self.ldap_atts['lulPrimaryAffiliation'][0].strip().lower() + affiliation = self._simple_map('lulPrimaryAffiliation').lower() else: affiliation = r'\N' - if affiliation == 'ug' or affiliation == 'student': - return 13 - elif affiliation == 'gr': - return 12 - elif affiliation == 'al': - return 14 - elif affiliation == 'faculty': - return 11 - elif affiliation == 'staff': - return 15 - elif affiliation == 'proxy': - return None - return None + profile_map = { + 'ug': 13, + 'student': 13, + 'gr': 12, + 'al': 14, + 'faculty': 11, + 'staff': 15, + 'proxy': None, + 'affiliate': 15 + } + + if affiliation in profile_map: + return profile_map[affiliation] + else: + print >> sys.stderr, "Affiliation '%s' not mapped to a profile " \ + "for user %s" % (affiliation, self.usrname) def get_home_ou(self): """ @@ -291,8 +304,8 @@ def find_ldap_users(con, ldap_filter, attributes, auth): if ARGS.dump_ldap: dump_data(result_data) if ARGS.create_users: - barcode = create_evergreen_user(auth, user) - update_ldap_barcode(con, user, barcode) + res = create_evergreen_user(auth, user) + update_ldap_barcode(con, user) if ARGS.push_barcode: try: uid = find_evergreen_user(auth, user) @@ -303,8 +316,8 @@ def find_ldap_users(con, ldap_filter, attributes, auth): print >> sys.stderr, "User not found in Evergreen: %s" % \ (user.ident_value) continue - barcode = get_barcode(auth, uid) - update_ldap_barcode(con, user, barcode) + user.barcode = get_barcode(auth, uid) + update_ldap_barcode(con, user) except ldap.LDAPError, exc: print >> sys.stderr, exc @@ -335,7 +348,7 @@ def find_evergreen_user(auth, user): limit = 1 sort = None - search_ou = 1 + search_depth = 1 include_inactive = True print("Trying to find user: %s %s %s" % @@ -345,7 +358,7 @@ def find_evergreen_user(auth, user): by_id = osrf_request( 'open-ils.actor', 'open-ils.actor.patron.search.advanced', auth, {'ident_value': {'value': user.ident_value, 'group': 0}}, limit, - sort, include_inactive, search_ou + sort, include_inactive, search_depth ).send() if by_id and len(by_id): @@ -354,7 +367,7 @@ def find_evergreen_user(auth, user): by_email = osrf_request( 'open-ils.actor', 'open-ils.actor.patron.search.advanced', auth, {'email': {'value': user.email}}, limit, - sort, include_inactive, search_ou + sort, include_inactive, search_depth ).send() if by_email and len(by_email): @@ -363,7 +376,7 @@ def find_evergreen_user(auth, user): by_usrname = osrf_request( 'open-ils.actor', 'open-ils.actor.patron.search.advanced', auth, {'usrname': {'value': user.usrname}}, limit, - sort, include_inactive, search_ou + sort, include_inactive, search_depth ).send() if by_usrname and len(by_usrname): @@ -412,11 +425,14 @@ def create_evergreen_user(auth, user): 'open-ils.actor', 'open-ils.actor.patron.update', auth, newau ).send() except Exception, exc: - print exc + print >> sys.stderr, exc + return None evt = oils.event.Event.parse_event(usr) if evt and not evt.success: - raise AuthException(evt.text_code) + print >> sys.stderr, "Create Evergreen user failed for %s: %s" % ( + AuthException(evt.text_code), user.usrname) + return None # Generate a barcode for the user try: @@ -425,21 +441,22 @@ def create_evergreen_user(auth, user): auth, usr.id() ).send() except Exception, exc: - print exc + print >> sys.stderr, exc + return None evt = oils.event.Event.parse_event(barcode) if evt and not evt.success: - raise AuthException(evt.text_code) + print >> sys.stderr, "Create barcode failed for %s: %s" % ( + AuthException(evt.text_code), user.usrname) + return None - barcode = barcode['evergreen.lu_update_barcode'] - - create_stat_cats(newau, result_data) + user.barcode = barcode['evergreen.lu_update_barcode'] - print("Created: %s with barcode %s" % (newau.usrname(), barcode)) + create_stat_cats(newau, user) - return barcode + print("Created: %s with barcode %s" % (newau.usrname(), user.barcode)) -def update_ldap_barcode(con, user, barcode): +def update_ldap_barcode(con, user): """ Updates the LDAP directory with the new barcode for a given user @@ -448,22 +465,21 @@ def update_ldap_barcode(con, user, barcode): """ try: - mod_attrs = [(ldap.MOD_REPLACE, 'lulLibraryBarcode', [barcode])] + mod_attrs = [(ldap.MOD_REPLACE, 'lulLibraryBarcode', [user.barcode])] con.modify_s(user.cname, mod_attrs) except Exception, exc: print >> sys.stderr, exc + return False return True -def create_stat_cats(user, result_data): +def create_stat_cats(eg_user, ldap_user): """ Map statistical categories """ - # Reasonable default - lang = 'E' - if 'preferredLanguage' in result_data: - lang = result_data['preferredLanguage'][0].strip() + if ldap_user.lang_pref: + pass # XXX Now we should generate stat cats eh? @@ -532,7 +548,7 @@ def parse_args(): parser.add_argument('--query-id', help='Search LDAP for a specific user by id attribute' ) - parser.add_argument('-date', '--query-date', + parser.add_argument('--query-date', help='Search LDAP for users created since (YYYYMMDDHHMMSSZ)' ) parser.add_argument('-U', '--eg-user', nargs='?',