incubator-allura-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From john...@apache.org
Subject [09/11] git commit: [#5650] ticket:323 Pagination for forum (topics list)
Date Wed, 26 Jun 2013 22:57:04 GMT
[#5650] ticket:323 Pagination for forum (topics list)


Project: http://git-wip-us.apache.org/repos/asf/incubator-allura/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-allura/commit/f1b854c9
Tree: http://git-wip-us.apache.org/repos/asf/incubator-allura/tree/f1b854c9
Diff: http://git-wip-us.apache.org/repos/asf/incubator-allura/diff/f1b854c9

Branch: refs/heads/master
Commit: f1b854c9b455856ac3f01360465acd614221008c
Parents: fda008f
Author: Igor Bondarenko <jetmind2@gmail.com>
Authored: Tue May 7 09:31:57 2013 +0000
Committer: Cory Johns <cjohns@slashdotmedia.com>
Committed: Wed Jun 26 22:56:12 2013 +0000

----------------------------------------------------------------------
 .../forgediscussion/controllers/root.py         | 23 ++++++++++++++++++--
 ForgeDiscussion/forgediscussion/model/forum.py  |  7 ------
 .../tests/functional/test_rest.py               | 16 ++++++++++++++
 3 files changed, 37 insertions(+), 9 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-allura/blob/f1b854c9/ForgeDiscussion/forgediscussion/controllers/root.py
----------------------------------------------------------------------
diff --git a/ForgeDiscussion/forgediscussion/controllers/root.py b/ForgeDiscussion/forgediscussion/controllers/root.py
index bef02b1..7ff7fcb 100644
--- a/ForgeDiscussion/forgediscussion/controllers/root.py
+++ b/ForgeDiscussion/forgediscussion/controllers/root.py
@@ -26,6 +26,7 @@ from pylons import tmpl_context as c, app_globals as g
 from pylons import request
 from formencode import validators
 from webob import exc
+import pymongo
 
 from allura.lib.security import require_access, has_access, require_authenticated
 from allura.lib.search import search_app
@@ -289,8 +290,26 @@ class ForumRestController(BaseController):
         require_access(self.forum, 'read')
 
     @expose('json:')
-    def index(self, **kw):
-        return dict(forum=self.forum)
+    def index(self, limit=100, page=0, **kw):
+        limit, page, start = g.handle_paging(int(limit), int(page))
+        topics = model.Forum.thread_class().query.find(dict(discussion_id=self.forum._id))
+        topics = topics.sort([('flags', pymongo.DESCENDING),
+                              ('last_post_date', pymongo.DESCENDING)])
+        topics = topics.skip(start).limit(limit)
+        count = topics.count()
+        json = {}
+        json['forum'] = self.forum.__json__()
+        json['forum']['topics'] = [dict(_id=t._id,
+                                        subject=t.subject,
+                                        num_replies=t.num_replies,
+                                        num_views=t.num_views,
+                                        url=h.absurl('/rest' + t.url()),
+                                        last_post=t.last_post)
+                                   for t in topics]
+        json['count'] = count
+        json['page'] = page
+        json['limit'] = limit
+        return json
 
     @expose()
     def _lookup(self, thread, thread_id, *remainder):

http://git-wip-us.apache.org/repos/asf/incubator-allura/blob/f1b854c9/ForgeDiscussion/forgediscussion/model/forum.py
----------------------------------------------------------------------
diff --git a/ForgeDiscussion/forgediscussion/model/forum.py b/ForgeDiscussion/forgediscussion/model/forum.py
index d6c5ba1..e8f920a 100644
--- a/ForgeDiscussion/forgediscussion/model/forum.py
+++ b/ForgeDiscussion/forgediscussion/model/forum.py
@@ -142,13 +142,6 @@ class Forum(M.Discussion):
     def __json__(self):
         json = super(Forum, self).__json__()
         json.pop('threads')  # it's always empty and useless here
-        json['topics'] = [dict(_id=t._id,
-                               subject=t.subject,
-                               num_replies=t.num_replies,
-                               num_views=t.num_views,
-                               url=h.absurl('/rest' + t.url()),
-                               last_post=t.last_post)
-                          for t in self.all_threads]
         return json
 
 class ForumFile(M.File):

http://git-wip-us.apache.org/repos/asf/incubator-allura/blob/f1b854c9/ForgeDiscussion/forgediscussion/tests/functional/test_rest.py
----------------------------------------------------------------------
diff --git a/ForgeDiscussion/forgediscussion/tests/functional/test_rest.py b/ForgeDiscussion/forgediscussion/tests/functional/test_rest.py
index 5909822..87c2fda 100644
--- a/ForgeDiscussion/forgediscussion/tests/functional/test_rest.py
+++ b/ForgeDiscussion/forgediscussion/tests/functional/test_rest.py
@@ -115,6 +115,22 @@ class TestRootRestController(TestDiscussionApiBase):
         assert_equal(resp.json['page'], 1)
         assert_equal(resp.json['limit'], 1)
 
+    def test_forum_pagination(self):
+        resp = self.app.get('/rest/p/test/discussion/general/?limit=1')
+        topics = resp.json['forum']['topics']
+        assert_equal(len(topics), 1)
+        assert_equal(topics[0]['subject'], 'Hi guys')
+        assert_equal(resp.json['count'], 2)
+        assert_equal(resp.json['page'], 0)
+        assert_equal(resp.json['limit'], 1)
+        resp = self.app.get('/rest/p/test/discussion/general/?limit=1&page=1')
+        topics = resp.json['forum']['topics']
+        assert_equal(len(topics), 1)
+        assert_equal(topics[0]['subject'], 'Let\'s talk')
+        assert_equal(resp.json['count'], 2)
+        assert_equal(resp.json['page'], 1)
+        assert_equal(resp.json['limit'], 1)
+
     def test_security(self):
         p = M.Project.query.get(shortname='test')
         acl = p.app_instance('discussion').config.acl


Mime
View raw message