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

9
10
11
12
13
14
15
16
17
18
19
20
21
22

def pfinfo(uname):
    base_url = "http://peoplefinder.b1.akshaykarthik.com/"
    url = base_url + "basic/all/" + str(uname)
    try:
        metadata = requests.get(url)
        metadata.raise_for_status()
    except requests.exceptions.RequestException as e:
        print e
    else:
        pfjson = metadata.json()
        name = pfjson['results'][0]['name']
        return name.split(',')

Daniel W Bond's avatar
Daniel W Bond committed
23

24
25
26
27
28
29
30
def create_user(tree):

    username = tree[0][0].text
    user, user_created = User.objects.get_or_create(username=username)

    if user_created:
        user.email = "%s@%s" % (username, settings.ORGANIZATION_EMAIL_DOMAIN)
31
32
33
34
35

        name_list = pfinfo(username)
        user.first_name = name_list[1].rstrip()
        user.last_name = name_list[0]

36
        user.save()
37

Jason D Yeomans's avatar
Jason D Yeomans committed
38
39
        new_student = Student.objects.create(user=user)
        new_student.save()
40
41

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