Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
SRCT
go
Commits
9a7b2de1
Commit
9a7b2de1
authored
Feb 17, 2016
by
Nicholas Anderson
Browse files
Uh, new added Peoplefinder support! Closes
#8
parent
4ff03c00
Changes
3
Hide whitespace changes
Inline
Side-by-side
go/go/cas_callbacks.py
View file @
9a7b2de1
from
__future__
import
absolute_import
,
print_function
from
django.contrib.auth.models
import
User
from
django.contrib.auth.models
import
User
from
django.conf
import
settings
from
django.conf
import
settings
import
requests
def
pfparse
(
pf_name_result
):
# name comes in format of Anderson, Nicholas J
print
(
pf_name_result
)
name_list
=
pf_name_result
.
split
(
','
)
# there's random whitespace with the first name
first_name_section
=
name_list
[
1
].
strip
()
# check if there's a middle initial
mi_q
=
first_name_section
.
split
(
' '
)
# make sure that the additional elements aren't multiple names
if
len
(
mi_q
[
-
1
])
==
1
:
first_name
=
' '
.
join
(
mi_q
[:
-
1
])
else
:
first_name
=
first_name_section
new_name_list
=
[
first_name
,
name_list
[
0
]]
return
new_name_list
def
pfinfo
(
uname
):
base_url
=
settings
.
PF_URL
url
=
base_url
+
"basic/all/"
+
str
(
uname
)
print
(
"Using url %s"
,
url
)
try
:
metadata
=
requests
.
get
(
url
,
timeout
=
5
)
print
(
"Retrieving information from the peoplefinder api."
)
metadata
.
raise_for_status
()
except
requests
.
exceptions
.
RequestException
as
e
:
print
(
"Cannot resolve to peoplefinder api:"
,
e
)
print
(
"Returning empty user info tuple."
)
return
[
u
''
,
u
''
]
else
:
pfjson
=
metadata
.
json
()
try
:
if
len
(
pfjson
[
'results'
])
==
1
:
return
pfparse
(
pfjson
[
'results'
][
0
][
'name'
])
else
:
return
pfparse
(
pfjson
[
'results'
][
1
][
'name'
])
# if the name is not in peoplefinder, return empty first and last name
except
IndexError
:
print
(
"Name not found in peoplefinder."
)
return
[
u
''
,
u
''
]
except
Exception
as
e
:
print
(
"Unknown peoplefinder error:"
,
e
)
print
(
"Returning empty user info tuple."
)
return
[
u
''
,
u
''
]
def
create_user_dud
(
tree
):
print
(
"Parsing CAS information."
)
try
:
username
=
tree
[
0
][
0
].
text
user
,
user_created
=
User
.
objects
.
get_or_create
(
username
=
username
)
except
Exception
as
e
:
print
(
"CAS callback unsuccessful:"
,
e
)
info_name
=
pfinfo
(
username
)
try
:
if
user_created
:
print
(
"Created user object %s!"
%
username
)
user
.
email
=
"%s@%s"
%
(
username
,
settings
.
ORGANIZATION_EMAIL_DOMAIN
)
# Password is a required User object field, though doesn't matter for our
# purposes because all user auth is handled through CAS, not Django's login.
user
.
set_password
(
"cas_used_instead"
)
# a list of empty strings is False
if
not
info_name
:
user
.
first_name
=
info_name
[
0
]
user
.
last_name
=
info_name
[
1
]
print
(
"Added user's name, %s %s."
%
(
info_name
[
0
],
info_name
[
1
]))
else
:
print
(
"Unable to add user %s's name."
%
username
)
user
.
save
()
else
:
print
(
"User %s already exists"
%
username
)
except
Exception
as
e
:
print
(
"Unhandled user creation error:"
,
e
)
def
create_user
(
tree
):
def
create_user
(
tree
):
username
=
tree
[
0
][
0
].
text
print
(
"Parsing CAS information."
)
user
,
user_created
=
User
.
objects
.
get_or_create
(
username
=
username
)
try
:
username
=
tree
[
0
][
0
].
text
user
,
user_created
=
User
.
objects
.
get_or_create
(
username
=
username
)
except
Exception
as
e
:
print
(
"CAS callback unsuccessful:"
,
e
)
# error handling in pfinfo function
info_name
=
pfinfo
(
username
)
try
:
if
user_created
:
print
(
"Created user object %s."
%
username
)
# set and save the user's email
email_str
=
"%s@%s"
%
(
username
,
settings
.
ORGANIZATION_EMAIL_DOMAIN
)
user
.
email
=
email_str
# Password is a required User object field, though doesn't matter for our
# purposes because all user auth is handled through CAS, not Django's login.
user
.
set_password
(
'cas_used_instead'
)
user
.
save
()
print
(
"Added user's email, %s."
%
email_str
)
if
len
(
info_name
[
0
])
>
0
:
user
.
first_name
=
info_name
[
0
]
user
.
last_name
=
info_name
[
1
]
user
.
save
()
print
(
"Added user's name, %s %s."
%
(
info_name
[
0
],
info_name
[
1
]))
else
:
print
(
"Unable to add user's name."
)
print
(
"User object creation process completed."
)
if
user_created
:
else
:
user
.
email
=
"%s@%s"
%
(
username
,
settings
.
ORGANIZATION_EMAIL_DOMAIN
)
print
(
"User object already exists."
)
user
.
save
()
print
(
"Created user %s!"
%
username
)
print
(
"CAS callback successful."
)
except
Exception
as
e
:
print
(
"Unhandled user creation error:"
,
e
)
# mail the administrators
\ No newline at end of file
go/go/views.py
View file @
9a7b2de1
...
@@ -230,7 +230,7 @@ def signup(request):
...
@@ -230,7 +230,7 @@ def signup(request):
if
request
.
user
.
is_staff
:
if
request
.
user
.
is_staff
:
signup_form
=
SignupForm
()
signup_form
=
SignupForm
()
else
:
else
:
signup_form
=
SignupForm
(
initial
=
{
'username'
:
request
.
user
.
username
})
signup_form
=
SignupForm
(
initial
=
{
'username'
:
request
.
user
.
username
,
'full_name'
:
request
.
user
.
first_name
+
" "
+
request
.
user
.
last_name
})
signup_form
.
fields
[
'username'
].
widget
.
attrs
[
'readonly'
]
=
'readonly'
signup_form
.
fields
[
'username'
].
widget
.
attrs
[
'readonly'
]
=
'readonly'
if
request
.
method
==
'POST'
:
if
request
.
method
==
'POST'
:
...
...
go/settings/settings.py.template
View file @
9a7b2de1
...
@@ -28,6 +28,8 @@ DATABASES = {
...
@@ -28,6 +28,8 @@ DATABASES = {
# e.g. Which domains this app should listen to requests from.
# e.g. Which domains this app should listen to requests from.
ALLOWED_HOSTS = ['127.0.0.1']
ALLOWED_HOSTS = ['127.0.0.1']
# Peoplefinder API
PF_URL = "http://api.srct.gmu.edu/pf/v1/"
TIME_ZONE = 'America/New_York'
TIME_ZONE = 'America/New_York'
LANGUAGE_CODE = 'en-us'
LANGUAGE_CODE = 'en-us'
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment