def tweet_votes_yday(self, if_major): # Tweet count of votes yesterday, by vote type if there were any major votes. from vote.models import Vote, VoteCategory votes = Vote.objects.filter( created__gte=timezone.now().date() - timedelta(days=1), created__lt=timezone.now().date(), ) if votes.count() == 0: return has_major = len([v for v in votes if v.is_major]) > 0 if not has_major and if_major: return if not has_major: count = votes.count() msg = "%d minor vote%s held by Congress yesterday." % ( count, "s were" if count != 1 else " was", ) else: counts = defaultdict(lambda: 0) for v in votes: counts[v.category] += 1 counts = list(counts.items()) counts.sort(key=lambda kv: (VoteCategory.by_value(kv[0]).importance, -kv[1])) msg = "Votes held by Congress yesterday: " + ", ".join( str(value) + " on " + VoteCategory.by_value(key).label for key, value in counts) self.post_tweet("%s:votes" % timezone.now().date().isoformat(), msg, "https://www.govtrack.us/congress/votes")
def tweet_votes_yday(self, if_major): # Tweet count of votes yesterday, by vote type if there were any major votes. from vote.models import Vote, VoteCategory votes = Vote.objects.filter( created__gte=timezone.now().date()-timedelta(days=1), created__lt=timezone.now().date(), ) if votes.count() == 0: return has_major = len([v for v in votes if v.is_major]) > 0 if not has_major and if_major: return if not has_major: msg = "%d vote%s held by Congress yesterday." % ( votes.count(), "s were" if count != 1 else " was", ) else: counts = defaultdict(lambda : 0) for v in votes: counts[v.category] += 1 counts = list(counts.items()) counts.sort(key = lambda kv : (VoteCategory.by_value(kv[0]).importance, -kv[1])) msg = "Votes held by Congress yesterday: " + ", ".join( str(value) + " on " + VoteCategory.by_value(key).label for key, value in counts ) self.post_tweet( "%s:votes" % timezone.now().date().isoformat(), msg, "https://www.govtrack.us/congress/votes")
def get_context_data(self, **kwargs): """Insert the form into the context dict.""" if 'categories' not in kwargs: kwargs['categories'] = VoteCategory.eligible_category( self.request.user) return super().get_context_data(**kwargs)
from django import forms from vote.models import Vote, CongressChamber, VoteCategory YEARS = [('', 'Any')] + [(x, str(x)) for x in range(2011, 1788, -1)] CHAMBERS = [('', 'Any')] + CongressChamber.choices() CATEGORIES = [('', 'Any')] + VoteCategory.choices() YEAR_FIELD = forms.ChoiceField(choices=YEARS, required=False) class VoteFilterForm(forms.Form): year = forms.ChoiceField(choices=YEARS, required=False) chamber = forms.ChoiceField(choices=CHAMBERS, required=False) category = forms.ChoiceField(choices=CATEGORIES, required=False) def filter(self, qs): data = self.cleaned_data if data['year']: qs = qs.filter(created__year=data['year']) if data['chamber']: qs = qs.filter(chamber=data['chamber']) if data['category']: qs = qs.filter(category=data['category']) return qs
else: state_pop_year = { state: pop for (state, year), pop in state_pop.items() if year == pop_year } # Sum up the total population represented by each vote option # (i.e. yes/no), # apportioning the population of each state evenly across the legislators representing # that state who voted. Also just count total yes/no/not voting votes. pop_by_option = defaultdict(lambda : 0) count_by_option = defaultdict(lambda : 0) for option, state, personid in votes: if state == "": continue # Vice President's tie-breaker if state not in state_pop_year: continue # one of the island territories, for which we don't have population data pop_by_option[option] += state_pop_year[state] / legislators_per_state[state] count_by_option[option] += 1 # Skip votes where the ayes were in the minority. Those votes represent failed outcomes. We're # only interested in the legitimacy of actual outcomes. if "+" not in pop_by_option or count_by_option["+"] < count_by_option.get("-", 0): continue # get the % of yes voters and % of the country's population represented by those voters count_percent_yea = count_by_option["+"]/(count_by_option["+"] + count_by_option.get("-", 0)) pop_percent_yea = pop_by_option["+"]/(pop_by_option["+"] + pop_by_option.get("-", 0)) # of those voting pop_percent_yea_of_total = pop_by_option["+"]/sum(pop_by_option.values()) # of sworn senators # print W.writerow([vote.congress, vote.session if len(vote.session) == 4 else vote.created.year, vote.created.isoformat(), vote.chamber_name, VoteCategory.by_value(vote.category).key, str(vote), percent(count_percent_yea), percent(pop_percent_yea), percent(pop_percent_yea_of_total), "https://www.govtrack.us"+vote.get_absolute_url()])
def addCategory(request): resp = {} app = VoteApplication.objects.filter(id=request.GET.get('appid')) if (app.count() == 0): resp['code'] = 'application does not exist. check code' return HttpResponse(json.dumps(resp), content_type='application/json') else: app = app[0] user = VoteUser.objects.filter(userkey=request.GET.get('userkey'))[0] if (user is None): resp['code'] = 'authentication error' return HttpResponse(json.dumps(resp), content_type='application/json') cat = VoteCategory() cat.color = request.GET.get('color') cat.name = request.GET.get('name') cat.parent = VoteCategory.objects.filter(id=request.GET.get('parentid'))[0] cat.catType = request.GET.get('catType') # cat.parent.catType cat.languageCode = request.GET.get('lang') cat.creation = datetime.now() cat.creator = user cat.application = app cat.save() resp['id'] = cat.id return HttpResponse(json.dumps(resp), content_type='application/json')
def createRootCategory(lang, app, parent, user): cat = VoteCategory() cat.name = app.name cat.parent = parent cat.catType = -1 # cat.parent.catType - cat.languageCode = lang cat.creation = datetime.now() cat.creator = user cat.application = app cat.save() cat.parent = cat cat.save()
# Get the total U.S. population in this time. Interpolate (or beyond the last year, extrapolate). country_population = us_pop_at(pop_year) # Sum up the total population represented by each vote option # (i.e. yes/no), # apportioning the population of each state evenly across the legislators representing # that state who voted. Also just count total yes/no/not voting votes. pop_by_option = defaultdict(lambda : 0) count_by_option = defaultdict(lambda : 0) for option, state, personid in votes: if state == "": continue # Vice President's tie-breaker if option not in ("+", "-"): continue # we only care about voting legislators pop_by_option[option] += state_pop_year[state] / voting_legislators_per_state[state] count_by_option[option] += 1 # Skip votes where the ayes were in the minority. Those votes represent failed outcomes. We're # only interested in the legitimacy of actual outcomes. if "+" not in pop_by_option or count_by_option["+"] < count_by_option.get("-", 0): continue # get the % of yes voters out of senators voting and out of the country's population as a whole count_percent_yea = count_by_option["+"] / (count_by_option["+"] + count_by_option.get("-", 0)) statepop_percent_yea = pop_by_option["+"] / state_pop_total uspop_percent_yea = pop_by_option["+"] / country_population # print W.writerow([vote.congress, vote.session if len(vote.session) == 4 else vote.created.year, vote.created.isoformat(), VoteCategory.by_value(vote.category).key, str(vote), percent(count_percent_yea), percent(statepop_percent_yea), percent(uspop_percent_yea), "https://www.govtrack.us"+vote.get_absolute_url()])
from django import forms from vote.models import Vote, CongressChamber, VoteCategory YEARS = [('', 'Any')] + [(x, str(x)) for x in xrange(2011, 1788, -1)] CHAMBERS = [('', 'Any')] + CongressChamber.choices() CATEGORIES = [('', 'Any')] + VoteCategory.choices() YEAR_FIELD = forms.ChoiceField(choices=YEARS, required=False) class VoteFilterForm(forms.Form): year = forms.ChoiceField(choices=YEARS, required=False) chamber = forms.ChoiceField(choices=CHAMBERS, required=False) category = forms.ChoiceField(choices=CATEGORIES, required=False) def filter(self, qs): data = self.cleaned_data if data['year']: qs = qs.filter(created__year=data['year']) if data['chamber']: qs = qs.filter(chamber=data['chamber']) if data['category']: qs = qs.filter(category=data['category']) return qs