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 ''
 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
     else:
         if not zip_code:
             zip_code = settings.DEFAULT_ZIP_CODE
     try:
         (location, conditions, hourly_forecasts, daily_forecasts, alerts) = get_intellicast_data(zip_code)
     except TypeError:
         return ''
     except:
         #if settings.DEBUG:
         #    raise
         return ''
     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)
         context[self.var_name] = alert_items
     except:
         pass
     
     return ''
示例#3
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,
    })
def texting_weather(request):

    site=get_current_site(request)
    data = get_intellicast_data(site.profile.zip_code)

    current_temp = data[1]['TempF']
    sky = data[1]['Sky']

    return HttpResponse(current_temp + ' degrees, ' + sky + '. Standard Message & data rates may apply. Text STOP to quit. HELP for info.', content_type='text/plain')
示例#6
0
def daily_weather_detail(request, year=None, month=None, day=None):

    try:
        forecast_date = datetime.date(year=int(year), month=int(month), day=int(day))
    except ValueError:
        raise Http404
    today = timezone.now().date()
    if forecast_date < today:
        return render(request, "intellicast/daily_weather_detail.html", {"unavailable": True})

    difference = forecast_date - today
    day_index = str(1 + difference.days)

    try:
        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)
    except TypeError:
        return render(request, "intellicast/daily_weather_detail.html", {"unavailable": True})

    if int(day_index) > 1:
        prev_date = forecast_date - datetime.timedelta(days=1)
    else:
        prev_date = None

    if int(day_index) < 7:
        next_date = forecast_date + datetime.timedelta(days=1)
    else:
        next_date = None

    if not daily_forecasts:
        return render(request, "intellicast/daily_weather_detail.html", {"unavailable": True})
    try:
        forecast = daily_forecasts[day_index]
    except KeyError:
        return render(request, "intellicast/daily_weather_detail.html", {"unavailable": True})

    day_forecast_dict = create_forecast_dict("Day", forecast)
    night_forecast_dict = create_forecast_dict("Night", forecast)

    return render(
        request,
        "intellicast/daily_weather_detail.html",
        {
            "location": location,
            "forecast_date": forecast_date,
            "prev_date": prev_date,
            "next_date": next_date,
            "day_forecast": day_forecast_dict,
            "night_forecast": night_forecast_dict,
            "unavailable": False,
            "geo_form": geo_form,
        },
    )
def daily_weather_detail(request, year=None, month=None, day=None):
    
    try:
        forecast_date = datetime.date(year=int(year), month=int(month), day=int(day))
    except ValueError:
        raise Http404
    today = timezone.now().date()
    if forecast_date < today:
        return render(request, 'intellicast/daily_weather_detail.html', {'unavailable': True})
    
    difference = forecast_date - today
    day_index = str(1 + difference.days)
    
    try:
        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)
    except TypeError:
        return render(request, 'intellicast/daily_weather_detail.html', {'unavailable': True})
    
    if int(day_index) > 1:
        prev_date = forecast_date - datetime.timedelta(days=1)
    else:
        prev_date = None
    
    if int(day_index) < 7:
        next_date = forecast_date + datetime.timedelta(days=1)
    else:
        next_date = None
   
    if not daily_forecasts:
        return render(request, 'intellicast/daily_weather_detail.html', {'unavailable': True})
    try:
        forecast = daily_forecasts[day_index]
    except KeyError:
        return render(request, 'intellicast/daily_weather_detail.html', {'unavailable': True})

    day_forecast_dict = create_forecast_dict('Day', forecast)
    night_forecast_dict = create_forecast_dict('Night', forecast)
    
    return render(request, 'intellicast/daily_weather_detail.html', {
        'location': location,
        'forecast_date': forecast_date,
        'prev_date': prev_date,
        'next_date': next_date,
        'day_forecast': day_forecast_dict,
        'night_forecast': night_forecast_dict,
        'unavailable': False,
        'geo_form': geo_form,
    })
def prefetch_intellicast_data():
    try:
        site_zip_codes = Site.objects.values_list('profile__zip_code', flat=True)
    except FieldError:
        site_zip_codes = settings.INTELLICAST_PREFETCH_ZIPS

    now = timezone.now()
    for zipcode in site_zip_codes:
        if zipcode == '':
            continue
        intellicast_data = get_intellicast_data(zipcode, False, True)
        if not all(intellicast_data[:4]):
            last_success_time = cache.get('intellicast_fetch_success')
            if not last_success_time or now - last_success_time > timedelta(hours=1):
                cache.set('intellicast_fetch_success', now, 60 * 60 * 2)
                raise Exception('Intellicast seems to be down.')
            break
    else:
        cache.set('intellicast_fetch_success', now, 60 * 60 * 2)
 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)
     
         conditions_badge = {
             'zipcode': zip_code, 
             'current_temp': conditions['TempF'], 
             'icon_code': conditions['IconCode'],
             'feels_like': conditions['FeelsLikeF'],
             'wind_direction': conditions['WndDirCardinal'],
             'wind_speed': conditions['WndSpdMph'],
             'sky': conditions['Sky'],
         }
         context[self.var_name] = conditions_badge
     except:
         return ''
     return ''