Ejemplo n.º 1
0
    def save_forecasts(self, forecasts):
        weather_forecasts = []
        for forecast in forecasts:
            date = datetime.now() + timedelta(
                days=int(forecast['DaySequence']))
            forecast_date = date.date()
            day_forecast = self.get_day_forecast(forecast_date)
            if not day_forecast:
                weather_forecasts.append(
                    Forecast(
                        **{
                            'date': forecast_date,
                            'wind': forecast['WindSpeed'],
                            'rain': forecast['Rainfall'],
                            'maximum_temperature': forecast['HighTemp'],
                            'minimum_temperature': forecast['LowTemp']
                        }))
            else:
                day_forecast.wind = forecast['WindSpeed']
                day_forecast.rain = forecast['Rainfall']
                day_forecast.maximum_temperature = forecast['HighTemp']
                day_forecast.minimum_temperature = forecast['LowTemp']
                day_forecast.save()

        Forecast.objects.bulk_create(weather_forecasts)
Ejemplo n.º 2
0
def update_forecast():
    json = _get_forecast_json()
    if json is not None:
        try:
            new_forecast = Forecast()
            temp_in_celsius = json['main']['temp'] - 273.15
            new_forecast.temperatue = temp_in_celsius
            new_forecast.description = json['weather'][0]['description']
            new_forecast.city = json['name']
            new_forecast.save()
        except:
            pass
Ejemplo n.º 3
0
    def handle(self, *args, **options):
        for file_path in options['file_path']:
            with open(file_path, 'r') as weather_file:
                json_data = json.load(weather_file)
                try:
                    city_forecast = OpenWeatherMap.process_city_forecasts(
                        json_data)
                except OpenWeatherException as import_error:
                    raise CommandError(import_error.message)

                # Pops the forecasts to make is easier to create new city
                forecasts = city_forecast.pop('forecasts')

                # Get or create City
                try:
                    city_record, created = City.objects.get_or_create(
                        **city_forecast)
                except MultipleObjectsReturned:
                    # Gets only one city and warns the user
                    city_record = City.objects.filter(
                        name=city_forecast.get('name'),
                        country_code=city_forecast.get('country_code'))[0]
                    self.stderr.write(
                        self.style.WARNING('Multiple records '
                                           'returned for %s' %
                                           city_forecast.get('name')))

                # Update of create forecasts for the city
                for forecast in forecasts:
                    try:
                        obj = Forecast.objects.get(city=city_record,
                                                   date=forecast.get('date'),
                                                   time=forecast.get('time'))
                        for key, value in forecast.items():
                            setattr(obj, key, value)
                        obj.save()
                    except Forecast.DoesNotExist:
                        obj = Forecast(city=city_record, **forecast)
                        obj.save()

                self.stdout.write(
                    self.style.SUCCESS('Successfully imported '
                                       'data of "%s"' % file_path))