Exemplo n.º 1
0
def post_weather(current_user):
    data = request.get_json()
    """ STRUCTURE:
	{
		"place": {
			"name": "Aarhus",
			"lat": 58.986462,
			"lon": 6.190466
		},
		"temp": 13.231,
		"humidity": 54,
		"press": 1030
	}"""

    try:
        new_place = Place(name=data['place']['name'],
                          lat=data['place']['lat'],
                          lon=data['place']['lon'])
        new_weather = WeatherData(place=new_place,
                                  temperature=data['temp'],
                                  humidity=data['humidity'],
                                  pressure=data['press'])
        weatherDict = new_weather.convertToDict()

        socketio.emit('new data', weatherDict)

        weatherCol.insert_one(weatherDict)

        return jsonify({"message": "Posted new weatherdata!"}), 200
    except:
        return jsonify({"message": "Error in posting weatherdata!"}), 400
Exemplo n.º 2
0
def index():
    if request.method == 'POST':
        new_temperature = request.form['temperature']
        new_humidity = request.form['humidity']
        new_pressure = request.form['pressure']

        try:
            newWeather = WeatherData(temperature=new_temperature,
                                     humidity=new_humidity,
                                     pressure=new_pressure)
            newWeatherDict = newWeather.convertToDict()

            mongoCol.insert_one(newWeatherDict)

            return redirect('/')
        except:
            return 'There was an issue adding your data'
    else:  #Get request
        cursor = mongoCol.find()  # make cursor with no sorting
        listOfWeatherData = list(
            cursor
        )  # return all the objects as a list and set the cursor to last index

        listOfWeatherData = convertObjectId(listOfWeatherData)  # See function

        return json.dumps(listOfWeatherData)