hokiefinder / geolocator /

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):
    if request.method == 'POST':
        form = forms.HokieFinderForm(request.POST)
        if form.is_valid():
            query = form.cleaned_data
            print query['filter_type']
            reader = Ldap_Reader(query['filter_type'], query['term'])
            response = reader.get()
            for person in response:
                personInfo = person[1]
                uupid = personInfo['uupid'][0]
                name = personInfo['displayName'][0]
                addr = personInfo['localPostalAddress'][0]
                addr, lat, lng = google_geocoding.get_location(addr)
                if addr == None:
                    phone = personInfo['localPhone'][0]
                except KeyError:
                    phone = None
                    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)
            return HttpResponseRedirect('/')
        form = forms.HokieFinderForm()
    return render_to_response("index.html", {'form': form},

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
                    pass #Inactive user (probably not applicable)
               return HttpResponseRedirect('/accounts/invalid/')
        form = forms.UserLoginForm()
    return render_to_response("login.html", {'form': form})

def user_logout_view(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'])
            return HttpResponseRedirect("/accounts/thanks/")
        form = forms.UserRegistrationForm()
    return render_to_response("register.html", {'form': form})

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