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_x_year, q)
    elif location_level == "msa":
        q = CountryMSAYear.query\
            .filter_by(location_id=entity_id)\
            .all()
        return marshal(schemas.country_x_year, q)
    elif location_level == "country":
        q = CountryCountryYear.query\
            .filter_by(location_id=entity_id)\
            .all()
        return marshal(schemas.country_x_year, q)
    elif location_level == "municipality":
        q = CountryMunicipalityYear.query\
            .filter_by(location_id=entity_id)\
            .all()
        return marshal(schemas.country_x_year, q)
    else:
        msg = "Data doesn't exist at location level {}"\
            .format(location_level)
        abort(400, body=msg)
def eeey_location_products(entity_type, entity_id, buildingblock_level,
                           sub_id):

    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 == "municipality":
        q = db.session.query(*get_all_model_fields(CountryMunicipalityProductYear))\
            .filter_by(location_id=entity_id)\
            .filter_by(product_id=sub_id)\
            .all()
        return marshal(
            schemas.country_municipality_product_year,
            rectangularize([x._asdict() for x in q],
                           ["country_id", "product_id", "year"]))
    elif location_level == "department":
        q = db.session.query(*get_all_model_fields(CountryDepartmentProductYear))\
            .filter_by(location_id=entity_id)\
            .filter_by(product_id=sub_id)\
            .all()
        return marshal(
            schemas.country_department_product_year,
            rectangularize([x._asdict() for x in q],
                           ["country_id", "product_id", "year"]))

    else:
        msg = "Data doesn't exist at location level {}"\
            .format(location_level)
        abort(400, body=msg)
Beispiel #3
0
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_x_year, q)
    elif location_level == "msa":
        q = CountryMSAYear.query\
            .filter_by(location_id=entity_id)\
            .all()
        return marshal(schemas.country_x_year, q)
    elif location_level == "country":
        q = CountryCountryYear.query\
            .filter_by(location_id=entity_id)\
            .all()
        return marshal(schemas.country_x_year, q)
    elif location_level == "municipality":
        q = CountryMunicipalityYear.query\
            .filter_by(location_id=entity_id)\
            .all()
        return marshal(schemas.country_x_year, q)
    else:
        msg = "Data doesn't exist at location level {}"\
            .format(location_level)
        abort(400, body=msg)
Beispiel #4
0
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)
Beispiel #5
0
def eey_industry_occupations(entity_type, entity_id, buildingblock_level):

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

    q = OccupationIndustryYear.query\
        .filter_by(industry_id=entity_id)\
        .all()
    return marshal(schemas.occupation_year, q)
Beispiel #6
0
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":
        msg = "Data doesn't exist at building block level {}"\
            .format(buildingblock_level)
        abort(400, body=msg)

    q = OccupationIndustryYear.query\
        .filter_by(industry_id=entity_id)\
        .all()
    return marshal(schemas.occupation_year, q)
Beispiel #8
0
def eey_farmsize_locations(entity_type, entity_id, location_level):

    if location_level in farmsize_year_region_mapping:
        q = farmsize_year_region_mapping[location_level]["model"].query\
            .filter_by(farmsize_id=entity_id)\
            .all()
        schema = schemas.XFarmSizeYearSchema(many=True)
        return marshal(schema, q)
    else:
        msg = "Data doesn't exist at location level {}"\
            .format(location_level)
        abort(400, body=msg)
Beispiel #9
0
def eey_nonag_locations(entity_type, entity_id, location_level):

    if location_level in nonag_year_region_mapping:
        q = nonag_year_region_mapping[location_level]["model"].query\
            .filter_by(nonag_id=entity_id)\
            .all()
        schema = schemas.XNonagriculturalActivityYearSchema(many=True)
        return marshal(schema, q)
    else:
        msg = "Data doesn't exist at location level {}"\
            .format(location_level)
        abort(400, body=msg)
Beispiel #10
0
def eey_product_exporters(entity_type, entity_id, location_level):

    if location_level in product_year_region_mapping:
        q = product_year_region_mapping[location_level]["model"].query\
            .filter_by(product_id=entity_id)\
            .all()
        schema = schemas.XProductYearSchema(many=True)
        schema.context = {'id_field_name': location_level + '_id'}
        return marshal(schema, q)
    else:
        msg = "Data doesn't exist at location level {}"\
            .format(location_level)
        abort(400, body=msg)
def eey_product_exporters(entity_type, entity_id, location_level):

    if location_level in product_year_region_mapping:
        q = product_year_region_mapping[location_level]["model"].query\
            .filter_by(product_id=entity_id)\
            .all()
        schema = schemas.XProductYearSchema(many=True)
        schema.context = {'id_field_name': location_level + '_id'}
        return marshal(schema, q)
    else:
        msg = "Data doesn't exist at location level {}"\
            .format(location_level)
        abort(400, body=msg)
def hierarchy(entity_name):

    from_level = request.args.get("from_level", None)
    to_level = request.args.get("to_level", None)

    if entity_name == "products":
        if from_level == "4digit" and to_level == "section":
            p, p2, p3 = HSProduct, aliased(HSProduct), aliased(HSProduct)
            q = db.session.query(p.id, p3.id)\
                .join(p2, p2.id == p.parent_id)\
                .join(p3, p3.id == p2.parent_id)\
                .all()
            return jsonify(data=dict(q))
    elif entity_name == "industries":
        if from_level == "4digit" and to_level == "section":
            i, i2, i3 = Industry, aliased(Industry), aliased(Industry)
            q = db.session.query(i.id, i3.id)\
                .join(i2, i2.id == i.parent_id)\
                .join(i3, i3.id == i2.parent_id)\
                .all()
            return jsonify(data=dict(q))

    raise abort(400,
                body="""This API is still a fixture, try
                ?from_level=4digit&to_level=section.""")
Beispiel #13
0
def eey_location_farmsizes(entity_type, entity_id, buildingblock_level):

    location_level = lookup_classification_level("location", entity_id)

    if location_level in farmsize_year_region_mapping:
        query_model = farmsize_year_region_mapping[location_level]["model"]
        q = query_model.query\
            .filter_by(farmsize_level=buildingblock_level)\

        if hasattr(query_model, "location_id"):
            q = q.filter_by(location_id=entity_id)

        schema = schemas.XFarmSizeYearSchema(many=True)
        return marshal(schema, q)
    else:
        msg = "Data doesn't exist at location level {}"\
            .format(location_level)
        abort(400, body=msg)
Beispiel #14
0
def eey_location_products(entity_type, entity_id, buildingblock_level):

    location_level = lookup_classification_level("location", entity_id)

    if location_level in product_year_region_mapping:
        query_model = product_year_region_mapping[location_level]["model"]
        q = query_model.query\
            .filter_by(level=buildingblock_level)\

        if hasattr(query_model, "location_id"):
            q = q.filter_by(location_id=entity_id)

        schema = schemas.XProductYearSchema(many=True)
        schema.context = {'id_field_name': location_level + '_id'}
        return marshal(schema, q)
    else:
        msg = "Data doesn't exist at location level {}"\
            .format(location_level)
        abort(400, body=msg)
def get_or_fail(name, dictionary):
    """Lookup a key in a dict, abort with helpful error on failure."""
    thing = dictionary.get(name, None)
    if thing is None:
        msg = "{} is not valid. Try one of: {}"\
            .format(
                name,
                list(dictionary.keys()))
        raise abort(400, body=msg)
    return thing
def eey_location_products(entity_type, entity_id, buildingblock_level):

    location_level = lookup_classification_level("location", entity_id)

    if location_level in product_year_region_mapping:
        query_model = product_year_region_mapping[location_level]["model"]
        q = query_model.query\
            .filter_by(level=buildingblock_level)\

        if hasattr(query_model, "location_id"):
            q = q.filter_by(location_id=entity_id)

        schema = schemas.XProductYearSchema(many=True)
        schema.context = {'id_field_name': location_level + '_id'}
        return marshal(schema, q)
    else:
        msg = "Data doesn't exist at location level {}"\
            .format(location_level)
        abort(400, body=msg)
Beispiel #17
0
def get_or_fail(name, dictionary):
    """Lookup a key in a dict, abort with helpful error on failure."""
    thing = dictionary.get(name, None)
    if thing is None:
        msg = "{} is not valid. Try one of: {}"\
            .format(
                name,
                list(dictionary.keys()))
        raise abort(400, body=msg)
    return thing
Beispiel #18
0
def eeey_location_products(entity_type, entity_id, buildingblock_level,
                           sub_id):

    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 == "municipality":
        q = db.session.query(*get_all_model_fields(CountryMunicipalityProductYear))\
            .filter_by(location_id=entity_id)\
            .filter_by(product_id=sub_id)\
            .all()
        return marshal(schemas.country_municipality_product_year,
                       rectangularize([x._asdict() for x in q],
                                      ["country_id", "product_id", "year"]))
    elif location_level == "department":
        q = db.session.query(*get_all_model_fields(CountryDepartmentProductYear))\
            .filter_by(location_id=entity_id)\
            .filter_by(product_id=sub_id)\
            .all()
        return marshal(schemas.country_department_product_year,
                       rectangularize([x._asdict() for x in q],
                                      ["country_id", "product_id", "year"]))
    elif location_level == "msa":
        q = db.session.query(*get_all_model_fields(CountryMSAProductYear))\
            .filter_by(location_id=entity_id)\
            .filter_by(product_id=sub_id)\
            .all()
        return marshal(schemas.country_msa_product_year,
                       rectangularize([x._asdict() for x in q],
                                      ["country_id", "product_id", "year"]))
    else:
        msg = "Data doesn't exist at location level {}"\
            .format(location_level)
        abort(400, body=msg)
def hierarchy(entity_name):

    from_level = request.args.get("from_level", None)
    to_level = request.args.get("to_level", None)

    if entity_name == "products":
        if from_level == "4digit" and to_level == "section":
            p, p2, p3 = HSProduct, aliased(HSProduct), aliased(HSProduct)
            q = db.session.query(p.id, p3.id)\
                .join(p2, p2.id == p.parent_id)\
                .join(p3, p3.id == p2.parent_id)\
                .all()
            return jsonify(data=dict(q))
    elif entity_name == "industries":
        if from_level == "4digit" and to_level == "section":
            i, i2, i3 = Industry, aliased(Industry), aliased(Industry)
            q = db.session.query(i.id, i3.id)\
                .join(i2, i2.id == i.parent_id)\
                .join(i3, i3.id == i2.parent_id)\
                .all()
            return jsonify(data=dict(q))

    raise abort(400, body="""This API is still a fixture, try
                ?from_level=4digit&to_level=section.""")
Beispiel #20
0
def get_level():
    """Shortcut to get the ?level= query param"""
    level = request.args.get("level", None)
    if level is None:
        raise abort(400, body="Must specify ?level=")
    return level
def get_level():
    """Shortcut to get the ?level= query param"""
    level = request.args.get("level", None)
    if level is None:
        raise abort(400, body="Must specify ?level=")
    return level