Пример #1
0
def create(request):
    """Create an annotation from the POST payload."""
    json_payload = _json_payload(request)

    # Validate the annotation for, and create the annotation in, PostgreSQL.
    if request.feature('postgres'):
        schema = schemas.CreateAnnotationSchema(request)
        appstruct = schema.validate(copy.deepcopy(json_payload))
        annotation = storage.create_annotation(request, appstruct)

    # Validate the annotation for, and create the annotation in, Elasticsearch.
    legacy_schema = schemas.LegacyCreateAnnotationSchema(request)
    legacy_appstruct = legacy_schema.validate(copy.deepcopy(json_payload))

    # When 'postgres' is on make sure that annotations in the legacy
    # Elasticsearch database use the same IDs as the PostgreSQL ones.
    if request.feature('postgres'):
        assert annotation.id
        legacy_appstruct['id'] = annotation.id

    legacy_annotation = storage.legacy_create_annotation(
        request, legacy_appstruct)

    if request.feature('postgres'):
        _publish_annotation_event(request, annotation, 'create')
        return AnnotationJSONPresenter(request, annotation).asdict()

    _publish_annotation_event(request, legacy_annotation, 'create')
    return AnnotationJSONPresenter(request, legacy_annotation).asdict()
Пример #2
0
Файл: views.py Проект: hashin/h
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')

    presenter = AnnotationJSONPresenter(annotation)
    return presenter.asdict()
Пример #3
0
Файл: views.py Проект: hashin/h
def update(annotation, request):
    """Update the specified annotation with data from the PUT payload."""
    schema = schemas.UpdateAnnotationSchema(request, annotation=annotation)
    appstruct = schema.validate(_json_payload(request))
    annotation = storage.update_annotation(request, annotation.id, appstruct)

    _publish_annotation_event(request, annotation, 'update')

    presenter = AnnotationJSONPresenter(annotation)
    return presenter.asdict()
Пример #4
0
Файл: views.py Проект: hashin/h
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')

    presenter = AnnotationJSONPresenter(annotation)
    return presenter.asdict()
Пример #5
0
Файл: views.py Проект: hashin/h
def update(annotation, request):
    """Update the specified annotation with data from the PUT payload."""
    schema = schemas.UpdateAnnotationSchema(request, annotation=annotation)
    appstruct = schema.validate(_json_payload(request))
    annotation = storage.update_annotation(request, annotation.id, appstruct)

    _publish_annotation_event(request, annotation, 'update')

    presenter = AnnotationJSONPresenter(annotation)
    return presenter.asdict()
Пример #6
0
    def test_asdict_extra_cannot_override_other_data(self, document_asdict):
        request = DummyRequest()
        ann = mock.Mock(id='the-real-id', extra={'id': 'the-extra-id'})
        document_asdict.return_value = {}

        presented = AnnotationJSONPresenter(request, ann).asdict()
        assert presented['id'] == 'the-real-id'
Пример #7
0
    def test_target_missing_selectors(self):
        request = DummyRequest()
        ann = mock.Mock(target_uri='http://example.com', target_selectors=None)

        expected = [{'source': 'http://example.com'}]
        actual = AnnotationJSONPresenter(request, ann).target
        assert expected == actual
Пример #8
0
    def test_asdict(self, document_asdict):
        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'
            },
            'references': ['referenced-id-1', 'referenced-id-2'],
            'extra-1':
            'foo',
            'extra-2':
            'bar'
        }
        assert expected == AnnotationJSONPresenter(ann).asdict()
Пример #9
0
def _publish_annotation_event(request, annotation, action):
    """Publish an event to the annotations queue for this annotation action."""
    annotation_dict = None
    if action == 'delete':
        annotation_dict = AnnotationJSONPresenter(request, annotation).asdict()

    event = AnnotationEvent(request, annotation.id, action, annotation_dict)
    request.notify_after_commit(event)
Пример #10
0
    def test_asdict_extra_uses_copy_of_extra(self, document_asdict):
        extra = {'foo': 'bar'}
        request = DummyRequest()
        ann = mock.Mock(id='my-id', extra=extra)
        document_asdict.return_value = {}

        presented = AnnotationJSONPresenter(request, ann).asdict()

        # Presenting the annotation shouldn't change the "extra" dict.
        assert extra == {'foo': 'bar'}
Пример #11
0
    def test_asdict_with_link_generators(self, document_asdict):
        request = DummyRequest()
        ann = mock.Mock(id='my-id', extra={})
        document_asdict.return_value = {}

        add_annotation_link_generator(request.registry, 'giraffe',
                                      lambda r, a: 'http://giraffe.com')
        add_annotation_link_generator(request.registry, 'withid',
                                      lambda r, a: 'http://withid.com/' + a.id)

        presented = AnnotationJSONPresenter(request, ann).asdict()

        assert presented['links'] == {
            'giraffe': 'http://giraffe.com',
            'withid': 'http://withid.com/my-id',
        }
Пример #12
0
    def test_target(self):
        ann = mock.Mock(
            target_uri='http://example.com',
            target_selectors={'PositionSelector': {
                'start': 0,
                'end': 12
            }})

        expected = [{
            'source': 'http://example.com',
            'selector': {
                'PositionSelector': {
                    'start': 0,
                    'end': 12
                }
            }
        }]
        actual = AnnotationJSONPresenter(ann).target
        assert expected == actual
Пример #13
0
    def test_text(self):
        request = DummyRequest()
        ann = mock.Mock(text='It is magical!')
        presenter = AnnotationJSONPresenter(request, ann)

        assert 'It is magical!' == presenter.text
Пример #14
0
    def test_tags_missing(self):
        ann = mock.Mock(tags=None)
        presenter = AnnotationJSONPresenter(ann)

        assert [] == presenter.tags
Пример #15
0
    def test_tags(self):
        ann = mock.Mock(tags=['interesting', 'magic'])
        presenter = AnnotationJSONPresenter(ann)

        assert ['interesting', 'magic'] == presenter.tags
Пример #16
0
    def test_text_missing(self):
        ann = mock.Mock(text=None)
        presenter = AnnotationJSONPresenter(ann)

        assert '' == presenter.text
Пример #17
0
    def test_text(self):
        ann = mock.Mock(text='It is magical!')
        presenter = AnnotationJSONPresenter(ann)

        assert 'It is magical!' == presenter.text
Пример #18
0
    def test_text_missing(self):
        request = DummyRequest()
        ann = mock.Mock(text=None)
        presenter = AnnotationJSONPresenter(request, ann)

        assert '' == presenter.text
Пример #19
0
def _present_annotations(request, ids):
    """Load annotations by id from the database and present them."""
    annotations = storage.fetch_ordered_annotations(request.db, ids, load_documents=True)
    return [AnnotationJSONPresenter(request, ann).asdict() for ann in annotations]
Пример #20
0
def _present_searchdict(request, mapping):
    """Run an object returned from search through a presenter."""
    ann = storage.annotation_from_dict(mapping)
    return AnnotationJSONPresenter(request, ann).asdict()
Пример #21
0
    def test_tags(self):
        request = DummyRequest()
        ann = mock.Mock(tags=['interesting', 'magic'])
        presenter = AnnotationJSONPresenter(request, ann)

        assert ['interesting', 'magic'] == presenter.tags
Пример #22
0
    def test_tags_missing(self):
        request = DummyRequest()
        ann = mock.Mock(tags=None)
        presenter = AnnotationJSONPresenter(request, ann)

        assert [] == presenter.tags
Пример #23
0
def read(annotation, request):
    """Return the annotation (simply how it was stored in the database)."""
    presenter = AnnotationJSONPresenter(request, annotation)
    return presenter.asdict()
Пример #24
0
 def test_permissions(self, annotation, action, expected):
     request = DummyRequest()
     presenter = AnnotationJSONPresenter(request, annotation)
     assert expected == presenter.permissions[action]
Пример #25
0
Файл: views.py Проект: djcun95/h
def read(annotation, request):
    """Return the annotation (simply how it was stored in the database)."""
    presenter = AnnotationJSONPresenter(request, annotation)
    return presenter.asdict()
Пример #26
0
 def test_permissions(self, annotation, action, expected):
     presenter = AnnotationJSONPresenter(annotation)
     assert expected == presenter.permissions[action]