Skip to content

Commit

Permalink
Merge pull request #16 from cid-harvard/feature-industry-data-muni
Browse files Browse the repository at this point in the history
Add muni level industry scatterplot API
  • Loading branch information
makmanalp committed Jun 29, 2015
2 parents 449ffb0 + 114fba2 commit 9bfef3b
Showing 1 changed file with 26 additions and 21 deletions.
47 changes: 26 additions & 21 deletions colombia/data/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,26 +194,31 @@ def scatterplot(entity_type):

location = Location.query.get_or_404(location_id)

if location.level != "department":
raise abort(400, body="""Scatterplots are only available for
departments.""")

if entity_type == "industries":
q = db.session.query(DepartmentIndustryYear.distance,
IndustryYear.complexity,
IndustryYear.industry_id
)\
.filter_by(year=year, department_id=location_id)\
.join(IndustryYear, (DepartmentIndustryYear.industry_id == IndustryYear.id) & (DepartmentIndustryYear.year == IndustryYear.year))
return jsonify(data=[x._asdict() for x in q])
elif entity_type == "products":
q = db.session.query(DepartmentProductYear.distance,
ProductYear.pci.label("complexity"),
ProductYear.product_id
)\
.filter_by(year=year, department_id=location_id)\
.join(ProductYear, (DepartmentProductYear.product_id == ProductYear.id) & (DepartmentProductYear.year == ProductYear.year))
return jsonify(data=[x._asdict() for x in q])

if location.level == "department":
if entity_type == "industries":
q = db.session.query(DepartmentIndustryYear.distance,
IndustryYear.complexity,
IndustryYear.industry_id
)\
.filter_by(year=year, department_id=location_id)\
.join(IndustryYear, (DepartmentIndustryYear.industry_id == IndustryYear.id) & (DepartmentIndustryYear.year == IndustryYear.year))
return jsonify(data=[x._asdict() for x in q])
elif entity_type == "products":
q = db.session.query(DepartmentProductYear.distance,
ProductYear.pci.label("complexity"),
ProductYear.product_id
)\
.filter_by(year=year, department_id=location_id)\
.join(ProductYear, (DepartmentProductYear.product_id == ProductYear.id) & (DepartmentProductYear.year == ProductYear.year))
return jsonify(data=[x._asdict() for x in q])
elif location.level == "municipality":
if entity_type == "industries":
q = db.session.query(MunicipalityIndustryYear.distance,
IndustryYear.complexity,
IndustryYear.industry_id
)\
.filter_by(year=year, municipality_id=location_id)\
.join(IndustryYear, (MunicipalityIndustryYear.industry_id == IndustryYear.id) & (MunicipalityIndustryYear.year == IndustryYear.year))
return jsonify(data=[x._asdict() for x in q])

raise abort(400, body="Could not find data with the given parameters.")

0 comments on commit 9bfef3b

Please sign in to comment.