def get_queryset(self): FilterBillsForm = get_filter_bills_form(None) # Setup the paginator. get = self.request.GET.get show_per_page = getattr(self, 'show_per_page', 10) show_per_page = int(get('show_per_page', show_per_page)) page = int(get('page', 1)) if 100 < show_per_page: show_per_page = 100 if not self.request.GET: spec = {} cursor = db.bills.find(spec) cursor.sort([('updated_at', pymongo.DESCENDING)]) return self.paginator(cursor, page=page, show_per_page=show_per_page) form = FilterBillsForm(self.request.GET) params = [ 'chamber', 'subjects', 'sponsor__leg_id', 'actions__type', 'type'] search_text = form.data.get('search_text') if settings.ENABLE_ELASTICSEARCH: kwargs = {} chamber = form.data.get('chamber') if chamber: kwargs['chamber'] = chamber subjects = form.data.getlist('subjects') if subjects: kwargs['subjects'] = {'$all': filter(None, subjects)} cursor = Bill.search(search_text, **kwargs) cursor.sort([('updated_at', pymongo.DESCENDING)]) else: # Elastic search not enabled--query mongo normally. # Mainly here for local work on search views. for key in params: val = form.data.get(key) if val: key = key.replace('__', '.') spec[key] = val if search_text: spec['title'] = {'$regex': search_text, '$options': 'i'} cursor = db.bills.find(spec) cursor.sort([('updated_at', pymongo.DESCENDING)]) return self.paginator(cursor, page=page, show_per_page=show_per_page)
def read(self, request): bill_fields = {'title': 1, 'created_at': 1, 'updated_at': 1, 'bill_id': 1, 'type': 1, 'state': 1, 'level': 1, 'country': 1, 'session': 1, 'chamber': 1, 'subjects': 1, '_type': 1, 'id': 1} # replace with request's fields if they exist bill_fields = _build_field_list(request, bill_fields) # normal mongo search logic base_fields = _build_mongo_filter(request, ('state', 'chamber', 'subjects', 'bill_id', 'bill_id__in')) # process extra attributes query = request.GET.get('q') search_window = request.GET.get('search_window', 'all') since = request.GET.get('updated_since', None) sponsor_id = request.GET.get('sponsor_id') try: query = Bill.search(query, search_window=search_window, updated_since=since, sponsor_id=sponsor_id, bill_fields=bill_fields, **base_fields) except ValueError as e: resp = rc.BAD_REQUEST resp.write(': %s' % e) return resp # add pagination page = request.GET.get('page') per_page = request.GET.get('per_page') if page and not per_page: per_page = 50 if per_page and not page: page = 1 if page: page = int(page) per_page = int(per_page) query = query.limit(per_page).skip(per_page*(page-1)) else: # limit response size if query.count() > 5000: resp = rc.BAD_REQUEST resp.write(': request too large, try narrowing your search by ' 'adding more filters.') return resp # sorting sort = request.GET.get('sort') if sort == 'updated_at': query = query.sort([('updated_at', -1)]) elif sort == 'created_at': query = query.sort([('created_at', -1)]) return list(query)
def search(request, abbr): ''' Context: - search_text - abbr - metadata - found_by_id - bill_results - more_bills_available - legislators_list - nav_active Tempaltes: - billy/web/public/search_results_no_query.html - billy/web/public/search_results_bills_legislators.html - billy/web/public/bills_list_row_with_abbr_and_session.html ''' if not request.GET: return render(request, templatename('search_results_no_query'), {'abbr': abbr}) search_text = unicode(request.GET['search_text']).encode('utf8') # First try to get by bill_id. if re.search(r'\d', search_text): url = '/%s/bills?' % abbr url += urllib.urlencode([('search_text', search_text)]) return redirect(url) else: found_by_id = False kwargs = {} if abbr != 'all': kwargs['abbr'] = abbr bill_results = Bill.search(search_text, sort='last', **kwargs) # Limit the bills if it's a search. bill_result_count = len(bill_results) more_bills_available = (bill_result_count > 5) bill_results = bill_results[:5] # See if any legislator names match. First split up name to avoid # the Richard S. Madaleno problem. See Jira issue OS-32. textbits = search_text.split() textbits = filter(lambda s: 2 < len(s), textbits) textbits = filter(lambda s: '.' not in s, textbits) andspec = [] for text in textbits: andspec.append({'full_name': {'$regex': text, '$options': 'i'}}) if andspec: spec = {'$and': andspec} else: spec = {'full_name': {'$regex': search_text, '$options': 'i'}} # Run the query. if abbr != 'all': spec[settings.LEVEL_FIELD] = abbr legislator_results = list( db.legislators.find(spec).sort([('active', -1)])) if abbr != 'all': metadata = Metadata.get_object(abbr) else: metadata = None return render( request, templatename('search_results_bills_legislators'), dict( search_text=search_text, abbr=abbr, metadata=metadata, found_by_id=found_by_id, bill_results=bill_results, bill_result_count=bill_result_count, more_bills_available=more_bills_available, legislators_list=legislator_results, column_headers_tmplname=None, # not used rowtemplate_name=templatename('bills_list_row_with' '_abbr_and_session'), show_chamber_column=True, nav_active=None))
def search(request, abbr): ''' Context: - search_text - abbr - metadata - found_by_id - bill_results - more_bills_available - legislators_list - more_legislators_available - bill_column_headers_tmplname - nav_active Tempaltes: - billy/web/public/search_results_no_query.html - billy/web/public/search_results_bills_legislators.html - billy/web/public/bills_list_row_with_abbr_and_session.html ''' if not request.GET: return render(request, templatename('search_results_no_query'), {'abbr': abbr}) search_text = request.GET['search_text'] # First try to get by bill_id. if re.search(r'\d', search_text): url = '/%s/bills?' % abbr url += urllib.urlencode([('search_text', search_text)]) return redirect(url) else: found_by_id = False kwargs = {} if abbr != 'all': kwargs['abbr'] = abbr bill_results = Bill.search(search_text, **kwargs) # add sorting bill_results = bill_results.sort([('action_dates.last', -1)]) # Limit the bills if it's a search. more_bills_available = (5 < bill_results.count()) bill_results = bill_results.limit(5) # See if any legislator names match. spec = {'full_name': {'$regex': search_text, '$options': 'i'}} if abbr != 'all': spec[settings.LEVEL_FIELD] = abbr legislator_results = db.legislators.find(spec) more_legislators_available = (5 < legislator_results.count()) legislator_results = legislator_results.limit(5) if abbr != 'all': metadata = Metadata.get_object(abbr) else: metadata = None return render(request, templatename('search_results_bills_legislators'), dict(search_text=search_text, abbr=abbr, metadata=metadata, found_by_id=found_by_id, bill_results=bill_results, more_bills_available=more_bills_available, legislators_list=legislator_results, more_legislators_available=more_legislators_available, column_headers_tmplname=templatename('search_column_headers'), rowtemplate_name=templatename('bills_list_row_with' '_abbr_and_session'), show_chamber_column=True, nav_active=None))
def get_queryset(self): abbr = self.kwargs['abbr'] if abbr != 'all': metadata = Metadata.get_object(abbr) else: metadata = None FilterBillsForm = get_filter_bills_form(metadata) # start with the spec spec = {} if abbr != 'all': spec['abbr'] = abbr # Setup the paginator. get = self.request.GET.get show_per_page = getattr(self, 'show_per_page', 10) show_per_page = int(get('show_per_page', show_per_page)) try: page = int(get('page', 1)) except ValueError: raise Http404('no such page') if show_per_page > 100: show_per_page = 100 # If search params are given: form = FilterBillsForm(self.request.GET) search_text = form.data.get('search_text') if form.data: form_abbr = form.data.get('abbr') if form_abbr: spec['abbr'] = form_abbr chamber = form.data.get('chamber') if chamber: spec['chamber'] = chamber subjects = form.data.getlist('subjects') if subjects: spec['subjects'] = subjects sponsor_id = form.data.get('sponsor__leg_id') if sponsor_id: spec['sponsor_id'] = sponsor_id if 'status' in form.data: status_choices = form.data.getlist('status') spec['status'] = status_choices type_ = form.data.get('type') if type_: spec['type_'] = type_ session = form.data.get('session') if session: spec['session'] = session sort = self.request.GET.get('sort', 'last') result = Bill.search(search_text, sort=sort, **spec) return self.paginator(result, page=page, show_per_page=show_per_page)
def read(self, request): bill_fields = {'title': 1, 'created_at': 1, 'updated_at': 1, 'bill_id': 1, 'type': 1, settings.LEVEL_FIELD: 1, 'session': 1, 'chamber': 1, 'subjects': 1, '_type': 1, 'id': 1} # replace with request's fields if they exist bill_fields = _build_field_list(request, bill_fields) # normal mongo search logic base_fields = _build_mongo_filter(request, ('chamber', 'bill_id', 'bill_id__in')) # process extra attributes query = request.GET.get('q') abbr = request.GET.get(settings.LEVEL_FIELD) if abbr: abbr = abbr.lower() search_window = request.GET.get('search_window', 'all') since = request.GET.get('updated_since', None) last_action_since = request.GET.get('last_action_since', None) sponsor_id = request.GET.get('sponsor_id') subjects = request.GET.getlist('subject') type_ = request.GET.get('type') status = request.GET.getlist('status') # sorting sort = request.GET.get('sort', 'last') if sort == 'last_action': sort = 'last' try: query = Bill.search(query, abbr=abbr, search_window=search_window, updated_since=since, last_action_since=last_action_since, sponsor_id=sponsor_id, subjects=subjects, type_=type_, status=status, sort=sort, bill_fields=bill_fields, **base_fields) except ValueError as e: resp = rc.BAD_REQUEST resp.write('%s' % e) return resp # add pagination page = request.GET.get('page') per_page = request.GET.get('per_page') if page and not per_page: per_page = 50 if per_page and not page: page = 1 if page: page = int(page) per_page = int(per_page) start = per_page * (page - 1) end = start + per_page bills = query[start:end] else: # limit response size if len(query) > 10000: resp = rc.BAD_REQUEST resp.write('request too large, try narrowing your search by ' 'adding more filters.') return resp bills = query[:] bills = list(bills) # attach votes if necessary bill_ids = [bill['_id'] for bill in bills] vote_fields = _get_vote_fields(bill_fields) or [] if 'votes' in bill_fields or vote_fields: # add bill_id to vote_fields for relating back votes = list(db.votes.find({'bill_id': {'$in': bill_ids}}, fields=vote_fields + ['bill_id'])) votes_by_bill = defaultdict(list) for vote in votes: votes_by_bill[vote['bill_id']].append(vote) # remove bill_id unless they really requested it if 'bill_id' not in vote_fields: vote.pop('bill_id') for bill in bills: bill['votes'] = votes_by_bill[bill['_id']] return bills
def search(request, abbr): if not request.GET: return render(request, templatename('search_results_no_query'), {'abbr': abbr}) search_text = request.GET['search_text'] # First try to get by bill_id. if re.search(r'\d', search_text): url = '/%s/bills?' % abbr url += urllib.urlencode([('search_text', search_text)]) return redirect(url) else: found_by_id = False if settings.ENABLE_ELASTICSEARCH: kwargs = {} if abbr != 'all': kwargs['state'] = abbr bill_results = Bill.search(search_text, **kwargs) else: spec = {'title': {'$regex': search_text, '$options': 'i'}} if abbr != 'all': spec.update(state=abbr) bill_results = db.bills.find(spec) # add sorting bill_results = bill_results.sort([('action_dates.last', -1)]) # Limit the bills if it's a search. more_bills_available = (5 < bill_results.count()) bill_results = bill_results.limit(5) # See if any legislator names match. spec = {'full_name': {'$regex': search_text, '$options': 'i'}} if abbr != 'all': spec.update(state=abbr) legislator_results = db.legislators.find(spec) more_legislators_available = (5 < legislator_results.count()) legislator_results = legislator_results.limit(5) if abbr != 'all': metadata = Metadata.get_object(abbr) else: metadata = None return render(request, templatename('search_results_bills_legislators'), dict(search_text=search_text, abbr=abbr, metadata=metadata, found_by_id=found_by_id, bill_results=bill_results, more_bills_available=more_bills_available, legislators_list=legislator_results, more_legislators_available=more_legislators_available, bill_column_headers=('State', 'Title', 'Session', 'Introduced', 'Recent Action',), rowtemplate_name=templatename('bills_list_row_with' '_state_and_session'), show_chamber_column=True, statenav_active=None))
def read(self, request): bill_fields = { 'title': 1, 'created_at': 1, 'updated_at': 1, 'bill_id': 1, 'type': 1, settings.LEVEL_FIELD: 1, 'session': 1, 'chamber': 1, 'subjects': 1, '_type': 1, 'id': 1 } # replace with request's fields if they exist bill_fields = _build_field_list(request, bill_fields) # normal mongo search logic base_fields = _build_mongo_filter( request, ('chamber', 'bill_id', 'bill_id__in')) # process extra attributes query = request.GET.get('q') abbr = request.GET.get(settings.LEVEL_FIELD) if abbr: abbr = abbr.lower() search_window = request.GET.get('search_window', 'all') since = request.GET.get('updated_since', None) last_action_since = request.GET.get('last_action_since', None) sponsor_id = request.GET.get('sponsor_id') subjects = request.GET.getlist('subject') type_ = request.GET.get('type') status = request.GET.getlist('status') # sorting sort = request.GET.get('sort', 'last') if sort == 'last_action': sort = 'last' try: query = Bill.search(query, abbr=abbr, search_window=search_window, updated_since=since, last_action_since=last_action_since, sponsor_id=sponsor_id, subjects=subjects, type_=type_, status=status, sort=sort, bill_fields=bill_fields, **base_fields) except ValueError as e: resp = rc.BAD_REQUEST resp.write('%s' % e) return resp # add pagination page = request.GET.get('page') per_page = request.GET.get('per_page') if page and not per_page: per_page = 50 if per_page and not page: page = 1 if page: page = int(page) per_page = int(per_page) start = per_page * (page - 1) end = start + per_page bills = query[start:end] else: # limit response size if len(query) > 10000: resp = rc.BAD_REQUEST resp.write('request too large, try narrowing your search by ' 'adding more filters.') return resp bills = query[:] bills = list(bills) # attach votes if necessary bill_ids = [bill['_id'] for bill in bills] vote_fields = _get_vote_fields(bill_fields) or [] if 'votes' in bill_fields or vote_fields: # add bill_id to vote_fields for relating back votes = list( db.votes.find({'bill_id': { '$in': bill_ids }}, fields=vote_fields + ['bill_id'])) votes_by_bill = defaultdict(list) for vote in votes: votes_by_bill[vote['bill_id']].append(vote) # remove bill_id unless they really requested it if 'bill_id' not in vote_fields: vote.pop('bill_id') for bill in bills: bill['votes'] = votes_by_bill[bill['_id']] return bills
def get_queryset(self): abbr = self.kwargs["abbr"] if abbr != "all": metadata = Metadata.get_object(abbr) else: metadata = None FilterBillsForm = get_filter_bills_form(metadata) # start with the spec spec = {} if abbr != "all": spec["abbr"] = abbr # Setup the paginator. get = self.request.GET.get show_per_page = getattr(self, "show_per_page", 10) show_per_page = int(get("show_per_page", show_per_page)) try: page = int(get("page", 1)) except ValueError: raise Http404("no such page") if show_per_page > 100: show_per_page = 100 # If search params are given: form = FilterBillsForm(self.request.GET) search_text = form.data.get("search_text") if form.data: form_abbr = form.data.get("abbr") if form_abbr: spec["abbr"] = form_abbr chamber = form.data.get("chamber") if chamber: spec["chamber"] = chamber subjects = form.data.getlist("subjects") if subjects: spec["subjects"] = subjects sponsor_id = form.data.get("sponsor__leg_id") if sponsor_id: spec["sponsor_id"] = sponsor_id if "status" in form.data: status_choices = form.data.getlist("status") spec["status"] = status_choices type_ = form.data.get("type") if type_: spec["type_"] = type_ session = form.data.get("session") if session: spec["session"] = session sort = self.request.GET.get("sort", "last") result = Bill.search(search_text, sort=sort, **spec) return self.paginator(result, page=page, show_per_page=show_per_page)
def search(request, abbr): ''' Context: - search_text - abbr - metadata - found_by_id - bill_results - more_bills_available - legislators_list - nav_active Tempaltes: - billy/web/public/search_results_no_query.html - billy/web/public/search_results_bills_legislators.html - billy/web/public/bills_list_row_with_abbr_and_session.html ''' if not request.GET: return render(request, templatename('search_results_no_query'), {'abbr': abbr}) search_text = unicode(request.GET['search_text']).encode('utf8') # First try to get by bill_id. if re.search(r'\d', search_text): url = '/%s/bills?' % abbr url += urllib.urlencode([('search_text', search_text)]) return redirect(url) else: found_by_id = False kwargs = {} if abbr != 'all': kwargs['abbr'] = abbr bill_results = Bill.search(search_text, sort='last', **kwargs) # Limit the bills if it's a search. more_bills_available = (len(bill_results) > 5) bill_result_count = len(bill_results) bill_results = bill_results[:5] # See if any legislator names match. First split up name to avoid # the Richard S. Madaleno problem. See Jira issue OS-32. textbits = search_text.split() textbits = filter(lambda s: 2 < len(s), textbits) textbits = filter(lambda s: '.' not in s, textbits) andspec = [] for text in textbits: andspec.append({'full_name': {'$regex': text, '$options': 'i'}}) if andspec: spec = {'$and': andspec} else: spec = {'full_name': {'$regex': search_text, '$options': 'i'}} # Run the query. if abbr != 'all': spec[settings.LEVEL_FIELD] = abbr legislator_results = list(db.legislators.find(spec).sort( [('active', -1)])) if abbr != 'all': metadata = Metadata.get_object(abbr) else: metadata = None return render( request, templatename('search_results_bills_legislators'), dict(search_text=search_text, abbr=abbr, metadata=metadata, found_by_id=found_by_id, bill_results=bill_results, bill_result_count=bill_result_count, more_bills_available=more_bills_available, legislators_list=legislator_results, column_headers_tmplname=None, # not used rowtemplate_name=templatename('bills_list_row_with' '_abbr_and_session'), show_chamber_column=True, nav_active=None))
def get_queryset(self): abbr = self.kwargs['abbr'] if abbr != 'all': metadata = Metadata.get_object(abbr) else: metadata = None FilterBillsForm = get_filter_bills_form(metadata) # start with the spec spec = {} if abbr != 'all': spec['abbr'] = abbr # Setup the paginator. get = self.request.GET.get show_per_page = getattr(self, 'show_per_page', 10) show_per_page = int(get('show_per_page', show_per_page)) page = int(get('page', 1)) if show_per_page > 100: show_per_page = 100 # If search params are given: form = FilterBillsForm(self.request.GET) search_text = form.data.get('search_text') chamber = form.data.get('chamber') if chamber: spec['chamber'] = chamber subjects = form.data.get('subjects') if subjects: spec['subjects'] = {'$all': [filter(None, subjects)]} sponsor_id = form.data.get('sponsor__leg_id') if sponsor_id: spec['sponsor_id'] = sponsor_id status = form.data.get('status') if status: spec['status'] = {'action_dates.%s' % status: {'$ne': None}} type_ = form.data.get('type') if type_: spec['type_'] = type_ session = form.data.get('session') if session: spec['session'] = session cursor = Bill.search(search_text, **spec) sort = self.request.GET.get('sort', 'last') if sort not in ('first', 'last', 'signed', 'passed_upper', 'passed_lower'): sort = 'last' sort_key = 'action_dates.{0}'.format(sort) # do sorting on the cursor cursor.sort([(sort_key, pymongo.DESCENDING)]) return self.paginator(cursor, page=page, show_per_page=show_per_page)
def search(request, scope): abbr = None search_text = request.GET['q'] scope_name = None spec = {} # If the input looks like a bill id, try to fetch the bill. if re.search(r'\d', search_text): bill_id = fix_bill_id(search_text).upper() collection = db.bills spec.update(bill_id=bill_id) if scope != 'all': abbr = scope docs = collection.find(spec, limit=10) # If there were actual results, return a bill_id result view. if 0 < docs.count(): def sortkey(doc): session = doc['session'] years = re.findall(r'\d{4}', session) try: return int(years[-1]) except IndexError: return session docs = sorted(docs, key=operator.itemgetter('session'), reverse=True) return render(request, templatename('search_results_bill_id'), dict(bill_id=bill_id, abbr=abbr, rowtemplate_name=templatename('bills_list_row_with_state_and_session'), object_list=IteratorPaginator(docs), use_table=True, column_headers=('Title', 'Session', 'Introduced', 'Recent Action', 'Votes'), statenav_active=None)) # The input didn't contain \d{4}, so assuming it's not a bill, # search bill title and legislator names. if settings.ENABLE_ELASTICSEARCH: kwargs = {} if scope != 'all': kwargs['state'] = scope bill_results = Bill.search(search_text, **kwargs) else: spec = {'title': {'$regex': search_text, '$options': 'i'}} if scope != 'all': abbr = scope scope_name = Metadata.get_object(abbr)['name'] spec.update(state=abbr) bill_results = db.bills.find(spec) # See if any legislator names match. spec = {'full_name': {'$regex': search_text, '$options': 'i'}} if scope != 'all': abbr = scope scope_name = Metadata.get_object(abbr)['name'] spec.update(state=abbr) legislator_results = db.legislators.find(spec) return render(request, templatename('search_results_bills_legislators'), dict(search_text=search_text, abbr=abbr, scope_name=scope_name, bills_list=bill_results.limit(5), more_bills_available=(5 < bill_results.count()), legislators_list=legislator_results.limit(5), more_legislators_available=(5 < legislator_results.count()), bill_column_headers=('State', 'Title', 'Session', 'Introduced', 'Recent Action',), rowtemplate_name=templatename('bills_list_row_with_state_and_session'), show_chamber_column=True, statenav_active=None))
def get_queryset(self): abbr = self.kwargs['abbr'] if abbr != 'all': metadata = Metadata.get_object(abbr) else: metadata = None FilterBillsForm = get_filter_bills_form(metadata) # start with the spec spec = {} if abbr != 'all': spec['state'] = abbr # Setup the paginator. get = self.request.GET.get show_per_page = getattr(self, 'show_per_page', 10) show_per_page = int(get('show_per_page', show_per_page)) page = int(get('page', 1)) if show_per_page > 100: show_per_page = 100 # If search params are given: form = FilterBillsForm(self.request.GET) # First try to get by bill_id. search_text = form.data.get('search_text') if search_text: found_by_bill_id = search_by_bill_id(self.kwargs['abbr'], search_text) if found_by_bill_id: return IteratorPaginator(found_by_bill_id) if settings.ENABLE_ELASTICSEARCH: chamber = form.data.get('chamber') if chamber: spec['chamber'] = chamber subjects = form.data.get('subjects') if subjects: spec['subjects'] = {'$all': [filter(None, subjects)]} sponsor_id = form.data.get('sponsor__leg_id') if sponsor_id: spec['sponsor_id'] = sponsor_id status = form.data.get('status') if status: spec['status'] = {'action_dates.%s' % status: {'$ne': None}} type_ = form.data.get('type') if type_: spec['type_'] = type_ session = form.data.get('session') if session: spec['session'] = session cursor = Bill.search(search_text, **spec) else: # Elastic search not enabled--query mongo normally. # Mainly here for local work on search views. params = ['chamber', 'subjects', 'sponsor__leg_id', 'actions__type', 'type', 'status', 'session'] for key in params: val = form.data.get(key) if val: key = key.replace('__', '.') spec[key] = val if search_text: spec['title'] = {'$regex': search_text, '$options': 'i'} cursor = db.bills.find(spec) sort = self.request.GET.get('sort', 'last') if sort not in ('first', 'last', 'signed', 'passed_upper', 'passed_lower'): sort = 'last' sort_key = 'action_dates.{0}'.format(sort) # do sorting on the cursor cursor.sort([(sort_key, pymongo.DESCENDING)]) return self.paginator(cursor, page=page, show_per_page=show_per_page)
def search(request, abbr): ''' Context: - search_text - abbr - metadata - found_by_id - bill_results - more_bills_available - legislators_list - more_legislators_available - nav_active Tempaltes: - billy/web/public/search_results_no_query.html - billy/web/public/search_results_bills_legislators.html - billy/web/public/bills_list_row_with_abbr_and_session.html ''' if not request.GET: return render(request, templatename('search_results_no_query'), {'abbr': abbr}) search_text = request.GET['search_text'] # First try to get by bill_id. if re.search(r'\d', search_text): url = '/%s/bills?' % abbr url += urllib.urlencode([('search_text', search_text)]) return redirect(url) else: found_by_id = False kwargs = {} if abbr != 'all': kwargs['abbr'] = abbr bill_results = Bill.search(search_text, **kwargs) # add sorting bill_results = bill_results.sort([('action_dates.last', -1)]) # Limit the bills if it's a search. more_bills_available = (5 < bill_results.count()) bill_results = bill_results.limit(5) # See if any legislator names match. spec = {'full_name': {'$regex': search_text, '$options': 'i'}} if abbr != 'all': spec[settings.LEVEL_FIELD] = abbr legislator_results = db.legislators.find(spec) more_legislators_available = (5 < legislator_results.count()) legislator_results = legislator_results.limit(5) if abbr != 'all': metadata = Metadata.get_object(abbr) else: metadata = None return render( request, templatename('search_results_bills_legislators'), dict( search_text=search_text, abbr=abbr, metadata=metadata, found_by_id=found_by_id, bill_results=bill_results, more_bills_available=more_bills_available, legislators_list=legislator_results, more_legislators_available=more_legislators_available, column_headers_tmplname=None, # not used rowtemplate_name=templatename('bills_list_row_with' '_abbr_and_session'), show_chamber_column=True, nav_active=None))
def get_queryset(self): abbr = self.kwargs['abbr'] if abbr != 'all': metadata = Metadata.get_object(abbr) else: metadata = None FilterBillsForm = get_filter_bills_form(metadata) # start with the spec spec = {} if abbr != 'all': spec['abbr'] = abbr # Setup the paginator. get = self.request.GET.get show_per_page = getattr(self, 'show_per_page', 10) show_per_page = int(get('show_per_page', show_per_page)) page = int(get('page', 1)) if show_per_page > 100: show_per_page = 100 # If search params are given: form = FilterBillsForm(self.request.GET) # First try to get by bill_id. search_text = form.data.get('search_text') if search_text: found_by_bill_id = search_by_bill_id(self.kwargs['abbr'], search_text) if found_by_bill_id: return IteratorPaginator(found_by_bill_id) chamber = form.data.get('chamber') if chamber: spec['chamber'] = chamber subjects = form.data.get('subjects') if subjects: spec['subjects'] = {'$all': [filter(None, subjects)]} sponsor_id = form.data.get('sponsor__leg_id') if sponsor_id: spec['sponsor_id'] = sponsor_id status = form.data.get('status') if status: spec['status'] = {'action_dates.%s' % status: {'$ne': None}} type_ = form.data.get('type') if type_: spec['type_'] = type_ session = form.data.get('session') if session: spec['session'] = session cursor = Bill.search(search_text, **spec) sort = self.request.GET.get('sort', 'last') if sort not in ('first', 'last', 'signed', 'passed_upper', 'passed_lower'): sort = 'last' sort_key = 'action_dates.{0}'.format(sort) # do sorting on the cursor cursor.sort([(sort_key, pymongo.DESCENDING)]) return self.paginator(cursor, page=page, show_per_page=show_per_page)