コード例 #1
0
ファイル: app.py プロジェクト: haseebgit/datagrepper
def msg_id():
    if 'id' not in flask.request.args:
        flask.abort(400)
    msg = dm.Message.query.filter_by(msg_id=flask.request.args['id']).first()
    mimetype = flask.request.headers.get('Accept')

    # get paging argument for size and chrome
    size = flask.request.args.get('size', 'large')
    chrome = flask.request.args.get('chrome', 'true')
    # get paging argument for is_raw
    # is_raw checks if card comes from /raw url
    is_raw = flask.request.args.get('is_raw', 'false')

    meta = flask.request.args.getlist('meta')

    # check size value
    if size not in ['small', 'medium', 'large']:
        raise ValueError("size must be in one of these 'small', 'medium' or 'large'")
    # checks chrome value
    if chrome not in ['true', 'false']:
        raise ValueError("chrome should be either 'true' or 'false'")
    # checks is_raw value
    if is_raw not in ['true', 'false']:
        raise ValueError("is_raw should be either 'true' or 'false'")

    if msg:
        # converts message from sqlalchemy objects to json-like dicts
        msg = msg.__json__()
        if meta:
            msg = meta_argument(msg, meta)

        if request_wants_html():
            # convert string into python dictionary
            obj = json.loads(fedmsg.encoding.dumps(msg))
            message = []
            if is_raw == 'true':
                message_dict = message_card(obj, size)
                message_dict['is_raw'] = 'true'
                message.append(message_dict)
            else:
                message.append(message_card(obj, size))

            if chrome=='true':
                return flask.render_template("base.html", response=message, heading="Message by ID")
            else:
                return flask.render_template("raw.html", response=message)

        else:
            return flask.Response (
                response=fedmsg.encoding.dumps(msg),
                status=200,
                mimetype=mimetype,
            )
    else:
        flask.abort(404)
コード例 #2
0
ファイル: app.py プロジェクト: fedora-infra/datagrepper
def msg_id():
    if 'id' not in flask.request.args:
        flask.abort(400)
    msg = dm.Message.query.filter_by(msg_id=flask.request.args['id']).first()
    mimetype = flask.request.headers.get('Accept')

    # get paging argument for size and chrome
    size = flask.request.args.get('size', 'large')
    chrome = flask.request.args.get('chrome', 'true')
    # get paging argument for is_raw
    # is_raw checks if card comes from /raw url
    is_raw = flask.request.args.get('is_raw', 'false')

    callback = flask.request.args.get('callback', None)
    meta = flask.request.args.getlist('meta')

    sizes = ['small', 'medium', 'large', 'extra-large']
    # check size value
    if size not in sizes:
        raise ValueError("size must be in one of these '%s'" %
                         "', '".join(sizes))

    # checks chrome value
    if chrome not in ['true', 'false']:
        raise ValueError("chrome should be either 'true' or 'false'")
    # checks is_raw value
    if is_raw not in ['true', 'false']:
        raise ValueError("is_raw should be either 'true' or 'false'")

    if msg:
        # converts message from sqlalchemy objects to json-like dicts
        msg = msg.__json__()
        if meta:
            msg = meta_argument(msg, meta)

        if not callback and request_wants_html():
            # convert string into python dictionary
            msg_string = pygments.highlight(
                fedmsg.encoding.pretty_dumps(msg),
                pygments.lexers.JavascriptLexer(),
                pygments.formatters.HtmlFormatter(
                    noclasses=True,
                    style="emacs",
                )
            ).strip()
            message_dict = message_card(msg, size)

            if is_raw == 'true':
                message_dict['is_raw'] = 'true'

            template = 'base.html'
            if chrome != 'true':
                template = 'raw.html'

            return flask.render_template(
                template,
                size=size,
                response=[message_dict],
                msg_string=msg_string,
                heading="Message by ID",
            )
        else:
            body = fedmsg.encoding.dumps(msg)

            if callback:
                mimetype = 'application/javascript'
                body = "%s(%s);" % (callback, body)

            return flask.Response(
                response=body,
                status=200,
                mimetype=mimetype,
            )
    else:
        flask.abort(404)
コード例 #3
0
ファイル: app.py プロジェクト: fedora-infra/datagrepper
def raw():
    """ Main API entry point. """

    # Perform our complicated datetime logic
    start = flask.request.args.get('start', None)
    end = flask.request.args.get('end', None)
    delta = flask.request.args.get('delta', None)
    start, end, delta = assemble_timerange(start, end, delta)

    start_id = flask.request.args.get('start_id', None)
    end_id = flask.request.args.get('end_id', None)

    # Further filters, all ANDed together in CNF style.
    users = flask.request.args.getlist('user')
    packages = flask.request.args.getlist('package')
    categories = flask.request.args.getlist('category')
    topics = flask.request.args.getlist('topic')
    contains = flask.request.args.getlist('contains')

    # Still more filters.. negations of the previous ones.
    not_users = flask.request.args.getlist('not_user')
    not_packages = flask.request.args.getlist('not_package')
    not_categories = flask.request.args.getlist('not_category')
    not_topics = flask.request.args.getlist('not_topic')

    # Paging arguments
    page = int(flask.request.args.get('page', 1))
    rows_per_page = int(flask.request.args.get('rows_per_page', 25))
    order = flask.request.args.get('order', 'desc')
    # adding size as paging arguments
    size = flask.request.args.get('size', 'large')
    # adding chrome as paging arguments
    chrome = flask.request.args.get('chrome', 'true')

    # Response formatting arguments
    callback = flask.request.args.get('callback', None)
    meta = flask.request.args.getlist('meta')
    grouped = flask.request.args.get('grouped', False, asbool)

    arguments = dict(
        start=start,
        delta=delta,
        end=end,
        start_id=start_id,
        end_id=end_id,
        users=users,
        packages=packages,
        categories=categories,
        topics=topics,
        contains=contains,
        not_users=not_users,
        not_packages=not_packages,
        not_categories=not_categories,
        not_topics=not_topics,
        page=page,
        rows_per_page=rows_per_page,
        order=order,
        meta=meta,
        grouped=grouped,
    )

    if page < 1:
        raise ValueError("page must be > 0")

    if rows_per_page > 100:
        raise ValueError("rows_per_page must be <= 100")

    if order not in ['desc', 'asc']:
        raise ValueError("order must be either 'desc' or 'asc'")

    # check size value
    possible_sizes = ['small', 'medium', 'large', 'extra-large']
    if size not in possible_sizes:
        raise ValueError("size must be in one of these %r" % possible_sizes)

    # checks chrome value
    if chrome not in ['true', 'false']:
        raise ValueError("chrome should be either 'true' or 'false'")

    if start or end or delta:
        start_id = None
        end_id = None
    else:
        if start_id:
            start_msg = dm.Message.query.filter_by(msg_id=start_id).first()
            start = start_msg.timestamp.strftime('%s')
        if end_id:
            end_msg = dm.Message.query.filter_by(msg_id=end_id).first()
            end = end_msg.timestamp.strftime('%s')

        start, end, delta = assemble_timerange(start, end, delta)

    if contains and datetime.fromtimestamp(start or 0) < (datetime.utcnow() - timedelta(weeks=4*8)):
        raise BadRequest('When using contains, specify a start at most '
                         'eight months into the past')

    if contains and not (categories or topics):
        raise BadRequest('When using contains, specify either a topic or'
                         ' a category as well')

    try:
        # This fancy classmethod does all of our search for us.
        total, pages, messages = dm.Message.grep(
            start=start and datetime.fromtimestamp(start),
            end=end and datetime.fromtimestamp(end),
            page=page,
            rows_per_page=rows_per_page,
            order=order,
            users=users,
            packages=packages,
            categories=categories,
            topics=topics,
            contains=contains,
            not_users=not_users,
            not_packages=not_packages,
            not_categories=not_categories,
            not_topics=not_topics,
        )

        # Convert our messages from sqlalchemy objects to json-like dicts
        messages = [msg.__json__() for msg in messages]
        if grouped:
            messages = fedmsg.meta.conglomerate(messages, **fedmsg_config)
            for message in messages:
                message['date'] = arrow.get(message['timestamp']).humanize()
        elif meta:
            for message in messages:
                message = meta_argument(message, meta)

        output = dict(
            raw_messages=messages,
            total=total,
            pages=pages,
            count=len(messages),
            arguments=arguments,
        )
        status = 200
    except Exception as e:
        traceback.print_exc()

        output = dict(
            error=str(e),
            arguments=arguments,
        )

        # :D
        if app.config.get('DEBUG', False):
            output['tb'] = traceback.format_exc().split('\n')

        status = 500

    body = fedmsg.encoding.dumps(output)

    mimetype = flask.request.headers.get('Accept')

    # Our default - http://da.gd/vIIV
    if mimetype == '*/*':
        mimetype = 'application/json'

    if callback:
        mimetype = 'application/javascript'
        body = "%s(%s);" % (callback, body)

    # return HTML content else json
    if not callback and request_wants_html():
        # convert string into python dictionary
        obj = json.loads(body)
        # extract the messages
        raw_message_list = obj.get("raw_messages", [])

        final_message_list = []

        for msg in raw_message_list:
            if not grouped:
                # message_card module will handle size
                message = message_card(msg, size)
                # add msg_id to the message dictionary
                if (msg['msg_id'] is not None):
                    message['msg_id'] = msg['msg_id']
            else:
                message = msg
                message['msg_id'] = None
                if len(message['msg_ids']) == 1:
                    message['msg_id'] = message['msg_ids'].keys()[0]
                message['date'] = arrow.get(message['timestamp'])

            final_message_list.append(message)

        # removes boilerlate codes if chrome value is false
        if chrome == 'true':
            return flask.render_template(
                "base.html",
                size=size,
                response=final_message_list,
                arguments=arguments,
                autoscroll=True,
            )
        else:
            return flask.render_template(
                "raw.html",
                size=size,
                response=final_message_list,
                arguments=arguments,
            )

    else:
        return flask.Response(
            response=body,
            status=status,
            mimetype=mimetype,
        )
コード例 #4
0
ファイル: app.py プロジェクト: kushal124/datagrepper
def raw():
    """ Main API entry point. """

    # Perform our complicated datetime logic
    start = flask.request.args.get('start', None)
    end = flask.request.args.get('end', None)
    delta = flask.request.args.get('delta', None)
    start, end, delta = assemble_timerange(start, end, delta)

    # Further filters, all ANDed together in CNF style.
    users = flask.request.args.getlist('user')
    packages = flask.request.args.getlist('package')
    categories = flask.request.args.getlist('category')
    topics = flask.request.args.getlist('topic')
    contains = flask.request.args.getlist('contains')

    # Still more filters.. negations of the previous ones.
    not_users = flask.request.args.getlist('not_user')
    not_packages = flask.request.args.getlist('not_package')
    not_categories = flask.request.args.getlist('not_category')
    not_topics = flask.request.args.getlist('not_topic')

    # Paging arguments
    page = int(flask.request.args.get('page', 1))
    rows_per_page = int(flask.request.args.get('rows_per_page', 20))
    order = flask.request.args.get('order', 'desc')
    # adding size as paging arguments
    size = flask.request.args.get('size', 'large')
    # adding chrome as paging arguments
    chrome = flask.request.args.get('chrome', 'true')

    # Response formatting arguments
    callback = flask.request.args.get('callback', None)
    meta = flask.request.args.getlist('meta')

    arguments = dict(
        start=start,
        delta=delta,
        end=end,
        users=users,
        packages=packages,
        categories=categories,
        topics=topics,
        contains=contains,
        not_users=not_users,
        not_packages=not_packages,
        not_categories=not_categories,
        not_topics=not_topics,
        page=page,
        rows_per_page=rows_per_page,
        order=order,
        meta=meta,
    )

    if page < 1:
        raise ValueError("page must be > 0")

    if rows_per_page > 100:
        raise ValueError("rows_per_page must be <= 100")

    if order not in ['desc', 'asc']:
        raise ValueError("order must be either 'desc' or 'asc'")

    # check size value
    possible_sizes = ['small', 'medium', 'large', 'extra-large']
    if size not in possible_sizes:
        raise ValueError("size must be in one of these %r" % possible_sizes)

    # checks chrome value
    if chrome not in ['true', 'false']:
        raise ValueError("chrome should be either 'true' or 'false'")

    try:
        # This fancy classmethod does all of our search for us.
        total, pages, messages = dm.Message.grep(
            start=start and datetime.fromtimestamp(start),
            end=end and datetime.fromtimestamp(end),
            page=page,
            rows_per_page=rows_per_page,
            order=order,
            users=users,
            packages=packages,
            categories=categories,
            topics=topics,
            contains=contains,
            not_users=not_users,
            not_packages=not_packages,
            not_categories=not_categories,
            not_topics=not_topics,
        )

        # Convert our messages from sqlalchemy objects to json-like dicts
        messages = [msg.__json__() for msg in messages]
        if meta:
            for message in messages:
                message = meta_argument(message, meta)

        output = dict(
            raw_messages=messages,
            total=total,
            pages=pages,
            count=len(messages),
            arguments=arguments,
        )
        status = 200
    except Exception as e:
        output = dict(
            error=str(e),
            arguments=arguments,
        )

        # :D
        if app.config.get('DEBUG', False):
            output['tb'] = traceback.format_exc().split('\n')

        status = 500

    body = fedmsg.encoding.dumps(output)

    mimetype = flask.request.headers.get('Accept')

    if callback:
        mimetype = 'application/javascript'
        body = "%s(%s);" % (callback, body)

    # return HTML content else json
    if not callback and request_wants_html():
        # convert string into python dictionary
        obj = json.loads(body)
        # extract the messages
        raw_message_list = obj["raw_messages"]

        final_message_list = []

        for msg in raw_message_list:
            # message_card module will handle size
            message = message_card(msg, size)
            # add msg_id to the message dictionary
            if (msg["msg_id"] is not None):
                message['msg_id'] = msg["msg_id"]
            final_message_list.append(message)

        # removes boilerlate codes if chrome value is false
        if chrome == 'true':
            return flask.render_template(
                "base.html",
                size=size,
                response=final_message_list,
                heading="Raw Messages",
            )
        else:
            return flask.render_template(
                "raw.html",
                size=size,
                response=final_message_list,
            )

    else:
        return flask.Response(
            response=body,
            status=status,
            mimetype=mimetype,
        )