Example #1
0
    def put(self, name):

        data = Country.parser.parse_args()
        today = str(date.today())
        country = CountryModel(name, today, **data)

        country.send_to_queue()

        return country.json()
 def get(self, country_code):
     country = CountryModel.find_by_code(country_code)
     if country:
         return country.json(), 200
     return {
         'message': "Country -'{}'- not found.".format(country_code)
     }, 404
def populate_country_table():
    # when working locally use ./raw_data for relative file path.
    # when running on remote server use abolute file path
    # with open('/var/www/html/items-rest/raw_data/country_codes.csv', 'r') as country_code_file:
    with open('./raw_data/country_codes.csv', 'r') as country_code_file:
        country_reader = csv.reader(country_code_file)
        for row in country_reader:
            new_country_object = CountryModel(*row)

            country = CountryModel.find_by_code(
                new_country_object.country_code)
            if country:
                continue
            else:
                if new_country_object.country_name == "name":
                    continue
                new_country_object.save_to_db()
 def get(self, search_term):
     countries = [
         country.json()
         for country in CountryModel.search_by_name(search_term)
     ]
     if countries:
         return {'countries': countries}, 200
     return {'message': "No countries match that search term"}, 404
 def delete(self, country_name):
     country = CountryModel.find_by_name(country_name)
     if country:
         country.delete_from_db()
         return {
             'message': "Country '{}' deleted".format(country_name)
         }, 200
     return {'message': "Country '{}' not found.".format(country_name)}, 404
Example #6
0
    def json(self):
        country = CountryModel.find_by_code(self.country_code)
        if country:
            country = country.json()

        return {
            'code': self.admin_code,
            'country': country,
            'description': self.description
        }
Example #7
0
    def delete(cls, name: str):
        # claims = get_current_user()
        # if not claims['is_admin']:
        #     return {'message': 'Admin privilege required.'}, 401

        item = CountryModel.find_by_name(name)
        if item:
            item.delete_from_db()

        return {'message': ITEM_DELETED.format(name)}
Example #8
0
 def get(self, name):
     cached_country = CountryCache(name)
     if cached_country.find_by_country():
         return cached_country.find_by_country()
     else:
         country = CountryModel.find_by_country(name)
         if country:
             cached_country.cache(country.json())
             return country.json()
         return {'message': 'Country not found'}, 404
    def put(self, name):

        data = Country.parser.parse_args()

        country = CountryModel.find_by_country(name)

        if country is None:
            country = CountryModel(name, **data)
        else:
            country.cases = data['cases']
            country.deaths = data['deaths']
            country.deaths = data['deaths']

        country.save_to_db()

        return country.json()
    def put(self, country_name):
        data = CountryName.parser.parse_args()
        country = CountryModel.find_by_name(country_name)

        if country:
            country.country_code = data['country_code']
        else:
            country = CountryModel(data['country_code'], country_name)

        country.save_to_db()
        return country.json()
Example #11
0
    def put(cls, name: str):
        item_json = request.get_json()
        item = CountryModel.find_by_name(name)

        if item:
            item.population = item_json['population']
            item.land_area = item_json['land_area']
            item.migrants = item_json['migrants']
            item.medium_age = item_json['medium_age']
            item.urban_pop = item_json['urban_pop']
        else:
            item_json['name'] = name
            item = item_schema.load(item_json)

        item.save_to_db()

        return item_schema.dump(item), 200
Example #12
0
    def post(cls, name):
        if RegionModel.find_by_name(name):
            return {'message': NAME_ALREADY_EXISTS.format(name)}, 400

        item_json = request.get_json()
        country = CountryModel.find_by_name(item_json['country_name'])

        item_json['name'] = name
        item_json['country_id'] = country.id
        item = item_schema.load(item_json)

        try:
            item.save_to_db()
        except:
            return {'message': ERROR_INSERTING}, 500

        return item_schema.dump(item), 201
Example #13
0
    def post(cls, name: str):
        if CountryModel.find_by_name(name):
            return {'message': NAME_ALREADY_EXISTS.format(name)}, 400

        try:
            data = request.get_json()
        except Exception:
            return {'message': 'Data is empty.'}
        data['name'] = name

        try:
            item = item_schema.load(data)
        except TypeError:
            return {'message': 'Data is not correct.'}

        try:
            item.save_to_db()
        except:
            return {'message': ERROR_INSERTING}, 500

        return item_schema.dump(item), 201
    def post(self, name):
        if CountryModel.find_by_country(name):
            return {
                "message":
                "An country with the name '{}' already exist.".format(name)
            }, 400

        data = Country.parser.parse_args()

        country = CountryModel(name, **data)
        try:
            country.save_to_db()
        except:
            return {
                "message": "An error occured while inserting the country."
            }, 500

        return country.json(), 201
    def post(self, country_name):
        if CountryModel.find_by_name(country_name):
            return {
                'message':
                "A country with name '{}' already exists.".format(country_name)
            }, 400

        data = CountryName.parser.parse_args()

        if CountryModel.find_by_code(data['country_code']):
            return {
                'message':
                "A country with code '{}' already exists.".format(
                    data['country_code'])
            }, 400

        country = CountryModel(data['country_code'], country_name)

        try:
            country.save_to_db()
        except:
            return {'message': "An error occurred inserting the country."}, 500
        return country.json(), 201
 def get(self):
     countries = [country.json() for country in CountryModel.find_all()]
     return {'countries': countries}, 200
    def get(self):

        query = {}

        country_code = request.args.get('country', type=str)
        if country_code:
            if not valid_country_code(country_code):
                return {
                    'message':
                    '{} is not a valid country_code'.format(country_code)
                }, 400
            query['country_code'] = country_code

        year = request.args.get('year', type=str)
        if year:
            if not valid_year(year):
                return {'message': 'Please enter a valid year'}, 400
            query['year'] = year

        sex = request.args.get('sex', type=str)
        if sex:
            if not valid_sex(sex):
                return {'message': 'Please enter a valid sex code'}, 400
            query['sex'] = sex

        admin = request.args.get('admin', type=str)
        if admin:
            if not valid_admin(admin):
                return {'message': 'Please enter a valid admin code'}, 400
            query['admin_code'] = admin

        subdiv = request.args.get('subdiv', type=str)
        if subdiv:
            if not valid_subdiv(subdiv):
                return {'message': 'Please enter a valid subdiv code'}, 400
            query['subdiv_code'] = subdiv

        cause = request.args.get('cause', type=str)
        if cause:
            cause_upper = cause.upper()
            if not valid_cause(cause_upper):
                return {'message': 'Please enter a valid cause code'}, 400
            cause_code_list_extended = {}

            icd_codes = [
                code.json() for code in IcdModel.find_by_code(cause_upper)
            ]
            for icd_code in icd_codes:

                matching_codes = [
                    matching_icd_code.json() for matching_icd_code in
                    IcdModel.search_specific(icd_code['description'])
                ]
            unspecified_codes = []
            generic_codes = []

            pattern = "unspecified"
            match = re.search(pattern, icd_code['description'])
            if not match:
                unspecified_code = icd_code['description'] + \
                    " (unspecified)"
                unspecified_codes = [
                    matching_icd_code.json() for matching_icd_code in
                    IcdModel.search_specific(unspecified_code)
                ]
            else:

                position = match.span()
                start = position[0] - 2
                end = position[1] + 1

                generic_code = icd_code['description'][0:start] + \
                    icd_code['description'][end:]
                generic_codes = [
                    matching_icd_code.json() for matching_icd_code in
                    IcdModel.search_specific(generic_code)
                ]

            for matching_code in matching_codes:
                cause_code_list_extended[
                    matching_code['list']] = matching_code['code']

            if generic_codes:
                for matching_code in generic_codes:
                    cause_code_list_extended[
                        matching_code['list']] = matching_code['code']
            if unspecified_codes:
                for matching_code in unspecified_codes:
                    cause_code_list_extended[
                        matching_code['list']] = matching_code['code']

            if "103" not in cause_code_list_extended:
                try:
                    cause_code_list_extended['103'] = cause_code_list_extended[
                        '104'][:-1]
                except:
                    pass

            code_list_entry = CodeListRefModel.find_by_year_and_country(
                year, country_code)

            try:
                cause = cause_code_list_extended[code_list_entry.code_list]
            except:
                return {
                    'message':
                    "Can't find matching cause code for the specified country."
                }, 400

            query['cause'] = cause

        results = [
            entry.json()
            for entry in MortalityDataModel.search_mortalities(query)
        ]

        if code_list_entry.code_list == "104" and len(cause_code_list_extended[
                code_list_entry.code_list]) == 3 and len(results) == 0:
            cause = cause + "9"
            query['cause'] = cause
            results = [
                entry.json()
                for entry in MortalityDataModel.search_mortalities(query)
            ]

        if code_list_entry.code_list == "10M" and len(cause_code_list_extended[
                code_list_entry.code_list]) == 3 and len(results) == 0:
            cause = cause + "9"
            query['cause'] = cause
            result = [
                entry.json()
                for entry in MortalityDataModel.search_mortalities(query)
            ]

        if code_list_entry.code_list == "10M" and len(cause_code_list_extended[
                code_list_entry.code_list]) == 4 and len(results) == 0:
            cause = cause[:-1]
            query['cause'] = cause
            result = [
                entry.json()
                for entry in MortalityDataModel.search_mortalities(query)
            ]

        if not results:
            return {'message': "No mortality entries match your query."}, 404

        if results:
            if len(results) > 1:
                return {
                    'message':
                    "More than one population entry was found matching your query."
                }, 400

            for entry in results:
                pop_query = {}
                pop_query['country_code'] = entry['country']['code']
                pop_query['year'] = entry['year']
                pop_query['sex'] = entry['sex']['code']

                admin = entry['admin']
                if admin != "None":
                    pop_query['admin'] = admin['code']

                subdiv = entry['subdiv']
                if subdiv != "None":
                    pop_query['subdiv'] = subdiv['code']
                pop_data = PopulationModel.search_single_population(pop_query)
                if pop_data:
                    pop_data = pop_data.json()
                else:
                    country = CountryModel.find_by_code(country_code).json()
                    return {
                        'message':
                        "No population data available for '{}' in year {} for sex '{}'."
                        .format(country['description'], year, sex)
                    }, 404

                del entry['infant_age_breakdown']
                del entry['age_format']
                del entry['infant_age_format']

                things_to_skip = [
                    "country", "admin", "subdiv", "year", "code_list", "cause",
                    "sex", "infant_age_breakdown"
                ]
                for key, value in entry.items():
                    if key in things_to_skip:
                        continue

                    # format age_breakdown data per 100,000
                    if key == "age_breakdown":
                        ages_to_delete = []
                        for age_range, value in entry[key].items():
                            pop = pop_data[key].get(age_range)
                            entry[key][age_range] = pop
                            if pop:
                                if pop == "0":
                                    continue
                                entry[key][age_range] = str(
                                    round_up(
                                        float(value) / float(pop) * 100000, 3))
                            if pop == None:
                                ages_to_delete.append(age_range)
                        for index in ages_to_delete:
                            del entry[key][index]
                    else:
                        entry[key] = str(
                            round_up(
                                float(value) / float(pop_data[key]) * 100000,
                                3))

            return {'adjusted_entries': results}, 200
    def get(self):
        def strip_whitespace(string):
            return ('').join(string.split(' '))

        country_code_input = request.args.get('country', type=str)
        year_input = request.args.get('year', type=str)
        sex_code_input = request.args.get('sex', type=str)
        cause_code_input = request.args.get('cause', type=str)
        admin_code_input = request.args.get('admin', type=str)
        subdiv_code_input = request.args.get('subdiv', type=str)

        if not country_code_input or not year_input or not sex_code_input or not cause_code_input:
            return {
                'message':
                "Please add at least a year, country, sex and cause variable"
            }, 400

        country_code_list = strip_whitespace(country_code_input).split(',')
        year_list = strip_whitespace(year_input).split(',')
        sex_code_list = strip_whitespace(sex_code_input).split(',')
        cause_code_list = strip_whitespace(cause_code_input).split(',')
        cause_code_list = list(map(lambda x: x.upper(), cause_code_list))

        cause_code_list_extended = {}
        for icd_code in filter(valid_cause, cause_code_list):

            complete_icd_codes = [
                code.json() for code in IcdModel.find_by_code(icd_code)
            ]

            for icd_code in complete_icd_codes:
                matching_codes = [
                    matching_icd_code.json() for matching_icd_code in
                    IcdModel.search_specific(icd_code['description'])
                ]
                # try to make it possible to compare countries that use different code lists
                # check if contains "unspecified". if not, add it and search again. else remove "(unspecified)"
                # TODO: in case that unspecified doesn't match. sum up all instances of code + extra digit.
                # e.g. A02 input by user. if code list is 104, sum all A020, A021, ...A029. return sum
                # TODO: if user input is 4 digit cause code, remove last digit and search for 103 code list with that
                unspecified_codes = []
                generic_codes = []

                pattern = "unspecified"
                match = re.search(pattern, icd_code['description'])
                if not match:
                    unspecified_code = icd_code['description'] + \
                        " (unspecified)"
                    unspecified_codes = [
                        matching_icd_code.json() for matching_icd_code in
                        IcdModel.search_specific(unspecified_code)
                    ]
                else:

                    position = match.span()
                    start = position[0] - 2
                    end = position[1] + 1

                    generic_code = icd_code['description'][0:start] + \
                        icd_code['description'][end:]
                    generic_codes = [
                        matching_icd_code.json() for matching_icd_code in
                        IcdModel.search_specific(generic_code)
                    ]

                for matching_code in matching_codes:
                    cause_code_list_extended[
                        matching_code['list']] = matching_code['code']

                if generic_codes:
                    for matching_code in generic_codes:
                        cause_code_list_extended[
                            matching_code['list']] = matching_code['code']
                if unspecified_codes:
                    for matching_code in unspecified_codes:
                        cause_code_list_extended[
                            matching_code['list']] = matching_code['code']

                if "103" not in cause_code_list_extended:
                    try:
                        cause_code_list_extended[
                            '103'] = cause_code_list_extended['104'][:-1]
                    except:
                        pass

        # add "" to admin and subdiv lists to ensure some results if no specific code given
        admin_code_list = []
        if admin_code_input:
            admin_code_list = strip_whitespace(admin_code_input).split(',')
        admin_code_list.append("")

        subdiv_code_list = []
        if subdiv_code_input:
            subdiv_code_list = strip_whitespace(subdiv_code_input).split(',')
        subdiv_code_list.append("")

        # results of each mortality adj search
        results = []
        # loop over all permutations of list items and validate codes.
        for country_code in filter(valid_country_code, country_code_list):
            for year in filter(valid_year, year_list):
                for sex in filter(valid_sex, sex_code_list):
                    for cause in filter(valid_cause, cause_code_list):
                        for admin in filter(valid_admin, admin_code_list):
                            for subdiv in filter(valid_subdiv,
                                                 subdiv_code_list):

                                # generate query
                                code_list_entry = CodeListRefModel.find_by_year_and_country(
                                    year, country_code)

                                try:
                                    cause = cause_code_list_extended[
                                        code_list_entry.code_list]
                                except:
                                    continue

                                query = {}
                                query['country_code'] = country_code
                                query['sex'] = sex
                                query['year'] = year
                                query['cause'] = cause
                                query['admin_code'] = admin
                                query['subdiv_code'] = subdiv

                                result = [
                                    entry.json()
                                    for entry in MortalityDataModel.
                                    search_mortalities(query)
                                ]

                                if code_list_entry.code_list == "104" and len(
                                        cause_code_list_extended[
                                            code_list_entry.code_list]
                                ) == 3 and len(result) == 0:
                                    cause = cause + "9"
                                    query['cause'] = cause
                                    result = [
                                        entry.json()
                                        for entry in MortalityDataModel.
                                        search_mortalities(query)
                                    ]

                                if code_list_entry.code_list == "10M" and len(
                                        cause_code_list_extended[
                                            code_list_entry.code_list]
                                ) == 3 and len(result) == 0:
                                    cause = cause + "9"
                                    query['cause'] = cause
                                    result = [
                                        entry.json()
                                        for entry in MortalityDataModel.
                                        search_mortalities(query)
                                    ]

                                if code_list_entry.code_list == "10M" and len(
                                        cause_code_list_extended[
                                            code_list_entry.code_list]
                                ) == 4 and len(result) == 0:
                                    cause = cause[:-1]
                                    query['cause'] = cause
                                    result = [
                                        entry.json()
                                        for entry in MortalityDataModel.
                                        search_mortalities(query)
                                    ]

                                if result:
                                    if len(result) > 1:
                                        continue

                                # adjust for population and add to results list
                                for entry in result:
                                    pop_query = {}
                                    pop_query['country_code'] = entry[
                                        'country']['code']
                                    pop_query['year'] = entry['year']
                                    pop_query['sex'] = entry['sex']['code']

                                    admin = entry['admin']
                                    if admin != "None":
                                        pop_query['admin'] = admin['code']

                                    subdiv = entry['subdiv']
                                    if subdiv != "None":
                                        pop_query['subdiv'] = subdiv['code']

                                    pop_data = PopulationModel.search_single_population(
                                        pop_query)
                                    if pop_data:
                                        pop_data = pop_data.json()
                                    else:
                                        country = CountryModel.find_by_code(
                                            country_code).json()

                                        del entry['infant_age_breakdown']
                                        del entry['age_format']
                                        del entry['infant_age_format']
                                        del entry['age_breakdown']
                                        entry[
                                            'all_ages'] = "No population data."
                                        continue

                                    # remove infant mortality data as no related population data given
                                    del entry['infant_age_breakdown']
                                    # delete age format numbers as now irrelevant
                                    del entry['age_format']
                                    del entry['infant_age_format']

                                    # divide mortality data by corresponding population number and multiply by 100,000
                                    # population is population of that specific age/sex. not total country population
                                    things_to_skip = [
                                        "country", "admin", "subdiv", "year",
                                        "code_list", "cause", "sex",
                                        "infant_age_breakdown"
                                    ]
                                    for key, value in entry.items():
                                        if key in things_to_skip:
                                            continue

                                        # format age_breakdown data per 100,000
                                        if key == "age_breakdown":
                                            ages_to_delete = []
                                            for age_range, value in entry[
                                                    key].items():
                                                pop = pop_data[key].get(
                                                    age_range)
                                                entry[key][age_range] = pop
                                                if pop:
                                                    if pop == "0":
                                                        continue
                                                    entry[key][
                                                        age_range] = str(
                                                            round_up(
                                                                float(value) /
                                                                float(pop) *
                                                                100000, 3))
                                                if pop == None:
                                                    ages_to_delete.append(
                                                        age_range)
                                            for index in ages_to_delete:
                                                del entry[key][index]
                                        else:
                                            entry[key] = str(
                                                round_up(
                                                    float(value) /
                                                    float(pop_data[key]) *
                                                    100000, 3))

                                # append to results list if anything found
                                if result:
                                    results.append(result[0])

        if len(results) == 0:
            return {'message': "No mortality entries match your query."}, 404

        return {'results': results}, 200
Example #19
0
def valid_country_code(country_code):
    if CountryModel.find_by_code(country_code):
        return True
    return False
Example #20
0
    def json(self):
        admin = AdminModel.find_by_code_and_country(self.admin,
                                                    self.country_code)
        if admin:
            admin = {
                'code': admin.admin_code,
                'description': admin.description
            }
        else:
            admin = 'None'

        subdiv = SubdivModel.find_by_code(self.subdiv)
        if subdiv:
            subdiv = {
                'code': subdiv.subdiv_code,
                'description': subdiv.description
            }
        else:
            subdiv = 'None'

        sex = SexModel.find_by_code(self.sex)
        if sex:
            sex = sex.json()
        else:
            sex = "Sex code '{}' not found.".format(self.sex)

        country = CountryModel.find_by_code(self.country_code)
        if country:
            country = country.json()
        else:
            country = "Country with code '{}' not found.".format(
                self.country_code)

        # sort the age data according to the given age format for the population entry
        age_breakdown_format = AgeFormatModel.find_by_code(
            self.age_format).json()
        pops = [
            self.pop2, self.pop3, self.pop4, self.pop5, self.pop6, self.pop7,
            self.pop8, self.pop9, self.pop10, self.pop11, self.pop12,
            self.pop13, self.pop14, self.pop15, self.pop16, self.pop17,
            self.pop18, self.pop19, self.pop20, self.pop21, self.pop22,
            self.pop23, self.pop24, self.pop25, self.pop26
        ]
        age_breakdown = {}
        count = 0
        for pop, age_range in age_breakdown_format.items():
            if pop == "age_format_code":
                continue
            age_breakdown[age_range] = pops[count]
            count += 1
        empties = []
        for age_range, value in age_breakdown.items():
            if value == "":
                empties.append(age_range)
        for index in empties:
            del age_breakdown[index]

        return {
            'id': self.id,
            'country': country,
            'admin': admin,
            'subdiv': subdiv,
            'year': self.year,
            'sex': sex,
            'age_format': self.age_format,
            'all_ages': self.pop1,
            'age_breakdown': age_breakdown,
            'live_births': self.live_births
        }
 def delete(self, name):
     country = CountryModel.find_by_country(name)
     if country:
         country.delete_from_db()
         return {"message": "Country '{}' deleted".format(name)}, 200
     return {"message": "Country '{}' not found".format(name)}, 404
 def get(self, name):
     country = CountryModel.find_by_country(name)
     if country:
         return country.json()
     return {'message': 'Country not found'}, 404
Example #23
0
 def get(cls):
     items = item_list_schema.dump(CountryModel.find_all())
     if items:
         return {'countries': items}, 200
     return {'message': 'Objects list is empty.'}, 200
Example #24
0
 def get(cls, name: str):
     item = CountryModel.find_by_name(name)
     if item:
         return item_schema.dump(item), 200
     return {'message': ITEM_NOT_FOUND}, 404