From 5426ef0b78c3b7bc0cc9ca695245b5665d9a159b Mon Sep 17 00:00:00 2001 From: Mali Akmanalp Date: Tue, 11 Aug 2015 12:31:10 -0400 Subject: [PATCH] Colombia level 4digit products COL-370 --- colombia/api_schemas.py | 24 ++++++++++++++++++++---- colombia/data/models.py | 2 ++ colombia/data/views.py | 5 +++++ colombia/datasets.py | 1 + 4 files changed, 28 insertions(+), 4 deletions(-) diff --git a/colombia/api_schemas.py b/colombia/api_schemas.py index f9d9f6d..221dcec 100644 --- a/colombia/api_schemas.py +++ b/colombia/api_schemas.py @@ -19,18 +19,33 @@ def marshal(schema, data, json=True, many=True): return serialization_result.data +XPY_FIELDS = ("import_value", "export_value", "export_rca", "distance", "cog", + "coi", "product_id", "year") + + +class CountryProductYearSchema(ma.Schema): + + country_id = ma.fields.Constant(0) + import_value = ma.fields.Constant(None) + export_rca = ma.fields.Constant(None) + distance = ma.fields.Constant(None) + cog = ma.fields.Constant(None) + coi = ma.fields.Constant(None) + + class Meta: + fields = XPY_FIELDS + ("country_id", ) + + class DepartmentProductYearSchema(ma.Schema): class Meta: - fields = ("import_value", "export_value", "export_rca", "distance", - "cog", "coi", "department_id", "product_id", "year") + fields = XPY_FIELDS + ("department_id", ) class MunicipalityProductYearSchema(ma.Schema): class Meta: - fields = ("import_value", "export_value", "export_rca", "distance", - "cog", "coi", "municipality_id", "product_id", "year") + fields = XPY_FIELDS + ("municipality_id", ) class CountryMunicipalityProductYearSchema(ma.Schema): @@ -105,6 +120,7 @@ class ColombiaMetadataSchema(MetadataSchema): description_es = ma.fields.Str(required=False) +country_product_year = CountryProductYearSchema(many=True) department_product_year = DepartmentProductYearSchema(many=True) municipality_product_year = MunicipalityProductYearSchema(many=True) country_municipality_product_year = CountryMunicipalityProductYearSchema(many=True) diff --git a/colombia/data/models.py b/colombia/data/models.py index f650464..0151cc4 100644 --- a/colombia/data/models.py +++ b/colombia/data/models.py @@ -140,6 +140,8 @@ class ProductYear(BaseModel, IDMixin): pci = db.Column(db.Float) pci_rank = db.Column(db.Integer) + export_value = db.Column(db.BIGINT) + class IndustryYear(BaseModel, IDMixin): diff --git a/colombia/data/views.py b/colombia/data/views.py index 1dfe5ad..095e873 100644 --- a/colombia/data/views.py +++ b/colombia/data/views.py @@ -122,6 +122,11 @@ def eey_location_products(entity_type, entity_id, buildingblock_level): location_level = lookup_classification_level("location", entity_id) + if location_level == "country": + q = ProductYear.query\ + .filter_by(level=buildingblock_level)\ + .all() + return marshal(schemas.country_product_year, q) if location_level == "department": q = DepartmentProductYear.query\ .filter_by(department_id=entity_id)\ diff --git a/colombia/datasets.py b/colombia/datasets.py index 133d0e7..1ff2412 100644 --- a/colombia/datasets.py +++ b/colombia/datasets.py @@ -65,6 +65,7 @@ def prefix_path(to_prefix): }, ("product_id", "year"): { "pci": first, + "export_value": sumGroup, }, ("department_id", "product_id", "year"): { "export_value": first,