Commit ef23d13b authored by Daniel W Bond's avatar Daniel W Bond

added additional print statement logging and try/except's for the user creation callback

parent 318b82bc
# core django imports # standard library imports
from django.conf import settings from __future__ import absolute_import, print_function
from django.contrib.auth.models import User # core django imports
# third-part imports from django.conf import settings
import requests from django.contrib.auth.models import User
# imports from your apps from django.conf import settings
from django.conf import settings from django.core.exceptions import ObjectDoesNotExist
from .models import Student # third-part imports
import requests
# imports from your apps
def pfinfo(u_name): from .models import Student
pf_url = settings.PF_URL
url = str(pf_url) + "basic/all/" + str(u_name)
try: def pfinfo(u_name):
metadata = requests.get(url) pf_url = settings.PF_URL
metadata.raise_for_status() url = str(pf_url) + "basic/all/" + str(u_name)
except requests.exceptions.RequestException as e: try:
print e metadata = requests.get(url, timeout=5)
else: print("Retrieving information from the peoplefinder api.")
pf_json = metadata.json() metadata.raise_for_status()
try: except requests.exceptions.RequestException as e:
name = pf_json['results'][0]['name'] print("Cannot resolve to peoplefinder api:" , e)
return name.split(',') else:
# if the name is not in peoplefinder, return empty first and last name try:
except IndexError: pf_json = metadata.json()
return ['', ''] name = pf_json['results'][0]['name']
return name.split(',')
# if the name is not in peoplefinder, return empty first and last name
def create_user(tree): except IndexError:
username = tree[0][0].text return ['', '']
print username
user, user_created = User.objects.get_or_create(username=username)
def create_user(tree):
if user_created:
user.email = "%s@%s" % (username, settings.ORGANIZATION_EMAIL_DOMAIN) print("Parsing CAS information.")
print "hello" try:
name_list = pfinfo(str(username)) username = tree[0][0].text
print name_list, "name_list" user, user_created = User.objects.get_or_create(username=username)
first_name = name_list[1].lstrip().split(' ')
if len(first_name) > 1: if user_created:
no_mi = first_name[:-1] print("Created user object %s." % username)
user.first_name = ' '.join(no_mi) user.email = "%s@%s" % (username, settings.ORGANIZATION_EMAIL_DOMAIN)
else: user.set_password('cas_used_instead')
user.first_name = ' '.join(first_name)
last_name = name_list[0] try:
user.last_name = name_list[0] name_list = pfinfo(str(username))
first_name = name_list[1].lstrip().split(' ')
print "world" if len(first_name) > 1:
user.save() no_mi = first_name[:-1] # no middle initial
new_student = Student.objects.create(user=user) user.first_name = ' '.join(no_mi)
new_student.save() else:
user.first_name = ' '.join(first_name)
print("Created user %s!" % username) last_name = name_list[0]
user.last_name = name_list[0]
print("Added user's name, %s %s." % first_name, last_name)
except:
print("Problem setting user's name via peoplefinder.")
user.save()
print("User object creation completed.")
else:
print("User object already exists.")
try:
Student.objects.get(user=user)
print("Student object already exists.")
except ObjectDoesNotExist:
new_student = Student.objects.create(user=user)
new_student.save()
print("Student object creation completed.")
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