コード例 #1
0
    def render_json_subject(self, graph, subject, seen=()):
        data, inv_data = {}, {}
        if isinstance(subject, rdflib.URIRef):
            data['_uri'] = unicode(subject)
        elif isinstance(subject, rdflib.BNode):
            data['_id'] = unicode(subject)
        if subject in seen:
            data['_nested'] = True
            return data
        seen += (subject, )

        for p in graph.predicates(subject):
            data[contract(p, '_')] = objects = []
            for o in graph.objects(subject, p):
                if isinstance(o, rdflib.Literal):
                    objects.append(unicode(o))
                else:
                    objects.append(self.render_json_subject(graph, o, seen))

        for p in graph.predicates(object=subject):
            objects = []
            for o in graph.subjects(p, subject):
                if len(seen) >= 2 and o == seen[-2]:
                    continue
                if isinstance(o, rdflib.Literal):
                    objects.append(unicode(o))
                else:
                    objects.append(self.render_json_subject(graph, o, seen))
            if objects:
                inv_data[contract(p)] = objects
        if inv_data:
            data['_inv'] = inv_data

        return data
コード例 #2
0
ファイル: json.py プロジェクト: ox-it/humfrey
    def render_json_subject(self, graph, subject, seen=()):
        data, inv_data = {}, {}
        if isinstance(subject, rdflib.URIRef):
            data['_uri'] = unicode(subject)
        elif isinstance(subject, rdflib.BNode):
            data['_id'] = unicode(subject)
        if subject in seen:
            data['_nested'] = True
            return data
        seen += (subject,)

        for p in graph.predicates(subject):
            data[contract(p, '_')] = objects = []
            for o in graph.objects(subject, p):
                if isinstance(o, rdflib.Literal):
                    objects.append(unicode(o))
                else:
                    objects.append(self.render_json_subject(graph, o, seen))

        for p in graph.predicates(object=subject):
            objects = []
            for o in graph.subjects(p, subject):
                if len(seen) >= 2 and o == seen[-2]:
                    continue
                if isinstance(o, rdflib.Literal):
                    objects.append(unicode(o))
                else:
                    objects.append(self.render_json_subject(graph, o, seen))
            if objects:
                inv_data[contract(p)] = objects
        if inv_data:
            data['_inv'] = inv_data

        return data
コード例 #3
0
ファイル: utils.py プロジェクト: graingert/humfrey
def get_labels(subjects, endpoint=None):
    if not subjects:
        return {}
    if not endpoint:
        endpoint = Endpoint(settings.ENDPOINT_QUERY)
    elif isinstance(endpoint, basestring):
        endpoint = Endpoint(endpoint)
    elif isinstance(endpoint, Endpoint):
        pass
    else:
        raise TypeError("endpoint parameter should be an Endpoint instance.")
    
    subjects = map(URIRef, subjects)
    
    query = """
        CONSTRUCT {
          ?s ?p ?label
        } WHERE {
          ?s ?p ?label .
          FILTER ( %s ) .
          FILTER ( %s )
        }""" % (' || '.join('?s = %s' % s.n3() for s in subjects if s and IRI.match(s)),
                ' || '.join('?p = %s' % p for p in label_predicates))
    
    graph = endpoint.query(query)
    
    labels = {}
    for subject in subjects:
        subject_labels = sorted(graph.objects(subject), key=language_key)
        if subject_labels:
            labels[subject] = subject_labels[0]
        else:
            labels[subject] = contract(subject)
    return labels
コード例 #4
0
ファイル: views.py プロジェクト: shnizzedy/humfrey
    def get_results(self, parameters, cleaned_data):
        page = cleaned_data.get('page') or 1
        page_size = cleaned_data.get('page_size') or self.page_size
        start = (page - 1) * page_size

        query = self.get_query(parameters, cleaned_data, start, page_size)

        # If there aren't any filters defined, we don't want a filter part of
        # our query.
        if 'filter' in query:
            if 'and' in query['filter'] and not query['filter']['and']:
                del query['filter']['and']
            if not query['filter']:
                del query['filter']

        results = self.search_endpoint.query(query)

        results.update(self.get_pagination(page_size, page, start, results))
        results['q'] = cleaned_data['q']

        facet_labels = set()
        for key in query['facets']:
            meta = results['facets'][key]['meta'] = query['facets'][key]
            filter_value = parameters.get(
                'filter.%s' % query['facets'][key]['terms']['field'])
            results['facets'][key]['filter'] = {
                'present': filter_value is not None,
                'value': filter_value
            }
            if meta['terms']['field'].endswith('.uri'):
                for term in results['facets'][key]['terms']:
                    facet_labels.add(term['term'])
                    term['value'] = contract(term['term'])
            else:
                for term in results['facets'][key]['terms']:
                    term['value'] = term['term']

        labels = get_labels(map(rdflib.URIRef, facet_labels),
                            endpoint=self.endpoint)
        for key in query['facets']:
            if results['facets'][key]['meta']['terms']['field'].endswith(
                    '.uri'):
                for term in results['facets'][key]['terms']:
                    uri = rdflib.URIRef(term['term'])
                    if uri in labels:
                        term['label'] = unicode(labels[uri])

        for hit in results['hits']['hits']:
            try:
                hit['_url'] = doc_forwards(hit['_source']['uri'])[None]
            except KeyError:
                raise

        return results
コード例 #5
0
ファイル: views.py プロジェクト: ox-it/humfrey
    def get_results(self, parameters, cleaned_data):
        page = cleaned_data.get('page') or 1
        page_size = cleaned_data.get('page_size') or self.page_size
        start = (page - 1) * page_size

        query = self.get_query(parameters, cleaned_data, start, page_size)

        # If there aren't any filters defined, we don't want a filter part of
        # our query.
        if 'filter' in query:
            if 'and' in query['filter'] and not query['filter']['and']:
                del query['filter']['and']
            if not query['filter']:
                del query['filter']

        results = self.search_endpoint.query(query)

        results.update(self.get_pagination(page_size, page, start, results))
        results['q'] = cleaned_data['q']

        facet_labels = set()
        for key in query['facets']:
            meta = results['facets'][key]['meta'] = query['facets'][key]
            filter_value = parameters.get('filter.%s' % query['facets'][key]['terms']['field'])
            results['facets'][key]['filter'] = {'present': filter_value is not None,
                                                'value': filter_value}
            if meta['terms']['field'].endswith('.uri'):
                for term in results['facets'][key]['terms']:
                    facet_labels.add(term['term'])
                    term['value'] = contract(term['term'])
            else:
                for term in results['facets'][key]['terms']:
                    term['value'] = term['term']

        labels = get_labels(map(rdflib.URIRef, facet_labels), endpoint=self.endpoint)
        for key in query['facets']:
            if results['facets'][key]['meta']['terms']['field'].endswith('.uri'):
                for term in results['facets'][key]['terms']:
                    uri = rdflib.URIRef(term['term'])
                    if uri in labels:
                        term['label'] = unicode(labels[uri])

        for hit in results['hits']['hits']:
            try:
                hit['_url'] = doc_forwards(hit['_source']['uri'])[None]
            except KeyError:
                raise

        return results
コード例 #6
0
ファイル: utils.py プロジェクト: ox-it/humfrey
def get_labels(subjects, endpoint=None, mapping=True):
    if not subjects:
        return {}
    if isinstance(subjects, ConjunctiveGraph):
        subjects = itertools.chain(subjects.subjects(),
                                   subjects.predicates(),
                                   subjects.objects())
    if not endpoint:
        endpoint = Endpoint(settings.ENDPOINT_QUERY)
    elif isinstance(endpoint, basestring):
        endpoint = Endpoint(endpoint)
    elif hasattr(endpoint, 'query'):
        pass
    else:
        raise TypeError("endpoint parameter should be an Endpoint instance.")

    subjects = set(s for s in subjects if isinstance(s, URIRef) and IRI.match(s))

    query = u"""
        CONSTRUCT {{
          ?s ?p ?label
        }} WHERE {{
          VALUES ?p {{ {predicates} }}
          VALUES ?s {{ {subjects} }}
          ?s ?p ?label
        }}""".format(predicates=u' '.join(p.n3() for p in label_predicates),
                     subjects=u' '.join(s.n3() for s in subjects))

    graph = endpoint.query(query)
    
    if not mapping:
        return graph

    labels = {}
    for subject in subjects:
        subject_labels = sorted(graph.objects(subject), key=language_key)
        if subject_labels:
            labels[subject] = subject_labels[0]
        else:
            labels[subject] = contract(subject)
    return labels
コード例 #7
0
ファイル: organization.py プロジェクト: ox-it/dataox
 def __init__(self, *args, **kwargs):
     endpoint = kwargs['endpoint']
     types_choices = [(unicode(k), '{0} ({1})'.format(v, contract(k))) for k, v in endpoint.query(self._types_query)]
     types_choices.sort(key=lambda c: c[1])
     self.base_fields['type'].choices = types_choices
     super(OrganizationFeedForm, self).__init__(*args, **kwargs)
コード例 #8
0
ファイル: views.py プロジェクト: graingert/humfrey
    def get_results(self, parameters, cleaned_data):
        page = cleaned_data.get('page') or 1
        page_size = cleaned_data.get('page_size') or self.page_size
        start = (page - 1) * page_size

        query = {
            'query': {'query_string': {'query': cleaned_data['q'],
                                       'default_operator': 'AND'}},
            'from': start,
            'size': page_size,
            # A blank conjunctive filter. We'll remove this later if necessary.
            'filter': {'and': []},
        }

        # Parse query parameters of the form 'filter.FIELDNAME'.
        for key in list(parameters):
            parameter = parameters[key]
            if key.startswith('filter.'):
                if not parameter:
                    del parameters[key]
                    continue
                elif parameter == '-':
                    filter = {'missing': {'field': key[7:]}}
                else:
                    if key.endswith('.uri') and ':' in parameter:
                        parameter = expand(parameter)
                    filter = {'term': {key[7:]: parameter}}
                query['filter']['and'].append(filter)

        # If there aren't any filters defined, we don't want a filter part of
        # our query.
        if not query['filter']['and']:
            del query['filter']['and']
        if not query['filter']:
            del query['filter']

        if self.facets:
            # Copy the facet definitions as we'll be playing with them shortly.
            facets = copy.deepcopy(self.facets)

            # Add facet filters for all active filters except any acting on this
            # particular facet.
            if 'filter' in query:
                for facet in facets.itervalues():
                    for filter in query['filter']['and']:
                        if facet['terms']['field'] not in (filter.get('term') or filter['missing']):
                            if 'facet_filter' not in facet:
                                facet['facet_filter'] = {'and': []}
                            facet['facet_filter']['and'].append(filter)
            query['facets'] = facets

        response = urllib2.urlopen(self.search_url, json.dumps(query))
        results = self.Deunderscorer(json.load(response))

        results.update(self.get_pagination(page_size, page, start, results))
        results['q'] = cleaned_data['q']

        facet_labels = set()
        for key in query['facets']:
            meta = results['facets'][key]['meta'] = query['facets'][key]
            filter_value = parameters.get('filter.%s' % query['facets'][key]['terms']['field'])
            results['facets'][key]['filter'] = {'present': filter_value is not None,
                                                'value': filter_value}
            if meta['terms']['field'].endswith('.uri'):
                for term in results['facets'][key]['terms']:
                    facet_labels.add(term['term'])
                    term['value'] = contract(term['term'])
            else:
                for term in results['facets'][key]['terms']:
                    term['value'] = term['term']
        
        labels = get_labels(facet_labels, endpoint=self.endpoint)
        for key in query['facets']:
            if results['facets'][key]['meta']['terms']['field'].endswith('.uri'):
                for term in results['facets'][key]['terms']:
                    uri = URIRef(term['term'])
                    if uri in labels:
                        term['label'] = unicode(labels[uri])

        for hit in results['hits']['hits']:
            try:
                hit['_url'] = doc_forwards(hit['_source']['uri'])[None]
            except KeyError:
                raise

        return results