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 } )
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 })
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 )
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 } )