from django.shortcuts import render
from datetime import date, timedelta
from nyc.models import NYCBill
from councilmatic_core.models import Event, Organization
from councilmatic_core.views import *
from haystack.query import SearchQuerySet

class NYCIndexView(IndexView):
    template_name = 'nyc/index.html'
    bill_model = NYCBill

class NYCAboutView(AboutView):
    template_name = 'nyc/about.html'

class NYCBillDetailView(BillDetailView):
    model = NYCBill

class NYCBillWidgetView(BillWidgetView):
    model = NYCBill

class NYCCommitteesView(CommitteesView):

    def get_queryset(self):
        return []

    def get_context_data(self, **kwargs):
        context = super(CommitteesView, self).get_context_data(**kwargs)

        committees = Organization.committees().filter(name__startswith='Committee')
        context['committees'] = [c for c in committees if c.memberships.all()]

        subcommittees = Organization.committees().filter(name__startswith='Subcommittee')
        context['subcommittees'] = [c for c in subcommittees if c.memberships.all()]

        taskforces = Organization.committees().filter(name__startswith='Task Force')
        context['taskforces'] = [c for c in taskforces if c.memberships.all()]

        return context

class NYCCouncilmaticFacetedSearchView(CouncilmaticFacetedSearchView):

    def build_form(self, form_kwargs=None):
        form = super(CouncilmaticFacetedSearchView, self).build_form(form_kwargs=form_kwargs)

        # For faceted search functionality.
        if form_kwargs is None:
            form_kwargs = {}

        form_kwargs['selected_facets'] = self.request.GET.getlist("selected_facets")

        # For remaining search functionality.
        data = None
        kwargs = {
            'load_all': self.load_all,
        }

        sqs = SearchQuerySet().facet('bill_type')\
                      .facet('sponsorships', sort='index')\
                      .facet('controlling_body')\
                      .facet('inferred_status')\
                      .highlight()

        if form_kwargs:
            kwargs.update(form_kwargs)

        if len(self.request.GET):
            data = self.request.GET
            dataDict = dict(data)

        if self.searchqueryset is not None:
            kwargs['searchqueryset'] = sqs

            try:
                for el in dataDict['sort_by']:
                    # Do this, because sometimes the 'el' may include a '?' from the URL
                    if 'date' in el:
                        try:
                            dataDict['ascending']
                            kwargs['searchqueryset'] = sqs.order_by('last_action_date')
                        except:
                            kwargs['searchqueryset'] = sqs.order_by('-last_action_date')
                    if 'title' in el:
                        try:
                            dataDict['descending']
                            kwargs['searchqueryset'] = sqs.order_by('-sort_name')
                        except:
                            kwargs['searchqueryset'] = sqs.order_by('sort_name')
                    if 'relevance' in el:
                        kwargs['searchqueryset'] = sqs

            except:
                kwargs['searchqueryset'] = sqs.order_by('-last_action_date')

        return self.form_class(data, **kwargs)

@@ -153,17 +153,23 @@
 nization
+, Bill
 %0A
-
 from cou
@@ -241,16 +241,49 @@
 erySet%0A%0A
+from django.http import Http404%0A%0A
 class NY
@@ -474,24 +474,24 @@
 etailView):%0A
-
     model = 
@@ -491,32 +491,741 @@
 odel = NYCBill%0A%0A
+    def get_object(self):%0A        slug = self.kwargs%5B'slug'%5D%0A%0A        print(slug)%0A%0A        try:%0A            bill = self.model.objects.get(slug=slug)%0A        except NYCBill.DoesNotExist:%0A            bill = None%0A%0A        if bill is None:%0A            identifier_title, identifier_number = slug.split('-', 1)%0A            if identifier_title == 'lu':%0A                identifier_title = 'LU'%0A            else:%0A                identifier_title = identifier_title.title()%0A%0A            full_identifier = ' '.join(%5Bidentifier_title, identifier_number%5D)%0A            try:%0A                bill = self.model.objects.get(identifier=full_identifier)%0A            except:%0A                raise Http404()%0A%0A        return bill%0A%0A%0A
 class NYCBillWid

Add redirect for bill