예제 #1
0
def execute(request, query, page_size):
    search_result = _execute_search(request, query, page_size)

    result = ActivityResults(total=search_result.total,
                             aggregations=search_result.aggregations,
                             timeframes=[])

    if result.total == 0:
        return result

    # Load all referenced annotations from the database, bucket them, and add
    # the buckets to result.timeframes.
    anns = fetch_annotations(request.db, search_result.annotation_ids)
    result.timeframes.extend(bucketing.bucket(anns))

    # Fetch all groups
    group_pubids = set([a.groupid
                        for t in result.timeframes
                        for b in t.document_buckets.values()
                        for a in b.annotations])
    groups = {g.pubid: g for g in _fetch_groups(request.db, group_pubids)}

    # Add group information to buckets and present annotations
    for timeframe in result.timeframes:
        for bucket in timeframe.document_buckets.values():
            bucket.presented_annotations = []
            for annotation in bucket.annotations:
                bucket.presented_annotations.append({
                    'annotation': presenters.AnnotationHTMLPresenter(annotation),
                    'group': groups.get(annotation.groupid),
                    'html_link': links.html_link(request, annotation),
                    'incontext_link': links.incontext_link(request, annotation)
                })

    return result
예제 #2
0
파일: links_test.py 프로젝트: ziqizh/h
    def test_html_link_returns_links_for_first_party_annotations(
            self, annotation, pyramid_request):
        # Specify the authority so that it's a first-party annotation.
        annotation.authority = pyramid_request.authority

        link = links.html_link(pyramid_request, annotation)

        assert link == 'http://example.com/a/ANNOTATION_ID'
예제 #3
0
파일: query.py 프로젝트: hypothesis/h
def execute(request, query, page_size):
    search_result = _execute_search(request, query, page_size)

    result = ActivityResults(
        total=search_result.total,
        aggregations=search_result.aggregations,
        timeframes=[],
    )

    if result.total == 0:
        return result

    # Load all referenced annotations from the database, bucket them, and add
    # the buckets to result.timeframes.
    anns = fetch_annotations(request.db, search_result.annotation_ids)
    result.timeframes.extend(bucketing.bucket(anns))

    # Fetch all groups
    group_pubids = set(
        [
            a.groupid
            for t in result.timeframes
            for b in t.document_buckets.values()
            for a in b.annotations
        ]
    )
    groups = {g.pubid: g for g in _fetch_groups(request.db, group_pubids)}

    # Add group information to buckets and present annotations
    for timeframe in result.timeframes:
        for bucket in timeframe.document_buckets.values():
            bucket.presented_annotations = []
            for annotation in bucket.annotations:
                bucket.presented_annotations.append(
                    {
                        "annotation": presenters.AnnotationHTMLPresenter(annotation),
                        "group": groups.get(annotation.groupid),
                        "html_link": links.html_link(request, annotation),
                        "incontext_link": links.incontext_link(request, annotation),
                    }
                )

    return result
예제 #4
0
파일: links_test.py 프로젝트: ziqizh/h
    def test_html_link_returns_None_for_third_party_annotations(
            self, annotation, pyramid_request):
        # Specify the authority so that it's a third-party annotation.
        annotation.authority = 'elifesciences.org'

        assert links.html_link(pyramid_request, annotation) is None
예제 #5
0
def execute(request, query, page_size):
    search_result = _execute_search(request, query, page_size)

    result = ActivityResults(total=search_result.total,
                             aggregations=search_result.aggregations,
                             timeframes=[])

    if result.total == 0:
        return result

    # Load all referenced annotations from the database, bucket them, and add
    # the buckets to result.timeframes.
    anns = fetch_annotations(request.db, search_result.annotation_ids)
    result.timeframes.extend(bucketing.bucket(anns))

    # Fetch all groups
    group_pubids = set([a.groupid
                        for t in result.timeframes
                        for b in t.document_buckets.values()
                        for a in b.annotations])
    groups = {g.pubid: g for g in _fetch_groups(request.db, group_pubids)}

    # Add group information to buckets and present annotations
    result.docs = dict()
    for timeframe in result.timeframes:
        for bucket in timeframe.document_buckets.values():
            bucket.presented_annotations = []
            for annotation in bucket.annotations:
                bucket.presented_annotations.append({
                    'annotation': presenters.AnnotationHTMLPresenter(annotation),
                    'group': groups.get(annotation.groupid),
                    'html_link': links.html_link(request, annotation),
                    'incontext_link': links.incontext_link(request, annotation)
                })
                # XXX: redo the bucketing, fake a bucket
                if annotation.document.title not in result.docs:
                    result.docs[annotation.document.title] = doc = lambda: None
                    doc.title = annotation.document.title
                    doc.annotations = []
                    doc.tags = set()
                    doc.users = set()
                    doc.annotations_count = 0
                    doc.incontext_link = types.MethodType(_incontext_link, doc)

                    presented_document = presenters.DocumentHTMLPresenter(annotation.document)

                    if presented_document.web_uri:
                        parsed = urlparse.urlparse(presented_document.web_uri)
                        doc.uri = parsed.geturl()
                        doc.domain = parsed.netloc
                    else:
                        doc.domain = _('Local file')

                doc = result.docs[annotation.document.title]
                doc.annotations.append(annotation)
                doc.tags.update(set(annotation.tags))
                doc.users.add(annotation.userid)
                doc.annotations_count += 1
    for key in result.docs:
        doc = result.docs[key]
        doc.annotations = sorted(doc.annotations, cmp=_sort_by_position)
        doc.presented_annotations = []
        for annotation in doc.annotations:
            doc.presented_annotations.append({
                'annotation': presenters.AnnotationHTMLPresenter(annotation),
                'group': groups.get(annotation.groupid),
                'html_link': links.html_link(request, annotation),
                'incontext_link': links.incontext_link(request, annotation)
            })

    return result