Exemplo n.º 1
0
    def get(self, report_uid, attachment_path):
        """Get an attachment for a specific Testplan report given their uids."""
        attachment_path = os.path.abspath(
            os.path.join(
                app.config["DATA_PATH"], defaults.ATTACHMENTS, attachment_path
            )
        )

        if os.path.exists(attachment_path):
            return send_from_directory(
                directory=os.path.dirname(attachment_path),
                filename=os.path.basename(attachment_path),
            )
        else:
            raise exceptions.NotFound()
Exemplo n.º 2
0
def del_engine(id):
    engine = (
        models
        .Engine
        .query
        .filter_by(id=id)
        .first())

    if not engine:
        raise exceptions.NotFound('Engine not found')

    db.session.delete(engine)
    db.session.commit()

    return flask.Response(status=204)
Exemplo n.º 3
0
def redirect_to_context(node_id):
    """Redirects to the context URL of the node.

    Comment: redirects to whatever the comment is attached to + #node_id
        (unless 'whatever the comment is attached to' already contains '#', then
         '#node_id' isn't appended)
    Post: redirects to main or project-specific blog post
    Other: redirects to project.url + #node_id
    """

    if node_id.lower() == '{{objectid}}':
        log.warning(
            "JavaScript should have filled in the ObjectID placeholder, but didn't. "
            "URL=%s and referrer=%s", request.url, request.referrer)
        raise wz_exceptions.NotFound('Invalid ObjectID')

    try:
        url = url_for_node(node_id)
    except ValueError as ex:
        log.warning("%s: URL=%s and referrer=%s", str(ex), request.url,
                    request.referrer)
        raise wz_exceptions.NotFound('Invalid ObjectID')

    return redirect(url)
Exemplo n.º 4
0
 def get(self, module_id):
     policy.authorize(flask.request.context, 'v2_rating:get_module', {})
     try:
         module = self.rating_modules[module_id]
     except KeyError:
         raise http_exceptions.NotFound(
             "Module '{}' not found".format(module_id))
     infos = module.obj.module_info.copy()
     return {
         'module_id': module_id,
         'description': infos['description'],
         'enabled': infos['enabled'],
         'hot_config': infos['hot_config'],
         'priority': infos['priority'],
     }
Exemplo n.º 5
0
def force_remove_multiple(*models, silent=False):
    count = 0
    for m in models:
        if isinstance(m, db.Model):
            db.session.delete(m)
            count += 1
        else:
            msg = "DbModel Not Found:<{}:{}>".format(type(m), m)
            if silent:
                logger.error(msg)
            else:
                db.session.rollback()
                raise errors.NotFound(msg)
    if count > 0:
        db.session.commit()
Exemplo n.º 6
0
    async def _handle_ws_reverse(self, adapter: str):
        websocket: QuartWebSocket = _websocket
        ws = WebSocket(websocket.http_version, websocket.scheme,
                       websocket.path, websocket.query_string,
                       dict(websocket.headers), websocket)

        if adapter not in self._adapters:
            logger.warning(
                f'Unknown adapter {adapter}. Please register the adapter before use.'
            )
            raise exceptions.NotFound()

        BotClass = self._adapters[adapter]
        self_id, response = await BotClass.check_permission(self, ws)

        if not self_id:
            raise exceptions.Unauthorized(
                description=(response and response.body or b"").decode())

        if self_id in self._clients:
            logger.opt(colors=True).warning(
                "There's already a websocket connection, "
                f"<y>{escape_tag(adapter.upper())} Bot {escape_tag(self_id)}</y> ignored."
            )
            raise exceptions.Forbidden(description='Client already exists.')

        bot = BotClass(self_id, ws)
        await ws.accept()
        logger.opt(colors=True).info(
            f"WebSocket Connection from <y>{escape_tag(adapter.upper())} "
            f"Bot {escape_tag(self_id)}</y> Accepted!")
        self._bot_connect(bot)

        try:
            while not ws.closed:
                try:
                    data = await ws.receive()
                except asyncio.CancelledError:
                    logger.warning("WebSocket disconnected by peer.")
                    break
                except Exception as e:
                    logger.opt(exception=e).error(
                        "Error when receiving data from websocket.")
                    break

                asyncio.create_task(bot.handle_message(data.encode()))
        finally:
            self._bot_disconnect(bot)
Exemplo n.º 7
0
    def testErrorPageIsRenderedForUndefinedURL(self):
        """Test that error page is shown for undefined URL."""
        self.driver.get(self.args.server_url + '/foobar123')

        error_code_element = self.driver.find_element(
            *ErrorPage.ERROR_CODE_ELEMENT)
        error_description_element = self.driver.find_element(
            *ErrorPage.ERROR_DESCRIPTION_ELEMENT)

        error_404 = exceptions.NotFound()

        self.assertEquals('Error ' + str(error_404.code),
                          error_code_element.text)

        self.assertEquals(error_404.description.replace('  ', ' '),
                          error_description_element.text.encode('ascii'))
Exemplo n.º 8
0
    def get(self, report_uid):
        """Get a Testplan report (JSON) given it's uid."""
        # report_uid will be used when looking up the report from a database.
        report_path = os.path.abspath(
            os.path.join(
                app.config["DATA_PATH"], app.config["TESTPLAN_REPORT_NAME"]
            )
        )

        if os.path.exists(report_path):
            return send_from_directory(
                directory=os.path.dirname(report_path),
                filename=os.path.basename(report_path),
            )
        else:
            raise exceptions.NotFound()
Exemplo n.º 9
0
def get_all_vars(vtype, job):
    vt = Vtype[vtype]
    values = request.args.get("values", type=util.boolstr)
    only = request.args.get("only")
    if only is not None:
        only = [vn.strip() for vn in only.split(",")]
    if vt == Vtype.results:
        tasks.flush_updates()
    try:
        with db.transact() as conn:
            vars = _get_vars(db.get_state(conn), vt, job)
            return jsonify({k: vars[k]
                            for k in only}
                           if only else dict(vars) if values else list(vars))
    except KeyError as e:
        raise wexc.NotFound("No such variable: %s" % e) from e
Exemplo n.º 10
0
def search_model(model, query):

    known_columns = model.__table__.columns.keys()

    for search_column in flask.request.args:
        if search_column not in known_columns:
            raise exceptions.NotFound(
                'Search term %s is not supported' % search_column)

        search_terms = flask.request.args.getlist(search_column)
        if search_terms:
            search_terms = [term.lower() for term in search_terms]
            query = query.filter(
                func.lower(getattr(model, search_column)).in_(search_terms))

    return query
Exemplo n.º 11
0
def remove_category(category):
    current_category = Categories.query.filter(
        Categories.category_id == category).first()
    if not current_category:
        return exceptions.NotFound()

    form = KillMii()
    if form.validate_on_submit():
        db.session.delete(current_category)
        db.session.commit()

        delete_category_thumbnail(current_category.category_id)

        return redirect(url_for("list_categories"))

    return render_template("category_delete.html", form=form, item_id=category)
Exemplo n.º 12
0
def home_jstree():
    """Entry point to view the home project as JSTree"""
    api = system_util.pillar_api()

    try:
        project = Project.find_from_endpoint('/bcloud/home-project',
                                             params={'projection': {
                                                 '_id': 1,
                                                 'permissions': 1,
                                                 'category': 1,
                                                 'user': 1}},
                                             api=api)
    except ResourceNotFound:
        raise wz_exceptions.NotFound('No such project')

    return jsonify(items=jstree_get_children(None, project._id))
Exemplo n.º 13
0
def del_lab_agent(id, agent_id):
    lab_agent = (
        models
        .LabAgent
        .query
        .filter_by(agent_id=agent_id, lab_id=id)
        .first())

    if not lab_agent:
        raise exceptions.NotFound('Lab <-> agent binding not found')

    db.session.delete(lab_agent)
    db.session.commit()

    engine = models.Lab.query.filter_by(id=id).first()
    schema = schemas.LabSchema()
    return schema.jsonify(engine), 204
Exemplo n.º 14
0
def show_supervisors(id=None):
    supervisor_query = (models.Supervisor.query.outerjoin(models.Process))

    if id is None:
        supervisors = supervisor_query.all()

    else:
        supervisor_query = (supervisor_query.filter(
            models.Supervisor.id == id))

        supervisors = supervisor_query.first()

        if not supervisors:
            raise exceptions.NotFound('Supervisor not found')

    schema = schemas.SupervisorSchema(many=id is None)
    return schema.jsonify(supervisors), 200
Exemplo n.º 15
0
def del_engine_endpoint(id, endpoint_id):
    engine_endpoint = (
        models
        .EngineEndpoint
        .query
        .filter_by(engine_id=id, endpoint_id=endpoint_id).first())

    if not engine_endpoint:
        raise exceptions.NotFound('Engine <-> endpoint binding not found')

    db.session.delete(engine_endpoint)
    db.session.commit()

    engine = models.Engine.query.filter_by(id=id).first()
    schema = schemas.EngineSchema()

    return schema.jsonify(engine), 204
Exemplo n.º 16
0
def del_engine_user(id, user_id):
    engine_user = (
        models
        .EngineUser
        .query
        .filter_by(engine_id=id, user_id=user_id)
        .first())

    if not engine_user:
        raise exceptions.NotFound('Engine <-> user binding not found')

    db.session.delete(engine_user)
    db.session.commit()

    engine = models.Engine.query.filter_by(id=id).first()
    schema = schemas.EngineSchema()
    return schema.jsonify(engine), 204
Exemplo n.º 17
0
 def find_service_account_id(self,
                             manager_id: bson.ObjectId) -> bson.ObjectId:
     _, manager = self._get_manager(mngr_doc_id=manager_id,
                                    projection={'service_account': 1})
     users_coll = current_app.db('users')
     service_account_id = manager['service_account']
     service_account = users_coll.find_one({
         '_id': service_account_id,
         'service.flamenco_manager': {
             '$exists': True
         }
     })
     if not service_account:
         self._log.error('Unable to find service account %s for manager %s',
                         service_account_id, manager_id)
         raise wz_exceptions.NotFound()
     return service_account_id
Exemplo n.º 18
0
def jstree(project_url):
    """Entry point to view a project as JSTree"""
    api = system_util.pillar_api()

    try:
        project = Project.find_one(
            {
                'projection': {
                    '_id': 1
                },
                'where': {
                    'url': project_url
                }
            }, api=api)
    except ResourceNotFound:
        raise wz_exceptions.NotFound('No such project')

    return jsonify(items=jstree_get_children(None, project._id))
Exemplo n.º 19
0
def show_console(id, page_id=None):
    console_query = (models.ConsolePage.query.join(models.Process).filter(
        models.Process.id == id).order_by(models.ConsolePage.timestamp.asc()))

    if page_id is None:
        pages = console_query.all()

    else:
        console_query = (console_query.filter(
            models.ConsolePage.id == page_id))

        pages = console_query.first()

        if not pages:
            raise exceptions.NotFound('Console page not found')

    schema = schemas.ConsoleSchema(many=page_id is None)
    return schema.jsonify(pages), 200
Exemplo n.º 20
0
def url_for_node(node_id=None, node=None):
    assert isinstance(node_id, (str, type(None)))

    api = system_util.pillar_api()

    if node_id is None and node is None:
        raise ValueError('Either node or node_id must be given')

    if node is None:
        try:
            node = Node.find(node_id, api=api)
        except ResourceNotFound:
            log.warning(
                'url_for_node(node_id=%r, node=None): Unable to find node.',
                node_id)
            raise wz_exceptions.NotFound('Unable to find node %r' % node_id)

    return finders.find_url_for_node(node)
Exemplo n.º 21
0
def change_lab_power(id, state):
    lab = (
        models.Lab
        .query
        .filter_by(id=id)
        .first())

    if not lab:
        raise exceptions.NotFound('Lab not found')

    lab.power = state.lower()

    db.session.commit()

    lab = models.Lab.query.filter_by(id=id).first()

    schema = schemas.LabSchema()
    return schema.jsonify(lab), 200
Exemplo n.º 22
0
def update_component(model, req, box_id, id):
    component = (model.query.filter_by(box_id=box_id, id=id).first())

    if not component:
        raise exceptions.NotFound(str(model) + ' not found')

    for field in req:
        prev_attr = getattr(component, field)
        if isinstance(req[field], (type(None), type(prev_attr))) or isinstance(
                prev_attr, type(None)):
            setattr(component, field, req[field])
        else:
            raise exceptions.BadRequest('wrong datatype: ' +
                                        str(type(req[field])) +
                                        ' instead of ' + str(type(prev_attr)))

    db.session.add(component)
    db.session.commit()
Exemplo n.º 23
0
def render_node_page(project_url, page_url, api):
    """Custom behaviour for pages, which are nodes, but accessible on a custom
    route base.
    """

    # TODO: ensure this is not called for the home project, as it would
    # generate conflicting websites
    project = find_project_or_404(project_url, api=api)
    try:
        page = Node.find_one({
            'where': {
                'project': project['_id'],
                'node_type': 'page',
                'properties.url': page_url}}, api=api)
    except ResourceNotFound:
        raise wz_exceptions.NotFound('No such node')

    return project, page
Exemplo n.º 24
0
def edit_comment(user_id, node_id, patch):
    """Edits a single comment.

    Doesn't do permission checking; users are allowed to edit their own
    comment, and this is not something you want to revoke anyway. Admins
    can edit all comments.
    """

    # Find the node. We need to fetch some more info than we use here, so that
    # we can pass this stuff to Eve's patch_internal; that way the validation &
    # authorisation system has enough info to work.
    nodes_coll = current_app.data.driver.db['nodes']
    projection = {'user': 1,
                  'project': 1,
                  'node_type': 1}
    node = nodes_coll.find_one(node_id, projection=projection)
    if node is None:
        log.warning('User %s wanted to patch non-existing node %s' % (user_id, node_id))
        raise wz_exceptions.NotFound('Node %s not found' % node_id)

    if node['user'] != user_id and not authorization.user_has_role('admin'):
        raise wz_exceptions.Forbidden('You can only edit your own comments.')

    # Use Eve to PATCH this node, as that also updates the etag.
    r, _, _, status = current_app.patch_internal('nodes',
                                                 {'properties.content': patch['content'],
                                                  'project': node['project'],
                                                  'user': node['user'],
                                                  'node_type': node['node_type']},
                                                 concurrency_check=False,
                                                 _id=node_id)
    if status != 200:
        log.error('Error %i editing comment %s for user %s: %s',
                  status, node_id, user_id, r)
        raise wz_exceptions.InternalServerError('Internal error %i from Eve' % status)
    else:
        log.info('User %s edited comment %s', user_id, node_id)

    # Fetch the new content, so the client can show these without querying again.
    node = nodes_coll.find_one(node_id, projection={
        'properties.content': 1,
        'properties._content_html': 1,
    })
    return status, node
Exemplo n.º 25
0
    def get(self,
            offset=0,
            limit=100,
            scope_id=None,
            scope_key=None,
            fetcher=None,
            collector=None,
            active=None):

        policy.authorize(
            flask.request.context, 'scope:get_state',
            {'project_id': scope_id or flask.request.context.project_id})
        results = self._storage_state.get_all(identifier=scope_id,
                                              scope_key=scope_key,
                                              fetcher=fetcher,
                                              collector=collector,
                                              offset=offset,
                                              limit=limit,
                                              active=active)

        if len(results) < 1:
            raise http_exceptions.NotFound(
                "No resource found for provided filters.")
        return {
            'results': [{
                'scope_id':
                r.identifier,
                'scope_key':
                r.scope_key,
                'fetcher':
                r.fetcher,
                'collector':
                r.collector,
                'state':
                r.last_processed_timestamp.isoformat(),
                'last_processed_timestamp':
                r.last_processed_timestamp.isoformat(),
                'active':
                r.active,
                'scope_activation_toggle_date':
                r.scope_activation_toggle_date.isoformat()
                if r.scope_activation_toggle_date else None
            } for r in results]
        }
Exemplo n.º 26
0
def show_endpoints(id=None, endpoint_id=None):
    endpoint_query = (models.Endpoint.query.join(models.Process))

    if id is not None:
        endpoint_query = (endpoint_query.filter(models.Process.id == id))

    if endpoint_id is None:
        endpoints = endpoint_query.all()

    else:
        endpoint_query = (endpoint_query.filter(models.Endpoint.id == id))

        endpoints = endpoint_query.first()

        if not endpoints:
            raise exceptions.NotFound('Endpoint not found')

    schema = schemas.EndpointSchema(many=endpoint_id is None)
    return schema.jsonify(endpoints), 200
Exemplo n.º 27
0
def error_api():
    if request.form.get("action") is None:
        print("Received an error!")

        print_multi(request.args)
        print_multi(request.form)

        return action_list["webApi_document_template"](request)

    try:
        # These values should be consistent for both v1 and v512.
        if request.form["platform"] != "wii":
            return exceptions.BadRequest()

        action = request.form["action"]
        return action_list[action](request)
    except KeyError:
        # This is not an action or a format we know of.
        return exceptions.NotFound()
Exemplo n.º 28
0
    def get(self, offset=0, limit=100, begin=None, end=None, filters=None):

        policy.authorize(
            flask.request.context,
            'dataframes:get',
            {'tenant_id': flask.request.context.project_id},
        )

        begin = begin or tzutils.get_month_start()
        end = end or tzutils.get_next_month()

        if filters and 'type' in filters:
            metric_types = [filters.pop('type')]
        else:
            metric_types = None

        if not flask.request.context.is_admin:
            if flask.request.context.project_id is None:
                # Unscoped non-admin user
                return {'total': 0, 'dataframes': []}
            scope_key = CONF.collect.scope_key
            if filters:
                filters[scope_key] = flask.request.context.project_id
            else:
                filters = {scope_key: flask.request.context.project_id}

        results = self._storage.retrieve(
            begin=begin,
            end=end,
            filters=filters,
            metric_types=metric_types,
            offset=offset,
            limit=limit,
        )

        if results['total'] < 1:
            raise http_exceptions.NotFound(
                "No resource found for provided filters.")

        return {
            'total': results['total'],
            'dataframes': results['dataframes'],
        }
Exemplo n.º 29
0
    def doc(self):
        """Doc for the controller."""
        if not self._doc:
            partial = self.params['partial']
            ext_config = structures.DeepReferenceDict(
                self.pod.extensions_controller.extension_config(
                    'extensions.editor.EditorExtension'))

            collection_path = ext_config['screenshots.partials.collection']

            if not collection_path:
                raise werkzeug_exceptions.BadRequest(
                    'No collection path defined for partial screenshots')

            col = self.pod.get_collection(collection_path)

            pod_partial = self.pod.partials.get_partial(partial)
            partial_example = pod_partial.config.get('editor', {}).get(
                'examples', {}).get(self.params['key'])
            if not partial_example:
                raise werkzeug_exceptions.NotFound(
                    'Unable to find example in partial: {}'.format(
                        self.params['key']))

            partial_example['partial'] = partial

            doc_fields = {
                '$view': ext_config['screenshots.partials.view'],
                'partials': [
                    partial_example,
                ],
            }

            locale = self.route_info.meta.get('locale',
                                              self.params.get('locale'))
            pod_path = os.path.join(collection_path, '_partial.yaml')
            self._doc = document.Document(pod_path,
                                          locale=locale,
                                          _pod=self.pod,
                                          _collection=col)
            self._doc.format.update(fields=doc_fields)
        return self._doc
Exemplo n.º 30
0
def pay_list_category(list_id: int):
    queried_categories = PayCategories.query.order_by(
        PayCategories.name.asc()).all()

    retrieved_data = (db.session.query(PayCategories).filter(
        PayCategories.genre_id == list_id).order_by(
            PayCategories.category_id).all())
    filler = []

    if list_id <= 9:
        for i, pay_categories in enumerate(queried_categories):
            # Items must be indexed by 1.
            filler.append(
                RepeatedElement({
                    "place": i + 1,
                    "categid": pay_categories.category_id,
                    "name": pay_categories.name,
                    "sppageid": 0,
                    "splinktext": "Link Text",
                }))
    else:
        if not retrieved_data:
            # Looks like this category does not exist, or contains no movies.
            return exceptions.NotFound()

        for i, pay_categories in enumerate(retrieved_data):
            # This section allows us to sort the categories into genres
            # Instead of them showing up in all of the genres
            filler.append(
                RepeatedElement({
                    "place": i + 1,
                    "categid": pay_categories.category_id,
                    "name": pay_categories.name,
                    "sppageid": 0,
                    "splinktext": "Link Text",
                }))

    return {
        "type": 3,
        "img": 1,
        "categinfo": filler,
    }