示例#1
0
def index(request):
    twitter_feed = cache.get("our_twitter_feed")
    if twitter_feed == None:
        try:
            import twitter
            twitter_api = twitter.Api()
            twitter_feed = twitter_api.GetUserTimeline("govtrack", since_id=0, count=3)
        
            # replace links
            from django.utils.html import conditional_escape
            from django.utils.safestring import mark_safe
            re_url = re.compile(r"(?i)\b((?:[a-z][\w-]+:(?:/{1,3}|[a-z0-9%])|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'\".,<>?«»“”‘’]))")
            for item in twitter_feed:
                item.text = re_url.sub(lambda m : "<a target=\"_blank\" href=\"" + m.group(0) + "\">" + m.group(0) + "</a>", conditional_escape(item.text))
            cache.set("our_twitter_feed", twitter_feed, 60*30) # 30 minutes
        except:
            twitter_feed = []
            cache.set("our_twitter_feed", twitter_feed, 60*2) # 2 minutes
            
        
    blog_feed = cache.get("our_blog_feed")
    if not blog_feed:
        blog_feed = get_blog_items()[0:2]
        cache.set("our_blog_feed", blog_feed, 60*30) # 30 min
    
    events_feed = cache.get("frontpage_events_feed")
    if not events_feed:
        events_feed = Feed.get_events_for([fn for fn in ("misc:activebills2", "misc:billsummaries", "misc:allvotes") if Feed.objects.filter(feedname=fn).exists()], 6)
        cache.set("frontpage_events_feed", events_feed, 60*15) # 15 minutes

    return {
        'events': events_feed,
        'tweets': twitter_feed,
        'blog': blog_feed,
        }
示例#2
0
def homepage_summary(request):
	# parse & validate parameters
	try:
		state = request.GET["district"][0:2]
		district = int(request.GET["district"][2:])
		if state not in statenames: raise Exception()
	except:
		return None

	from django.contrib.humanize.templatetags.humanize import ordinal

	# view
	people = Person.from_state_and_district(state, district)
	feeds = [p.get_feed() for p in people]
	events = Feed.get_events_for(feeds, 6)

	from events.templatetags.events_utils import render_event
	for i in range(len(events)):
		events[i] = render_event(events[i], feeds)
		if not isinstance(events[i]["date"], str):
			events[i]["date"] = events[i]["date"].strftime('%B %d, %Y') # can't JSON-serialize a datetime anyway, TODO handle date_has_no_time
		for k in list(events[i]): # remove anything else in case it is not JSON-serializable
			if k not in ('type', 'date', 'title', 'body_html', 'url'):
				del events[i][k]

	# form output
	return {
		"state": state,
		"district": district,
		"state_name": statenames[state],
		"district_ordinal": ordinal(district),
		"reps": [ { "link": p.get_absolute_url(), "name": p.name_and_title(), "title": p.role.get_description(), "photo": p.get_photo_url() } for p in people],
		"events": events,
	}
示例#3
0
def template_context_processor(request):
    # These are good to have in a context processor and not middleware
    # because they won't be evaluated until template evaluation, which
    # might have user-info blocked already for caching (a good thing).
    
    context = dict(base_context) # clone
    
    # Add top-tracked feeds.
    from events.models import Feed
    global trending_feeds
    if settings.DEBUG and False:
        trending_feeds = [None, []]
    elif not trending_feeds or trending_feeds[0] < datetime.datetime.now()-datetime.timedelta(hours=2):
        trf = cache.get("trending_feeds")
        if not trf:
            trf = Feed.get_trending_feeds()
            cache.set("trending_feeds", trf, 60*60*2)
        trending_feeds = (datetime.datetime.now(), [Feed.objects.get(id=f) for f in trf])
    context["trending_feeds"] = trending_feeds[1]
    context["trending_bill_feeds"] = [f for f in trending_feeds[1] if f.feedname.startswith("bill:")]

    # Add site-wide tracked events.
    all_tracked_events = cache.get("all_tracked_events")
    if not all_tracked_events:
        all_tracked_events = Feed.get_events_for([fn for fn in ("misc:activebills2", "misc:billsummaries", "misc:allvotes") if Feed.objects.filter(feedname=fn).exists()], 6)
        cache.set("all_tracked_events", all_tracked_events, 60*15) # 15 minutes
    context["all_tracked_events"] = all_tracked_events

    # Get our latest Medium posts.
    medium_posts = cache.get("medium_posts")
    if not medium_posts:
        from website.models import MediumPost
        medium_posts = MediumPost.objects.order_by('-published')[0:6]
        cache.set("medium_posts", medium_posts, 60*15) # 15 minutes
    context["medium_posts"] = medium_posts

    # Add context variables for whether the user is in the
    # House or Senate netblocks.
    
    try:
        ip = request.META["REMOTE_ADDR"]
        ip = ip.replace("::ffff:", "") # ipv6 wrapping ipv4
        
        if is_ip_in_any_range(ip, HOUSE_NET_RANGES):
            context["remote_net_house"] = True
            request._track_this_user = True
        if is_ip_in_any_range(ip, SENATE_NET_RANGES):
            context["remote_net_senate"] = True
            request._track_this_user = True
        if is_ip_in_any_range(ip, EOP_NET_RANGES):
            context["remote_net_eop"] = True
            request._track_this_user = True
    except:
        pass
    
    return context
示例#4
0
def index(request):
    blog_feed = cache.get("our_blog_feed")
    if not blog_feed:
        blog_feed = get_blog_items()[0:4]
        cache.set("our_blog_feed", blog_feed, 60*30) # 30 min
    
    events_feed = cache.get("frontpage_events_feed")
    if not events_feed:
        events_feed = Feed.get_events_for([fn for fn in ("misc:activebills2", "misc:billsummaries", "misc:allvotes") if Feed.objects.filter(feedname=fn).exists()], 6)
        cache.set("frontpage_events_feed", events_feed, 60*15) # 15 minutes

    return {
        'events': events_feed,
        'blog': blog_feed,
        }
示例#5
0
def index(request):
    blog_feed = cache.get("our_blog_feed")
    if not blog_feed:
        blog_feed = get_blog_items()[0:4]
        cache.set("our_blog_feed", blog_feed, 60*30) # 30 min
    
    events_feed = cache.get("frontpage_events_feed")
    if not events_feed:
        events_feed = Feed.get_events_for([fn for fn in ("misc:activebills2", "misc:billsummaries", "misc:allvotes") if Feed.objects.filter(feedname=fn).exists()], 6)
        cache.set("frontpage_events_feed", events_feed, 60*15) # 15 minutes

    return {
        'events': events_feed,
        'blog': blog_feed,
        }
示例#6
0
 def items(self):
     events = [render_event(item, feedlist) for item in Feed.get_events_for(feedlist, 25)]
     return [e for e in events if e != None]
示例#7
0
def template_context_processor(request):
    # These are good to have in a context processor and not middleware
    # because they won't be evaluated until template evaluation, which
    # might have user-info blocked already for caching (a good thing).
    
    context = dict(base_context) # clone
    
    #if hasattr(request, 'user') and request.user.is_authenticated() and BouncedEmail.objects.filter(user=request.user).exists(): context["user_has_bounced_mail"] = True
    
    # Add top-tracked feeds.
    from events.models import Feed
    global trending_feeds
    if settings.DEBUG and False:
        trending_feeds = [None, []]
    elif not trending_feeds or trending_feeds[0] < datetime.datetime.now()-datetime.timedelta(hours=2):
        trf = cache.get("trending_feeds")
        if not trf:
            trf = Feed.get_trending_feeds()
            cache.set("trending_feeds", trf, 60*60*2)
        trending_feeds = (datetime.datetime.now(), [Feed.objects.get(id=f) for f in trf])
    context["trending_feeds"] = trending_feeds[1]
    context["trending_bill_feeds"] = [f for f in trending_feeds[1] if f.feedname.startswith("bill:")]

    # Add site-wide tracked events.
    all_tracked_events = cache.get("all_tracked_events")
    if not all_tracked_events:
        all_tracked_events = Feed.get_events_for([fn for fn in ("misc:activebills2", "misc:billsummaries", "misc:allvotes") if Feed.objects.filter(feedname=fn).exists()], 6)
        cache.set("all_tracked_events", all_tracked_events, 60*15) # 15 minutes
    context["all_tracked_events"] = all_tracked_events

    # Get our latest Medium posts.
    medium_posts = cache.get("medium_posts")
    if not medium_posts:
        from website.models import MediumPost
        medium_posts = MediumPost.objects.order_by('-published')[0:6]
        cache.set("medium_posts", medium_posts, 60*15) # 15 minutes
    context["medium_posts"] = medium_posts

    # Get a campaign from if.then.fund.
    itf_active_campaign = 50
    if_then_fund_campaign = cache.get("if_then_fund_campaign")
    if not if_then_fund_campaign and itf_active_campaign:
        try:
            if_then_fund_campaign = json.load(urllib2.urlopen("https://if.then.fund/a/%d.json" % itf_active_campaign))
        except:
            if_then_fund_campaign = "UHM" # something that is truthy otherwise we'll ping on every request
    	cache.set("if_then_fund_campaign", if_then_fund_campaign, 60*45) # 45 minutes
    context["if_then_fund_campaign"] = if_then_fund_campaign

    # Add context variables for whether the user is in the
    # House or Senate netblocks.
    
    def ip_to_quad(ip):
        return [int(s) for s in ip.split(".")]
    def compare_ips(ip1, ip2):
        return cmp(ip_to_quad(ip1), ip_to_quad(ip2))
    def is_ip_in_range(ip, block):
       return compare_ips(ip, block[0]) >= 0 and compare_ips(ip, block[1]) <= 0
    def is_ip_in_any_range(ip, blocks):
       for block in blocks:
           if is_ip_in_range(ip, block):
               return True
       return False
    
    try:
        ip = request.META["REMOTE_ADDR"]
        ip = ip.replace("::ffff:", "") # ipv6 wrapping ipv4
        
        if is_ip_in_any_range(ip, HOUSE_NET_RANGES):
            context["remote_net_house"] = True
            request._track_this_user = True
        if is_ip_in_any_range(ip, SENATE_NET_RANGES):
            context["remote_net_senate"] = True
            request._track_this_user = True
        if is_ip_in_any_range(ip, EOP_NET_RANGES):
            context["remote_net_eop"] = True
            request._track_this_user = True
    except:
        pass
    
    # Add a context variable for if the user is near DC geographically.

    user_loc = None
    try:
        if settings.GEOIP_DB_PATH and not request.path.startswith("/api/") and False:
            user_loc = geo_ip_db.geos(ip)
            context["is_dc_local"] = user_loc.distance(washington_dc) < .5
    except:
        pass

    if not hasattr(request, 'user') or not request.user.is_authenticated():
        # Have we put the user's district in a cookie?
        try:
            cong_dist = json.loads(request.COOKIES["cong_dist"])
            x = cong_dist["state"] # validate fields are present
            x = int(cong_dist["district"]) # ...and valid
        except:
            cong_dist = None

        # Geolocate to a congressional district if not known and save it in
        # a cookie for next time.
        if user_loc and not cong_dist and not request.path.startswith("/api/"):
            try:
                from person.views import do_district_lookup
                cong_dist = do_district_lookup(*user_loc.coords)
                x = cong_dist["state"] # validate fields are present
                x = int(cong_dist["district"]) # ...and valid
                request._save_cong_dist = cong_dist
            except:
                cong_dist = None

    else:
        # If the user is logged in, is the district in the user's profile?
        profile = request.user.userprofile()
        if profile.congressionaldistrict != None:
            # pass through XX00 so site knows not to prompt
            cong_dist = { "state": profile.congressionaldistrict[0:2], "district": int(profile.congressionaldistrict[2:]) }
        else:
            cong_dist = None

    # If we have a district, get its MoCs.
    if cong_dist:
        from person.models import Person
        context["congressional_district"] = json.dumps(cong_dist)
        context["congressional_district_mocs"] = json.dumps([p.id for p in Person.from_state_and_district(cong_dist["state"], cong_dist["district"])])

    return context
示例#8
0
 def items(self):
     events = [
         render_event(item, feedlist)
         for item in Feed.get_events_for(feedlist, 25)
     ]
     return [e for e in events if e != None]
def template_context_processor(request):
    # These are good to have in a context processor and not middleware
    # because they won't be evaluated until template evaluation, which
    # might have user-info blocked already for caching (a good thing).

    context = dict(base_context)  # clone

    #if hasattr(request, 'user') and request.user.is_authenticated() and BouncedEmail.objects.filter(user=request.user).exists(): context["user_has_bounced_mail"] = True

    # Add top-tracked feeds.
    from events.models import Feed
    global trending_feeds
    if settings.DEBUG and False:
        trending_feeds = [None, []]
    elif not trending_feeds or trending_feeds[0] < datetime.datetime.now(
    ) - datetime.timedelta(hours=2):
        trf = cache.get("trending_feeds")
        if not trf:
            trf = Feed.get_trending_feeds()
            cache.set("trending_feeds", trf, 60 * 60 * 2)
        trending_feeds = (datetime.datetime.now(),
                          [Feed.objects.get(id=f) for f in trf])
    context["trending_feeds"] = trending_feeds[1]
    context["trending_bill_feeds"] = [
        f for f in trending_feeds[1] if f.feedname.startswith("bill:")
    ]

    # Add site-wide tracked events.
    all_tracked_events = cache.get("all_tracked_events")
    if not all_tracked_events:
        all_tracked_events = Feed.get_events_for([
            fn for fn in ("misc:activebills2", "misc:billsummaries",
                          "misc:allvotes")
            if Feed.objects.filter(feedname=fn).exists()
        ], 6)
        cache.set("all_tracked_events", all_tracked_events,
                  60 * 15)  # 15 minutes
    context["all_tracked_events"] = all_tracked_events

    # Get our latest Medium posts.
    medium_posts = cache.get("medium_posts")
    if not medium_posts:
        from website.models import MediumPost
        medium_posts = MediumPost.objects.order_by('-published')[0:6]
        cache.set("medium_posts", medium_posts, 60 * 15)  # 15 minutes
    context["medium_posts"] = medium_posts

    # Get a campaign from if.then.fund.
    itf_active_campaign = 0
    if_then_fund_campaign = cache.get("if_then_fund_campaign_%d" %
                                      itf_active_campaign)
    if not if_then_fund_campaign and itf_active_campaign:
        try:
            if_then_fund_campaign = json.load(
                urllib2.urlopen("https://if.then.fund/a/%d.json" %
                                itf_active_campaign))
        except:
            if_then_fund_campaign = "UHM"  # something that is truthy otherwise we'll ping on every request
        cache.set("if_then_fund_campaign_%d" % itf_active_campaign,
                  if_then_fund_campaign, 60 * 45)  # 45 minutes
    context["if_then_fund_campaign"] = if_then_fund_campaign

    # Add context variables for whether the user is in the
    # House or Senate netblocks.

    def ip_to_quad(ip):
        return [int(s) for s in ip.split(".")]

    def compare_ips(ip1, ip2):
        return cmp(ip_to_quad(ip1), ip_to_quad(ip2))

    def is_ip_in_range(ip, block):
        return compare_ips(ip, block[0]) >= 0 and compare_ips(ip,
                                                              block[1]) <= 0

    def is_ip_in_any_range(ip, blocks):
        for block in blocks:
            if is_ip_in_range(ip, block):
                return True
        return False

    try:
        ip = request.META["REMOTE_ADDR"]
        ip = ip.replace("::ffff:", "")  # ipv6 wrapping ipv4

        if is_ip_in_any_range(ip, HOUSE_NET_RANGES):
            context["remote_net_house"] = True
            request._track_this_user = True
        if is_ip_in_any_range(ip, SENATE_NET_RANGES):
            context["remote_net_senate"] = True
            request._track_this_user = True
        if is_ip_in_any_range(ip, EOP_NET_RANGES):
            context["remote_net_eop"] = True
            request._track_this_user = True
    except:
        pass

    # Add a context variable for if the user is near DC geographically.

    user_loc = None
    try:
        if settings.GEOIP_DB_PATH and not request.path.startswith(
                "/api/") and False:
            user_loc = geo_ip_db.geos(ip)
            context["is_dc_local"] = user_loc.distance(washington_dc) < .5
    except:
        pass

    if not hasattr(request, 'user') or not request.user.is_authenticated():
        # Have we put the user's district in a cookie?
        try:
            cong_dist = json.loads(request.COOKIES["cong_dist"])
            x = cong_dist["state"]  # validate fields are present
            x = int(cong_dist["district"])  # ...and valid
        except:
            cong_dist = None

        # Geolocate to a congressional district if not known and save it in
        # a cookie for next time.
        if user_loc and not cong_dist and not request.path.startswith("/api/"):
            try:
                from person.views import do_district_lookup
                cong_dist = do_district_lookup(*user_loc.coords)
                x = cong_dist["state"]  # validate fields are present
                x = int(cong_dist["district"])  # ...and valid
                request._save_cong_dist = cong_dist
            except:
                cong_dist = None

    else:
        # If the user is logged in, is the district in the user's profile?
        profile = request.user.userprofile()
        if profile.congressionaldistrict != None:
            # pass through XX00 so site knows not to prompt
            cong_dist = {
                "state": profile.congressionaldistrict[0:2],
                "district": int(profile.congressionaldistrict[2:])
            }
        else:
            cong_dist = None

    # If we have a district, get its MoCs.
    if cong_dist:
        from person.models import Person
        context["congressional_district"] = json.dumps(cong_dist)
        context["congressional_district_mocs"] = json.dumps([
            p.id for p in Person.from_state_and_district(
                cong_dist["state"], cong_dist["district"])
        ])

    return context
示例#10
0
def template_context_processor(request):
    # These are good to have in a context processor and not middleware
    # because they won't be evaluated until template evaluation, which
    # might have user-info blocked already for caching (a good thing).
    
    context = {
        "SITE_ROOT_URL": settings.SITE_ROOT_URL,
        "GOOGLE_ANALYTICS_KEY": settings.GOOGLE_ANALYTICS_KEY,
        "STATE_CHOICES": sorted([(kv[0], kv[1], us.stateapportionment[kv[0]]) for kv in us.statenames.items() if kv[0] in us.stateapportionment], key = lambda kv : kv[1]),
    }
    
    if hasattr(request, 'user') and request.user.is_authenticated() and BouncedEmail.objects.filter(user=request.user).exists(): context["user_has_bounced_mail"] = True
    
    # Add top-tracked feeds.
    from events.models import Feed
    global trending_feeds
    if settings.DEBUG and False:
        trending_feeds = [None, []]
    elif not trending_feeds or trending_feeds[0] < datetime.datetime.now()-datetime.timedelta(hours=2):
        trf = cache.get("trending_feeds")
        if not trf:
            trf = Feed.get_trending_feeds()
            cache.set("trending_feeds", trf, 60*60*2)
        trending_feeds = (datetime.datetime.now(), [Feed.objects.get(id=f) for f in trf])
    context["trending_feeds"] = trending_feeds[1]
    context["trending_bill_feeds"] = [f for f in trending_feeds[1] if f.feedname.startswith("bill:")]

    # Add site-wide tracked events.
    all_tracked_events = cache.get("all_tracked_events")
    if not all_tracked_events:
        all_tracked_events = Feed.get_events_for([fn for fn in ("misc:activebills2", "misc:billsummaries", "misc:allvotes") if Feed.objects.filter(feedname=fn).exists()], 6)
        cache.set("all_tracked_events", all_tracked_events, 60*15) # 15 minutes
    context["all_tracked_events"] = all_tracked_events

    # Highlight a recent vote. We don't yet need to know the user's district
    # --- that will happen client-side.
    def get_highlighted_vote():
        from vote.models import Vote, VoteCategory
        candidate_votes = Vote.objects.filter(category__in=Vote.MAJOR_CATEGORIES).exclude(related_bill=None).order_by('-created')
        for v in candidate_votes:
            return { "title": v.question, "link": v.get_absolute_url(), "data": v.simple_record() }
        return "NONE"
    highlighted_vote = cache.get("highlighted_vote")
    if highlighted_vote is None:
        highlighted_vote = get_highlighted_vote()
        cache.set("highlighted_vote", highlighted_vote, 60*60*2)
    if highlighted_vote != "NONE":
        context["highlighted_vote"] = highlighted_vote

    # Get our latest Medium posts.
    def get_medium_posts():
        medium_posts = urllib.urlopen("https://medium.com/govtrack-insider?format=json").read()
        # there's some crap before the JSON object starts
        medium_posts = medium_posts[medium_posts.index("{"):]
        medium_posts = json.loads(medium_posts)
        def format_post(postid):
            post = medium_posts['payload']['references']['Post'][postid]
            collection = medium_posts['payload']['references']['Collection'][post['homeCollectionId']]
            return {
                "title": post['title'],
                "url": "https://medium.com/" + collection['slug'] + "/" + post['uniqueSlug'],
                "date": post['virtuals']['firstPublishedAtEnglish'],
                "preview": post['virtuals']['snippet'],
                "image": post['virtuals']['previewImage']['imageId'] if post['virtuals'].get('previewImage') else None,
                #"preview": " ".join([
                #    para['text']
                #    for para in post['previewContent']['bodyModel']['paragraphs']
                #    if para['type'] == 1 # not sure but a paragraph? vs a heading?
                #])
            }
        return [ format_post(postid) for postid in medium_posts['payload']['value']['sections'][1]['postListMetadata']['postIds'] ]
    medium_posts = cache.get("medium_posts")
    if not medium_posts:
        try:
            medium_posts = get_medium_posts()
        except:
            medium_posts = []
        cache.set("medium_posts", medium_posts, 60*15) # 15 minutes
    context["medium_posts"] = medium_posts[0:3]
    
    # Add context variables for whether the user is in the
    # House or Senate netblocks.
    
    def ip_to_quad(ip):
        return [int(s) for s in ip.split(".")]
    def compare_ips(ip1, ip2):
        return cmp(ip_to_quad(ip1), ip_to_quad(ip2))
    def is_ip_in_range(ip, block):
       return compare_ips(ip, block[0]) >= 0 and compare_ips(ip, block[1]) <= 0
    def is_ip_in_any_range(ip, blocks):
       for block in blocks:
           if is_ip_in_range(ip, block):
               return True
       return False
    
    try:
        ip = request.META["REMOTE_ADDR"]
        ip = ip.replace("::ffff:", "") # ipv6 wrapping ipv4
        
        if is_ip_in_any_range(ip, HOUSE_NET_RANGES):
            context["remote_net_house"] = True
            request._track_this_user = True
        if is_ip_in_any_range(ip, SENATE_NET_RANGES):
            context["remote_net_senate"] = True
            request._track_this_user = True
        if is_ip_in_any_range(ip, EOP_NET_RANGES):
            context["remote_net_eop"] = True
            request._track_this_user = True
    except:
        pass
    
    # Add a context variable for if the user is near DC geographically.

    user_loc = None
    try:
        if settings.GEOIP_DB_PATH and not request.path.startswith("/api/"):
            user_loc = geo_ip_db.geos(ip)
            context["is_dc_local"] = user_loc.distance(washington_dc) < .5
    except:
        pass

    if not hasattr(request, 'user') or not request.user.is_authenticated():
        # Have we put the user's district in a cookie?
        try:
            cong_dist = json.loads(request.COOKIES["cong_dist"])
            x = cong_dist["state"] # validate fields are present
            x = int(cong_dist["district"]) # ...and valid
        except:
            cong_dist = None

        # Geolocate to a congressional district if not known and save it in
        # a cookie for next time.
        if user_loc and not cong_dist and not request.path.startswith("/api/"):
            try:
                from person.views import do_district_lookup
                cong_dist = do_district_lookup(*user_loc.coords)
                x = cong_dist["state"] # validate fields are present
                x = int(cong_dist["district"]) # ...and valid
                request._save_cong_dist = cong_dist
            except:
                cong_dist = None

    else:
        # If the user is logged in, is the district in the user's profile?
        profile = request.user.userprofile()
        if profile.congressionaldistrict != None:
            # pass through XX00 so site knows not to prompt
            cong_dist = { "state": profile.congressionaldistrict[0:2], "district": int(profile.congressionaldistrict[2:]) }
        else:
            cong_dist = None

    # If we have a district, get its MoCs.
    if cong_dist:
        from person.models import Person
        context["congressional_district"] = json.dumps(cong_dist)
        context["congressional_district_mocs"] = json.dumps([p.id for p in Person.from_state_and_district(cong_dist["state"], cong_dist["district"])])

    return context
示例#11
0
def template_context_processor(request):
    # These are good to have in a context processor and not middleware
    # because they won't be evaluated until template evaluation, which
    # might have user-info blocked already for caching (a good thing).
    
    context = dict(base_context) # clone
    
    # Add top-tracked feeds.
    from events.models import Feed
    global trending_feeds
    if settings.DEBUG and False:
        trending_feeds = [None, []]
    elif not trending_feeds or trending_feeds[0] < datetime.datetime.now()-datetime.timedelta(hours=2):
        trf = cache.get("trending_feeds")
        if not trf:
            trf = Feed.get_trending_feeds()
            cache.set("trending_feeds", trf, 60*60*2)
        trending_feeds = (datetime.datetime.now(), [Feed.objects.get(id=f) for f in trf])
    context["trending_feeds"] = trending_feeds[1]
    context["trending_bill_feeds"] = [f for f in trending_feeds[1] if f.feedname.startswith("bill:")]

    # Add site-wide tracked events.
    all_tracked_events = cache.get("all_tracked_events")
    if not all_tracked_events:
        all_tracked_events = Feed.get_events_for([fn for fn in ("misc:activebills2", "misc:billsummaries", "misc:allvotes") if Feed.objects.filter(feedname=fn).exists()], 6)
        cache.set("all_tracked_events", all_tracked_events, 60*15) # 15 minutes
    context["all_tracked_events"] = all_tracked_events

    # Get our latest Medium posts.
    medium_posts = cache.get("medium_posts")
    if not medium_posts:
        from website.models import MediumPost
        medium_posts = MediumPost.objects.order_by('-published')[0:6]
        cache.set("medium_posts", medium_posts, 60*15) # 15 minutes
    context["medium_posts"] = medium_posts

    # Add context variables for whether the user is in the
    # House or Senate netblocks.
    
    def ip_to_quad(ip):
        return [int(s) for s in ip.split(".")]
    def compare_ips(ip1, ip2):
        return cmp(ip_to_quad(ip1), ip_to_quad(ip2))
    def is_ip_in_range(ip, block):
       return compare_ips(ip, block[0]) >= 0 and compare_ips(ip, block[1]) <= 0
    def is_ip_in_any_range(ip, blocks):
       for block in blocks:
           if is_ip_in_range(ip, block):
               return True
       return False
    
    try:
        ip = request.META["REMOTE_ADDR"]
        ip = ip.replace("::ffff:", "") # ipv6 wrapping ipv4
        
        if is_ip_in_any_range(ip, HOUSE_NET_RANGES):
            context["remote_net_house"] = True
            request._track_this_user = True
        if is_ip_in_any_range(ip, SENATE_NET_RANGES):
            context["remote_net_senate"] = True
            request._track_this_user = True
        if is_ip_in_any_range(ip, EOP_NET_RANGES):
            context["remote_net_eop"] = True
            request._track_this_user = True
    except:
        pass
    
    return context