Commit 79441961 authored by David Haynes's avatar David Haynes
Browse files

Cleanup plus names now added

- it was not correctly grabbing names before
- not sure why I thought it was working..
parent 3d6b5e92
from __future__ import absolute_import, print_function
# Django Imports
# core django imports
from django.contrib.auth.models import User
from django.conf import settings
# Other Imports
from django.contrib import messages
# third party imports
import requests
def pfparse(pf_name_result):
# name comes in format of Anderson, Nicholas J
print(pf_name_result)
name_list = pf_name_result.split(',')
# there's random whitespace with the first name
first_name_section = name_list[1].strip()
......@@ -22,10 +22,10 @@ def pfparse(pf_name_result):
new_name_list = [first_name, name_list[0]]
return new_name_list
def pfinfo(uname):
base_url = settings.PF_URL
url = base_url + "basic/all/" + str(uname)
print("Using url %s", url)
try:
metadata = requests.get(url, timeout=5)
print("Retrieving information from the peoplefinder api.")
......@@ -38,50 +38,22 @@ def pfinfo(uname):
pfjson = metadata.json()
try:
if len(pfjson['results']) == 1:
return pfparse(pfjson['results'][0]['name'])
name_str = pfjson['results'][0]['name']
name = pfparse(name_str)
return name
else:
return pfparse(pfjson['results'][1]['name'])
name_str = pfjson['results'][1]['name']
name = pfparse(name_str)
return name
# if the name is not in peoplefinder, return empty first and last name
except IndexError:
print("Name not found in peoplefinder.")
return [u'', u'']
return [u'',u'']
except Exception as e:
print("Unknown peoplefinder error:", e)
print("Returning empty user info tuple.")
return [u'', u'']
def create_user_dud(tree):
print("Parsing CAS information.")
try:
username = tree[0][0].text
user, user_created = User.objects.get_or_create(username=username)
except Exception as e:
print("CAS callback unsuccessful:", e)
info_name = pfinfo(username)
try:
if user_created:
print("Created user object %s!" % username)
user.email = "%s@%s" % (username, settings.ORGANIZATION_EMAIL_DOMAIN)
# Password is a required User object field, though doesn't matter for our
# purposes because all user auth is handled through CAS, not Django's login.
user.set_password("cas_used_instead")
# a list of empty strings is False
if not info_name:
user.first_name = info_name[0]
user.last_name = info_name[1]
print("Added user's name, %s %s." % (info_name[0], info_name[1]))
else:
print("Unable to add user %s's name." % username)
user.save()
else:
print("User %s already exists" % username)
except Exception as e:
print("Unhandled user creation error:", e)
def create_user(tree):
......@@ -100,7 +72,7 @@ def create_user(tree):
print("Created user object %s." % username)
# set and save the user's email
email_str = "%s@%s" % (username, settings.ORGANIZATION_EMAIL_DOMAIN)
email_str = "%s%s" % (username, settings.EMAIL_DOMAIN)
user.email = email_str
# Password is a required User object field, though doesn't matter for our
# purposes because all user auth is handled through CAS, not Django's login.
......@@ -108,13 +80,10 @@ def create_user(tree):
user.save()
print("Added user's email, %s." % email_str)
if len(info_name[0]) > 0:
user.first_name = info_name[0]
user.last_name = info_name[1]
user.save()
print("Added user's name, %s %s." % (info_name[0], info_name[1]))
else:
print("Unable to add user's name.")
user.first_name = info_name[0]
user.last_name = info_name[1]
user.save()
print("Added user's name, %s %s." % (info_name[0], info_name[1]))
print("User object creation process completed.")
......@@ -124,4 +93,3 @@ def create_user(tree):
print("CAS callback successful.")
except Exception as e:
print("Unhandled user creation error:", e)
# mail the administrators
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment