Skip to content

Commit

Permalink
Add ProductPartnerYear API. COL-710
Browse files Browse the repository at this point in the history
  • Loading branch information
makmanalp committed Dec 3, 2015
1 parent ba2357c commit 7d0c2e7
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 3 deletions.
9 changes: 9 additions & 0 deletions colombia/api_schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,15 @@ class Meta:
fields = ("export_value", "import_value", "country_id", "product_id", "year")


class PartnerProductYearSchema(ma.Schema):

department_id = ma.fields.Integer(attribute="location_id")

class Meta:
fields = ("export_value", "import_value", "export_num_plants",
"import_num_plants", "country_id", "product_id", "year")


class CountryXYearSchema(ma.Schema):

class Meta:
Expand Down
22 changes: 21 additions & 1 deletion colombia/data/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
from ..core import db

from ..metadata.models import (Location, HSProduct, Industry, Occupation,
product_enum, industry_enum, occupation_enum)
Country, product_enum, industry_enum,
occupation_enum)


class XProductYear(BaseModel, IDMixin):
Expand Down Expand Up @@ -160,6 +161,25 @@ class CountryMunicipalityYear(CountryXYear):
__tablename__ = "country_municipality_year"


class PartnerProductYear(BaseModel, IDMixin):

__tablename__ = "partner_product_year"

country_id = db.Column(db.Integer, db.ForeignKey(Country.id))
product_id = db.Column(db.Integer, db.ForeignKey(HSProduct.id))
level = db.Column(product_enum)

partner = db.relationship(Country)
product = db.relationship(HSProduct)

year = db.Column(db.Integer)

export_value = db.Column(db.BIGINT)
import_value = db.Column(db.BIGINT)
export_num_plants = db.Column(db.Integer)
import_num_plants = db.Column(db.Integer)


class DepartmentYear(BaseModel, IDMixin):

__tablename__ = "department_year"
Expand Down
18 changes: 17 additions & 1 deletion colombia/data/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
CountryDepartmentProductYear, OccupationYear,
OccupationIndustryYear, CountryCountryYear,
CountryDepartmentYear, CountryMSAYear,
CountryMunicipalityYear, MSAYear)
CountryMunicipalityYear, MSAYear, PartnerProductYear)
from ..api_schemas import marshal
from .routing import lookup_classification_level
from .. import api_schemas as schemas
Expand Down Expand Up @@ -240,6 +240,19 @@ def eey_location_partners(entity_type, entity_id, buildingblock_level):
abort(400, body=msg)


def eey_product_partners(entity_type, entity_id, buildingblock_level):

if buildingblock_level != "country":
msg = "Data doesn't exist at level {}. Try country.".format(buildingblock_level)
abort(400, body=msg)

q = PartnerProductYear.query\
.filter_by(product_id=entity_id)\
.all()

return marshal(schemas.PartnerProductYearSchema(many=True), q)


def eey_industry_occupations(entity_type, entity_id, buildingblock_level):

if buildingblock_level != "minor_group":
Expand Down Expand Up @@ -268,6 +281,9 @@ def eey_industry_occupations(entity_type, entity_id, buildingblock_level):
"subdatasets": {
"exporters": {
"func": eey_product_exporters
},
"partners": {
"func": eey_product_partners
}
}
},
Expand Down
6 changes: 6 additions & 0 deletions colombia/datasets.py
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,12 @@ def replace_country(df):
"import_value": sum_group,
"import_num_plants": sum_group,
},
("product_id", "country_id", "year"): {
"export_value": sum_group,
"export_num_plants": sum_group,
"import_value": sum_group,
"import_num_plants": sum_group,
},
("country_id", "location_id", "product_id", "year"): {
"export_value": first,
"export_num_plants": first,
Expand Down
4 changes: 4 additions & 0 deletions colombia/import.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,10 @@
df.to_sql("country_country_year", db.engine,
index=False, chunksize=10000, if_exists="append")

df = ret[("product_id", "country_id", "year")].reset_index()
df["level"] = "4digit"
df.to_sql("partner_product_year", db.engine,
index=False, chunksize=10000, if_exists="append")

# MSA - trade rcpy
ret = process_dataset(trade4digit_rcpy_msa)
Expand Down
2 changes: 1 addition & 1 deletion colombia/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@
MSAIndustryYear, OccupationYear,
OccupationIndustryYear, CountryCountryYear,
CountryDepartmentYear, CountryMSAYear,
CountryMunicipalityYear, MSAYear)
CountryMunicipalityYear, MSAYear, PartnerProductYear)

1 change: 1 addition & 0 deletions doc/rest.rst
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@ All data API requests *must* supply a ?level= query parameter.

.. http:get:: /data/(string:entity_type)/(int:entity_id)/(string:subdataset)/
.. http:get:: /data/product/(int:entity_id)/exporters/
.. http:get:: /data/product/(int:entity_id)/partners/
.. http:get:: /data/industry/(int:entity_id)/participants/
.. http:get:: /data/industry/(int:entity_id)/occupations/
.. http:get:: /data/location/(int:entity_id)/products/
Expand Down

0 comments on commit 7d0c2e7

Please sign in to comment.