From 39dfb5173e0fbfaada55746efc8c729d4b8a7d90 Mon Sep 17 00:00:00 2001 From: Mali Akmanalp Date: Mon, 29 Jun 2015 12:26:50 -0400 Subject: [PATCH] Municipality-Industry-Year API --- colombia/api_schemas.py | 8 ++++++++ colombia/data/views.py | 11 ++++++++++- 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/colombia/api_schemas.py b/colombia/api_schemas.py index cf81bf1..0ddb9af 100644 --- a/colombia/api_schemas.py +++ b/colombia/api_schemas.py @@ -33,6 +33,13 @@ class Meta: "department_id", "industry_id", "year") +class MunicipalityIndustryYearSchema(ma.Schema): + + class Meta: + fields = ("employment", "wages", "rca", "distance", "cog", "coi", + "municipality_id", "industry_id", "year") + + class DepartmentSchema(ma.Schema): class Meta: @@ -65,6 +72,7 @@ class ColombiaMetadataSchema(MetadataSchema): department_product_year = DepartmentProductYearSchema(many=True) department_industry_year = DepartmentIndustryYearSchema(many=True) +municipality_industry_year = MunicipalityIndustryYearSchema(many=True) product_year = ProductYearSchema(many=True) department = DepartmentSchema(many=True) metadata = ColombiaMetadataSchema(many=True) diff --git a/colombia/data/views.py b/colombia/data/views.py index 8cefe2f..ec24aae 100644 --- a/colombia/data/views.py +++ b/colombia/data/views.py @@ -1,6 +1,7 @@ from flask import Blueprint, request from .models import (DepartmentProductYear, DepartmentIndustryYear, - ProductYear, Location, IndustryYear, DepartmentYear) + MunicipalityIndustryYear, ProductYear, Location, + IndustryYear, DepartmentYear) from ..api_schemas import marshal from .. import api_schemas as schemas @@ -164,11 +165,19 @@ def industries_index(product_id=None): q = DepartmentIndustryYear.query\ .filter_by(year=year, department_id=location_id) return marshal(schemas.department_industry_year, q) + elif location_type == "municipality": + q = MunicipalityIndustryYear.query\ + .filter_by(year=year, municipality_id=location_id) + return marshal(schemas.municipality_industry_year, q) elif location_id is not None: if location_type == "department": q = DepartmentIndustryYear.query\ .filter_by(department_id=location_id) return marshal(schemas.department_industry_year, q) + elif location_type == "municipality": + q = MunicipalityIndustryYear.query\ + .filter_by(municipality_id=location_id) + return marshal(schemas.municipality_industry_year, q) raise abort(400, body="Could not find data with the given parameters.")