Commit b92a75e6 authored by David Haynes's avatar David Haynes 🙆

Merge branch 'loginErrorMaster' into 'master'

Login error master

:dhaynes: pls

See merge request !10
parents ef23d13b cfd39b69
# 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()
try: try:
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):
username = tree[0][0].text
print username
user, user_created = User.objects.get_or_create(username=username)
print("Parsing CAS information.") if user_created:
try: user.email = "%s@%s" % (username, settings.ORGANIZATION_EMAIL_DOMAIN)
username = tree[0][0].text user.set_password('cas_used_instead')
user, user_created = User.objects.get_or_create(username=username) print("Added user email and default password.")
if user_created:
print("Created user object %s." % username)
user.email = "%s@%s" % (username, settings.ORGANIZATION_EMAIL_DOMAIN)
user.set_password('cas_used_instead')
try:
name_list = pfinfo(str(username))
first_name = name_list[1].lstrip().split(' ')
if len(first_name) > 1:
no_mi = first_name[:-1] # no middle initial
user.first_name = ' '.join(no_mi)
else:
user.first_name = ' '.join(first_name)
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.")
print "Start peoplefinder parsing"
try: try:
Student.objects.get(user=user) name_list = pfinfo(str(username))
print("Student object already exists.") print name_list, "name_list"
except ObjectDoesNotExist: first_name = name_list[1].lstrip().split(' ')
new_student = Student.objects.create(user=user) if len(first_name) > 1:
new_student.save() no_mi = first_name[:-1]
print("Student object creation completed.") user.first_name = ' '.join(no_mi)
else:
print("CAS callback successful.") user.first_name = ' '.join(first_name)
last_name = name_list[0]
except Exception as e: user.last_name = name_list[0]
print("Unhandled user creation error:", e)
# mail the administrators except Exception as e:
print("Unhandled peoplefinder exception:", e)
user.save()
print("Created user %s!" % username)
else:
print("User object already exists.")
# Student Creation Section
try:
Student.objects.get(user=user)
print("Student object already exists")
except ObjectDoesNotExist:
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
new_student.save()
print("Created student object for user %s!" % username)
print("CAS callback completed.")
...@@ -7,7 +7,7 @@ argparse==1.2.1 ...@@ -7,7 +7,7 @@ argparse==1.2.1
boto==2.38.0 boto==2.38.0
django-autoslug==1.7.2 django-autoslug==1.7.2
django-braces==1.4.0 django-braces==1.4.0
git+https://github.com/the-ben-waters/django-cas.git git+https://github.com/kstateome/django-cas.git
django-crispy-forms==1.4.0 django-crispy-forms==1.4.0
django-gravatar2==1.1.4 django-gravatar2==1.1.4
django-haystack==2.3.1 django-haystack==2.3.1
......
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