diff --git a/colombia/api_schemas.py b/colombia/api_schemas.py index f5a0dc2..a49ed6f 100644 --- a/colombia/api_schemas.py +++ b/colombia/api_schemas.py @@ -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: diff --git a/colombia/data/models.py b/colombia/data/models.py index 87d2a64..f4a1b64 100644 --- a/colombia/data/models.py +++ b/colombia/data/models.py @@ -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): @@ -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" diff --git a/colombia/data/views.py b/colombia/data/views.py index 6b09051..c8f5037 100644 --- a/colombia/data/views.py +++ b/colombia/data/views.py @@ -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 @@ -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": @@ -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 } } }, diff --git a/colombia/datasets.py b/colombia/datasets.py index 9d680fa..ebfc185 100644 --- a/colombia/datasets.py +++ b/colombia/datasets.py @@ -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, diff --git a/colombia/import.py b/colombia/import.py index 7d9e6fa..1228ac0 100644 --- a/colombia/import.py +++ b/colombia/import.py @@ -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) diff --git a/colombia/models.py b/colombia/models.py index 3f5919d..0700386 100644 --- a/colombia/models.py +++ b/colombia/models.py @@ -7,5 +7,5 @@ MSAIndustryYear, OccupationYear, OccupationIndustryYear, CountryCountryYear, CountryDepartmentYear, CountryMSAYear, - CountryMunicipalityYear, MSAYear) + CountryMunicipalityYear, MSAYear, PartnerProductYear) diff --git a/doc/rest.rst b/doc/rest.rst index 1b3c8ed..08ce798 100644 --- a/doc/rest.rst +++ b/doc/rest.rst @@ -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/