Commit ece3a02d authored by Daniel W Bond's avatar Daniel W Bond Committed by Mark Stenglein

added support for storing peoplefinder names (helps with moderation)

parent b92a75e6
<<<<<<< b92a75e64037f60a7eef65f6e4d4089620a9400d
# core django imports
from django.conf import settings
from django.contrib.auth.models import User
......@@ -77,3 +78,63 @@ def create_user(tree):
print("CAS callback completed.")
=======
# core django imports
from django.conf import settings
from django.contrib.auth.models import User
# third-part imports
import requests
# imports from your apps
from django.conf import settings
from .models import Student
def pfinfo(u_name):
pf_url = settings.PF_URL
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()
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 ['', '']
def create_user(tree):
username = tree[0][0].text
print username
user, user_created = User.objects.get_or_create(username=username)
if user_created:
user.email = "%s@%s" % (username, settings.ORGANIZATION_EMAIL_DOMAIN)
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"
user.save()
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 user %s!" % username)
>>>>>>> added support for storing peoplefinder names (helps with moderation)
......@@ -12,10 +12,35 @@ class Student(TimeStampedModel):
# django user includes username, password, first name, and last name
user = models.OneToOneField(User)
pf_first_name = models.CharField(max_length=255, blank=True)
pf_last_name = models.CharField(max_length=255, blank=True)
slug = AutoSlugField(populate_from='user', unique=True)
emails_sent = models.PositiveIntegerField(default=0)
def has_nickname(self):
pf_name = "%s %s" % (self.pf_first_name, self.pf_last_name)
if (self.user.get_full_name() != pf_name) and (pf_name != " "):
return True
else:
return False
def get_nickname(self):
# different first name
if (self.user.first_name != self.pf_first_name) and (self.user.last_name == self.pf_last_name):
return "%s \"%s\" %s" % (self.pf_first_name, self.user.first_name, self.user.last_name)
# different last name
elif (self.user.first_name == self.pf_first_name) and (self.user.last_name != self.pf_last_name):
return "%s %s \"%s\"" % (self.user.first_name, self.pf_last_name, self.user.last_name)
# both
elif (self.user.first_name != self.pf_first_name) and (self.user.last_name != self.pf_last_name):
return "%s \"%s %s\" %s" % (self.pf_first_name, self.user.first_name, self.user.last_name, self.pf_last_name)
# failing gracefully
else:
return self.user.get_full_name()
def get_absolute_url(self):
return reverse('profile', kwargs={'slug': self.slug})
......
......@@ -30,7 +30,12 @@ SRCT Bookshare &bull; Mod
{% widthratio student.emails_sent student.num_books 1 as ratio %}
<td>{{ ratio }}</td>
<td><a href="{{ student.get_absolute_url }}" target="_blank">
<strong>{{ student.user.get_full_name }}</strong> (<em>{{ student.user.username }}</em>)
{% if student.has_nickname %}
<strong>{{ student.get_nickname }}</strong>
{% else %}
<strong>{{ student.user.get_full_name }}</strong>
{% endif %}
(<em>{{ student.user.username }}</em>)
</a></td>
<td>
<a href="#">
......
......@@ -29,7 +29,11 @@ SRCT Bookshare &bull; Mod
<strong>{{ listing.title }}</strong>
</a></td>
<td><a href="{{ listing.poster.get_absolute_url }}" target="_blank">
{{ listing.poster.user.get_full_name }}
{% if listing.poster.has_nickname %}
{{ listing.poster.get_nickname }}
{% else %}
{{ listing.poster.user.get_full_name }}
{% endif %}
</a></td>
<td><a href="{% url 'delete_listing' listing.slug %}">
<span class="label label-danger"><strong>Delete</strong></span>
......
......@@ -25,7 +25,12 @@ SRCT Bookshare &bull; Mod
<tr>
<td>{{ student.num_books }}</td>
<td><a href="{{ student.get_absolute_url }}" target="_blank">
<strong>{{ student.user.get_full_name }}</strong> (<em>{{ student.user.username }}</em>)
{% if student.has_nickname %}
<strong>{{ student.get_nickname }}</strong>
{% else %}
<strong>{{ student.user.get_full_name }}</strong>
{% endif %}
(<em>{{ student.user.username }}</em>)
</a></td>
<td>
<a href="#">
......
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