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


# TODO 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 = None
    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 != None:
                return render_to_response('index.html', {'form': form, 'results': queryList},
                                            context_instance=RequestContext(request))                                
            else:
                reader = Ldap_Reader(query['filter_type'], query['term'])
                response = reader.get()
                personList = []
                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
                    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},
                              context_instance=RequestContext(request))

def user_invalid_view(request):
    return render_to_response("invalid.html")

def user_login_view(request):
    if request.method == 'POST':
        form = forms.UserLoginForm(request.POST)
        if form.is_valid():
            cd = form.cleaned_data
            user = auth.authenticate(username=cd['email'], password=cd['password'])
            if user is not None:
                if user.is_active:
                    auth.login(request, user)
                    return HttpResponseRedirect('/') #Redirect to success page
                else:
                    pass #Inactive user (probably not applicable)
            else:
               return HttpResponseRedirect('/accounts/invalid/')
    else:
        form = forms.UserLoginForm()
    return render_to_response("login.html", {'form': form})

@login_required
def user_logout_view(request):
    auth.logout(request)
    return HttpResponseRedirect('/accounts/logout/success/')

def user_logout_complete_view(request):
    return render_to_response('logout.html')

def user_registration_view(request):
    if request.method == 'POST':
        form = forms.UserRegistrationForm(request.POST)
        if form.is_valid():
            cd = form.cleaned_data
            user = User.objects.create_user(username=cd['email'], email=cd['email'], password=cd['password_confirmation'])
            user.save()
            return HttpResponseRedirect("/accounts/thanks/")
    else:
        form = forms.UserRegistrationForm()
    return render_to_response("register.html", {'form': form})

@login_required
def user_registration_complete_view(request):
    return render_to_response("thanks.html")