cas_callbacks.py 1.53 KB
Newer Older
1
# core django imports
2
from django.conf import settings
3
4
from django.contrib.auth.models import User
# third-part imports
5
import requests
6
7
# imports from your apps
from .models import Student
8

Daniel W Bond's avatar
Daniel W Bond committed
9

10
11
12
13
14
15
16
17
18
19
def pfinfo(u_name):
    pf_url = "http://peoplefinder.b1.akshaykarthik.com/"
    url = str(pf_url) + "basic/all/" + str(u_name)
    try:
        metadata = requests.get(url)
        metadata.raise_for_status()
    except requests.exceptions.RequestException as e:
        print e
    else:
        pf_json = metadata.json()
20
21
22
23
24
25
        try:
            name = pf_json['results'][0]['name']
            return name.split(',')
        # if the name is not in peoplefinder, return empty first and last name
        except IndexError:
            return ['', '']
26

27

Daniel W Bond's avatar
Daniel W Bond committed
28
def create_user(tree):
29
    username = tree[0][0].text
30
    print username
31
32
33
34
    user, user_created = User.objects.get_or_create(username=username)

    if user_created:
        user.email = "%s@%s" % (username, settings.ORGANIZATION_EMAIL_DOMAIN)
35
36
37
38
39
40
41
42
43
44
45
46
47
        print "hello"
        name_list = pfinfo(str(username))
        print name_list, "name_list"
        first_name = name_list[1].lstrip().split(' ')
        if len(first_name) > 1:
            no_mi = first_name[:-1]
            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 "world"
48
        user.save()
Daniel W Bond's avatar
Daniel W Bond committed
49
50
        new_student = Student.objects.create(user=user)
        new_student.save()
51
52

        print("Created user %s!" % username)