Permalink
Browse files

Reimplemented regional downloads. Closes #103.

  • Loading branch information...
1 parent 05987bd commit a15fb490915c4b0e2bffa60dea0549c0bbb0ca9f cgroskopf committed Jun 8, 2011
@@ -14,6 +14,7 @@
<script src="{{ MEDIA_URL }}js/jquery.cookie.js"></script>
<script src="{{ MEDIA_URL }}js/jquery.tree.js"></script>
<script type="text/javascript">
+ window.DATASET = "{{ settings.DATASET }}";
window.API_URL = "{{ settings.API_URL }}";
</script>
</head>
@@ -9,18 +9,34 @@
def strip_callback(data):
return data[data.index('(') + 1:-1]
-def fetch_geography(geoid):
- url = '%s%s.jsonp' % (settings.API_URL, geoid)
+def _api_fetch(url):
r = requests.get(url)
content = strip_callback(zlib.decompress(r.content))
return simplejson.loads(content)
+def fetch_tracts_by_state(geoid):
+ url = '%s/tracts_%s.jsonp' % (settings.API_URL, geoid)
+ return _api_fetch(url)
+
+def fetch_tracts_by_county(geoid):
+ url = '%s/tracts_%s.jsonp' % (settings.API_URL, geoid)
+ return _api_fetch(url)
+
+def fetch_counties_by_state(geoid):
+ url = '%s/counties_%s.jsonp' % (settings.API_URL, geoid)
+ return _api_fetch(url)
+
+def fetch_places_by_state(geoid):
+ url = '%s/places_%s.jsonp' % (settings.API_URL, geoid)
+ return _api_fetch(url)
+def fetch_geography(geoid):
+ url = '%s/%s.jsonp' % (settings.API_URL, geoid)
+ return _api_fetch(url)
+
def fetch_geographies(geoids):
return [fetch_geography(geoid) for geoid in geoids]
def fetch_labels(dataset):
- url = '%s%s_labels.jsonp' % (settings.API_URL, dataset)
- r = requests.get(url)
- content = strip_callback(zlib.decompress(r.content))
- return simplejson.loads(content)
+ url = '%s/%s_labels.jsonp' % (settings.API_URL, dataset)
+ return _api_fetch(url)
@@ -10,7 +10,7 @@
import csv
import help_text
-import mongoutils # TODO: factor out
+import mongoutils
import utils
from datetime import datetime
@@ -27,22 +27,21 @@ def data(request, geoids):
return render_to_response('data.html', { 'settings': settings }, context_instance=RequestContext(request))
def download_data_for_region(request, sumlev='', containerlev='', container='', datatype=''):
- # TODO - reimplement
if sumlev == '140' and containerlev == '040':
- geo_list = mongoutils.get_tracts_by_state(container)
+ geo_list = utils.fetch_tracts_by_state(container)
elif sumlev == '140' and containerlev == '050':
- geo_list = mongoutils.get_tracts_by_county(container)
+ geo_list = utils.fetch_tracts_by_county(container)
elif sumlev == '160' and containerlev == '040':
- geo_list = mongoutils.get_places_by_state(container)
+ geo_list = utils.fetch_places_by_state(container)
elif sumlev == '050' and containerlev == '040':
- geo_list = mongoutils.get_counties_by_state(container)
+ geo_list = utils.fetch_counties_by_state(container)
geoids = ','.join([g[1] for g in geo_list])
if datatype == 'csv':
- return data_as_csv(request,geoids)
+ return data_as_csv(request, geoids)
elif datatype == 'json':
- return data_as_json(request,geoids)
+ return data_as_json(request, geoids)
def data_as_json(request, geoids):
geographies = {}
@@ -126,7 +126,9 @@
LABELS_DB = 'census_labels'
LABELS_COLLECTION = 'labels'
-API_URL = 'http://s3.amazonaws.com/census-test/'
+# Application settings
+DATASET = 'PL'
+API_URL = 'http://s3.amazonaws.com/census-test'
TEST_RUNNER='api.tests.TestRunner'
@@ -1,5 +1,4 @@
$(function(){
- var dataset = "PL";
var report_template = _.template($('#report-template').html());
removeColumn = function() {
@@ -73,7 +72,7 @@ $(function(){
}
window.loadLabels = function() {
- apiRequest(dataset + "_labels.jsonp", "labels_" + dataset, function(labels_data) {
+ apiRequest(window.DATASET + "_labels.jsonp", "labels_" + window.DATASET, function(labels_data) {
window.labels_data = labels_data;
window.geoids = parseGeoids();
//var tables = parseTables();
@@ -86,7 +86,7 @@ $(function(){
this.mappings.summarylevelDisplays[SUMLEV_STATE] = 'States';
this.mappings.summarylevelDisplays[SUMLEV_NATION] = 'USA';
- $.ajax(API_URL + "states.jsonp", {
+ $.ajax(API_URL + "/states.jsonp", {
dataType: "jsonp",
jsonpCallback: "states",
success: function(states) {
@@ -281,7 +281,7 @@ $(function(){
},
loadCounties: function() {
- $.ajax(API_URL + 'counties_' + this.get(SUMLEV_STATE) + '.jsonp', {
+ $.ajax(API_URL + '/counties_' + this.get(SUMLEV_STATE) + '.jsonp', {
dataType: "jsonp",
jsonpCallback: "counties_" + this.get(SUMLEV_STATE),
success: _.bind(function(response) {
@@ -292,7 +292,7 @@ $(function(){
},
loadPlaces: function() {
- $.ajax(API_URL + 'places_' + this.get(SUMLEV_STATE) + '.jsonp', {
+ $.ajax(API_URL + '/places_' + this.get(SUMLEV_STATE) + '.jsonp', {
dataType: "jsonp",
jsonpCallback: "places_" + this.get(SUMLEV_STATE),
success: _.bind(function(response) {
@@ -303,7 +303,7 @@ $(function(){
},
loadTracts: function() {
- $.ajax(API_URL + 'tracts_' + this.get(SUMLEV_COUNTY) + '.jsonp', {
+ $.ajax(API_URL + '/tracts_' + this.get(SUMLEV_COUNTY) + '.jsonp', {
dataType: "jsonp",
jsonpCallback: "tracts_" + this.get(SUMLEV_COUNTY),
success: _.bind(function(response) {

0 comments on commit a15fb49

Please sign in to comment.