示例#1
0
    def delete(self):
        data = City.parser.parse_args()
        city = CityModel.find_by_city_name(data['city_name'])
        if city:
            city.delete_from_db()

        return {'message': 'City deleted'}
示例#2
0
    def get(self):
        city_name = request.args.get('city')
        date = request.args.get('date')

        # Response if no filter is provided
        if date is None and city_name is None:
            return [self.get_weather(city.name)for city in CityModel.query.all()], 200

        if city_name is None:
            return {'message' : 'Please enter a city name to get weather information'}, 400

        city = CityModel.find_by_name(city_name.lower())

        # Check if provided city exists in the database or not
        if city:
            # If city exists check if date is provided or not
            if date:
                input_date = parse(date, parserinfo(dayfirst = True)).replace(hour = 0, minute = 0, second = 0, microsecond = 0)
                # Check if weather information for a particular date is available or not
                weather_obj = WeatherModel.find_by_date(city.name, input_date)
                if weather_obj:
                    return weather_obj.json(), 200
                # If absent, fetch the data for the city from the API
                else:
                    new_forecast = forecast(darksky_key, city.latitude, city.longitude, time = input_date.isoformat())
                    return {'summary' : new_forecast.summary, 'temperature' : new_forecast.temperature, 'humidity' : new_forecast.humidity}, 200
            else:
                return self.get_weather(city.name), 200
        
        return {'message' : 'Requested city is not present in the database'}, 400
def index_post():
    new_city = request.form['city_name']

    # prevent blank
    if new_city:
        city_exists = CityModel.check_city_exists(new_city)

        if not city_exists:
            new_city_obj = CityModel.check_city_valid(new_city)
            if new_city_obj:
                new_city_obj.save_to_db()
            else:
                flash("This city does not exist!", "error")
        else:
            flash("This city is already shown!", "error")

        flash("City has been successfully added!", "info")

    return redirect(url_for('weather.index_get'))
示例#4
0
    def delete(self, name):
        city = CityModel.find_by_name(name)

        if city:
            weather_list = WeatherModel.find_by_name(city.name)
            for obj in weather_list:
                obj.delete_from_db()

            city.delete_from_db()
            return {'message': 'Deleted successfully'}, 200

        return {'message': 'Requested city was not found'}, 400
示例#5
0
    def post(self):
        data = City.parser.parse_args()
        xapi_key = request.headers.get('X-Api-Key')
        if CityModel.find_by_city_name(data['city_name']):
            return {
                'message':
                "An city with this name '{}' already exists.".format(
                    data['city_name'])}, 400

        weather_map = city_weather(xapi_key, data['city_name'])
        if weather_map['cod'] != 200 or weather_map['name'] != data['city_name']:
            return {
                'message':
                "This city '{}' does not exist in Api.".format(
                    data['city_name'])}, 400

        city = CityModel(data['city_name'])

        try:
            city.save_to_db()
        except:
            return {
                "message": "An error occurred inserting the city."}, 500

        return city.json(), 201
    def get(self, city_name):
        try:
            city_list = CityModel.find_by_city(city_name)

            if city_list:
                return {'results': city_list}, 200

            else:
                return '{} is an invalid city name'.format(city_name), 404
        except:
            return {
                "message": "An error occurred retrieving the team list."
            }, 500  # internal server error
示例#7
0
    def put(cls, name):
        item_json = request.get_json()
        item = CityModel.find_by_name(name)

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

        item.save_to_db()

        return item_schema.dump(item), 200
示例#8
0
    def post(cls, name):
        if CityModel.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
示例#9
0
    def post(self):
        request_data = request.get_json(silent=True)

        if request_data is None:
            return {'message': 'Please add a city name before continuing'}, 400
        name = request_data.get('name')

        # Check if city already exists
        if CityModel.find_by_name(name.lower()):
            return {
                'message': 'A city with name {} already exists'.format(name)
            }, 400

        # Fetch latitude and longitude from GMaps
        gmaps = googlemaps.Client(key=google_key)
        latitude = gmaps.geocode(name)[0]['geometry']['location']['lat']
        longitude = gmaps.geocode(name)[0]['geometry']['location']['lng']

        todays_datetime = datetime.now()

        # Add last 5 day's information
        for day in range(1, 6):
            past_date = todays_datetime.replace(
                hour=0, minute=0, second=0,
                microsecond=0) - timedelta(days=day)
            weather_obj = forecast(darksky_key,
                                   latitude,
                                   longitude,
                                   time=past_date.isoformat())
            weather = WeatherModel(weather_obj.summary,
                                   weather_obj.temperature,
                                   weather_obj.humidity, name, past_date)
            weather.save_to_db()

        try:
            city = CityModel(name.lower(), latitude, longitude)
            city.save_to_db()
        except:
            return {'message': 'Some error occured while saving'}, 500

        return city.json(), 201
def delete_city(name):
    CityModel.req_delete(name)
    return redirect(url_for('weather.index_get'))
示例#11
0
 def get(self):
     citys = City.get_all_dicts(self.db)
     self.set_header("Cache-Control", "max-age=3600, must-revalidate")
     self.finish_json(result=dict(
             citys=citys,
         ))
 def get(self):
     return CityModel.get_all_cities_list()
示例#13
0
 def get(cls):
     items = item_list_schema.dump(CityModel.find_all())
     if items:
         return {'cities': items}, 200
     return {'message': 'Objects list is empty.'}, 200
示例#14
0
    def delete(cls, name):
        item = CityModel.find_by_name(name)
        if item:
            item.delete_from_db()

        return {'message': ITEM_DELETED.format(name)}
示例#15
0
 def get(cls, name):
     item = CityModel.find_by_name(name)
     if item:
         return item_schema.dump(item), 200
     return {'message': ITEM_NOT_FOUND}, 404