Example #1
0
    def test_get_field_titles(self):
        """Test settings.get_field_titles()"""

        titles = utils.get_field_titles(lambda x: x)

        assert len(titles) > 2, 'Found less than 3 field titles'
        assert 'tags' in titles, 'No tags field found in field titles'
        assert 'authorstring' in titles, 'No authorstring field found in field titles'
    def test_get_field_titles(self):
        """Test settings.get_field_titles()"""

        titles = utils.get_field_titles(lambda x: x)

        assert len(titles) > 2, 'Found less than 3 field titles'
        assert 'tags' in titles, 'No tags field found in field titles'
        assert 'authorstring' in titles, 'No authorstring field found in field titles'
Example #3
0
    def test_get_field_titles_translate(self):
        """Test settings.get_field_titles() translation"""

        translator = lambda x: x[::-1]  # Reverse string

        titles = utils.get_field_titles(translator)

        assert translator(_FIELD_TITLES['tags']) in titles.values(), 'No tags field found in field titles'
        assert translator(_FIELD_TITLES['authorstring']) in titles.values(), 'No authorstring found in field titles'
    def test_get_field_titles_translate(self):
        """Test settings.get_field_titles() translation"""

        translator = lambda x: x[::-1]  # Reverse string

        titles = utils.get_field_titles(translator)

        assert translator(_FIELD_TITLES['tags']) in titles.values(), 'No tags field found in field titles'
        assert translator(_FIELD_TITLES['authorstring']) in titles.values(), 'No authorstring found in field titles'
Example #5
0
    def before_search(self, data_dict):
        '''
        Things to do before querying Solr. Basically used by
        the advanced search feature.

        :param data_dict: data_dict to modify
        '''

        extras = data_dict.get('extras')
        if extras:
            query = data_dict.get('q', '')
            data_dict['q'] = (u'(({q})) '.format(q=query) if query else
                              '') + self.constrain_by_temporal_coverage(extras)

        if 'sort' in data_dict and data_dict['sort'] is None:
            data_dict['sort'] = settings.DEFAULT_SORT_BY

            # This is to get the correct one pre-selected on the HTML form.
            try:
                c.sort_by_selected = settings.DEFAULT_SORT_BY
            except TypeError:
                pass  # c doesn't exist when using from API (mostly a testing issue)

        try:
            c.translated_field_titles = utils.get_field_titles(toolkit._)
        except TypeError:
            pass  # c doesn't exist when using from API (mostly a testing issue)

        if data_dict.get('q') and 'owner_org' not in data_dict['q']:
            data_dict['defType'] = 'edismax'

        data_dict['facet.field'] = settings.FACETS

        # log.debug("before_search(): data_dict: %r" % data_dict)
        # Uncomment below to show query with results and in the search field
        # c.q = data_dict['q']

        # Log non-empty search queries and constraints (facets)
        q = data_dict.get('q')
        fq = data_dict.get('fq')
        if q or (fq and fq != '+dataset_type:dataset'):
            if fq and '!id:' in fq:
                # Peter: manage_datasets query in ckanext-showcase breaks down if
                # there are colons in query constraints. i.e. 'urn:nbn:fi...'
                # This is bypassed by escaping these constraints.
                matched_content = MATCH_PAREN_RE.search(fq)
                if matched_content:
                    escaped_content = ESCAPE_CHARS_RE.sub(
                        r'\\\g<char>', matched_content.group(0))
                    data_dict["fq"] = MATCH_PAREN_RE.sub(escaped_content, fq)

            log.info(u"[{t}] Search query: {q};  constraints: {c}".format(
                t=datetime.datetime.now(), q=q, c=fq))

        return data_dict
Example #6
0
    def before_search(self, data_dict):
        '''
        Things to do before querying Solr. Basically used by
        the advanced search feature.

        :param data_dict: data_dict to modify
        '''

        extras = data_dict.get('extras')
        if extras:
            query = data_dict.get('q', '')
            data_dict['q'] = (u'(({q})) '.format(q=query) if query else '') + self.constrain_by_temporal_coverage(extras)

        if 'sort' in data_dict and data_dict['sort'] is None:
            data_dict['sort'] = settings.DEFAULT_SORT_BY

            # This is to get the correct one pre-selected on the HTML form.
            try:
                c.sort_by_selected = settings.DEFAULT_SORT_BY
            except TypeError:
                pass  # c doesn't exist when using from API (mostly a testing issue)

        try:
            c.translated_field_titles = utils.get_field_titles(toolkit._)
        except TypeError:
            pass  # c doesn't exist when using from API (mostly a testing issue)


        if data_dict.get('q') and 'owner_org' not in data_dict['q']:
            data_dict['defType'] = 'edismax'

        data_dict['facet.field'] = settings.FACETS

        # log.debug("before_search(): data_dict: %r" % data_dict)
        # Uncomment below to show query with results and in the search field
        # c.q = data_dict['q']

        # Log non-empty search queries and constraints (facets)
        q = data_dict.get('q')
        fq = data_dict.get('fq')
        if q or (fq and fq != '+dataset_type:dataset'):
            if fq and '!id:' in fq:
                # Peter: manage_datasets query in ckanext-showcase breaks down if
                # there are colons in query constraints. i.e. 'urn:nbn:fi...'
                # This is bypassed by escaping these constraints.
                matched_content = MATCH_PAREN_RE.search(fq)
                if matched_content:
                    escaped_content = ESCAPE_CHARS_RE.sub(r'\\\g<char>', matched_content.group(0))
                    data_dict["fq"] = MATCH_PAREN_RE.sub(escaped_content, fq)

            log.info(u"[{t}] Search query: {q};  constraints: {c}".format(t=datetime.datetime.now(), q=q, c=fq))

        return data_dict
Example #7
0
    def before_search(self, data_dict):
        '''
        Things to do before querying Solr. Basically used by
        the advanced search feature.

        :param data_dict: data_dict to modify
        '''

        if data_dict.has_key('sort') and data_dict['sort'] is None:
            data_dict['sort'] = settings.DEFAULT_SORT_BY
            c.sort_by_selected = settings.DEFAULT_SORT_BY  # This is to get the correct one pre-selected on the HTML form.

        c.search_fields = settings.SEARCH_FIELDS
        c.translated_field_titles = utils.get_field_titles(toolkit._)

        # Start advanced search parameter parsing
        if data_dict.has_key('extras') and len(data_dict['extras']) > 0:
            #log.debug("before_search(): data_dict['extras']: %r" %
            #          data_dict['extras'].items())

            extra_terms, extra_ops, extra_dates, c.advanced_search = self.extract_search_params(data_dict)
            #log.debug("before_search(): extra_terms: %r; extra_ops: %r; "
            #          + "extra_dates: %r", extra_terms, extra_ops, extra_dates)

            if len(extra_terms) > 0:
                self.parse_search_terms(data_dict, extra_terms, extra_ops)
            if len(extra_dates) > 0:
                self.parse_search_dates(data_dict, extra_dates)

            #log.debug("before_search(): c.current_search_rows: %s; \
            #    c.current_search_limiters: %s" % (c.current_search_rows,
            #    c.current_search_limiters))
        # End advanced search parameter parsing

        data_dict['facet.field'] = settings.FACETS

        #log.debug("before_search(): data_dict: %r" % data_dict)
        # Uncomment below to show query with results and in the search field
        #c.q = data_dict['q']

        # Log non-empty search queries and constraints (facets)
        q = data_dict.get('q')
        fq = data_dict.get('fq')
        if q or (fq and fq != '+dataset_type:dataset'):
            log.info(u"[{t}] Search query: {q};  constraints: {c}".format(t=datetime.datetime.now(), q=q, c=fq))

        return data_dict
Example #8
0
 def _get_common_facets(self):
     titles = utils.get_field_titles(toolkit._)
     return OrderedDict((field, titles[field]) for field in settings.FACETS)
Example #9
0
 def _get_common_facets(self):
     titles = utils.get_field_titles(toolkit._)
     return OrderedDict((field, titles[field]) for field in settings.FACETS)