def showMap(request): c = Context() if 'location' in request.GET and request.GET['location']: loc = request.GET['location'] c.__setitem__('locationName', loc) params = urllib.urlencode({"q": loc}) conn = HTTPConnection("where.yahooapis.com") conn.request("GET", "/geocode?%s" % params) resp = conn.getresponse() if resp.status == 200: data = resp.read() root = etree.XML(data) resultNodes = root.xpath('/ResultSet/Result') locationList = [] idx = 0 for node in resultNodes: idx += 1 locinfo = LocationInfo() locinfo.location = loc locinfo.latitude = float(node.xpath('latitude')[0].text) locinfo.longitude = float(node.xpath('longitude')[0].text) locinfo.city = node.xpath('city')[0].text locinfo.state = node.xpath('state')[0].text locinfo.country = node.xpath('country')[0].text locinfo.divId = 'map_canvas_%d' % idx locationList.append(locinfo) c.__setitem__('locationList', locationList) t = loader.get_template("templates/googleMap.html") return HttpResponse(t.render(c))
def showLocationForm(request): if 'user' not in request.session: return HttpResponseForbidden(loader.get_template("templates/403.html").render(Context())) t = loader.get_template('templates/locationForm.html') c = Context() c.__setitem__('user', request.session['user']) return HttpResponse(t.render(c))
def index(request): c = Context() if 'user' in request.session: c.__setitem__('user', request.session['user']) excursions = Excursion.objects.filter(user=request.session['user']) c.__setitem__('excursions', excursions) return homepage(request, c)
def searchBySpecies(request): c = Context() sightingList = [] if 'species' in request.GET and request.GET['species']: c.__setitem__('species', request.GET['species']) sightingList = Sighting.objects.filter(species=request.GET['species']) c.__setitem__('sightings', sightingList) t = loader.get_template('templates/speciesSearchResult.html') return HttpResponse(t.render(c))
def addLocation(request): if 'user' not in request.session: return HttpResponseForbidden(loader.get_template("templates/403.html").render(Context())) c = Context() if 'location' in request.POST and request.POST['location'] and 'date' in request.POST and request.POST['date'] and 'time' in request.POST and request.POST['time']: excursion = Excursion() excursion.user = request.session['user'] excursion.location = request.POST['location'] excursion.date = request.POST['date'] excursion.time = request.POST['time'] excursion.save() c.__setitem__('user', request.session['user']) c.__setitem__('excursion', excursion) t = loader.get_template('templates/sightingForm.html') return HttpResponse(t.render(c))
def searchByLocation(request): c = Context() sightingList = [] if 'location' in request.GET and request.GET['location']: c.__setitem__('location', request.GET['location']) excursions = Excursion.objects.filter(location=request.GET['location']) if excursions: for excur in excursions: sightings = Sighting.objects.filter(excursion=excur) for s in sightings: sightingList.append(s) c.__setitem__('sightings', sightingList) t = loader.get_template("templates/locationSearchResult.html") return HttpResponse(t.render(c))
def showWikipediaLinks(request): c = Context() if 'species' in request.GET and request.GET['species']: species = request.GET['species'] c.__setitem__('species', species) params = urllib.urlencode({"action":"opensearch", "search":species, "format":"xml"}) conn = HTTPConnection("en.wikipedia.org") headerMap = {"User-Agent" : "Shamim Ahmed ([email protected])"} conn.request("GET", "/w/api.php?%s" % params, headers=headerMap) resp = conn.getresponse() if resp.status == 200: data = resp.read() root = etree.XML(data) namespaceMap = {'n':'http://opensearch.org/searchsuggest2'} itemNodes = root.xpath("/n:SearchSuggestion/n:Section/n:Item", namespaces=namespaceMap) wikiEntryList = [] for item in itemNodes: wikiEntry = WikiEntryInfo() imageNodes = item.xpath("n:Image", namespaces=namespaceMap) textNode = item.xpath("n:Text", namespaces=namespaceMap)[0] descriptionNode = item.xpath("n:Description", namespaces=namespaceMap)[0] urlNode = item.xpath("n:Url", namespaces=namespaceMap)[0] wikiEntry.title = textNode.text wikiEntry.description = descriptionNode.text wikiEntry.articleUrl = urlNode.text if len(imageNodes) > 0: wikiEntry.imageUrl = imageNodes[0].get('source') wikiEntry.imageWidth = 2 * int(imageNodes[0].get('width')) wikiEntry.imageHeight = 2 * int(imageNodes[0].get('height')) wikiEntryList.append(wikiEntry) c.__setitem__('wikiEntryList', wikiEntryList) t = loader.get_template("templates/wikipedia.html"); return HttpResponse(t.render(c))
def register(request): t = None c = Context() if 'userName' in request.POST and request.POST['userName'] and 'password' in request.POST and request.POST['password']: name = request.POST['userName'] passwd = request.POST['password'] users = User.objects.filter(userName=name) if len(users) > 0: c.__setitem__('errorMessage', 'A user with the userName %s already exists' % name) t = loader.get_template("templates/registerForm.html") else: user = User() user.userName = name user.password = passwd user.save() t = loader.get_template('templates/registerSuccess.html') else: c.__setitem__('errorMessage', 'Both userName and password must be specified') t = loader.get_template("templates/registerForm.html") return HttpResponse(t.render(c))
def login(request): uname = '' pwd = '' if 'userName' in request.POST and request.POST['userName'] and 'password' in request.POST and request.POST['password']: uname = request.POST['userName'] pwd = request.POST['password'] c = Context() users = User.objects.filter(userName=uname,password=pwd) if len(users) > 0: user = users[0] request.session['user'] = user c.__setitem__('user', user) excursions = Excursion.objects.filter(user=user) c.__setitem__('excursions', excursions) if 'user' not in c: c.__setitem__('errorMessage', 'Authentication Failure') return homepage(request, c)