def populate_admin_table():
    for row in admin_codes:
        new_admin_model = AdminModel(*row)
        if AdminModel.find_by_code_and_country(new_admin_model.admin_code,
                                               new_admin_model.country_code):
            continue
        new_admin_model.save_to_db()
Example #2
0
 def get(self, admin_code, country_code):
     admin = AdminModel.find_by_code_and_country(admin_code, country_code)
     if admin:
         return admin.json()
     return {
         'message':
         "No admin entry found for code {} and country {}.".format(
             admin_code, country_code)
     }, 404
Example #3
0
    def put(self, admin_code, country_code):
        data = Admin.parser.parse_args()

        entry = AdminModel.find_by_code_and_country(admin_code, country_code)

        if entry:
            entry.description = data['description']
        else:
            entry = AdminModel(admin_code, country_code, data['description'])
        try:
            entry.save_to_db()
        except:
            {'message': 'Something went wrong inserting the admin.'}, 500
        return entry.json(), 201
Example #4
0
    def delete(self, admin_code, country_code):

        entry = AdminModel.find_by_code_and_country(admin_code, country_code)

        if entry:
            entry.delete_from_db()
            return {
                'message':
                "Admin code '{}' for country code '{}' deleted.".format(
                    admin_code, country_code)
            }, 200
        return {
            'message':
            "Admin code '{}' not found for country with code '{}'.".format(
                admin_code, country_code)
        }, 404
Example #5
0
    def post(self, admin_code, country_code):
        data = Admin.parser.parse_args()

        if AdminModel.find_by_code_and_country(admin_code, country_code):
            return {
                'message':
                "A '{}' admin code already exists for country with code '{}'.".
                format(admin_code, country_code)
            }, 400

        entry = AdminModel(admin_code, country_code, data['description'])
        try:
            entry.save_to_db()
        except:
            {'message': 'Something went wrong inserting the admin.'}, 500
        return entry.json(), 201
Example #6
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
        }