Beispiel #1
0
def create_annotation():
    # Only registered users can create annotations
    if g.user is None:
        return _failed_authz_response('create annotation')

    if request.json is not None:
        annotation = g.annotation_class(
            _filter_input(
                request.json,
                CREATE_FILTER_FIELDS))

        annotation['consumer'] = g.user.consumer.key
        if _get_annotation_user(annotation) != g.user.id:
            annotation['user'] = g.user.id

        if hasattr(g, 'before_annotation_create'):
            g.before_annotation_create(annotation)

        if hasattr(g, 'after_annotation_create'):
            annotation.save(refresh=False)
            g.after_annotation_create(annotation)

        refresh = request.args.get('refresh') != 'false'
        annotation.save(refresh=refresh)

        location = url_for('.read_annotation', id=annotation['id'])

        return jsonify(annotation), 201, {'Location': location}
    else:
        return jsonify('No JSON payload sent. Annotation not created.',
                       status=400)
Beispiel #2
0
def create_annotation():
    # Only registered users can create annotations
    if g.user is None:
        return _failed_authz_response('create annotation')

    if request.json is not None:
        annotation = g.annotation_class(
            _filter_input(request.json, CREATE_FILTER_FIELDS))

        annotation['consumer'] = g.user.consumer.key
        if _get_annotation_user(annotation) != g.user.id:
            annotation['user'] = g.user.id

        if hasattr(g, 'before_annotation_create'):
            g.before_annotation_create(annotation)

        if hasattr(g, 'after_annotation_create'):
            annotation.save(refresh=False)
            g.after_annotation_create(annotation)

        refresh = request.args.get('refresh') != 'false'
        annotation.save(refresh=refresh)

        return jsonify(annotation)
    else:
        return jsonify('No JSON payload sent. Annotation not created.',
                       status=400)
Beispiel #3
0
def create_annotation():
    # Only registered users can create annotations
    if g.user is None:
        return _failed_authz_response("create annotation")

    if request.json is not None:
        annotation = Annotation(_filter_input(request.json, CREATE_FILTER_FIELDS))

        annotation["consumer"] = g.user.consumer.key
        if _get_annotation_user(annotation) != g.user.id:
            annotation["user"] = g.user.id

        if hasattr(g, "before_annotation_create"):
            g.before_annotation_create(annotation)

        if hasattr(g, "after_annotation_create"):
            annotation.save(refresh=False)
            g.after_annotation_create(annotation)

        refresh = request.args.get("refresh") != "false"
        annotation.save(refresh=refresh)

        return jsonify(annotation)
    else:
        return jsonify("No JSON payload sent. Annotation not created.", status=400)
Beispiel #4
0
def create_annotation():
    # Only registered users can create annotations
    if g.user is None:
        return _failed_authz_response('create annotation')

    if request.json:
        annotation = Annotation(_filter_input(request.json, CREATE_FILTER_FIELDS))

        annotation['consumer'] = g.user.consumer.key
        if _get_annotation_user(annotation) != g.user.id:
            annotation['user'] = g.user.id

        annotation.save()

        if hasattr(g, 'after_annotation_create'):
            g.after_annotation_create(annotation)
            annotation.save()

        return jsonify(annotation)
    else:
        return jsonify('No JSON payload sent. Annotation not created.', status=400)
Beispiel #5
0
def create_annotation_bundle():
    # Only registered users can create annotations
    if g.user is None:
        return _failed_authz_response('create annotation')

    if request.json is not None:

        locations = []
        annotations = []
        for raw_annotation in request.json:
            annotation = g.annotation_class(
                _filter_input(
                    raw_annotation,
                    CREATE_FILTER_FIELDS))

            # print raw_annotation

            annotation['consumer'] = g.user.consumer.key
            if _get_annotation_user(annotation) != g.user.id:
                annotation['user'] = g.user.id

            if hasattr(g, 'before_annotation_create'):
                g.before_annotation_create(annotation)

            if hasattr(g, 'after_annotation_create'):
                annotation.save(refresh=False)
                g.after_annotation_create(annotation)

            refresh = request.args.get('refresh') != 'false'
            annotation.save(refresh=refresh)

            location = url_for('.read_annotation', docid=annotation['id'])
            locations.append(location)
            annotations.append(annotation)

        # print "Total %d annotations added\n" % (len(annotations))
        return jsonify(annotations), 201
    else:
        return jsonify('No JSON payload sent. Annotation not created.',
                       status=400)
Beispiel #6
0
def create_annotation():
    consumer, user = g.auth.request_credentials(request)

    # Only registered users can create annotations
    if not (consumer and user):
        return _failed_authz_response('create annotation')

    if request.json:
        annotation = Annotation(_filter_input(request.json, CREATE_FILTER_FIELDS))

        annotation['consumer'] = consumer
        if _get_annotation_user(annotation) != user:
            annotation['user'] = user

        annotation.save()

        if hasattr(g, 'after_annotation_create'):
            g.after_annotation_create(annotation)
            annotation.save()

        return jsonify(annotation)
    else:
        return jsonify('No JSON payload sent. Annotation not created.', status=400)
Beispiel #7
0
def create_annotation():
    # Only registered users can create annotations
    if not current_user.is_administrator():
        return _failed_authz_response('create annotation')
    if request.json is not None:
        entry = Entry.query.order_by(Entry.id.desc()).first()
        annotation = Annotation.from_json(request.json)
        if hasattr(g, 'before_annotation_create'):
            g.before_annotation_create(annotation)
        db.session.add(annotation)
        annotation = Annotation.query.order_by(Annotation.id.desc()).first()
        entry.annotations.append(annotation)
        db.session.add(entry)
        if hasattr(g, 'after_annotation_create'):
            g.after_annotation_create(annotation)
        location = url_for('.read_annotation',
                           id=annotation.id,
                           _external=True)
        return jsonify(Annotation.to_json(annotation)), 201, {
            'Location': location
        }
    else:
        return jsonify('No JSON payload sent. Annotation not created.',
                       status=400)