def _publish_annotation_event(request, annotation, action): """Publish an event to the annotations queue for this annotation action.""" links_service = request.find_service(name='links') annotation_dict = None if action == 'delete': presenter = AnnotationJSONPresenter(annotation, links_service) annotation_dict = presenter.asdict() event = AnnotationEvent(request, annotation.id, action, annotation_dict) request.notify_after_commit(event)
def create(request): """Create an annotation from the POST payload.""" schema = schemas.CreateAnnotationSchema(request) appstruct = schema.validate(_json_payload(request)) annotation = storage.create_annotation(request, appstruct) _publish_annotation_event(request, annotation, 'create') links_service = request.find_service(name='links') presenter = AnnotationJSONPresenter(annotation, links_service) return presenter.asdict()
def update(annotation, request): """Update the specified annotation with data from the PUT payload.""" schema = schemas.UpdateAnnotationSchema(request, annotation.target_uri, annotation.groupid) appstruct = schema.validate(_json_payload(request)) annotation = storage.update_annotation(request.db, annotation.id, appstruct) _publish_annotation_event(request, annotation, 'update') links_service = request.find_service(name='links') presenter = AnnotationJSONPresenter(annotation, links_service) return presenter.asdict()
def test_asdict_extra_cannot_override_other_data(self, document_asdict, fake_links_service): ann = mock.Mock(id='the-real-id', extra={'id': 'the-extra-id'}) document_asdict.return_value = {} presented = AnnotationJSONPresenter(ann, fake_links_service).asdict() assert presented['id'] == 'the-real-id'
def update(context, request): """Update the specified annotation with data from the PUT payload.""" schema = schemas.UpdateAnnotationSchema(request, context.annotation.target_uri, context.annotation.groupid) appstruct = schema.validate(_json_payload(request)) annotation = storage.update_annotation(request.db, context.annotation.id, appstruct) _publish_annotation_event(request, annotation, 'update') links_service = request.find_service(name='links') presenter = AnnotationJSONPresenter(annotation, links_service) return presenter.asdict()
def test_asdict_extra_uses_copy_of_extra(self, document_asdict, fake_links_service): extra = {'foo': 'bar'} ann = mock.Mock(id='my-id', extra=extra) document_asdict.return_value = {} presented = AnnotationJSONPresenter(ann, fake_links_service).asdict() # Presenting the annotation shouldn't change the "extra" dict. assert extra == {'foo': 'bar'}
def update(context, request): """Update the specified annotation with data from the PATCH payload.""" if request.method == 'PUT' and hasattr(request, 'stats'): request.stats.incr('memex.api.deprecated.put_update_annotation') schema = schemas.UpdateAnnotationSchema(request, context.annotation.target_uri, context.annotation.groupid) appstruct = schema.validate(_json_payload(request)) annotation = storage.update_annotation(request.db, context.annotation.id, appstruct) _publish_annotation_event(request, annotation, 'update') links_service = request.find_service(name='links') group_service = request.find_service(IGroupService) resource = AnnotationResource(annotation, group_service, links_service) presenter = AnnotationJSONPresenter(resource) return presenter.asdict()
def _present_annotations(request, ids): """Load annotations by id from the database and present them.""" def eager_load_documents(query): return query.options(subqueryload(models.Annotation.document)) annotations = storage.fetch_ordered_annotations( request.db, ids, query_processor=eager_load_documents) links_service = request.find_service(name='links') return [ AnnotationJSONPresenter(ann, links_service).asdict() for ann in annotations ]
def test_permissions(self, annotation, group_readable, action, expected, group_service, fake_links_service): annotation.deleted = False group_principals = { 'members': (security.Allow, 'group:{}'.format(annotation.groupid), 'read'), 'world': (security.Allow, security.Everyone, 'read'), None: security.DENY_ALL, } group = mock.Mock(spec_set=['__acl__']) group.__acl__.return_value = [group_principals[group_readable]] group_service.find.return_value = group resource = AnnotationResource(annotation, group_service, fake_links_service) presenter = AnnotationJSONPresenter(resource) assert expected == presenter.permissions[action]
def read(annotation, request): """Return the annotation (simply how it was stored in the database).""" links_service = request.find_service(name='links') presenter = AnnotationJSONPresenter(annotation, links_service) return presenter.asdict()
def read(context, request): """Return the annotation (simply how it was stored in the database).""" presenter = AnnotationJSONPresenter(context) return presenter.asdict()
def test_permissions(self, annotation, action, expected, fake_links_service): presenter = AnnotationJSONPresenter(annotation, fake_links_service) assert expected == presenter.permissions[action]
def test_asdict(self, document_asdict, fake_links_service): ann = mock.Mock(id='the-id', created=datetime.datetime(2016, 2, 24, 18, 3, 25, 768), updated=datetime.datetime(2016, 2, 29, 10, 24, 5, 564), userid='acct:luke', target_uri='http://example.com', text='It is magical!', tags=['magic'], groupid='__world__', shared=True, target_selectors=[{ 'TestSelector': 'foobar' }], references=['referenced-id-1', 'referenced-id-2'], extra={ 'extra-1': 'foo', 'extra-2': 'bar' }) document_asdict.return_value = {'foo': 'bar'} expected = { 'id': 'the-id', 'created': '2016-02-24T18:03:25.000768+00:00', 'updated': '2016-02-29T10:24:05.000564+00:00', 'user': '******', 'uri': 'http://example.com', 'text': 'It is magical!', 'tags': ['magic'], 'group': '__world__', 'permissions': { 'read': ['group:__world__'], 'admin': ['acct:luke'], 'update': ['acct:luke'], 'delete': ['acct:luke'] }, 'target': [{ 'source': 'http://example.com', 'selector': [{ 'TestSelector': 'foobar' }] }], 'document': { 'foo': 'bar' }, 'links': { 'giraffe': 'http://giraffe.com', 'toad': 'http://toad.net' }, 'references': ['referenced-id-1', 'referenced-id-2'], 'extra-1': 'foo', 'extra-2': 'bar' } result = AnnotationJSONPresenter(ann, fake_links_service).asdict() assert result == expected
def read(context, request): """Return the annotation (simply how it was stored in the database).""" links_service = request.find_service(name='links') presenter = AnnotationJSONPresenter(context.annotation, links_service) return presenter.asdict()