def auto_populate_station(): for location in latlngpairs: data = weather_utils.fetch_station(location.get("lat"), location.get("lng")) try: station = weather_models.WeatherStations.objects.get(requesturl=data["requesturl"]) except weather_models.WeatherStations.DoesNotExist: serializer = weather_serializer.WeatherStationSerializer(data=data) if serializer.is_valid(): serializer.save() print serializer.data.get("city") + " station added." else: print serializer.errors
def create(self, request): place = request.data.get("place") # get lat and lng from google geocoder api location, err = weather_utils.getLatlngFromPlace(place) if not location: return rest_response.Response(str(err), rest_status=400) # calling wunderground api to get station data data = weather_utils.fetch_station(location.get("lat"), location.get("lng")) try: station = weather_models.WeatherStations.objects.get(requesturl=data["requesturl"]) serializer = weather_serializer.WeatherStationSerializer(instance=station) except weather_models.WeatherStations.DoesNotExist: serializer = weather_serializer.WeatherStationSerializer(data=data) if not serializer.is_valid(): return rest_response.Response(serializer.errors, status=400) serializer.save() res_data = {"station": serializer.data} return rest_response.Response(res_data, status=rest_status.HTTP_201_CREATED)