Ejemplo n.º 1
0
def test_list_row_actions():
    app, admin = setup()
    client = app.test_client()

    from flask_admin.model import template

    # Test default actions
    view = MockModelView(Model, endpoint="test")
    admin.add_view(view)

    actions = view.get_list_row_actions()
    ok_(isinstance(actions[0], template.EditRowAction))
    ok_(isinstance(actions[1], template.DeleteRowAction))

    rv = client.get("/admin/test/")
    eq_(rv.status_code, 200)

    # Test default actions
    view = MockModelView(
        Model, endpoint="test1", can_edit=False, can_delete=False, can_view_details=True
    )
    admin.add_view(view)

    actions = view.get_list_row_actions()
    eq_(len(actions), 1)
    ok_(isinstance(actions[0], template.ViewRowAction))

    rv = client.get("/admin/test1/")
    eq_(rv.status_code, 200)

    # Test popups
    view = MockModelView(
        Model,
        endpoint="test2",
        can_view_details=True,
        details_modal=True,
        edit_modal=True,
    )
    admin.add_view(view)

    actions = view.get_list_row_actions()
    ok_(isinstance(actions[0], template.ViewPopupRowAction))
    ok_(isinstance(actions[1], template.EditPopupRowAction))
    ok_(isinstance(actions[2], template.DeleteRowAction))

    rv = client.get("/admin/test2/")
    eq_(rv.status_code, 200)

    # Test custom views
    view = MockModelView(
        Model,
        endpoint="test3",
        column_extra_row_actions=[
            template.LinkRowAction(
                "glyphicon glyphicon-off", "http://localhost/?id={row_id}"
            ),
            template.EndpointLinkRowAction(
                "glyphicon glyphicon-test", "test1.index_view"
            ),
        ],
    )
    admin.add_view(view)

    actions = view.get_list_row_actions()
    ok_(isinstance(actions[0], template.EditRowAction))
    ok_(isinstance(actions[1], template.DeleteRowAction))
    ok_(isinstance(actions[2], template.LinkRowAction))
    ok_(isinstance(actions[3], template.EndpointLinkRowAction))

    rv = client.get("/admin/test3/")
    eq_(rv.status_code, 200)

    data = rv.data.decode("utf-8")

    ok_("glyphicon-off" in data)
    ok_("http://localhost/?id=" in data)
    ok_("glyphicon-test" in data)
Ejemplo n.º 2
0
class TaskView(MongoView):

    collections_map = {
        'crawl': mongo_logs.crawl_task_infos,
        'authorized': mongo_logs.authorized_task_infos
    }

    can_view_details = True
    details_template = 'admin/model/task_details.html'

    column_details_list = ("_id", "_project", "_page", "_url", "_custom_id", "kwargs", "name", "received", "succeeded",
                           "root_id", "parent_id", "runtime", "retries", "eta", "expires", "result")
    column_list = ('_id', '_name', '_project', '_page', 'parent_id', 'root_id')


    # column_filters = (filters.FilterEqual('project', u'项目'),
    #                   filters.FilterEqual('page', u'页面'),
    #                   filters.FilterEqual('level', 'Name'))

    column_labels = {

        'project': u'关联项目',
        'page': u'关联页面',
        'task_id': u'关联任务',
        'level': u'日志级别',
        'message': u'信息',

        'created': u'记录时间',
    }

    column_formatters = {
        'message': message_format,
        # 'status': map_format({START: u'启用', PAUSE: u'暂停', STOP: u'停用'}),
        # 'category': map_format({'ds': u'电商', 'sb': u'社保', 'dx': u'电信运营商'}),
        # 'type': map_format({0: u'通用', 1: u'存储', 2: u'告警'}),
        'created': date_format,
    }

    column_extra_row_actions = [template.EndpointLinkRowAction('fa fa-file-code-o', '.get_page')]


    def initialize(self):
        if self.coll_name == 'crawl':
            self.column_list = ('_id', 'name', '_project', '_page', 'parent_id', 'root_id')
        else:
            self.column_list = ('_id', 'name', '_code', '_action', 'parent_id', 'root_id')


    @expose('/page/')
    def get_page(self):
        return_url = get_redirect_target() or self.get_url('.index_view')

        task_id = get_mdict_item_or_list(request.args, 'id')
        task = self.get_one(task_id)

        if task is None:
            flash(gettext('Record does not exist.'), 'error')
            return redirect(return_url)

        if self.coll_name == 'crawl':
            colname = 'crawl_%s' % task['_project']
        else:
            colname = 'auth_%s' % task['_code']
        # print PageDB[colname]
        coll = PageDB[colname]
        page = coll.find({'_task_id': self._get_valid_id(task_id)})
        page = list(page)
        if not page:
            flash(gettext('Record does not exist.'), 'error')
            return redirect(return_url)

        tmpl = 'admin/model/task_page_info.html'
        return self.render(tmpl,
                           # get_value=self.get_list_value,
                           get_value=lambda m, c: m[c],
                           get_type=type,
                           model=page)
Ejemplo n.º 3
0
def test_list_row_actions():
    app, admin = setup()
    client = app.test_client()

    from flask_admin.model import template

    # Test default actions
    view = MockModelView(Model, endpoint='test')
    admin.add_view(view)

    actions = view.get_list_row_actions()
    ok_(isinstance(actions[0], template.EditRowAction))
    ok_(isinstance(actions[1], template.DeleteRowAction))

    rv = client.get('/admin/test/')
    eq_(rv.status_code, 200)

    # Test default actions
    view = MockModelView(Model,
                         endpoint='test1',
                         can_edit=False,
                         can_delete=False,
                         can_view_details=True)
    admin.add_view(view)

    actions = view.get_list_row_actions()
    eq_(len(actions), 1)
    ok_(isinstance(actions[0], template.ViewRowAction))

    rv = client.get('/admin/test1/')
    eq_(rv.status_code, 200)

    # Test popups
    view = MockModelView(Model,
                         endpoint='test2',
                         can_view_details=True,
                         details_modal=True,
                         edit_modal=True)
    admin.add_view(view)

    actions = view.get_list_row_actions()
    ok_(isinstance(actions[0], template.ViewPopupRowAction))
    ok_(isinstance(actions[1], template.EditPopupRowAction))
    ok_(isinstance(actions[2], template.DeleteRowAction))

    rv = client.get('/admin/test2/')
    eq_(rv.status_code, 200)

    # Test custom views
    view = MockModelView(
        Model,
        endpoint='test3',
        column_extra_row_actions=[
            template.LinkRowAction('glyphicon glyphicon-off',
                                   'http://localhost/?id={row_id}'),
            template.EndpointLinkRowAction('glyphicon glyphicon-test',
                                           'test1.index_view')
        ])
    admin.add_view(view)

    actions = view.get_list_row_actions()
    ok_(isinstance(actions[0], template.EditRowAction))
    ok_(isinstance(actions[1], template.DeleteRowAction))
    ok_(isinstance(actions[2], template.LinkRowAction))
    ok_(isinstance(actions[3], template.EndpointLinkRowAction))

    rv = client.get('/admin/test3/')
    eq_(rv.status_code, 200)

    data = rv.data.decode('utf-8')

    ok_('glyphicon-off' in data)
    ok_('http://localhost/?id=' in data)
    ok_('glyphicon-test' in data)