From: Dan Scott Date: Thu, 1 Sep 2011 18:03:54 +0000 (-0400) Subject: Avoid generating barcodes that are already in the database X-Git-Url: https://old-git.evergreen-ils.org/?a=commitdiff_plain;h=ca49c2a77826b121e16c1317a8045b9d9e590b4c;p=contrib%2FConifer.git Avoid generating barcodes that are already in the database And complain when we get a colleague ID that is not the expected size. Signed-off-by: Dan Scott --- diff --git a/tools/patron-load/ldap_sync b/tools/patron-load/ldap_sync index 7546ef6567..3a68390482 100644 --- a/tools/patron-load/ldap_sync +++ b/tools/patron-load/ldap_sync @@ -180,17 +180,24 @@ CREATE SEQUENCE evergreen.lu_barcode START 200000; CREATE OR REPLACE FUNCTION evergreen.lu_update_barcode(usr_id INT) RETURNS TEXT AS $$ DECLARE - barcode TEXT; + bc_gen TEXT; + bc_holder TEXT; BEGIN - barcode := evergreen.lu_generate_barcode(); - INSERT INTO actor.card (usr, barcode) VALUES (usr_id, barcode); + LOOP + bc_gen := evergreen.lu_generate_barcode(); + + SELECT barcode INTO bc_holder FROM actor.card WHERE barcode = bc_gen; + EXIT WHEN bc_holder IS NULL; + END LOOP; + + INSERT INTO actor.card (usr, barcode) VALUES (usr_id, bc_gen); UPDATE actor.usr SET card = CURRVAL('actor.card_id_seq') WHERE id = usr_id; - RETURN barcode; + RETURN bc_gen; END; $$ LANGUAGE PLPGSQL; @@ -315,7 +322,7 @@ def insert_new_users(): WHERE actor.usr.ident_value = scratchpad.usr_staging.ident_value AND status IS NULL; - SELECT au.id, lu_generate_barcode() + SELECT au.id, lu_update_barcode(au.id) FROM actor.usr au INNER JOIN scratchpad.usr_staging su ON su.ident_value = au.ident_value WHERE au.home_ou IN ( SELECT id FROM actor.org_unit WHERE parent_ou = 105 @@ -359,6 +366,10 @@ def insert_into_staging(result_data): given_name = result_data['givenName'][0].strip() datatel = result_data['lulColleagueId'][0].strip() + if len(datatel) != 7: + print >> sys.stderr, 'Datatel number not 7 chars for %s (%s)' % (usrname, datatel) + return + if 'preferredLanguage' in result_data: lang = result_data['preferredLanguage'][0].strip() else: @@ -383,6 +394,24 @@ def dump_data(result_data): for key in result_data[0][1]: print(key, result_data[0][1][key]) +def list_new_accounts(): + """ + Generate CSV output of the new accounts that were created + + Returns the Colleague ID and barcode in the requested format of + double-quoted values using comma delimiters. + """ + + return """ +COPY ( + SELECT au.ident_value, ac.barcode + FROM actor.usr au + INNER JOIN actor.card ac ON au.card = ac.id + INNER JOIN scratchpad.usr_staging su ON su.ident_value = au.ident_value + WHERE su.status = 'new' + ) TO STDOUT WITH csv QUOTE '"' FORCE QUOTE * HEADER +;""" + def generate_ldap_sql(create_date): """ Generate the SQL required to create and update Evergreen accounts @@ -413,6 +442,7 @@ def generate_ldap_sql(create_date): print(update_existing_users()) print(insert_new_users()) + print(list_new_accounts()) if __name__ == '__main__': import doctest