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()
    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 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 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