Source

hokiefinder / geolocator / views.py

Full commit
from django.shortcuts import render_to_response
from django.template import RequestContext
from django.contrib.auth.decorators import login_required
from django.contrib.auth.models import User
from django.contrib import auth
from django.http import HttpResponseRedirect

from hokiefinder.geolocator import google_geocoding
from hokiefinder.geolocator import models
from hokiefinder.geolocator import forms
from hokiefinder.geolocator.ldap_reader import Ldap_Reader

reader = None
personList = []

# Pass query to ldap reader
# Store results in database, check if we have cached geocoded results
# Geocode results not yet geocoded in cache
# Build Google Maps JS calls from geocoded results and stored results
# If 'Next' button is pressed, get next page of results, repeat above
def hokie_finder_view(request):
    personList = []
    ldapCount = 0
    if request.method == 'POST':
        form = forms.HokieFinderForm(request.POST)
        if form.is_valid():
            query = form.cleaned_data
            queryList = None
            if query['filter_type'] == 'uupid':
                queryList = models.Person.objects.filter(pid__icontains=query['term'])
            elif query['filter_type'] == 'displayName':
                queryList = models.Person.objects.filter(display_name__icontains=query['term'])
            if queryList is not None:
                personList.extend([person for person in queryList])

            reader = Ldap_Reader(query['filter_type'], query['term'])
            response = reader.get()
            reader.close()
            ldapCount = len(response)
            for person in response:
                personInfo = person[1]
                try:
                    uupid = personInfo['uupid'][0]
                except KeyError:
                    try:
                        uupid = personInfo['mail'][0].partition('@')[0]
                    except KeyError:
                        continue  # Person does not have PID or email
                if queryList is not None and len(queryList.filter(pid = uupid)) > 0:
                    continue
                name = personInfo['displayName'][0]
                addr = personInfo['localPostalAddress'][0]
                geo_loc = google_geocoding.get_location(addr)
                if geo_loc == None:
                    continue
                else:
                    addr, lat, lng = geo_loc
                try:
                    phone = personInfo['localPhone'][0]
                except KeyError:
                    try:
                        phone = personInfo['telephoneNumber'][0]
                    except KeyError:
                        phone = None
                try:
                    major = personInfo['major'][0]
                except KeyError:
                    major = None
                newPerson = models.Person( \
                    pid = uupid, display_name = name, \
                    address = addr, phone_number = phone, \
                    major = major, longitude = lng, latitude = lat)
                newPerson.save()
                personList.append(newPerson)
    else:
        form = forms.HokieFinderForm()
    return render_to_response("index.html", {'form': form, 'results': personList,
                                'ldapCount': ldapCount},
                                context_instance=RequestContext(request))