From 5e561e1fb907fa91f88f2dd3ba300e34f803cd31 Mon Sep 17 00:00:00 2001 From: Mali Akmanalp Date: Mon, 2 Nov 2015 14:36:05 -0500 Subject: [PATCH] Add MSAYear model and API with ECI field, fixes COL-664 --- colombia/api_schemas.py | 7 +++++++ colombia/data/models.py | 12 ++++++++++++ colombia/data/views.py | 7 +++++-- colombia/import.py | 5 +++++ colombia/models.py | 2 +- 5 files changed, 30 insertions(+), 3 deletions(-) diff --git a/colombia/api_schemas.py b/colombia/api_schemas.py index f69e7d8..1d56ee0 100644 --- a/colombia/api_schemas.py +++ b/colombia/api_schemas.py @@ -111,6 +111,12 @@ class Meta: "department_id") +class MSAYearSchema(ma.Schema): + + class Meta: + fields = ("year", "eci", "location_id") + + class OccupationYearSchema(ma.Schema): class Meta: @@ -142,5 +148,6 @@ class ColombiaMetadataSchema(MetadataSchema): industry_year = IndustryYearSchema(many=True) occupation_year = OccupationYearSchema(many=True) department_year = DepartmentYearSchema(many=True) +msa_year = MSAYearSchema(many=True) metadata = ColombiaMetadataSchema(many=True) diff --git a/colombia/data/models.py b/colombia/data/models.py index 338cfe6..d9bdfdd 100644 --- a/colombia/data/models.py +++ b/colombia/data/models.py @@ -144,6 +144,18 @@ class DepartmentYear(BaseModel, IDMixin): num_establishments = db.Column(db.Integer) +class MSAYear(BaseModel, IDMixin): + + __tablename__ = "msa_year" + + location_id = db.Column(db.Integer, db.ForeignKey(Location.id)) + year = db.Column(db.Integer) + + location = db.relationship(Location) + + eci = db.Column(db.Float) + + class XIndustryYear(BaseModel, IDMixin): __abstract__ = True diff --git a/colombia/data/views.py b/colombia/data/views.py index b62443c..924b8d0 100644 --- a/colombia/data/views.py +++ b/colombia/data/views.py @@ -5,7 +5,7 @@ MunicipalityIndustryYear, ProductYear, IndustryYear, DepartmentYear, Location, CountryMunicipalityProductYear, CountryDepartmentProductYear, OccupationYear, - OccupationIndustryYear) + OccupationIndustryYear, MSAYear) from ..api_schemas import marshal from .routing import lookup_classification_level from .. import api_schemas as schemas @@ -55,8 +55,11 @@ def get_or_fail(name, dictionary): "department": { "model": DepartmentYear, "schema": schemas.department_year + }, + "msa": { + "model": MSAYear, + "schema": schemas.msa_year } - #"municipality" ... } product_year_region_mapping = { diff --git a/colombia/import.py b/colombia/import.py index 066fc3e..b1fdfd1 100644 --- a/colombia/import.py +++ b/colombia/import.py @@ -125,6 +125,11 @@ chunksize=10000, if_exists="append") + # MSA year + df = ret[('location_id', 'year')].reset_index() + df.to_sql("msa_year", db.engine, index=False, + chunksize=10000, if_exists="append") + # Municipality - trade rcpy ret = process_dataset(trade4digit_rcpy_municipality) diff --git a/colombia/models.py b/colombia/models.py index 903a0ba..839a7f3 100644 --- a/colombia/models.py +++ b/colombia/models.py @@ -5,4 +5,4 @@ DepartmentIndustryYear, IndustryYear, MunicipalityIndustryYear, MSAProductYear, MSAIndustryYear, OccupationYear, - OccupationIndustryYear) + OccupationIndustryYear, MSAYear)