Ejemplo n.º 1
0
def bill_list_current_session(request):
    ''' List of all bills for the current session.
    Organized by latest action.
    '''
    current_session = LegislativeSession.objects.get(name=settings.CURRENT_SESSION)
    subjects = get_all_subjects()

    filters = {
        'legislative_session__name': settings.CURRENT_SESSION
    }

    filter_subjects = []
    search_text = ''

    if request.GET.getlist('subjects'):
        filter_subjects = request.GET.getlist('subjects')
        filters['subject__contains'] = filter_subjects

    if request.GET.get('search_text'):
        search_text = request.GET.get('search_text')
        filters['versions__links__text__ftsearch'] = search_text

    bills = Bill.objects.filter(**filters).order_by(
        '-actions__date').select_related('legislative_session').prefetch_related(
            'sponsorships', 'actions').distinct()[:settings.NUMBER_OF_LATEST_ACTIONS]

    # force DB query now and append latest_action to each bill
    # set is called first to remove duplicates - distinct does not work above because
    # of the 'order-by' paramater - that adds a field and causes distinct to not work as expected
    bills = set(bills)
    for bill in bills:
        # use all() so the prefetched actions can be used, could possibly impove
        # via smarter use of Prefetch()
        all_actions = list(bill.actions.all())
        latest_action = all_actions[-1]
        for action in all_actions:
            date = time.strptime(action.date, '%Y-%m-%d')
            if date > time.strptime(latest_action.date, '%Y-%m-%d'):
                bill.latest_action = action
        bill.latest_action = latest_action

    # resort bills
    bills = sorted(bills, key=lambda b: b.latest_action.date, reverse=True)

    subjects = _mark_selected(subjects, filter_subjects)

    context = {
        'latest_bills': bills,
        'current_session': current_session.name,
        'subjects': subjects,
        'search_text': search_text
    }

    return render(
        request,
        'bills/current_session.html',
        context
    )
Ejemplo n.º 2
0
def bill_list_current_session(request):
    ''' List of all bills for the current session.
    Organized by latest action.
    '''
    current_session = LegislativeSession.objects.get(name=settings.CURRENT_SESSION)
    subjects = get_all_subjects()

    filters = {
        'legislative_session__name': settings.CURRENT_SESSION
    }

    filter_subjects = []
    search_text = ''

    if request.GET.getlist('subjects'):
        filter_subjects = request.GET.getlist('subjects')
        filters['subject__contains'] = filter_subjects

    if request.GET.get('search_text'):
        search_text = request.GET.get('search_text')
        filters['versions__links__text__ftsearch'] = search_text

    bills = Bill.objects.filter(**filters).order_by(
        '-actions__date').select_related('legislative_session').prefetch_related(
            'sponsorships', 'actions').distinct()[:settings.NUMBER_OF_LATEST_ACTIONS]

    # force DB query now and append latest_action to each bill
    # set is called first to remove duplicates - distinct does not work above because
    # of the 'order-by' paramater - that adds a field and causes distinct to not work as expected
    bills = set(bills)
    for bill in bills:
        # use all() so the prefetched actions can be used, could possibly impove
        # via smarter use of Prefetch()
        all_actions = list(bill.actions.all())
        latest_action = all_actions[-1]
        for action in all_actions:
            date = time.strptime(action.date, '%Y-%m-%d')
            if date > time.strptime(latest_action.date, '%Y-%m-%d'):
                bill.latest_action = action
        bill.latest_action = latest_action

    # resort bills
    bills = sorted(bills, key=lambda b: b.latest_action.date, reverse=True)

    subjects = _mark_selected(subjects, filter_subjects)

    context = {
        'latest_bills': bills,
        'current_session': current_session.name,
        'subjects': subjects,
        'search_text': search_text
    }

    return render(
        request,
        'bills/current_session.html',
        context
    )
Ejemplo n.º 3
0
Archivo: views.py Proyecto: tgrauer/tot
def bill_list_by_location(request):
    '''Sort bills based on Location
    '''
    alphalist = True
    locations = get_all_locations()
    current_session = LegislativeSession.objects.get(name=settings.CURRENT_SESSION)

    filter_locations = []

    filters = {'legislative_session': current_session}

    if request.GET.getlist('bill_sorters'):
        filter_locations += request.GET.getlist('bill_sorters')

    if filter_locations:
        filters['extras__places__contains'] = filter_locations

    all_bills = Bill.objects.filter(
        **filters
    ).order_by("title").prefetch_related('legislative_session')

    locations = _mark_selected(locations, filter_locations)

    bills = group_bills_by_sorter(all_bills=all_bills, sorter='location')

    sorted_bills = sort_bills_by_keyword(bills)

    if filter_locations:
        sorted_bills = [bill for bill in sorted_bills if bill['name'] in filter_locations]

    return render(
        request,
        'bills/all.html',
        {
            'bills': sorted_bills,
            'sorter_type': 'location',
            'sorters': locations,
            'current_session': current_session.name,
            'letters': ALL_LETTERS,
            'alphalist': alphalist
        }
    )
Ejemplo n.º 4
0
def bill_list_by_location(request):
    '''Sort bills based on Location
    '''
    alphalist = True
    locations = get_all_locations()
    current_session = LegislativeSession.objects.get(name=settings.CURRENT_SESSION)

    filter_locations = []

    filters = {'legislative_session': current_session}

    if request.GET.getlist('bill_sorters'):
        filter_locations += request.GET.getlist('bill_sorters')

    if filter_locations:
        filters['extras__places__contains'] = filter_locations

    all_bills = Bill.objects.filter(
        **filters
    ).order_by("title").prefetch_related('legislative_session')

    locations = _mark_selected(locations, filter_locations)

    bills = group_bills_by_sorter(all_bills=all_bills, sorter='location')

    sorted_bills = sort_bills_by_keyword(bills)

    if filter_locations:
        sorted_bills = [bill for bill in sorted_bills if bill['name'] in filter_locations]

    return render(
        request,
        'bills/all.html',
        {
            'bills': sorted_bills,
            'sorter_type': 'location',
            'sorters': locations,
            'current_session': current_session.name,
            'letters': ALL_LETTERS,
            'alphalist': alphalist
        }
    )
Ejemplo n.º 5
0
Archivo: views.py Proyecto: tgrauer/tot
def bill_list_by_topic(request):
    alphalist = True
    subjects = get_all_subjects()
    current_session = LegislativeSession.objects.get(name=settings.CURRENT_SESSION)

    filter_subjects = []

    filters = {'legislative_session': current_session}

    if request.GET.getlist('bill_sorters'):
        filter_subjects += request.GET.getlist('bill_sorters')

    if filter_subjects:
        filters['subject__contains'] = filter_subjects

    all_bills = Bill.objects.filter(
        **filters
    ).order_by("title").prefetch_related('legislative_session')

    subjects = _mark_selected(subjects, filter_subjects)

    bills = group_bills_by_sorter(all_bills=all_bills, sorter='subject')

    sorted_bills = sort_bills_by_keyword(bills)

    if filter_subjects:
        sorted_bills = [bill for bill in sorted_bills if bill['name'] in filter_subjects]

    return render(
        request,
        'bills/all.html',
        {
            'bills': sorted_bills,
            'sorter_type': 'subject',
            'sorters': subjects,
            'current_session': current_session.name,
            'letters': ALL_LETTERS,
            'alphalist': alphalist
        }
    )
Ejemplo n.º 6
0
Archivo: views.py Proyecto: tgrauer/tot
def bill_list_by_legislator(request):
    '''Sort bills based on Legislator that's the primary sponsor
    '''
    alphalist = True
    legislators = list(_get_current_people(position='senator'))
    legislators += list(_get_current_people(position='representative'))
    legislators = [person.name for person in legislators]
    current_session = LegislativeSession.objects.get(name=settings.CURRENT_SESSION)

    if request.GET.getlist('bill_sorters'):
        filter_whole_legislators = request.GET.getlist('bill_sorters')
        filter_legislators = [name.split(',')[0].strip() for name in filter_whole_legislators]
        all_bills = Bill.objects.filter(
            legislative_session=current_session,
            sponsorships__name__in=filter_legislators
        ).order_by("title").prefetch_related('legislative_session')
    else:
        filter_whole_legislators = []
        all_bills = Bill.objects.filter(
            legislative_session=current_session
        ).order_by("title").prefetch_related('legislative_session', 'sponsorships')

    legislators = _mark_selected(legislators, filter_whole_legislators)

    bills = group_bills_by_sorter(all_bills=all_bills, sorter='legislator')

    sorted_bills = sort_bills_by_keyword(bills)

    return render(
        request,
        'bills/all.html',
        {
            'bills': sorted_bills,
            'sorter_type': 'legislator',
            'sorters': legislators,
            'current_session': current_session.name,
            'letters': ALL_LETTERS,
            'alphalist': alphalist
        }
    )
Ejemplo n.º 7
0
def bill_list_by_topic(request):
    alphalist = True
    subjects = get_all_subjects()
    current_session = LegislativeSession.objects.get(name=settings.CURRENT_SESSION)

    filter_subjects = []

    filters = {'legislative_session': current_session}

    if request.GET.getlist('bill_sorters'):
        filter_subjects += request.GET.getlist('bill_sorters')

    if filter_subjects:
        filters['subject__contains'] = filter_subjects

    all_bills = Bill.objects.filter(
        **filters
    ).order_by("title").prefetch_related('legislative_session')

    subjects = _mark_selected(subjects, filter_subjects)

    bills = group_bills_by_sorter(all_bills=all_bills, sorter='subject')

    sorted_bills = sort_bills_by_keyword(bills)

    if filter_subjects:
        sorted_bills = [bill for bill in sorted_bills if bill['name'] in filter_subjects]

    return render(
        request,
        'bills/all.html',
        {
            'bills': sorted_bills,
            'sorter_type': 'subject',
            'sorters': subjects,
            'current_session': current_session.name,
            'letters': ALL_LETTERS,
            'alphalist': alphalist
        }
    )
Ejemplo n.º 8
0
def bill_list_by_legislator(request):
    '''Sort bills based on Legislator that's the primary sponsor
    '''
    alphalist = True
    legislators = list(_get_current_people(position='senator'))
    legislators += list(_get_current_people(position='representative'))
    legislators = [person.name for person in legislators]
    current_session = LegislativeSession.objects.get(name=settings.CURRENT_SESSION)

    if request.GET.getlist('bill_sorters'):
        filter_whole_legislators = request.GET.getlist('bill_sorters')
        filter_legislators = [name.split(',')[0].strip() for name in filter_whole_legislators]
        all_bills = Bill.objects.filter(
            legislative_session=current_session,
            sponsorships__name__in=filter_legislators
        ).order_by("title").prefetch_related('legislative_session')
    else:
        filter_whole_legislators = []
        all_bills = Bill.objects.filter(
            legislative_session=current_session
        ).order_by("title").prefetch_related('legislative_session', 'sponsorships')

    legislators = _mark_selected(legislators, filter_whole_legislators)

    bills = group_bills_by_sorter(all_bills=all_bills, sorter='legislator')

    sorted_bills = sort_bills_by_keyword(bills)

    return render(
        request,
        'bills/all.html',
        {
            'bills': sorted_bills,
            'sorter_type': 'legislator',
            'sorters': legislators,
            'current_session': current_session.name,
            'letters': ALL_LETTERS,
            'alphalist': alphalist
        }
    )
Ejemplo n.º 9
0
Archivo: views.py Proyecto: tgrauer/tot
def bill_list_latest(request):
    ''' List of bills with a latest action for the current session, based on preferences.
    Organized by topic, legislator, topic, and then by latest action.
    '''
    user = request.user
    current_session = LegislativeSession.objects.get(name=settings.CURRENT_SESSION)

    context = {
        'user': user,
        'current_session': current_session
    }

    filters = {
        'legislative_session__name': settings.CURRENT_SESSION
    }

    all_bills = Bill.objects.filter(**filters).order_by(
        '-actions__date').select_related('legislative_session').prefetch_related(
            'sponsorships', 'actions')

    # This will be a list of dicts.
    # There will be one entry for each of the topics, locations,legislators followed.
    bills_by_selected_filter = []

    if user.is_anonymous():
        subjects = get_all_subjects()
        locations = get_all_locations()

        people, topics, selected_locations = get_anonymous_selections(request)

        subjects = _mark_selected(subjects, topics)
        locations = _mark_selected(locations, selected_locations)

        context['subjects'] = subjects
        context['locations'] = locations

    else:
        people, topics, selected_locations = get_user_preferences(user)

    if people:
        for person in people:
            person_name = Person.objects.get(id=person).name
            person_bills = all_bills.filter(sponsorships__person_id=person)[:settings.NUMBER_OF_LATEST_ACTIONS]
            person_detail = {'heading': person_name, 'bills': person_bills}
            bills_by_selected_filter.append(person_detail)
    if topics:
        for topic in topics:
            topic_bills = all_bills.filter(subject__contains=[topic])[:settings.NUMBER_OF_LATEST_ACTIONS]
            topic_detail = {'heading': topic, 'bills': topic_bills}
            bills_by_selected_filter.append(topic_detail)

    if selected_locations:
        for place in selected_locations:
            location_bills = all_bills.filter(extras__places__in=place)
            location_detail = {'heading': place, 'bills': location_bills}
            bills_by_selected_filter.append(location_detail)

    for item in bills_by_selected_filter:
        for bill in item['bills']:
        # use all() so the prefetched actions can be used, could possibly impove
        # via smarter use of Prefetch()
            bill.latest_action = list(bill.actions.all())[-1]

    context['bills_by_selected_filter'] = bills_by_selected_filter

    return render(
        request,
        'bills/latest_actions.html',
        context
    )
Ejemplo n.º 10
0
def bill_list_latest(request):
    ''' List of bills with a latest action for the current session, based on preferences.
    Organized by topic, legislator, topic, and then by latest action.
    '''
    user = request.user
    current_session = LegislativeSession.objects.get(name=settings.CURRENT_SESSION)

    context = {
        'user': user,
        'current_session': current_session
    }

    filters = {
        'legislative_session__name': settings.CURRENT_SESSION
    }

    all_bills = Bill.objects.filter(**filters).order_by(
        '-actions__date').select_related('legislative_session').prefetch_related(
            'sponsorships', 'actions')

    # This will be a list of dicts.
    # There will be one entry for each of the topics, locations,legislators followed.
    bills_by_selected_filter = []

    if user.is_anonymous():
        subjects = get_all_subjects()
        locations = get_all_locations()

        people, topics, selected_locations = get_anonymous_selections(request)

        subjects = _mark_selected(subjects, topics)
        locations = _mark_selected(locations, selected_locations)

        context['subjects'] = subjects
        context['locations'] = locations

    else:
        people, topics, selected_locations = get_user_preferences(user)

    if people:
        for person in people:
            person_name = Person.objects.get(id=person).name
            person_bills = all_bills.filter(sponsorships__person_id=person)[:settings.NUMBER_OF_LATEST_ACTIONS]
            person_detail = {'heading': person_name, 'bills': person_bills}
            bills_by_selected_filter.append(person_detail)
    if topics:
        for topic in topics:
            topic_bills = all_bills.filter(subject__contains=[topic])[:settings.NUMBER_OF_LATEST_ACTIONS]
            topic_detail = {'heading': topic, 'bills': topic_bills}
            bills_by_selected_filter.append(topic_detail)

    if selected_locations:
        for place in selected_locations:
            location_bills = all_bills.filter(extras__places__in=place)
            location_detail = {'heading': place, 'bills': location_bills}
            bills_by_selected_filter.append(location_detail)

    for item in bills_by_selected_filter:
        for bill in item['bills']:
        # use all() so the prefetched actions can be used, could possibly impove
        # via smarter use of Prefetch()
            bill.latest_action = list(bill.actions.all())[-1]

    context['bills_by_selected_filter'] = bills_by_selected_filter

    return render(
        request,
        'bills/latest_actions.html',
        context
    )