def test_default_days_incorrect_range(self): args = MultiDict([('filter_range_date_gte', ['2014-6']), ('filter_range_date_lte', ['2014-1'])]) filter_dsl = filters.filter_dsl_from_multidict(args) #from nose.tools import set_trace;set_trace() assert(filter_dsl[0]['range']['date']['gte'] == '2014-1-1') assert(filter_dsl[0]['range']['date']['lte'] == '2014-6-30')
def url_for_page(self, pagenum): request = get_request() current_args = request.GET args_dict = MultiDict(current_args) if pagenum != 1: args_dict['page'] = pagenum elif 'page' in args_dict: del args_dict['page'] encoded = urlencode(args_dict, doseq=True) if encoded: url = "".join([request.path, "?", urlencode(args_dict, doseq=True)]) return url else: return request.path
def setUp(self): self.args = MultiDict([('filter_category', ['cats', 'dogs']), ('filter_planet', ['earth']), ('filter_range_date_lte', ['2014-6-1']), ('filter_range_comment_count_gt', ['100'])])
def test_default_days_correct_range(self): args = MultiDict([('filter_range_date_gte', ['2014-1']), ('filter_range_date_lte', ['2014-6'])]) filter_dsl = filters.filter_dsl_from_multidict(args) assert(filter_dsl[0]['range']['date']['gte'] == '2014-1-1') assert(filter_dsl[0]['range']['date']['lte'] == '2014-6-30')
def test_date_validation_with_days_incorrect_range(self): args = MultiDict([('filter_range_date_gte', ['2014-6-23']), ('filter_range_date_lte', ['2014-1-23'])]) filter_dsl = filters.filter_dsl_from_multidict(args) assert(filter_dsl[0]['range']['date']['gte'] == '2014-1-23') assert(filter_dsl[0]['range']['date']['lte'] == '2014-6-23')
def search(self, aggregations=None, use_url_arguments=True, size=10, **kwargs): query_file = json.loads(file(self.filename).read()) query_dict = query_file['query'] ''' These dict constructors split the kwargs from the template into filter arguments and arguments that can be placed directly into the query body. The dict constructor syntax supports python 2.6, 2.7, and 3.x If python 2.7, use dict comprehension and iteritems() With python 3, use dict comprehension and items() (items() replaces iteritems and is just as fast) ''' filter_args = dict((key, value) for (key, value) in kwargs.items() if key.startswith('filter_')) non_filter_args = dict((key, value) for (key, value) in kwargs.items() if not key.startswith('filter_')) query_dict.update(non_filter_args) pagenum = 1 request = get_request() # Add in filters from the template. new_multidict = MultiDict() # First add the url arguments if requested if use_url_arguments: new_multidict = MultiDict(request.GET.copy()) # Next add the arguments from the search() function used in the # template for key, value in filter_args.items(): new_multidict.update({key: value}) filters = filter_dsl_from_multidict(new_multidict) args_flat = request.GET.copy() query_body = {} if aggregations: aggs_dsl = {} if type(aggregations) is str: aggregations = [aggregations] # so we can treat it as a list for fieldname in aggregations: aggs_dsl[fieldname] = {'terms': {'field': fieldname, 'size': 10000}} query_body['aggs'] = aggs_dsl else: if 'page' in args_flat: args_flat['from_'] = int( query_dict.get('size', '10')) * (int(args_flat['page']) - 1) pagenum = int(args_flat['page']) args_flat_filtered = dict( [(k, v) for k, v in args_flat.items() if v]) query_dict.update(args_flat_filtered) query_body['query'] = {'filtered': {'filter': {}}} if filters: query_body['query']['filtered']['filter'][ 'and'] = [f for f in filters] if 'filters' in query_file: if 'and' not in query_body['query']['filtered']['filter']: query_body['query']['filtered']['filter']['and'] = [] for json_filter in query_file['filters']: query_body['query']['filtered'][ 'filter']['and'].append(json_filter) final_query_dict = dict((k, v) for (k, v) in query_dict.items() if k in ALLOWED_SEARCH_PARAMS) final_query_dict['index'] = self.es_index final_query_dict['body'] = query_body response = self.es.search(**final_query_dict) response['query'] = query_dict return QueryResults(self,response, pagenum)