community-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From n...@apache.org
Subject svn commit: r1239948 - in /comdev/nearby_people: nearby/views.py readme.txt urls.py
Date Fri, 03 Feb 2012 00:06:07 GMT
Author: nick
Date: Fri Feb  3 00:06:06 2012
New Revision: 1239948

URL: http://svn.apache.org/viewvc?rev=1239948&view=rev
Log:
Add a JSON API around the place finder, and RDF + JSON APIs around the people finder. Also
fixes some indents.

Modified:
    comdev/nearby_people/nearby/views.py
    comdev/nearby_people/readme.txt
    comdev/nearby_people/urls.py

Modified: comdev/nearby_people/nearby/views.py
URL: http://svn.apache.org/viewvc/comdev/nearby_people/nearby/views.py?rev=1239948&r1=1239947&r2=1239948&view=diff
==============================================================================
--- comdev/nearby_people/nearby/views.py (original)
+++ comdev/nearby_people/nearby/views.py Fri Feb  3 00:06:06 2012
@@ -34,19 +34,19 @@ comdev_foaf = FOAF(COMDEV_DOAP)
 speakers_foaf = FOAF(SPEAKERS_DOAP)
 
 def welcome(request):
-	form = PlaceForm()
-	form.enableTypeField()
-	form.initial["type"] = 'lm'
-
-	return render(request, "welcome.html", {
-		'form': form
-	})
+   form = PlaceForm()
+   form.enableTypeField()
+   form.initial["type"] = 'lm'
+
+   return render(request, "welcome.html", {
+      'form': form
+   })
 
 def pick_place(request):
-	places = []
-	message = None
+   places = []
+   message = None
 
-	if request.POST:
+   if request.POST:
 		form = PlaceForm(request.POST)
 		if form.is_valid():
 			result = geoname.search(
@@ -66,64 +66,81 @@ def pick_place(request):
 					})
 			else:
 				message = "We can't work out the location you mean, sorry. Please double check your spelling,
or try searching for a nearby place which is larger."
-	else:
-		form = PlaceForm()
-        form.set_type( request.GET.get("type","") )
-
-	return render(request, "pick_place.html", {
-		'form': form, 'places': places, "message": message,
- 		'gmaps_key': GMAPS_KEY
-	})
-
-def find_people(request):
-	data = retrieve_people_data(LocationForm(request.GET))
-	data['gmaps_key'] = GMAPS_KEY
-	return render(request, "people.html", data)
+   else:
+       form = PlaceForm()
+       form.set_type( request.GET.get("type","") )
+
+   # We support html+json for responses, only html for requests
+   types = ["html"]
+   if request.POST:
+      types.append("json")
+   type = identify_type(request, types)
+   mimetype = mimetypes.get(type, None)
+
+   # Render the appropriate template
+   return render(request, "pick_place.%s" % type, {
+      'form': form, 'places': places, "message": message,
+      'gmaps_key': GMAPS_KEY
+   }, mimetype)
 
 def retrieve_people_data(form):
-	if not form.is_valid():
-		return HttpResponseRedirect("/pick_place/?location_missing")
+   if not form.is_valid():
+      return HttpResponseRedirect("/pick_place/?location_missing")
 
-	# What do they want?
-	foaf = comdev_foaf
-	if form.data["type"] == "sp":
-		foaf = speakers_foaf
-
-	search_data = form.data
-	search_latitude = float(search_data["latitude"])
-	search_longitude = float(search_data["longitude"])
-	rows = int(search_data["rows"])
-	people = foaf.get_nearby(search_latitude, search_longitude, rows)
-
-	# Work out the people.apache.org links for people
-	for d,person in people:
-		person["link"] = "http://people.apache.org/list_%s.html#%s" % \
-				(person["surname"][0], person["uid"])
-
-	# Work out our zoom
-	min_lat = min( [float(d[1]["latitude"]) for d in people]+[search_latitude] )
-	max_lat = max( [float(d[1]["latitude"]) for d in people]+[search_latitude] )
-	min_long = min( [float(d[1]["longitude"]) for d in people]+[search_longitude] )
-	max_long = max( [float(d[1]["longitude"]) for d in people]+[search_longitude] )
-	center_lat = (min_lat+max_lat)/2
-	center_long = (min_long+max_long)/2
-
-	# Decide if we need to say sorry about the distances
-	large_distance = False
-	if len(people) > 0 and people[0][0] > 250*1000:
-		large_distance = True
-
-	# Render!
-	data = {
-		'location': search_data, 'people':people,
-		'center_lat': center_lat, 'center_long': center_long,
-		'bl_lat': min_lat, 'bl_long': min_long,
-		'tr_lat': max_lat, 'tr_long': max_long,
-		'large_distance': large_distance,
-	}
-	
-	return data
+   # What do they want?
+   foaf = comdev_foaf
+   people_type = "Local Mentors"
+   if form.data["type"] == "sp":
+      foaf = speakers_foaf
+      people_type = "Speakers"
+
+   search_data = form.data
+   search_latitude = float(search_data["latitude"])
+   search_longitude = float(search_data["longitude"])
+   rows = int(search_data["rows"])
+   people = foaf.get_nearby(search_latitude, search_longitude, rows)
+
+   # Work out the people.apache.org links for people
+   for d,person in people:
+      person["link"] = "http://people.apache.org/list_%s.html#%s" % \
+             (person["surname"][0], person["uid"])
+
+   # Work out our zoom
+   min_lat = min( [float(d[1]["latitude"]) for d in people]+[search_latitude] )
+   max_lat = max( [float(d[1]["latitude"]) for d in people]+[search_latitude] )
+   min_long = min( [float(d[1]["longitude"]) for d in people]+[search_longitude] )
+   max_long = max( [float(d[1]["longitude"]) for d in people]+[search_longitude] )
+   center_lat = (min_lat+max_lat)/2
+   center_long = (min_long+max_long)/2
+
+   # Decide if we need to say sorry about the distances
+   large_distance = False
+   if len(people) > 0 and people[0][0] > 250*1000:
+      large_distance = True
+
+   # Render!
+   data = {
+      'location': search_data, 'people':people,
+      'center_lat': center_lat, 'center_long': center_long,
+      'bl_lat': min_lat, 'bl_long': min_long,
+      'tr_lat': max_lat, 'tr_long': max_long,
+      'large_distance': large_distance,
+      'people_type': people_type,
+   }
+   return data
 	
+def find_people(request):
+   data = retrieve_people_data(LocationForm(request.GET))
+   data['gmaps_key'] = GMAPS_KEY
+
+   # What format are we responding in?
+   type = identify_type(request, ("html", "json", "rdf"))
+   mimetype = mimetypes.get(type, None)
+
+   # Have the appropriate data rendered
+   return render(request, "people.%s" % type, data, mimetype)
+
 def find_people_json(request):
-	data = retrieve_people_data(LocationForm(request.GET))
-	return HttpResponse(json_dump(data), mimetype='application/javascript')
+   "Old style JSON API, using a simple JSON dump"
+   data = retrieve_people_data(LocationForm(request.GET))
+   return HttpResponse(json_dump(data), mimetype=mimetypes["json"])

Modified: comdev/nearby_people/readme.txt
URL: http://svn.apache.org/viewvc/comdev/nearby_people/readme.txt?rev=1239948&r1=1239947&r2=1239948&view=diff
==============================================================================
--- comdev/nearby_people/readme.txt (original)
+++ comdev/nearby_people/readme.txt Fri Feb  3 00:06:06 2012
@@ -28,13 +28,17 @@ If you want to write an alternative inte
 Find people nearby
 ------------------
 
-http://localhost:8000//find_people_json/?latitude=51.7522210026&longitude=-1.25596046448&type=lm
+http://localhost:8000//find_people/?latitude=51.7522210026&longitude=-1.25596046448&type=lm&format=json
+http://localhost:8000//find_people/?latitude=51.7522210026&longitude=-1.25596046448&type=lm&format=rdf
 
-Will return a JSON file containing the people nearby the point defined in the
-latitude and longitude values.
+Will return either a JSON file or a RDF file, containing the people nearby the point defined

+in the latitude and longitude values.
 
 Type is either "lm" for "local mentors" or "sp" for "speakers"
 
+As well as specifying a format as a GET parameter, you can also send appropriate Content-Type
or
+Accept headers with your request, to select either JSON or RDF (where available)
+
 Links
 =====
 

Modified: comdev/nearby_people/urls.py
URL: http://svn.apache.org/viewvc/comdev/nearby_people/urls.py?rev=1239948&r1=1239947&r2=1239948&view=diff
==============================================================================
--- comdev/nearby_people/urls.py (original)
+++ comdev/nearby_people/urls.py Fri Feb  3 00:06:06 2012
@@ -29,6 +29,8 @@ urlpatterns = patterns('',
 	(r'^$',              'nearby_people.nearby.views.welcome'),
 	(r'^pick_place/$',  'nearby_people.nearby.views.pick_place'),
 	(r'^find_people/$', 'nearby_people.nearby.views.find_people'),
+
+	# Old style JSON support, not template based
 	(r'^find_people_json/$', 'nearby_people.nearby.views.find_people_json'),
 
     # Uncomment the admin/doc line below and add 'django.contrib.admindocs' 



Mime
View raw message