Пример #1
0
    def _parse_query_facets(self, facet_items):
        '''
        Parses query facet counts like this:
            {
                'min_price_USD_exact:[2001 TO *]': 306,
                'duration_exact:[16 TO 25]': 149,
                'min_price_USD_exact:[500 TO 1000]': 195,
                'duration_exact:[41 TO *]': 35,
                'duration_exact:[26 TO 40]': 52,
                'min_price_USD_exact:[0 TO 500]': 88,
                'duration_exact:[6 TO 10]': 329,
                'min_price_USD_exact:[1001 TO 2000]': 292,
                'duration_exact:[11 TO 15]': 256,
                'duration_exact:[* TO 5]': 205
            }

        Returns a list of Facet objects (one per parsed field name).
        '''
        facets = {}
        for field_query, count in facet_items.iteritems():
            field, sep, query = field_query.partition(':')
            field = field.replace('_exact', '')
            if field in facets:
                facet = facets[field]
            else:
                facet = QueryFacet(field=field,
                                   label=field.replace('_', ' ').title())
                facets[field] = facet
            item = FacetItem(query, count, label=humanize_range(query))
            item.is_selected = self._is_selected_facet(field, item.value)
            item.facet = facet
            facet.items.append(item)
        return [facet for field, facet in facets.iteritems()]
Пример #2
0
    def _parse_query_facets(self, facet_items):
        '''
        Parses query facet counts like this:
            {
                'min_price_USD_exact:[2001 TO *]': 306,
                'duration_exact:[16 TO 25]': 149,
                'min_price_USD_exact:[500 TO 1000]': 195,
                'duration_exact:[41 TO *]': 35,
                'duration_exact:[26 TO 40]': 52,
                'min_price_USD_exact:[0 TO 500]': 88,
                'duration_exact:[6 TO 10]': 329,
                'min_price_USD_exact:[1001 TO 2000]': 292,
                'duration_exact:[11 TO 15]': 256,
                'duration_exact:[* TO 5]': 205
            }

        Returns a list of Facet objects (one per parsed field name).
        '''
        facets = {}
        for field_query, count in facet_items.iteritems():
            field, sep, query = field_query.partition(':')
            field = field.replace('_exact', '')
            if field in facets:
                facet = facets[field]
            else:
                facet = QueryFacet(field=field, label=field.replace('_', ' ').title())
                facets[field] = facet
            item = FacetItem(query, count, label=humanize_range(query))
            item.is_selected = self._is_selected_facet(field, item.value)
            item.facet = facet
            facet.items.append(item)
        return [facet for field,facet in facets.iteritems()]
Пример #3
0
 def _parse_date_facets(self, facet_items):
     '''
     Parse date faceted fields like:
         {'departure_dates': {'2010-04-24T18:17:03Z': 105,
                            '2010-05-01T00:00:00Z': 323,
                            '2010-06-01T00:00:00Z': 334,
                            '2010-07-01T00:00:00Z': 468,
                            '2010-08-01T00:00:00Z': 504,
                            '2010-09-01T00:00:00Z': 515,
                            '2010-10-01T00:00:00Z': 519,
                            '2010-11-01T00:00:00Z': 478,
                            '2010-12-01T00:00:00Z': 457,
                            '2011-01-01T00:00:00Z': 370,
                            '2011-02-01T00:00:00Z': 357,
                            '2011-03-01T00:00:00Z': 370,
                            '2011-04-01T00:00:00Z': 359,
                            'end': '2011-05-01T00:00:00Z',
                            'gap': '+1MONTH/MONTH'}}
     '''
     facets = []
     for field, date_counts in facet_items.iteritems():
         facet = Facet(field=field, label=field.replace('_', ' ').title())
         gap = date_counts['gap']
         for date_string, count in date_counts.iteritems():
             match = DATETIME_REGEX.search(date_string)
             if not match: continue
             data = match.groupdict()
             date = datetime_safe.date(int(data['year']),
                                       int(data['month']), int(data['day']))
             item = FacetItem(date, count)
             if gap == '+1MONTH/MONTH':
                 item.label = date.strftime("%B")
                 item.value = date.strftime("%Y-%m")
             elif gap == '+1YEAR/YEAR':
                 item.label = date.strftime("%Y")
                 item.value = date.strftime("%Y-01")
             item.is_selected = self._is_selected_facet(
                 field, check_parse_date(item.value))
             item.facet = facet
             facet.items.append(item)
         facet.items.sort(key=lambda x: x.value)
         facets.append(facet)
     return facets
Пример #4
0
 def _parse_date_facets(self, facet_items):
     '''
     Parse date faceted fields like:
         {'departure_dates': {'2010-04-24T18:17:03Z': 105,
                            '2010-05-01T00:00:00Z': 323,
                            '2010-06-01T00:00:00Z': 334,
                            '2010-07-01T00:00:00Z': 468,
                            '2010-08-01T00:00:00Z': 504,
                            '2010-09-01T00:00:00Z': 515,
                            '2010-10-01T00:00:00Z': 519,
                            '2010-11-01T00:00:00Z': 478,
                            '2010-12-01T00:00:00Z': 457,
                            '2011-01-01T00:00:00Z': 370,
                            '2011-02-01T00:00:00Z': 357,
                            '2011-03-01T00:00:00Z': 370,
                            '2011-04-01T00:00:00Z': 359,
                            'end': '2011-05-01T00:00:00Z',
                            'gap': '+1MONTH/MONTH'}}
     '''
     facets = []
     for field, date_counts in facet_items.iteritems():
         facet = Facet(field=field, label=field.replace('_', ' ').title())
         gap = date_counts['gap']
         for date_string, count in date_counts.iteritems():
             match = DATETIME_REGEX.search(date_string)
             if not match: continue
             data = match.groupdict()
             date = datetime_safe.date(int(data['year']), int(data['month']), int(data['day']))
             item = FacetItem(date, count)
             if gap == '+1MONTH/MONTH':
                 item.label=date.strftime("%B")
                 item.value=date.strftime("%Y-%m")
             elif gap == '+1YEAR/YEAR':
                 item.label=date.strftime("%Y")
                 item.value=date.strftime("%Y-01")
             item.is_selected = self._is_selected_facet(field, check_parse_date(item.value))
             item.facet = facet
             facet.items.append(item)
         facet.items.sort(key=lambda x: x.value)
         facets.append(facet)
     return facets
Пример #5
0
 def _parse_field_facets(self, facet_items):
     '''
     Parses field facet counts like this:
         {'region': [('South America', 222),
                    ('Asia', 134),
                    ('Antarctica', 10),
                    ('South Pacific', 2)],
          'service_level': [('Standard', 351),
                           ('Basic', 202),
                           ('Comfort', 93),
                           ('Superior', 25)],}
     '''
     facets = []
     for field, counts in facet_items.iteritems():
         conf = self.field_facets[field]
         label = conf.get('label', field.replace('_', ' ').title())
         facet = Facet(field=field, label=label)
         for count in counts:
             item = FacetItem(count[0], count[1])
             item.is_selected = self._is_selected_facet(field, item.value)
             item.facet = facet
             facet.items.append(item)
         facets.append(facet)
     return facets
Пример #6
0
 def _parse_field_facets(self, facet_items):
     '''
     Parses field facet counts like this:
         {'region': [('South America', 222),
                    ('Asia', 134),
                    ('Antarctica', 10),
                    ('South Pacific', 2)],
          'service_level': [('Standard', 351),
                           ('Basic', 202),
                           ('Comfort', 93),
                           ('Superior', 25)],}
     '''
     facets = []
     for field, counts in facet_items.iteritems():
         conf = self.field_facets[field]
         label = conf.get('label', field.replace('_', ' ').title())
         facet = Facet(field=field, label=label)
         for count in counts:
             item = FacetItem(count[0], count[1])
             item.is_selected = self._is_selected_facet(field, item.value)
             item.facet = facet
             facet.items.append(item)
         facets.append(facet)
     return facets