Commit cfd39b69 authored by Mark Stenglein's avatar Mark Stenglein
Browse files

Fixed the creation of users and students so that it doesn't crash on no...

Fixed the creation of users and students so that it doesn't crash on no student existing for user. Also cleaned up comments and logging.
parent 5704d33c
# standard library imports
from __future__ import absolute_import, print_function
# core django imports # core django imports
from django.conf import settings from django.conf import settings
from django.contrib.auth.models import User from django.contrib.auth.models import User
from django.conf import settings
from django.core.exceptions import ObjectDoesNotExist from django.core.exceptions import ObjectDoesNotExist
# third-part imports # third-part imports
import requests import requests
# imports from your apps # imports from your apps
from django.conf import settings
from .models import Student from .models import Student
...@@ -15,14 +13,13 @@ def pfinfo(u_name): ...@@ -15,14 +13,13 @@ def pfinfo(u_name):
pf_url = settings.PF_URL pf_url = settings.PF_URL
url = str(pf_url) + "basic/all/" + str(u_name) url = str(pf_url) + "basic/all/" + str(u_name)
try: try:
metadata = requests.get(url, timeout=5) metadata = requests.get(url)
print("Retrieving information from the peoplefinder api.")
metadata.raise_for_status() metadata.raise_for_status()
except requests.exceptions.RequestException as e: except requests.exceptions.RequestException as e:
print("Cannot resolve to peoplefinder api:" , e) print e
else: else:
pf_json = metadata.json() pf_json = metadata.json()
name = pf_json['results'][0]['name'] name = pf_json['results'][0]['name']
return name.split(',') return name.split(',')
# if the name is not in peoplefinder, return empty first and last name # if the name is not in peoplefinder, return empty first and last name
...@@ -31,47 +28,52 @@ def pfinfo(u_name): ...@@ -31,47 +28,52 @@ def pfinfo(u_name):
def create_user(tree): def create_user(tree):
print("Parsing CAS information.")
username = tree[0][0].text username = tree[0][0].text
print username
user, user_created = User.objects.get_or_create(username=username) user, user_created = User.objects.get_or_create(username=username)
if user_created: if user_created:
print("Created user object %s." % username) = "%s@%s" % (username, settings.ORGANIZATION_EMAIL_DOMAIN) = "%s@%s" % (username, settings.ORGANIZATION_EMAIL_DOMAIN)
user.set_password('cas_used_instead') user.set_password('cas_used_instead')
print("Added user email and default password.")
print "Start peoplefinder parsing"
try: try:
name_list = pfinfo(str(username)) name_list = pfinfo(str(username))
print name_list, "name_list"
first_name = name_list[1].lstrip().split(' ') first_name = name_list[1].lstrip().split(' ')
if len(first_name) > 1: if len(first_name) > 1:
no_mi = first_name[:-1] # no middle initial no_mi = first_name[:-1]
user.first_name = ' '.join(no_mi) user.first_name = ' '.join(no_mi)
else: else:
user.first_name = ' '.join(first_name) user.first_name = ' '.join(first_name)
last_name = name_list[0] last_name = name_list[0]
user.last_name = name_list[0] user.last_name = name_list[0]
print("Added user's name, %s %s." % first_name, last_name)
except: except Exception as e:
print("Problem setting user's name via peoplefinder.") print("Unhandled peoplefinder exception:", e)
print("User object creation completed.") print("Created user %s!" % username)
else: else:
print("User object already exists.") print("User object already exists.")
# Student Creation Section
try: try:
Student.objects.get(user=user) Student.objects.get(user=user)
print("Student object already exists.") print("Student object already exists")
except ObjectDoesNotExist: except ObjectDoesNotExist:
new_student = Student.objects.create(user=user) new_student = Student.objects.create(user=user)
# save the name off of peoplefinder for later quality assurance purposes
new_student.pf_first_name = user.first_name
new_student.pf_last_name = user.last_name
print("Student object creation completed.") print("Created student object for user %s!" % username)
print("CAS callback successful.") print("CAS callback completed.")
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