def test_it_renders_replies(self, links_service, pyramid_request, search_run): ann = models.Annotation(userid='luke') pyramid_request.db.add(ann) pyramid_request.db.flush() reply1 = models.Annotation(userid='sarah', references=[ann.id]) reply2 = models.Annotation(userid='sarah', references=[ann.id]) pyramid_request.db.add_all([reply1, reply2]) pyramid_request.db.flush() search_run.return_value = SearchResult(1, [ann.id], [reply1.id, reply2.id], {}) pyramid_request.params = {'_separate_replies': '1'} expected = { 'total': 1, 'rows': [presenters.AnnotationJSONPresenter(ann, links_service).asdict()], 'replies': [ presenters.AnnotationJSONPresenter(reply1, links_service).asdict(), presenters.AnnotationJSONPresenter(reply2, links_service).asdict(), ] } assert views.search(pyramid_request) == expected
def create_annotation(request, data, group_service): """ Create an annotation from passed data. :param request: the request object :type request: pyramid.request.Request :param data: a dictionary of annotation properties :type data: dict :param group_service: a service object that adheres to ``memex.interfaces.IGroupService`` :type group_service: memex.interfaces.IGroupService :returns: the created and flushed annotation :rtype: dict """ created = updated = datetime.utcnow() document_uri_dicts = data['document']['document_uri_dicts'] document_meta_dicts = data['document']['document_meta_dicts'] del data['document'] # Replies must have the same group as their parent. if data['references']: top_level_annotation_id = data['references'][0] top_level_annotation = fetch_annotation(request.db, top_level_annotation_id) if top_level_annotation: data['groupid'] = top_level_annotation.groupid else: raise schemas.ValidationError( 'references.0: ' + _('Annotation {id} does not exist').format( id=top_level_annotation_id)) # The user must have permission to create an annotation in the group # they've asked to create one in. If the application didn't configure # a groupfinder we will allow writing this annotation without any # further checks. group = group_service.find(data['groupid']) if group is None or not request.has_permission('write', context=group): raise schemas.ValidationError('group: ' + _('You may not create annotations ' 'in the specified group!')) annotation = models.Annotation(**data) annotation.created = created annotation.updated = updated document = models.update_document_metadata(request.db, annotation.target_uri, document_meta_dicts, document_uri_dicts, created=created, updated=updated) annotation.document = document request.db.add(annotation) request.db.flush() return annotation
def test_it_skips_reindexing_unaltered_annotations(req, index): annotation_1 = models.Annotation(userid='luke', target_uri='http://example.org/') annotation_2 = models.Annotation( userid='luke', _target_uri='http://example.net/', _target_uri_normalized='http://example.net') req.db.add_all([annotation_1, annotation_2]) req.db.flush() indexer = index.BatchIndexer.return_value indexer.index.return_value = None normalize_uris.normalize_annotations(req) indexer.index.assert_called_once_with(set([annotation_2.id]))
def test_it_normalizes_annotation_target_uri(req): annotation_1 = models.Annotation( userid='luke', _target_uri='http://example.org/', _target_uri_normalized='http://example.org') annotation_2 = models.Annotation( userid='luke', _target_uri='http://example.net/', _target_uri_normalized='http://example.net') req.db.add_all([annotation_1, annotation_2]) req.db.flush() normalize_uris.normalize_annotations(req) assert annotation_1.target_uri_normalized == 'httpx://example.org' assert annotation_2.target_uri_normalized == 'httpx://example.net'
def create_annotation(request, data): """ Create an annotation from passed data. :param request: the request object :type request: pyramid.request.Request :param data: a dictionary of annotation properties :type data: dict :returns: the created and flushed annotation :rtype: dict """ created = updated = datetime.utcnow() document_uri_dicts = data['document']['document_uri_dicts'] document_meta_dicts = data['document']['document_meta_dicts'] del data['document'] # Replies must have the same group as their parent. if data['references']: top_level_annotation_id = data['references'][0] top_level_annotation = fetch_annotation(request.db, top_level_annotation_id) if top_level_annotation: data['groupid'] = top_level_annotation.groupid else: raise schemas.ValidationError( 'references.0: ' + _('Annotation {id} does not exist').format( id=top_level_annotation_id)) # The user must have permission to create an annotation in the group # they've asked to create one in. if data['groupid'] != '__world__': group_principal = 'group:{}'.format(data['groupid']) if group_principal not in request.effective_principals: raise schemas.ValidationError('group: ' + _('You may not create annotations ' 'in groups you are not a member ' 'of!')) annotation = models.Annotation(**data) annotation.created = created annotation.updated = updated document = models.update_document_metadata(request.db, annotation.target_uri, document_meta_dicts, document_uri_dicts, created=created, updated=updated) annotation.document = document request.db.add(annotation) request.db.flush() return annotation
def _annotation(**kwargs): args = { 'userid': 'acct:[email protected]', 'created': datetime.utcnow(), 'updated': datetime.utcnow(), 'target_selectors': [], 'document': models.Document(), } args.update(kwargs) return models.Annotation(**args)
def test_it_renders_search_results(self, links_service, pyramid_request, search_run): ann1 = models.Annotation(userid='luke') ann2 = models.Annotation(userid='sarah') pyramid_request.db.add_all([ann1, ann2]) pyramid_request.db.flush() search_run.return_value = SearchResult(2, [ann1.id, ann2.id], [], {}) expected = { 'total': 2, 'rows': [ presenters.AnnotationJSONPresenter(ann1, links_service).asdict(), presenters.AnnotationJSONPresenter(ann2, links_service).asdict(), ] } assert views.search(pyramid_request) == expected
def merge_data(self, db_session, request): master = document.Document( document_uris=[ document.DocumentURI( claimant='https://en.wikipedia.org/wiki/Main_Page', uri='https://en.wikipedia.org/wiki/Main_Page', type='self-claim') ], meta=[ document.DocumentMeta( claimant='https://en.wikipedia.org/wiki/Main_Page', type='title', value='Wikipedia, the free encyclopedia') ]) duplicate_1 = document.Document( document_uris=[ document.DocumentURI( claimant='https://m.en.wikipedia.org/wiki/Main_Page', uri='https://en.wikipedia.org/wiki/Main_Page', type='rel-canonical') ], meta=[ document.DocumentMeta( claimant='https://m.en.wikipedia.org/wiki/Main_Page', type='title', value='Wikipedia, the free encyclopedia') ]) duplicate_2 = document.Document( document_uris=[ document.DocumentURI( claimant='https://en.wikipedia.org/wiki/Home', uri='https://en.wikipedia.org/wiki/Main_Page', type='rel-canonical') ], meta=[ document.DocumentMeta( claimant='https://en.wikipedia.org/wiki/Home', type='title', value='Wikipedia, the free encyclopedia') ]) db_session.add_all([master, duplicate_1, duplicate_2]) db_session.flush() master_ann_1 = models.Annotation(userid='luke', document_id=master.id) master_ann_2 = models.Annotation(userid='alice', document_id=master.id) duplicate_1_ann_1 = models.Annotation(userid='lucy', document_id=duplicate_1.id) duplicate_1_ann_2 = models.Annotation(userid='bob', document_id=duplicate_1.id) duplicate_2_ann_1 = models.Annotation(userid='amy', document_id=duplicate_2.id) duplicate_2_ann_2 = models.Annotation(userid='dan', document_id=duplicate_2.id) db_session.add_all([ master_ann_1, master_ann_2, duplicate_1_ann_1, duplicate_1_ann_2, duplicate_2_ann_1, duplicate_2_ann_2 ]) return (master, duplicate_1, duplicate_2)
def annotation(self): return mock.Mock(spec=models.Annotation())
def annotation(self, db_session): ann = models.Annotation(userid="bob", target_uri="http://example.com") db_session.add(ann) db_session.flush() return ann
def annotation(self): return models.Annotation(userid="bob", target_uri="http://example.com")