def render(self, context):
     try:
         request = context['request']
         zip_code = get_current_site(request).profile.zip_code
     except (KeyError, AttributeError):
         zip_code = settings.DEFAULT_ZIP_CODE
     try:
         (location, conditions, hourly_forecasts, daily_forecasts, alerts) = get_intellicast_data(zip_code)
         (twelve_hour, twentyfour_hour, thirtysix_hour) = thirtysix_hour_outlook(daily_forecasts)
         if not conditions:
             return ''
         city_name = location['city'] + ',' + location['state']
         recent_precip = conditions['SixHrPrecip']
     except:
         if settings.DEBUG:
             raise
         return ''
         
     conditions_badge = {
         'city_name': city_name, 
         'zipcode': zip_code,
         'recent_precip': recent_precip,
         'current_temp': conditions['TempF'], 
         'icon_code': conditions['IconCode'],
         'feels_like': conditions['FeelsLikeF'],
         'wind_direction': conditions['WndDirCardinal'],
         'wind_speed': conditions['WndSpdMph'],
         'sky': conditions['Sky'],
         'twelve_hour': twelve_hour,
         'twentyfour_hour': twentyfour_hour,
         'thirtysix_hour': thirtysix_hour
     }
     context[self.var_name] = conditions_badge
     return ''
Пример #2
0
def weather_page(request):
    """
    Main weather landing page.  Features current conditions, a 36 hour forecast,
    and an interactive map (sadly, map is flash.)
    """
    rloc = _get_request_location(request)
    geo_form = GeolocationForm(initial={"geo": rloc.zip_code})
    (location, conditions, hourly_forecasts, daily_forecasts, alerts) = get_intellicast_data(rloc.zip_code)

    if not hourly_forecasts or not conditions or not daily_forecasts:
        return render(request, "intellicast/weather.html", {"unavailable": True})

    hourly_forecast_items = []
    for i in range(1, 24):
        forecast_dict = hourly_forecasts[str(i)]
        forecast_clean = create_forecast_dict("Hourly", forecast_dict)
        hourly_forecast_items.append(forecast_clean)

    daily_forecast_items = []
    for i in range(2, 9):
        forecast_dict = daily_forecasts[str(i)]
        forecast_clean = forecast_clean = create_forecast_dict("Daily", forecast_dict)
        daily_forecast_items.append(forecast_clean)
    try:
        alert_items = []
        for i, item in enumerate(alerts, 1):
            alerts_dict = alerts[i]
            alert_item = {
                "headline": alerts_dict["Headline"],
                "starttime": alerts_dict["StartTime"],
                "endtime": alerts_dict["EndTime"],
                "urgency": alerts_dict["Urgency"],
                "bulletin": string.lower(alerts_dict["Bulletin"]),
            }
            alert_items.append(alert_item)
    except:
        pass

    (forecast_12hr, forecast_24hr, forecast_36hr) = thirtysix_hour_outlook(daily_forecasts)

    today = timezone.now().date()
    return render(
        request,
        "intellicast/weather.html",
        {
            "location": location,
            "todays_date": today,
            "tomorrows_date": today + datetime.timedelta(days=1),
            "current_conditions": conditions,
            "forecast_12hr": forecast_12hr,
            "forecast_24hr": forecast_24hr,
            "forecast_36hr": forecast_36hr,
            "daily_forecasts": daily_forecast_items,
            "hourly_forecasts": hourly_forecast_items,
            "alerts": alert_items,
            "unavailable": False,
            "geo_form": geo_form,
        },
    )
def weather_page(request):
    """
    Main weather landing page.  Features current conditions, a 36 hour forecast,
    and an interactive map (sadly, map is flash.)
    """
    rloc = _get_request_location(request)
    geo_form = GeolocationForm(initial={'geo': rloc.zip_code})
    (location, conditions, hourly_forecasts, daily_forecasts, alerts) = get_intellicast_data(rloc.zip_code)

    if not hourly_forecasts or not conditions or not daily_forecasts:
        return render(request, 'intellicast/weather.html', {'unavailable': True})

    hourly_forecast_items = []
    for i in range(1, 24):
        forecast_dict = hourly_forecasts[str(i)]
        forecast_clean = create_forecast_dict('Hourly', forecast_dict)
        hourly_forecast_items.append(forecast_clean)
    
    daily_forecast_items = []
    for i in range(2, 9):
        forecast_dict = daily_forecasts[str(i)]
        forecast_clean = forecast_clean = create_forecast_dict('Daily', forecast_dict)
        daily_forecast_items.append(forecast_clean)
    try:
        alert_items = []
        for i, item in enumerate(alerts, 1):
            alerts_dict = alerts[i]
            alert_item = {
                'headline': alerts_dict['Headline'],
                'starttime': alerts_dict['StartTime'],
                'endtime': alerts_dict['EndTime'],
                'urgency': alerts_dict['Urgency'],
                'bulletin': string.lower(alerts_dict['Bulletin']),
            }
            alert_items.append(alert_item)
    except:
        pass
    
    (forecast_12hr, forecast_24hr, forecast_36hr) = thirtysix_hour_outlook(daily_forecasts)
    
    today = timezone.now().date()
    return render(request, 'intellicast/weather.html', {
        'location': location,
        'todays_date': today,
        'tomorrows_date': today + datetime.timedelta(days=1),
        'current_conditions': conditions,
        
        'forecast_12hr': forecast_12hr,
        'forecast_24hr': forecast_24hr,
        'forecast_36hr': forecast_36hr,

        'daily_forecasts': daily_forecast_items,
        'hourly_forecasts': hourly_forecast_items,
        'alerts': alert_items,
        'unavailable': False,
        'geo_form': geo_form,
    })