Exemplo n.º 1
0
    def post(self, sex):
        data = SexDesc.parser.parse_args()
        if SexModel.find_by_name(sex):
            return {'message': "Sex '{}' already exists.".format(sex)}, 400

        entry = SexModel(sex, data['sex_code'])

        try:
            entry.save_to_db()
        except:
            return {'message': "An error occurred inserting the sex."}, 500
        return entry.json(), 201
Exemplo n.º 2
0
    def put(self, sex):
        data = SexDesc.parser.parse_args()
        entry = SexModel.find_by_name(sex)

        if entry:
            entry.sex_code = data['sex_code']
        else:
            entry = SexModel(sex, data['sex_code'])

        try:
            entry.save_to_db()
        except:
            return {'message': "An error occurred inserting the sex."}, 500
        return entry.json(), 201
def populate_sex_table():
    for sex, sex_code in sex_codes.items():
        if SexModel.find_by_name(sex):
            continue
        new_sex_object = SexModel(sex, sex_code)
        new_sex_object.save_to_db()
Exemplo n.º 4
0
 def get(self, sex_code):
     sex_entry = SexModel.find_by_code(sex_code)
     if sex_entry:
         return sex_entry.json()
     return {'message': "Sex not found."}, 404
Exemplo n.º 5
0
 def get(self):
     sexes = [sex.json() for sex in SexModel.find_all()]
     return {'sexes': sexes}, 200
Exemplo n.º 6
0
 def delete(self, sex):
     entry = SexModel.find_by_name(sex)
     if entry:
         entry.delete_from_db()
         return {'message': "Sex '{}' deleted".format(sex)}, 200
     return {'message': "Sex '{}' not found.".format(sex)}, 404
Exemplo n.º 7
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
        }