コード例 #1
0
def post_documents():
    ###
    # Receive a posted document, and return the document id
    ##

    errors = []
    request = flask_rebar.get_validated_body()
    app.logger.debug(request)
    rc = 201

    if not request.get('title').strip():
        raise err.BadRequest('Empty title is not allowed')

    # The API will be responsible for generating the document ID
    ## Normalize name. Note: this also commits the record, to avoid a race
    doc_id = normalize_doc_id(request['title'])

    # Create a new doc
    doc = Document(
            doc_id=doc_id,
            title=request['title'],
            text=request['text']
    )
    db.session.add(doc)
    db.session.commit()

    return {
        'document': doc,
        'errors': errors, 
    }, rc
コード例 #2
0
    def wrapper(*args, **kwargs):
        if 'event_id' not in kwargs:
            raise errors.BadRequest('The request requires an event ID')
        event = validate_and_get_current_event(kwargs['event_id'])

        kwargs['event'] = event
        del kwargs['event_id']
        return func(*args, **kwargs)
コード例 #3
0
def _get_json_body_or_400():
    """
    Retrieves the JSON payload of the current request, throwing a 400 error
    if the request doesn't include a valid JSON payload.
    """
    if "application/json" not in request.headers.get("content-type", ""):
        raise errors.BadRequest(messages.unsupported_content_type)

    if (not request.data) or (len(request.data) == 0):
        raise errors.BadRequest(messages.empty_json_body)

    try:
        body = request.get_json()
    except WerkzeugBadRequest:
        raise errors.BadRequest(messages.invalid_json)

    if not isinstance(body, list) and not isinstance(body, dict):
        # request.get_json_from_resp() treats strings as valid JSON, which is technically
        # true... but they're not valid objects. So let's throw an error on
        # primitive types.
        raise errors.BadRequest(messages.invalid_json)

    return body
コード例 #4
0
def raise_400_for_marshmallow_errors(errs, msg):
    """
    Throws a 400 error properly formatted from the given marshmallow errors.

    :param dict errs: Error dictionary as returned by marshmallow
    :param str msg: The overall message to use in the response.
    :raises: errors.BadRequest
    """
    if not errs:
        return

    copied = copy.deepcopy(errs)

    _format_marshmallow_errors_for_response_in_place(copied)

    additional_data = {"errors": copied}

    raise errors.BadRequest(msg=msg, additional_data=additional_data)
コード例 #5
0
    def wrapper(*args, **kwargs):
        if 'challenge_id' not in kwargs:
            raise errors.BadRequest('The request requires a challenge ID')
        challenge = Challenge.query.filter_by(id=kwargs['challenge_id']).first()

        if challenge is None:
            raise errors.NotFound(f'The challenge with id {kwargs["challenge_id"]} was not found')

        event = Event.query.filter_by(id=challenge.category.event_id).first()
        if event is None or not event.is_visible:
            raise errors.NotFound(f"Event with ID {event.id} not found.")

        check_open_event(event)

        kwargs['challenge'] = challenge
        kwargs['event'] = event
        del kwargs['challenge_id']
        return func(*args, **kwargs)
コード例 #6
0
def raise_400_for_marshmallow_errors(errs, msg):
    """
    Throws a 400 error properly formatted from the given marshmallow errors.

    :param dict errs: Error dictionary as returned by marshmallow
    :param Union[str,messages.ErrorMessage] msg: The overall message to use in the response.
    :raises: errors.BadRequest
    """
    if not errs:
        return

    copied = copy.deepcopy(errs)

    _format_marshmallow_errors_for_response_in_place(copied)

    additional_data = {"errors": copied}
    message, rebar_code = msg if isinstance(msg, tuple) else msg, None

    raise errors.BadRequest(msg=msg, additional_data=additional_data)