Exemplo n.º 1
0
    def prepare_data(self, env, data):
        """View for index page."""
        stream = self.stream
        request = env.request

        # XXX Check permissions and return 403 if no access.
        # XXX Handle POST to edit/delete here.
        query = stream.item_query(env)
        filter_form = stream.get_filter_form(env)

        # Note: errors are displayed, but ignored in code.
        filter_form.accept(request.GET)
        filter_data = filter_form.get_data()
        query = filter_form.filter(query)

        query = stream.order(query)

        paginator = ModelPaginator(
            request,
            query,
            impl=FancyPageRange(),
            limit=getattr(stream.config, "limit", None),
            url=stream.url_for(env).qs_set(filter_data),
        )

        def item_url(item=None):
            item_id = item.id if item is not None else None
            return stream.url_for(env, "item", item=item_id).qs_set(filter_data)

        try:
            paginator.items = stream.config.modify_items(paginator.items)
        except AttributeError:
            pass

        result = dict(
            stream.template_data,
            paginator=paginator,
            stream=stream,
            item_url=item_url,
            list_fields=stream.list_fields,
            title=stream.config.title,
            filter_form=filter_form,
            allow_add=stream.has_permission(env, "c"),
            repr=repr,
        )
        result.update(self.list_form_data(env, paginator, filter_data))
        result = stream.process_list_template_data(env, result)
        return result
Exemplo n.º 2
0
    def prepare_data(self, env, data):
        '''View for index page.'''
        stream = self.stream
        request = env.request

        # XXX Check permissions and return 403 if no access.
        # XXX Handle POST to edit/delete here.
        query = stream.item_query(env)
        filter_form = stream.get_filter_form(env)

        # Note: errors are displayed, but ignored in code.
        filter_form.accept(request.GET)
        filter_data = filter_form.get_data()
        query = filter_form.filter(query)

        query = stream.order(query)

        paginator = ModelPaginator(request,
                                   query,
                                   impl=FancyPageRange(),
                                   limit=getattr(stream.config, 'limit', None),
                                   url=stream.url_for(env).qs_set(filter_data))

        def item_url(item=None):
            item_id = item.id if item is not None else None
            return stream.url_for(env, 'item',
                                  item=item_id).qs_set(filter_data)

        try:
            paginator.items = stream.config.modify_items(paginator.items)
        except AttributeError:
            pass

        result = dict(stream.template_data,
                      paginator=paginator,
                      stream=stream,
                      item_url=item_url,
                      list_fields=stream.list_fields,
                      title=stream.title,
                      filter_form=filter_form,
                      allow_add=stream.has_permission(env, 'c'),
                      live_search=stream.live_search,
                      repr=repr)
        result.update(self.list_form_data(env, paginator, filter_data))
        result = stream.process_list_template_data(env, result)
        return result
Exemplo n.º 3
0
    def test_model_paginator(self):
        query = MockQuery(range(20))
        paginator = ModelPaginator(Request.blank('/news?page=2'),
                                   query,
                                   limit=4)

        self.assertEqual(paginator.count, 20)
        self.assertEqual(paginator.page, 2)
        self.assertEqual(paginator.pages_count, 5)
        self.assertEqual(paginator.items, [4, 5, 6, 7])
        self.assertEqual(list(paginator), [4, 5, 6, 7])
        self.assertEqual(len(paginator), 4)
        self.assertEqual(paginator[0], 4)
Exemplo n.º 4
0
def global_log(env, data, stream=None):
    #insure_is_xhr(env)

    EditLog = env.edit_log_model

    assert (stream and getattr(data, 'item', None)) or \
           not (stream or getattr(data, 'item', None))

    if stream is None:
        query = env.db.query(EditLog)

        stream_names = [
            k for (k, s) in env.streams.items()
            if 'r' in s.get_permissions(env) and s.edit_log_action is not None
        ]
        stream_choices = [(k, env.streams[k].title) for k in stream_names]
        types = {}
        for k in stream_names:
            types.update(env.streams[k].edit_log_action.log_types)
        types = types.items()

        filter_form = EditLogFilterForm(env,
                                        streams=stream_choices,
                                        models=env.models_.admin,
                                        types=types)
    else:
        log_types = stream.edit_log_action.log_types
        filter_form = EditLogItemFilterForm(env,
                                            models=env.models,
                                            types=log_types.items())
        query = EditLog.query_for_item(env.db, data.item)

    filter_form.model = EditLog
    filter_form.accept(env.request.GET)
    query = filter_form.filter(query)

    #url = env.root.build_subreverse(env.current_location)
    #url = stream.url_for(env, self.action, item=data.item.id)
    paginator = ModelPaginator(
        env.request,
        query,
        impl=FancyPageRange(),
        limit=50,
        #url=url
    )
    _preload_users(env, paginator.items)

    if getattr(data, 'item', None) is None:
        expand_obj = partial(_expand, env)
        by_stream_and_id = _preload_items(env, paginator.items)
        paginator.items = [
            expand_obj(obj,
                       for_item=by_stream_and_id.get(
                           (obj.stream_name, obj.object_id)))
            for obj in paginator.items
        ]

    else:
        expand_obj = partial(_expand, env, for_item=data.item)
        paginator.items = [expand_obj(obj) for obj in paginator.items]

    paginator.items = filter(None, paginator.items)

    reverse_data = data.as_dict()
    if stream is not None:
        # XXX hack!
        reverse_data = dict(reverse_data, item=data.item.id)
    current_url = env.root.build_subreverse(env.current_location,
                                            **reverse_data)

    tdata = dict(
        paginator=paginator,
        title=u"Журнал изменений",
        filter_form=filter_form,
        current_url=current_url,
        no_layout='__no_layout' in env.request.GET,
    )
    if stream is not None:
        tdata = dict(tdata, item=data.item, stream=stream)

    return env.render_to_response('edit_log/index', tdata)
Exemplo n.º 5
0
def global_log(env, data, stream=None):
    #insure_is_xhr(env)

    EditLog = env.edit_log_model

    assert (stream and getattr(data, 'item', None)) or \
           not (stream or getattr(data, 'item', None))

    if stream is None:
        query = env.db.query(EditLog)

        stream_names = [k for (k, s) in env.streams.items()
                        if 'r' in s.get_permissions(env) and s.edit_log_action is not None]
        stream_choices = [(k, env.streams[k].title)
                          for k in stream_names]
        types = {}
        for k in stream_names:
            types.update(env.streams[k].edit_log_action.log_types)
        types = types.items()

        filter_form = EditLogFilterForm(env,
                                        streams=stream_choices,
                                        models=env.models_.admin,
                                        types=types)
    else:
        log_types = stream.edit_log_action.log_types
        filter_form = EditLogItemFilterForm(env,
                                            models=env.models,
                                            types=log_types.items())
        query = EditLog.query_for_item(env.db, data.item)

    filter_form.model = EditLog
    filter_form.accept(env.request.GET)
    query = filter_form.filter(query)

    #url = env.root.build_subreverse(env.current_location)
    #url = stream.url_for(env, self.action, item=data.item.id)
    paginator = ModelPaginator(env.request, query,
                               impl=FancyPageRange(),
                               limit=50,
                               #url=url
                               )
    _preload_users(env, paginator.items)

    if getattr(data, 'item', None) is None:
        expand_obj = partial(_expand, env)
        by_stream_and_id = _preload_items(env, paginator.items)
        paginator.items = [expand_obj(obj,
                                      for_item=by_stream_and_id.get((obj.stream_name, obj.object_id)))
                           for obj in paginator.items]

    else:
        expand_obj = partial(_expand, env, for_item=data.item)
        paginator.items = [expand_obj(obj) for obj in paginator.items]

    paginator.items = filter(None, paginator.items)

    reverse_data = data.as_dict()
    if stream is not None:
        # XXX hack!
        reverse_data = dict(reverse_data,
                            item=data.item.id)
    current_url = env.root.build_subreverse(env.current_location, **reverse_data)

    tdata = dict(paginator=paginator,
                 title=u"Журнал изменений",
                 filter_form=filter_form,
                 current_url=current_url,
                 no_layout='__no_layout' in env.request.GET,
                 )
    if stream is not None:
        tdata = dict(tdata, item=data.item,
                            stream=stream)

    return env.render_to_response('edit_log/index', tdata)