You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@allura.apache.org by br...@apache.org on 2012/09/21 22:18:34 UTC

[14/16] git commit: [#4077] ticket:166 add url_paginated method to Post

[#4077] ticket:166 add url_paginated method to Post


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

Branch: refs/heads/master
Commit: ea633a01117f12619aa56147ddec8a8ffc10cd5e
Parents: dd05a30
Author: Igor Bondarenko <je...@gmail.com>
Authored: Wed Sep 12 16:02:53 2012 +0300
Committer: Dave Brondsema <db...@geek.net>
Committed: Wed Sep 19 17:16:15 2012 +0000

----------------------------------------------------------------------
 Allura/allura/model/discuss.py               |   38 ++++++++++++++
 Allura/allura/tests/model/test_discussion.py |   54 ++++++++++++++++++++-
 2 files changed, 91 insertions(+), 1 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-allura/blob/ea633a01/Allura/allura/model/discuss.py
----------------------------------------------------------------------
diff --git a/Allura/allura/model/discuss.py b/Allura/allura/model/discuss.py
index 866a75c..01ca263 100644
--- a/Allura/allura/model/discuss.py
+++ b/Allura/allura/model/discuss.py
@@ -1,3 +1,4 @@
+import sys
 import logging
 from datetime import datetime
 
@@ -505,6 +506,43 @@ class Post(Message, VersionedArtifact, ActivityObject):
         else:  # pragma no cover
             return None
 
+    def url_paginated(self):
+        '''Return link to the thread with a #target that poins to this comment.
+
+        Also handle pagination properly.
+        '''
+        if not self.thread:  # pragma no cover
+            return None
+        limit, p, s = g.handle_paging(None, 0)  # get paging limit
+        if self.query.find(dict(thread_id=self.thread._id)).count() <= limit:
+            # all posts in a single page
+            page = 0
+        else:
+            posts = self.thread.find_posts(None, sys.maxint)
+            posts = self.thread.create_post_threads(posts)
+
+            def find_i(posts):
+                '''Find the index number of this post in the display order'''
+                q = []
+                def traverse(posts):
+                    for p in posts:
+                        if p['post']._id == self._id:
+                            return True  # found
+                        q.append(p)
+                        if traverse(p['children']):
+                            return True
+                traverse(posts)
+                return len(q)
+
+            page = find_i(posts) / limit
+
+        slug = h.urlquote(self.slug)
+        url = self.thread.url()
+        if page == 0:
+            return '%s?limit=%s#%s' % (url, limit, slug)
+        return '%s?limit=%s&page=%s#%s' % (url, limit, page, slug)
+
+
     def shorthand_id(self):
         if self.thread:
             return '%s#%s' % (self.thread.shorthand_id(), self.slug)

http://git-wip-us.apache.org/repos/asf/incubator-allura/blob/ea633a01/Allura/allura/tests/model/test_discussion.py
----------------------------------------------------------------------
diff --git a/Allura/allura/tests/model/test_discussion.py b/Allura/allura/tests/model/test_discussion.py
index 4ccb16b..27441eb 100644
--- a/Allura/allura/tests/model/test_discussion.py
+++ b/Allura/allura/tests/model/test_discussion.py
@@ -4,7 +4,7 @@ Model tests for artifact
 """
 from cStringIO import StringIO
 import time
-from datetime import datetime
+from datetime import datetime, timedelta
 from cgi import FieldStorage
 
 from pylons import c, g, request, response
@@ -233,3 +233,55 @@ def test_post_permission_check():
     except exc.HTTPUnauthorized:
         pass
     p2 = t.post('This post will pass the check.', ignore_security=True)
+
+
+@with_setup(setUp, tearDown)
+def test_post_url_paginated():
+    d = M.Discussion(shortname='test', name='test')
+    t = M.Thread(discussion_id=d._id, subject='Test Thread')
+    p = []  # posts in display order
+    ts = datetime.now() - timedelta(days=1)
+    for i in range(5):
+        ts += timedelta(minutes=1)
+        p.append(t.post('This is a post #%s' % i, timestamp=ts))
+
+    ts += timedelta(minutes=1)
+    p.insert(1, t.post(
+        'This is reply #0 to post #0', parent_id=p[0]._id, timestamp=ts))
+
+    ts += timedelta(minutes=1)
+    p.insert(2, t.post(
+        'This is reply #1 to post #0', parent_id=p[0]._id, timestamp=ts))
+
+    ts += timedelta(minutes=1)
+    p.insert(4, t.post(
+        'This is reply #0 to post #1', parent_id=p[3]._id, timestamp=ts))
+
+    ts += timedelta(minutes=1)
+    p.insert(6, t.post(
+        'This is reply #0 to post #2', parent_id=p[5]._id, timestamp=ts))
+
+    ts += timedelta(minutes=1)
+    p.insert(7, t.post(
+        'This is reply #1 to post #2', parent_id=p[5]._id, timestamp=ts))
+
+    ts += timedelta(minutes=1)
+    p.insert(8, t.post(
+        'This is reply #0 to reply #1 to post #2',
+        parent_id=p[7]._id, timestamp=ts))
+
+    # with default paging limit
+    for _p in p:
+        url = t.url() + '?limit=50#' + _p.slug
+        assert _p.url_paginated() == url, _p.url_paginated()
+
+    # with user paging limit
+    limit = 3
+    c.user.set_pref('results_per_page', limit)
+    for i, _p in enumerate(p):
+        page = i / limit
+        url = t.url() + '?limit=%s' % limit
+        if page > 0:
+            url += '&page=%s' % page
+        url += '#' + _p.slug
+        assert _p.url_paginated() == url, _p.url_paginated()