def bill_keywords(bill): """ Get the keyword set for all of a bill's titles. """ keywords = keywordize(bill['title']) for title in bill['alternate_titles']: keywords = keywords.union(keywordize(title)) return keywords
def bill_query(str, params, fields=None, all=True, limit=None, skip=None, sort=None): keywords = list(keywordize(str)) filter = {} if all: filter['_keywords'] = {'$all': keywords} else: filter['_keywords'] = {'$in': keywords} for key in ('state', 'session', 'chamber'): value = params.get(key) if value: filter[key] = value cursor = db.bills.find(filter, fields) if limit: cursor.limit(limit) if skip: cursor.skip(skip) if sort: cursor.sort(sort) return list(cursor)
def read(self, request): bill_fields = {'title': 1, 'created_at': 1, 'updated_at': 1, 'bill_id': 1, 'type': 1, 'state': 1, 'session': 1, 'chamber': 1} # normal mongo search logic _filter = _build_mongo_filter(request, ('state', 'chamber')) # process full-text query query = request.GET.get('q') if query: keywords = list(keywordize(query)) _filter['_keywords'] = {'$all': keywords} # process search_window search_window = request.GET.get('search_window', '').lower() if search_window: if search_window == 'session': _filter['_current_session'] = True elif search_window == 'term': _filter['_current_term'] = True elif search_window.startswith('session:'): _filter['session'] = search_window.split('session:')[1] elif search_window.startswith('term:'): _filter['_term'] = search_window.split('term:')[1] elif search_window == 'all': pass else: resp = rc.BAD_REQUEST resp.write(": invalid search_window. Valid choices are " "'term', 'session' or 'all'") return resp # process updated_since since = request.GET.get('updated_since') if since: try: _filter['updated_at'] = {'$gte': datetime.datetime.strptime( since, "%Y-%m-%d %H:%M")} except ValueError: try: _filter['updated_at'] = {'$gte': datetime.datetime.strptime( since, "%Y-%m-%d")} except ValueError: resp = rc.BAD_REQUEST resp.write(": invalid updated_since parameter." " Please supply a date in YYYY-MM-DD format.") return resp return list(db.bills.find(_filter, bill_fields))