Skip to content

Commit

Permalink
Merge pull request #32 from cid-harvard/feature/region-country-year
Browse files Browse the repository at this point in the history
[COL-434] Create Department-Country-Year data model and API
  • Loading branch information
makmanalp committed Nov 5, 2015
2 parents c14d594 + bdc9e06 commit 616af41
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 7 deletions.
8 changes: 8 additions & 0 deletions colombia/api_schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,12 @@ class Meta:
fields = ("export_value", "country_id", "product_id", "year")


class CountryDepartmentYearSchema(ma.Schema):

class Meta:
fields = ("export_value", "country_id", "location_id", "year")


class XIndustryYearSchema(ma.Schema):

location_id = ma.fields.Integer(default=0)
Expand Down Expand Up @@ -138,6 +144,8 @@ class ColombiaMetadataSchema(MetadataSchema):
country_municipality_product_year = CountryMunicipalityProductYearSchema(many=True)
country_department_product_year = CountryDepartmentProductYearSchema(many=True)

country_department_year = CountryDepartmentYearSchema(many=True)

product_year = ProductYearSchema(many=True)
industry_year = IndustryYearSchema(many=True)
occupation_year = OccupationYearSchema(many=True)
Expand Down
17 changes: 16 additions & 1 deletion colombia/data/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ def product_id(cls):
level = db.Column(product_enum)

export_value = db.Column(db.BIGINT)
num_plants = db.Column(db.Integer)
export_num_plants = db.Column(db.Integer)


class CountryDepartmentProductYear(CountryXProductYear):
Expand All @@ -118,6 +118,21 @@ class CountryMunicipalityProductYear(CountryXProductYear):
__tablename__ = "country_municipality_product_year"


class CountryDepartmentYear(BaseModel, IDMixin):

__tablename__ = "country_department_year"

country_id = db.Column(db.Integer, db.ForeignKey(Location.id))
location_id = db.Column(db.Integer, db.ForeignKey(Location.id))
year = db.Column(db.Integer)

location = db.relationship(Location, foreign_keys=[location_id])
country = db.relationship(Location, foreign_keys=[country_id])

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


class DepartmentYear(BaseModel, IDMixin):

__tablename__ = "department_year"
Expand Down
25 changes: 24 additions & 1 deletion colombia/data/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
MunicipalityIndustryYear, ProductYear, IndustryYear,
DepartmentYear, Location, CountryMunicipalityProductYear,
CountryDepartmentProductYear, OccupationYear,
OccupationIndustryYear)
OccupationIndustryYear, CountryDepartmentYear)
from ..api_schemas import marshal
from .routing import lookup_classification_level
from .. import api_schemas as schemas
Expand Down Expand Up @@ -203,6 +203,26 @@ def eey_location_subregions_trade(entity_type, entity_id, buildingblock_level):
return jsonify(data=[x._asdict() for x in q])


def eey_location_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)

# Assert level of sub_id is same as entity_id
location_level = lookup_classification_level("location", entity_id)

if location_level == "department":
q = CountryDepartmentYear.query\
.filter_by(location_id=entity_id)\
.all()
return marshal(schemas.country_department_year, q)
else:
msg = "Data doesn't exist at location level {}"\
.format(location_level)
abort(400, body=msg)


def eey_industry_occupations(entity_type, entity_id, buildingblock_level):

if buildingblock_level != "minor_group":
Expand Down Expand Up @@ -245,6 +265,9 @@ def eey_industry_occupations(entity_type, entity_id, buildingblock_level):
},
"subregions_trade": {
"func": eey_location_subregions_trade
},
"partners": {
"func": eey_location_partners
}
}
},
Expand Down
12 changes: 8 additions & 4 deletions colombia/datasets.py
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ def load_trade4digit_municipality():
"p": "product",
"yr": "year",
"X_rcpy_d": "export_value",
"NP_rcpy": "num_plants"
"NP_rcpy": "export_num_plants"
},
"classification_fields": {
"location": {
Expand All @@ -351,9 +351,13 @@ def load_trade4digit_municipality():
},
"facet_fields": ["location", "country", "product", "year"],
"facets": {
("country_id", "location_id", "year"): {
"export_value": first,
"export_num_plants": first
},
("country_id", "location_id", "product_id", "year"): {
"export_value": first,
"num_plants": first
"export_num_plants": first
}
}
}
Expand All @@ -367,7 +371,7 @@ def load_trade4digit_municipality():
"p": "product",
"yr": "year",
"X_rcpy_d": "export_value",
"NP_rcpy": "num_plants"
"NP_rcpy": "export_num_plants"
},
"classification_fields": {
"location": {
Expand All @@ -392,7 +396,7 @@ def load_trade4digit_municipality():
"facets": {
("country_id", "location_id", "product_id", "year"): {
"export_value": first,
"num_plants": 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 @@ -141,6 +141,10 @@
df.to_sql("country_department_product_year", db.engine,
index=False, chunksize=10000, if_exists="append")

df = ret[("country_id", "location_id", "year")].reset_index()
df.to_sql("country_department_year", db.engine,
index=False, chunksize=10000, if_exists="append")

# Country - industry- y ear
ret = process_dataset(industry4digit_country)
df = ret[('location_id', 'industry_id', 'year')].reset_index()
Expand Down
2 changes: 1 addition & 1 deletion colombia/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@
DepartmentIndustryYear, IndustryYear,
MunicipalityIndustryYear, MSAProductYear,
MSAIndustryYear, OccupationYear,
OccupationIndustryYear)
OccupationIndustryYear, CountryDepartmentYear)

0 comments on commit 616af41

Please sign in to comment.