示例#1
0
    def test_generic_pagination(self):
        items = [1, 2, 3, 4, 5, 6]

        # First page
        paginated_items = utils.generic_pagination(items, 1, 2)
        computed = [k for k in paginated_items]
        expected = [1, 2]
        self.assertEqual(computed, expected)
        self.assertFalse(paginated_items.has_previous())
        self.assertTrue(paginated_items.has_next())

        # Second page
        paginated_items = utils.generic_pagination(items, 2, 2)
        computed = [k for k in paginated_items]
        expected = [3, 4]
        self.assertEqual(computed, expected)
        self.assertTrue(paginated_items.has_previous())
        self.assertTrue(paginated_items.has_next())

        # Last page
        paginated_items = utils.generic_pagination(items, 3, 2)
        computed = [k for k in paginated_items]
        expected = [5, 6]
        self.assertEqual(computed, expected)
        self.assertTrue(paginated_items.has_previous())
        self.assertFalse(paginated_items.has_next())

        # If page bigger than the last page, always return last page
        paginated_items = utils.generic_pagination(items, 4, 2)
        computed = [k for k in paginated_items]
        expected = [5, 6]
        self.assertEqual(computed, expected)
示例#2
0
    def get_context_data(self, **kwargs):
        context = super(ChangeProjectView, self).get_context_data(**kwargs)
        page = self.request.GET.get('page', 1)

        context['projects'] = utils.generic_pagination(self._get_data(), page)

        return context
示例#3
0
    def get_context_data(self, **kwargs):
        context = super(ListProjectView, self).get_context_data(**kwargs)
        page = self.request.GET.get("page", 1)

        context["projects"] = utils.generic_pagination(self._get_data(), page)

        return context
示例#4
0
    def test_generic_pagination_return_except_emptypage(self, mock_paginator):
        mock_paginator.side_effect = EmptyPage()

        items = [1, 2, 3, 4, 5, 6]

        paginated_items = utils.generic_pagination(items, 1, 2)

        self.assertEqual(paginated_items.next_page_number(), 2)
示例#5
0
    def test_generic_pagination_return_except_pagenotinteger(
            self, mock_paginator):
        mock_paginator.side_effect = PageNotAnInteger()

        items = [1, 2, 3, 4, 5, 6]

        paginated_items = utils.generic_pagination(items, 1, 2)

        self.assertEqual(paginated_items.number, 1)
示例#6
0
    def get_context_data(self, **kwargs):
        context = super(ListProjectView, self).get_context_data(**kwargs)
        page = self.request.GET.get('page', 1)

        context['is_admin'] = self.request.path[0:16] == '/admin/projects/'

        try:
            projects = sorted(self.keystone.project_list(),
                              key=lambda l: l.name.lower())
            context['projects'] = utils.generic_pagination(projects, page)
        except Exception as e:
            log.exception('{}{}'.format(_('Exception:').encode('UTF-8'), e))
            messages.add_message(self.request, messages.ERROR,
                                 _('Unable to list projects'))

        return context
示例#7
0
文件: views.py 项目: globocom/vault
    def get_context_data(self, **kwargs):
        context = super(ListProjectView, self).get_context_data(**kwargs)
        page = self.request.GET.get('page', 1)

        context['is_admin'] = self.request.path[0:16] == '/admin/projects/'

        try:
            projects = sorted(self.keystone.project_list(),
                                key=lambda l: l.name.lower())
            context['projects'] = utils.generic_pagination(projects, page)
        except Exception as e:
            log.exception('{}{}'.format(_('Exception:').encode('UTF-8'), e))
            messages.add_message(self.request, messages.ERROR,
                                 _('Unable to list projects'))

        return context
示例#8
0
    def get_context_data(self, **kwargs):
        context = super(ListUserView, self).get_context_data(**kwargs)
        page = self.request.GET.get('page', 1)

        users = []

        try:
            users = self.keystone.user_list()
        except Exception as e:
            log.exception('{}{}'.format(_('Exception:').encode('UTF-8'), e))
            messages.add_message(self.request, messages.ERROR,
                                 _('Unable to list users'))

        sorted_users = sorted(users, key=lambda l: l.name.lower())
        context['users'] = utils.generic_pagination(sorted_users, page)

        return context
示例#9
0
文件: views.py 项目: globocom/vault
    def get_context_data(self, **kwargs):
        context = super(ListUserView, self).get_context_data(**kwargs)
        page = self.request.GET.get('page', 1)

        users = []

        try:
            users = self.keystone.user_list()
        except Exception as e:
            log.exception('{}{}'.format(_('Exception:').encode('UTF-8'), e))
            messages.add_message(self.request, messages.ERROR,
                                 _('Unable to list users'))

        sorted_users = sorted(users, key=lambda l: l.name.lower())
        context['users'] = utils.generic_pagination(sorted_users, page)

        return context