incubator-allura-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From john...@apache.org
Subject [01/11] git commit: [#5650] ticket:323 REST API endpoint for forum
Date Wed, 26 Jun 2013 22:56:56 GMT
Updated Branches:
  refs/heads/master 6d2320f7f -> a36f8c391


[#5650] ticket:323 REST API endpoint for forum


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

Branch: refs/heads/master
Commit: 383f697a66b34e7314f5ec1d39aa7a4200b12888
Parents: d998b4f
Author: Igor Bondarenko <jetmind2@gmail.com>
Authored: Thu May 2 13:11:12 2013 +0000
Committer: Cory Johns <cjohns@slashdotmedia.com>
Committed: Wed Jun 26 22:56:11 2013 +0000

----------------------------------------------------------------------
 .../forgediscussion/controllers/root.py         | 46 ++++++++++++++++++++
 .../tests/functional/test_rest.py               | 18 +++++++-
 2 files changed, 63 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-allura/blob/383f697a/ForgeDiscussion/forgediscussion/controllers/root.py
----------------------------------------------------------------------
diff --git a/ForgeDiscussion/forgediscussion/controllers/root.py b/ForgeDiscussion/forgediscussion/controllers/root.py
index 47cc297..74d5d18 100644
--- a/ForgeDiscussion/forgediscussion/controllers/root.py
+++ b/ForgeDiscussion/forgediscussion/controllers/root.py
@@ -20,6 +20,7 @@ import logging
 from urllib import unquote
 from itertools import imap
 
+import pymongo
 from tg import expose, validate, redirect, flash, response
 from tg.decorators import with_trailing_slash
 from pylons import tmpl_context as c, app_globals as g
@@ -217,6 +218,10 @@ class RootRestController(BaseController):
     def _check_security(self):
         require_access(c.app, 'read')
 
+    @expose()
+    def _lookup(self, forum, *remainder):
+        return ForumRestController(unquote(forum)), remainder
+
     @expose('json:')
     def index(self, **kw):
         forums = model.Forum.query.find(dict(
@@ -272,3 +277,44 @@ class RootRestController(BaseController):
             raise
             log.exception(e)
             return dict(status=False, errors=[str(e)])
+
+
+class ForumRestController(BaseController):
+
+    def __init__(self, forum):
+        self.forum = model.Forum.query.get(
+            app_config_id=c.app.config._id,
+            shortname=forum)
+        if not self.forum and not self.forum.deleted:
+            raise exc.HTTPNotFound()
+        super(ForumRestController, self).__init__()
+
+    def _check_security(self):
+        require_access(self.forum, 'read')
+
+    @expose('json:')
+    def index(self, **kw):
+        topics = model.ForumThread.query.find(
+            dict(discussion_id=self.forum._id, num_replies={'$gt': 0}))
+        topics = topics.sort([('flags', pymongo.DESCENDING),
+                              ('last_post_date', pymongo.DESCENDING)])
+        json = {
+            'forum': {
+                'name': self.forum.name,
+                'description': self.forum.description,
+                'topics': [],
+            }
+        }
+        for t in topics:
+            topic = {
+                'subject': t.subject,
+                'num_views': t.num_views,
+                'num_replies': t.num_replies,
+                'url': h.absurl('/rest' + t.url()),
+                'last_post': {
+                    'author': t.last_post.author().display_name,
+                    'date': t.last_post.mod_date
+                },
+            }
+            json['forum']['topics'].append(topic)
+        return json

http://git-wip-us.apache.org/repos/asf/incubator-allura/blob/383f697a/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 e7070ea..d094175 100644
--- a/ForgeDiscussion/forgediscussion/tests/functional/test_rest.py
+++ b/ForgeDiscussion/forgediscussion/tests/functional/test_rest.py
@@ -16,7 +16,7 @@ class TestDiscussionApiBase(TestRestApiBase):
     def setup_with_tools(self):
         h.set_context('test', 'discussion', neighborhood='Projects')
         self.create_forum('héllo', 'Say Héllo', 'Say héllo here')
-        self.create_topic('general', 'Lets talk', '1st post')
+        self.create_topic('general', 'Let\'s talk', '1st post')
         self.create_topic('general', 'Hi guys', 'Hi guys')
 
     def create_forum(self, shortname, name, description):
@@ -57,3 +57,19 @@ class TestRootRestController(TestDiscussionApiBase):
         assert_equal(forums[1]['description'], u'Say héllo here')
         assert_equal(forums[1]['num_topics'], 0)
         assert_equal(forums[1]['url'], 'http://localhost:80/rest/p/test/discussion/h%C3%A9llo/')
+
+    def test_forum(self):
+        forum = self.api_get('/rest/p/test/discussion/general/')
+        forum = forum.json['forum']
+        assert_equal(forum['name'], 'General Discussion')
+        assert_equal(forum['description'], 'Forum about anything you want to talk about.')
+        topics = forum['topics']
+        assert_equal(len(topics), 2)
+        assert_equal(topics[0]['subject'], 'Hi guys')
+        assert_equal(topics[0]['num_views'], 0)
+        assert_equal(topics[0]['num_replies'], 1)
+        assert_equal(topics[0]['last_post']['author'], 'Test Admin')
+        assert_equal(topics[1]['subject'], 'Let\'s talk')
+        assert_equal(topics[1]['num_views'], 0)
+        assert_equal(topics[1]['num_replies'], 1)
+        assert_equal(topics[1]['last_post']['author'], 'Test Admin')


Mime
View raw message