Exemple #1
0
 def test_09_country_new(self):
     new_country = Country(
         iso_code=self.country['iso_code'],
         iso_code_long=self.country['iso_code_long'],
         short_name=self.country['short_name'],
         formal_name=self.country['formal_name'],
         demonym=self.country['demonym'],
         country_code=self.country['country_code'],
         continental_code=self.country['continental_code'],
         coordinates=self.country['coordinates'],
         elevation=self.country['elevation'],
         elevation_low=self.country['elevation_low'],
         area=self.country['area'],
         land=self.country['land'],
         fertility=self.country['fertility'],
         population=self.country['population'],
         population_urban=self.country['population_urban'],
         birth=self.country['birth'],
         death=self.country['death'],
         itu=self.country['itu'],
         web=self.country['web'],
         gis=self.country['gis'],
         statistics=self.country['statistics'],
         flag=self.country['flag'],
         government=self.country['government'],
         boundary_box=self.country['boundary_box'],
         currency=self.country['currency']
     )
     new_country.save()
     self.assertEqual(new_country.iso_code, self.country['iso_code'], "New Countru ISO Code")
Exemple #2
0
    def post(self):
        """
        POST Method for Country. Login required

        :return: JSON response
        """
        errors = []
        try:
            self.validate_fields(Country.required_fields(), request.form)
        except ValueError:
            return self.response(400, 'Required fields: ' + ' '.join(Country.required_fields()))

        params = self.get_form_values(Country.get_columns(), request.form)

        validate = Country.query.filter_by(iso_code=params['iso_code']).first()
        if validate:
            errors.append("The iso_code already exists")

        if params['continental_code']:
            validate = Continent.query.filter_by(code=params['continental_code']).first()
            if validate is None:
                errors.append("The continental_code doesn't exists")

        if errors:
            return self.response(400, errors)

        new_country = Country(
            iso_code=params['iso_code'],
            iso_code_long=params['iso_code_long'],
            short_name=params['short_name'],
            formal_name=params['formal_name'],
            demonym=params['demonym'],
            country_code=params['country_code'],
            continental_code=params['continental_code'],
            coordinates=params['coordinates'],
            elevation=params['elevation'],
            elevation_low=params['elevation_low'],
            area=params['area'],
            land=params['land'],
            fertility=params['fertility'],
            death=params['death'],
            birth=params['birth'],
            population=params['population'],
            population_urban=params['population_urban'],
            itu=params['itu'],
            web=params['web'],
            gis=params['gis'],
            statistics=params['statistics'],
            flag=params['flag'],
            government=params['government'],
            boundary_box=params['boundary_box'],
            currency=params['currency'],
        )
        new_country.save()

        return self.response(201)
 def get_add_new_country(current_user, user_country):
     if request.method == 'GET':
         countries = Country.query.all()
         return make_response(
             jsonify({"countries": countries_schema.dump(countries)})), 200
     if request.method == 'POST':
         name = request.data.get('name', '')
         code = request.data.get('code', '')
         if name and code:
             new_country = Country(name, code)
             new_country.save()
             return make_response(
                 jsonify({
                     "message": "Country successfully saved!",
                     "country": country_schema.dump(new_country)
                 })), 201
         else:
             return make_response(
                 jsonify({"message": "Add a country name and code"})), 400