Пример #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
    )
Пример #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
    )
Пример #3
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
        }
    )
Пример #4
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
        }
    )
Пример #5
0
def user_preferences(request):
    user = request.user

    senators = _get_current_people(position='senator')
    representatives = _get_current_people(position='representative')
    locations = get_all_locations()
    subjects = get_all_subjects()

    people_followed = [
        individual.person
        for individual in PersonFollow.objects.filter(user=user)
    ]
    subjects_followed = [
        subject.topic for subject in TopicFollow.objects.filter(user=user)
    ]
    locations_followed = [
        location.location
        for location in LocationFollow.objects.filter(user=user)
    ]
    bills_followed = [
        bill.bill for bill in BillFollow.objects.filter(user=user)
    ]

    selected_reps = _mark_selected(representatives, people_followed)
    selected_senators = _mark_selected(senators, people_followed)
    selected_subjects = _mark_selected(subjects, subjects_followed)
    selected_locations = _mark_selected(locations, locations_followed)

    preferences, _ = Preferences.objects.get_or_create(user=user)

    # Defaults to the lat an lon of the Tallahassee Statehouse!
    lat = preferences.lat or 30.407741
    lng = preferences.lon or -84.2705644

    error_message = None
    address_senator = None
    address_representative = None
    if preferences.sen_from_address and preferences.rep_from_address:
        address_senator = json.loads(preferences.sen_from_address)
        address_representative = json.loads(preferences.rep_from_address)
        if address_senator['name'] == 'none found':
            error_message = 'No senators could be found for your query: Please make sure you have entered a valid FL address'

    if request.method == 'POST':
        with transaction.atomic():
            PersonFollow.objects.filter(user=user).delete()
            TopicFollow.objects.filter(user=user).delete()
            LocationFollow.objects.filter(user=user).delete()
            for senator in request.POST.getlist('senators'):
                PersonFollow.objects.create(user=user, person_id=senator)
            for representative in request.POST.getlist('representatives'):
                PersonFollow.objects.create(user=user,
                                            person_id=representative)
            for location in request.POST.getlist('locations'):
                LocationFollow.objects.create(user=user, location=location)
            for subject in request.POST.getlist('subjects'):
                TopicFollow.objects.create(user=user, topic=subject)

            preferences.email_frequency = request.POST.get(
                'email_frequency', 'N')
            preferences.email_type = request.POST.get('email_type', 'T')
            preferences.save()

        return redirect('.')

    return render(
        request, 'preferences/preferences.html', {
            'user': user,
            'senators': selected_senators,
            'representatives': selected_reps,
            'locations': selected_locations,
            'subjects': selected_subjects,
            'address': preferences.address,
            'address_senator': address_senator,
            'address_representative': address_representative,
            'error_message': error_message,
            'lat': lat,
            'lng': lng,
            'preferences': preferences,
            'email_frequencies': EMAIL_FREQUENCIES,
            'email_types': EMAIL_TYPES,
            'bills_followed': bills_followed
        })
Пример #6
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
    )
Пример #7
0
def user_preferences(request):
    user = request.user

    senators = _get_current_people(position='senator')
    representatives = _get_current_people(position='representative')
    locations = get_all_locations()
    subjects = get_all_subjects()

    people_followed = [individual.person for individual in PersonFollow.objects.filter(user=user)]
    subjects_followed = [subject.topic for subject in TopicFollow.objects.filter(user=user)]
    locations_followed = [location.location for location in LocationFollow.objects.filter(user=user)]

    selected_reps = _mark_selected(representatives, people_followed)
    selected_senators = _mark_selected(senators, people_followed)
    selected_subjects = _mark_selected(subjects, subjects_followed)
    selected_locations = _mark_selected(locations, locations_followed)

    preferences, _ = Preferences.objects.get_or_create(user=user)

    # Defaults to the lat an lon of the Tallahassee Statehouse!
    lat = preferences.lat or 30.407741
    lng = preferences.lon or -84.2705644

    error_message = None
    address_senator = None
    address_representative = None
    if preferences.sen_from_address and preferences.rep_from_address:
        address_senator = json.loads(preferences.sen_from_address)
        address_representative = json.loads(preferences.rep_from_address)
        if address_senator['name'] == 'none found':
            error_message = 'No senators could be found for your query: Please make sure you have entered a valid FL address'

    if request.method == 'POST':
        with transaction.atomic():
            PersonFollow.objects.filter(user=user).delete()
            TopicFollow.objects.filter(user=user).delete()
            LocationFollow.objects.filter(user=user).delete()
            for senator in request.POST.getlist('senators'):
                PersonFollow.objects.create(user=user, person_id=senator)
            for representative in request.POST.getlist('representatives'):
                PersonFollow.objects.create(user=user, person_id=representative)
            for location in request.POST.getlist('locations'):
                LocationFollow.objects.create(user=user, location=location)
            for subject in request.POST.getlist('subjects'):
                TopicFollow.objects.create(user=user, topic=subject)

        return redirect('.')

    return render(
        request,
        'preferences/preferences.html',
        {
            'user': user,
            'senators': selected_senators,
            'representatives': selected_reps,
            'locations': selected_locations,
            'subjects': selected_subjects,
            'address': preferences.address,
            'address_senator': address_senator,
            'address_representative': address_representative,
            'error_message': error_message,
            'lat': lat,
            'lng': lng
        }
    )
Пример #8
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
    )