<commit_before>import datetime
from urllib import urlencode

from django.contrib.syndication.views import Feed
from django.core import urlresolvers
from django.core.paginator import Paginator, InvalidPage, EmptyPage
from django.http import HttpResponse, Http404, HttpResponseRedirect, HttpResponsePermanentRedirect
from django.shortcuts import get_object_or_404
from django.template import Context, loader, RequestContext
from django.template.defaultfilters import date as format_date
from django.utils.safestring import mark_safe
from django.views.decorators.vary import vary_on_headers

from parliament.bills.models import Bill, VoteQuestion, MemberVote, BillInSession
from parliament.core.api import ModelListView, ModelDetailView, APIFilters
from parliament.core.models import Session
from parliament.hansards.models import Statement

def bill_pk_redirect(request, bill_id):
    bill = get_object_or_404(Bill, pk=bill_id)
    return HttpResponsePermanentRedirect(
        urlresolvers.reverse('parliament.bills.views.bill', kwargs={
        'session_id': bill.get_session().id, 'bill_number': bill.number}))


class BillDetailView(ModelDetailView):

    resource_name = 'Bill'

    def get_object(self, request, session_id, bill_number):
        return BillInSession.objects.select_related(
            'bill', 'sponsor_politician').get(session=session_id, bill__number=bill_number)

    def get_related_resources(self, request, qs, result):
        return {
            'bills_url': urlresolvers.reverse('bills')
        }

    def get_html(self, request, session_id, bill_number):
        PER_PAGE = 10
        bill = get_object_or_404(Bill, sessions=session_id, number=bill_number)
        statements = bill.statement_set.all().order_by('-time', '-sequence').select_related('member', 'member__politician', 'member__riding', 'member__party')
        paginator = Paginator(statements, PER_PAGE)

        try:
            pagenum = int(request.GET.get('page', '1'))
        except ValueError:
            pagenum = 1
        try:
            page = paginator.page(pagenum)
        except (EmptyPage, InvalidPage):
            page = paginator.page(paginator.num_pages)

        c = RequestContext(request, {
            'bill': bill,
            'page': page,
            'votequestions': bill.votequestion_set.all().order_by('-date', '-number'),
            'title': ('Bill %s' % bill.number) + (' (Historical)' if bill.session.end else ''), 
            'statements_full_date': True,
            'statements_context_link': True,
        })
        if request.is_ajax():
            t = loader.get_template("hansards/statement_page.inc")
        else:
            t = loader.get_template("bills/bill_detail.html")
        return HttpResponse(t.render(c))
bill = vary_on_headers('X-Requested-With')(BillDetailView.as_view())
    
class BillListView(ModelListView):

    resource_name = 'Bills'

    filters = {
        'session': APIFilters.dbfield(help="e.g. 41-1"),
        'introduced': APIFilters.dbfield(filter_types=APIFilters.numeric_filters,
            help="date bill was introduced, e.g. introduced__gt=2010-01-01"),
        'legisinfo_id': APIFilters.dbfield(help="integer ID assigned by parl.gc.ca's LEGISinfo"),
        'number': APIFilters.dbfield('bill__number',
            help="a string, not an integer: e.g. C-10"),
        'law': APIFilters.dbfield('bill__law',
            help="did it become law? True, False"),
        'private_member_bill': APIFilters.dbfield('bill__privatemember',
            help="is it a private member's bill? True, False"),
        'sponsor_politician': APIFilters.politician('sponsor_politician'),
        'sponsor_politician_membership': APIFilters.fkey(lambda u: {'sponsor_member': u[-1]}),
    }

    def get_qs(self, request):
        return BillInSession.objects.all().select_related('bill', 'sponsor_politician')

    def get_html(self, request):
        sessions = Session.objects.with_bills()
        len(sessions) # evaluate it
        bills = Bill.objects.filter(sessions=sessions[0])
        votes = VoteQuestion.objects.select_related('bill').filter(session=sessions[0])[:6]

        t = loader.get_template('bills/index.html')
        c = RequestContext(request, {
            'object_list': bills,
            'session_list': sessions,
            'votes': votes,
            'session': sessions[0],
            'title': 'Bills & Votes'
        })

        return HttpResponse(t.render(c))
index = BillListView.as_view()


class BillSessionListView(ModelListView):

    def get_json(self, request, session_id):
        return HttpResponseRedirect(urlresolvers.reverse('bills') + '?'
                                    + urlencode({'session': session_id}))

    def get_html(self, request, session_id):
        session = get_object_or_404(Session, pk=session_id)
        bills = Bill.objects.filter(sessions=session)
        votes = VoteQuestion.objects.select_related('bill').filter(session=session)[:6]

        t = loader.get_template('bills/bill_list.html')
        c = RequestContext(request, {
            'object_list': bills,
            'session': session,
            'votes': votes,
            'title': 'Bills for the %s' % session
        })
        return HttpResponse(t.render(c))
bills_for_session = BillSessionListView.as_view()


class VoteListView(ModelListView):

    resource_name = 'Votes'

    api_notes = mark_safe("""<p>What we call votes are <b>divisions</b> in official Parliamentary lingo.
        We refer to an individual person's vote as a <a href="/votes/ballots/">ballot</a>.</p>
    """)

    filters = {
        'session': APIFilters.dbfield(help="e.g. 41-1"),
        'yea_total': APIFilters.dbfield(filter_types=APIFilters.numeric_filters,
            help="# votes for"),
        'nay_total': APIFilters.dbfield(filter_types=APIFilters.numeric_filters,
            help="# votes against, e.g. nay_total__gt=10"),
        'paired_total': APIFilters.dbfield(filter_types=APIFilters.numeric_filters,
            help="paired votes are an odd convention that seem to have stopped in 2011"),
        'date': APIFilters.dbfield(filter_types=APIFilters.numeric_filters,
            help="date__gte=2011-01-01"),
        'number': APIFilters.dbfield(filter_types=APIFilters.numeric_filters,
            help="every vote in a session has a sequential number"),
        'bill': APIFilters.fkey(lambda u: {
            'bill__session': u[-2],
            'bill__number': u[-1]
        }, help="e.g. /bills/41-1/C-10/"),
        'result': APIFilters.choices('result', VoteQuestion)
    }

    def get_json(self, request, session_id=None):
        if session_id:
            return HttpResponseRedirect(urlresolvers.reverse('votes') + '?'<commit_msg>Fix typo in VoteQuestion API filters<commit_after>import datetime
from urllib import urlencode

from django.contrib.syndication.views import Feed
from django.core import urlresolvers
from django.core.paginator import Paginator, InvalidPage, EmptyPage
from django.http import HttpResponse, Http404, HttpResponseRedirect, HttpResponsePermanentRedirect
from django.shortcuts import get_object_or_404
from django.template import Context, loader, RequestContext
from django.template.defaultfilters import date as format_date
from django.utils.safestring import mark_safe
from django.views.decorators.vary import vary_on_headers

from parliament.bills.models import Bill, VoteQuestion, MemberVote, BillInSession
from parliament.core.api import ModelListView, ModelDetailView, APIFilters
from parliament.core.models import Session
from parliament.hansards.models import Statement

def bill_pk_redirect(request, bill_id):
    bill = get_object_or_404(Bill, pk=bill_id)
    return HttpResponsePermanentRedirect(
        urlresolvers.reverse('parliament.bills.views.bill', kwargs={
        'session_id': bill.get_session().id, 'bill_number': bill.number}))


class BillDetailView(ModelDetailView):

    resource_name = 'Bill'

    def get_object(self, request, session_id, bill_number):
        return BillInSession.objects.select_related(
            'bill', 'sponsor_politician').get(session=session_id, bill__number=bill_number)

    def get_related_resources(self, request, qs, result):
        return {
            'bills_url': urlresolvers.reverse('bills')
        }

    def get_html(self, request, session_id, bill_number):
        PER_PAGE = 10
        bill = get_object_or_404(Bill, sessions=session_id, number=bill_number)
        statements = bill.statement_set.all().order_by('-time', '-sequence').select_related('member', 'member__politician', 'member__riding', 'member__party')
        paginator = Paginator(statements, PER_PAGE)

        try:
            pagenum = int(request.GET.get('page', '1'))
        except ValueError:
            pagenum = 1
        try:
            page = paginator.page(pagenum)
        except (EmptyPage, InvalidPage):
            page = paginator.page(paginator.num_pages)

        c = RequestContext(request, {
            'bill': bill,
            'page': page,
            'votequestions': bill.votequestion_set.all().order_by('-date', '-number'),
            'title': ('Bill %s' % bill.number) + (' (Historical)' if bill.session.end else ''), 
            'statements_full_date': True,
            'statements_context_link': True,
        })
        if request.is_ajax():
            t = loader.get_template("hansards/statement_page.inc")
        else:
            t = loader.get_template("bills/bill_detail.html")
        return HttpResponse(t.render(c))
bill = vary_on_headers('X-Requested-With')(BillDetailView.as_view())
    
class BillListView(ModelListView):

    resource_name = 'Bills'

    filters = {
        'session': APIFilters.dbfield(help="e.g. 41-1"),
        'introduced': APIFilters.dbfield(filter_types=APIFilters.numeric_filters,
            help="date bill was introduced, e.g. introduced__gt=2010-01-01"),
        'legisinfo_id': APIFilters.dbfield(help="integer ID assigned by parl.gc.ca's LEGISinfo"),
        'number': APIFilters.dbfield('bill__number',
            help="a string, not an integer: e.g. C-10"),
        'law': APIFilters.dbfield('bill__law',
            help="did it become law? True, False"),
        'private_member_bill': APIFilters.dbfield('bill__privatemember',
            help="is it a private member's bill? True, False"),
        'sponsor_politician': APIFilters.politician('sponsor_politician'),
        'sponsor_politician_membership': APIFilters.fkey(lambda u: {'sponsor_member': u[-1]}),
    }

    def get_qs(self, request):
        return BillInSession.objects.all().select_related('bill', 'sponsor_politician')

    def get_html(self, request):
        sessions = Session.objects.with_bills()
        len(sessions) # evaluate it
        bills = Bill.objects.filter(sessions=sessions[0])
        votes = VoteQuestion.objects.select_related('bill').filter(session=sessions[0])[:6]

        t = loader.get_template('bills/index.html')
        c = RequestContext(request, {
            'object_list': bills,
            'session_list': sessions,
            'votes': votes,
            'session': sessions[0],
            'title': 'Bills & Votes'
        })

        return HttpResponse(t.render(c))
index = BillListView.as_view()


class BillSessionListView(ModelListView):

    def get_json(self, request, session_id):
        return HttpResponseRedirect(urlresolvers.reverse('bills') + '?'
                                    + urlencode({'session': session_id}))

    def get_html(self, request, session_id):
        session = get_object_or_404(Session, pk=session_id)
        bills = Bill.objects.filter(sessions=session)
        votes = VoteQuestion.objects.select_related('bill').filter(session=session)[:6]

        t = loader.get_template('bills/bill_list.html')
        c = RequestContext(request, {
            'object_list': bills,
            'session': session,
            'votes': votes,
            'title': 'Bills for the %s' % session
        })
        return HttpResponse(t.render(c))
bills_for_session = BillSessionListView.as_view()


class VoteListView(ModelListView):

    resource_name = 'Votes'

    api_notes = mark_safe("""<p>What we call votes are <b>divisions</b> in official Parliamentary lingo.
        We refer to an individual person's vote as a <a href="/votes/ballots/">ballot</a>.</p>
    """)

    filters = {
        'session': APIFilters.dbfield(help="e.g. 41-1"),
        'yea_total': APIFilters.dbfield(filter_types=APIFilters.numeric_filters,
            help="# votes for"),
        'nay_total': APIFilters.dbfield(filter_types=APIFilters.numeric_filters,
            help="# votes against, e.g. nay_total__gt=10"),
        'paired_total': APIFilters.dbfield(filter_types=APIFilters.numeric_filters,
            help="paired votes are an odd convention that seem to have stopped in 2011"),
        'date': APIFilters.dbfield(filter_types=APIFilters.numeric_filters,
            help="date__gte=2011-01-01"),
        'number': APIFilters.dbfield(filter_types=APIFilters.numeric_filters,
            help="every vote in a session has a sequential number"),
        'bill': APIFilters.fkey(lambda u: {
            'bill__sessions': u[-2],
            'bill__number': u[-1]
        }, help="e.g. /bills/41-1/C-10/"),
        'result': APIFilters.choices('result', VoteQuestion)
    }

    def get_json(self, request, session_id=None):
        if session_id:
            return HttpResponseRedirect(urlresolvers.reverse('votes') + '?'
{'fraction_non_alphanumeric': 0.11978087059520284, 'fraction_numerical': 0.009179745336097127, 'mean_word_length': 3.190136476426799, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1972126', 'n_tokens_mistral': 4218, 'n_tokens_neox': 4058, 'n_words': 809}