Beispiel #1
0
    def test_calls_groupfinder(self, pyramid_config, pyramid_request):
        groupfinder = mock.Mock()
        pyramid_config.registry[groups.GROUPFINDER_KEY] = groupfinder

        groups.find(pyramid_request, mock.sentinel.groupid)

        groupfinder.assert_called_once_with(pyramid_request, mock.sentinel.groupid)
Beispiel #2
0
    def test_returns_groupfinder_result(self, pyramid_config, pyramid_request):
        groupfinder = mock.Mock(return_value=mock.sentinel.groupcontext)
        pyramid_config.registry[groups.GROUPFINDER_KEY] = groupfinder

        result = groups.find(pyramid_request, mock.sentinel.groupid)

        assert result == mock.sentinel.groupcontext
Beispiel #3
0
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 the application didn't configure
    # a groupfinder we will allow writing this annotation without any
    # further checks.
    group = groups.find(request, 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
Beispiel #4
0
 def group(self):
     return groups.find(self.request, self.annotation.groupid)