def test_annotations_of_multiple_documents_in_one_timeframe( self, annotation_datetime, timeframe_label ): annotation_1 = factories.Annotation( target_uri="http://example1.com", updated=annotation_datetime ) annotation_2 = factories.Annotation( target_uri="http://example2.com", updated=annotation_datetime ) annotation_3 = factories.Annotation( target_uri="http://example3.com", updated=annotation_datetime ) timeframes = bucketing.bucket([annotation_1, annotation_2, annotation_3]) assert timeframes == [ timeframe_with( timeframe_label, { annotation_1.document: bucketing.DocumentBucket( annotation_1.document, [annotation_1] ), annotation_2.document: bucketing.DocumentBucket( annotation_2.document, [annotation_2] ), annotation_3.document: bucketing.DocumentBucket( annotation_3.document, [annotation_3] ), }, ) ]
def test_annotations_from_different_days_in_same_month(self): """ Test bucketing multiple annotations from different days of same month. Annotations from different days of the same month should go into one bucket. """ document = factories.Document() one_month_ago = UTCNOW - datetime.timedelta(days=30) annotations = [ factories.Annotation(document=document, updated=one_month_ago), factories.Annotation(document=document, updated=one_month_ago - datetime.timedelta(days=1)), factories.Annotation(document=document, updated=one_month_ago - datetime.timedelta(days=2)), ] timeframes = bucketing.bucket(annotations) expected_bucket = bucketing.DocumentBucket(document) expected_bucket.update(annotations) assert timeframes == [ timeframe_with('Jan 1970', {document: expected_bucket})]
def test_recent_and_older_annotations_together(self): results = [ factories.Annotation(target_uri='http://example1.com'), factories.Annotation(target_uri='http://example2.com'), factories.Annotation(target_uri='http://example3.com'), factories.Annotation(target_uri='http://example4.com', updated=THIRD_MARCH_1968), factories.Annotation(target_uri='http://example5.com', updated=THIRD_MARCH_1968), factories.Annotation(target_uri='http://example6.com', updated=THIRD_MARCH_1968), ] timeframes = bucketing.bucket(results) expected_bucket_1 = bucketing.DocumentBucket(results[0].document, [results[0]]) expected_bucket_2 = bucketing.DocumentBucket(results[1].document, [results[1]]) expected_bucket_3 = bucketing.DocumentBucket(results[2].document, [results[2]]) expected_bucket_4 = bucketing.DocumentBucket(results[3].document, [results[3]]) expected_bucket_5 = bucketing.DocumentBucket(results[4].document, [results[4]]) expected_bucket_6 = bucketing.DocumentBucket(results[5].document, [results[5]]) assert timeframes == [ timeframe_with('Last 7 days', { results[0].document: expected_bucket_1, results[1].document: expected_bucket_2, results[2].document: expected_bucket_3, }), timeframe_with('Mar 1968', { results[3].document: expected_bucket_4, results[4].document: expected_bucket_5, results[5].document: expected_bucket_6, }), ]
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), 'incontext_link': links.incontext_link(request, annotation) }) return result
def test_annotations_from_different_days_in_same_month(self): """ Test bucketing multiple annotations from different days of same month. Annotations from different days of the same month should go into one bucket. """ one_month_ago = UTCNOW - datetime.timedelta(days=30) annotations = [ factories.Annotation( target_uri="http://example.com", updated=one_month_ago ), factories.Annotation( target_uri="http://example.com", updated=one_month_ago - datetime.timedelta(days=1), ), factories.Annotation( target_uri="http://example.com", updated=one_month_ago - datetime.timedelta(days=2), ), ] timeframes = bucketing.bucket(annotations) expected_bucket = bucketing.DocumentBucket(annotations[0].document) expected_bucket.update(annotations) assert timeframes == [ timeframe_with("Jan 1970", {annotations[0].document: expected_bucket}) ]
def test_one_annotation(self, annotation_datetime, timeframe_label): annotation = factories.Annotation(document=factories.Document(), updated=annotation_datetime) timeframes = bucketing.bucket([annotation]) assert timeframes == [ timeframe_with(timeframe_label, { annotation.document: bucketing.DocumentBucket(annotation.document, [annotation]) }) ]
def test_one_annotation(self, annotation_datetime, timeframe_label): document = factories.Document() results = [factories.Annotation(document=document, updated=annotation_datetime)] timeframes = bucketing.bucket(results) assert timeframes == [ timeframe_with(timeframe_label, { document: bucketing.DocumentBucket(document, results) }) ]
def test_multiple_annotations_of_one_document_in_one_timeframe( self, annotation_datetime, timeframe_label): results = [ factories.Annotation(target_uri='https://example.com', updated=annotation_datetime) for _ in range(3)] timeframes = bucketing.bucket(results) document = results[0].document assert timeframes == [ timeframe_with(timeframe_label, { document: bucketing.DocumentBucket(document, results) }), ]
def test_multiple_annotations_of_one_document_in_one_timeframe( self, annotation_datetime, timeframe_label): document = factories.Document() results = [ factories.Annotation(document=document, updated=annotation_datetime) for _ in range(3)] timeframes = bucketing.bucket(results) assert timeframes == [ timeframe_with(timeframe_label, { document: bucketing.DocumentBucket(document, results) }), ]
def execute(request, query, page_size): search = Search(request) search.append_filter(TopLevelAnnotationsFilter()) for agg in aggregations_for(query): search.append_aggregation(agg) query = query.copy() page = request.params.get('page', '') if not page: page = '1' query['limit'] = page_size query['offset'] = (int(page) - 1) * page_size search_result = search.run(query) 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(): for index, annotation in enumerate(bucket.annotations): bucket.annotations[index] = { 'annotation': presenters.AnnotationHTMLPresenter(annotation), 'group': groups.get(annotation.groupid) } return result
def test_annotations_of_the_same_document_in_different_timeframes(self): document = factories.Document() results = [ factories.Annotation(document=document), factories.Annotation(document=document, updated=FIFTH_NOVEMBER_1969), factories.Annotation(document=document, updated=THIRD_MARCH_1968), ] timeframes = bucketing.bucket(results) expected_bucket_1 = bucketing.DocumentBucket(document, [results[0]]) expected_bucket_2 = bucketing.DocumentBucket(document, [results[1]]) expected_bucket_3 = bucketing.DocumentBucket(document, [results[2]]) assert timeframes == [ timeframe_with('Last 7 days', {document: expected_bucket_1}), timeframe_with('Nov 1969', {document: expected_bucket_2}), timeframe_with('Mar 1968', {document: expected_bucket_3}), ]
def test_annotations_of_the_same_document_in_different_timeframes(self): results = [ factories.Annotation(), factories.Annotation(updated=FIFTH_NOVEMBER_1969), factories.Annotation(updated=THIRD_MARCH_1968), ] document = factories.Document() for annotation in results: annotation.document = document timeframes = bucketing.bucket(results) expected_bucket_1 = bucketing.DocumentBucket(document, [results[0]]) expected_bucket_2 = bucketing.DocumentBucket(document, [results[1]]) expected_bucket_3 = bucketing.DocumentBucket(document, [results[2]]) assert timeframes == [ timeframe_with("Last 7 days", {document: expected_bucket_1}), timeframe_with("Nov 1969", {document: expected_bucket_2}), timeframe_with("Mar 1968", {document: expected_bucket_3}), ]
def test_recent_and_older_annotations_together(self): document_1 = factories.Document() document_2 = factories.Document() document_3 = factories.Document() document_4 = factories.Document() document_5 = factories.Document() document_6 = factories.Document() results = [ factories.Annotation(document=document_1), factories.Annotation(document=document_2), factories.Annotation(document=document_3), factories.Annotation(document=document_4, updated=THIRD_MARCH_1968), factories.Annotation(document=document_5, updated=THIRD_MARCH_1968), factories.Annotation(document=document_6, updated=THIRD_MARCH_1968), ] timeframes = bucketing.bucket(results) expected_bucket_1 = bucketing.DocumentBucket(document_1, [results[0]]) expected_bucket_2 = bucketing.DocumentBucket(document_2, [results[1]]) expected_bucket_3 = bucketing.DocumentBucket(document_3, [results[2]]) expected_bucket_4 = bucketing.DocumentBucket(document_4, [results[3]]) expected_bucket_5 = bucketing.DocumentBucket(document_5, [results[4]]) expected_bucket_6 = bucketing.DocumentBucket(document_6, [results[5]]) assert timeframes == [ timeframe_with('Last 7 days', { document_1: expected_bucket_1, document_2: expected_bucket_2, document_3: expected_bucket_3, }), timeframe_with('Mar 1968', { document_4: expected_bucket_4, document_5: expected_bucket_5, document_6: expected_bucket_6, }), ]
def test_annotations_of_multiple_documents_in_one_timeframe( self, annotation_datetime, timeframe_label): document_1 = factories.Document() document_2 = factories.Document() document_3 = factories.Document() results = [ factories.Annotation(document=document_1, updated=annotation_datetime), factories.Annotation(document=document_2, updated=annotation_datetime), factories.Annotation(document=document_3, updated=annotation_datetime), ] timeframes = bucketing.bucket(results) assert timeframes == [ timeframe_with(timeframe_label, { document_1: bucketing.DocumentBucket(document_1, [results[0]]), document_2: bucketing.DocumentBucket(document_2, [results[1]]), document_3: bucketing.DocumentBucket(document_3, [results[2]]), }), ]
def execute(request, query): search = Search(request) search.append_filter(TopLevelAnnotationsFilter()) for agg in aggregations_for(query): search.append_aggregation(agg) search_result = search.run(query) 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(): for index, annotation in enumerate(bucket.annotations): bucket.annotations[index] = { 'annotation': presenters.AnnotationHTMLPresenter(annotation), 'group': groups.get(annotation.groupid) } return result
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
def test_no_annotations(self): assert not bucketing.bucket([])
def test_no_annotations(self): assert bucketing.bucket([]) == []