incubator-allura-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From john...@apache.org
Subject [23/50] git commit: [#3154] ticket:407 ForgeBlog API refactoring
Date Mon, 12 Aug 2013 16:41:08 GMT
[#3154]  ticket:407 ForgeBlog API refactoring


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

Branch: refs/heads/cj/6422
Commit: e23020d03926ed022434aade5762221d9f3e486d
Parents: af5c8e6
Author: Yuriy Arhipov <yuriyarhipovua@yandex.ru>
Authored: Fri Jul 26 13:34:29 2013 +0400
Committer: Dave Brondsema <dbrondsema@slashdotmedia.com>
Committed: Wed Jul 31 21:00:24 2013 +0000

----------------------------------------------------------------------
 AlluraTest/alluratest/controller.py             |  2 +-
 ForgeBlog/forgeblog/main.py                     | 42 ++++++--------------
 ForgeBlog/forgeblog/model/blog.py               | 15 +++++++
 .../forgeblog/tests/functional/test_rest.py     | 36 +++++++++++++----
 .../forgeblog/tests/unit/test_blog_post.py      |  9 +++++
 5 files changed, 67 insertions(+), 37 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-allura/blob/e23020d0/AlluraTest/alluratest/controller.py
----------------------------------------------------------------------
diff --git a/AlluraTest/alluratest/controller.py b/AlluraTest/alluratest/controller.py
index 95d9b4b..7b38700 100644
--- a/AlluraTest/alluratest/controller.py
+++ b/AlluraTest/alluratest/controller.py
@@ -168,7 +168,7 @@ class TestRestApiBase(TestController):
         response = fn(
             str(path),
             params=params,
-            status=[200, 302, 400, 403, 404])
+            status=[200, 201, 302, 400, 403, 404])
         if response.status_int == 302:
             return response.follow()
         else:

http://git-wip-us.apache.org/repos/asf/incubator-allura/blob/e23020d0/ForgeBlog/forgeblog/main.py
----------------------------------------------------------------------
diff --git a/ForgeBlog/forgeblog/main.py b/ForgeBlog/forgeblog/main.py
index 0f4e7e6..8bc56c0 100644
--- a/ForgeBlog/forgeblog/main.py
+++ b/ForgeBlog/forgeblog/main.py
@@ -249,15 +249,7 @@ class RootController(BaseController, FeedController):
     @without_trailing_slash
     def save(self, **kw):
         require_access(c.app, 'write')
-        post = BM.BlogPost()
-        for k,v in kw.iteritems():
-            setattr(post, k, v)
-        post.neighborhood_id=c.project.neighborhood_id
-        post.make_slug()
-        post.commit()
-        M.Thread.new(discussion_id=post.app_config.discussion_id,
-               ref_id=post.index_id(),
-               subject='%s discussion' % post.title)
+        post = BM.BlogPost.new(**kw)
         redirect(h.really_unicode(post.url()).encode('utf-8'))
 
 
@@ -451,33 +443,25 @@ class RootRestController(BaseController):
         require_access(c.app, 'read')
 
     @expose('json:')
-    def index(self, title='', text='', state='draft', labels='', **kw):
+    def index(self, title='', text='', state='draft', labels='', limit=10, page=0, **kw):
         if request.method == 'POST':
             require_access(c.app, 'write')
-            post = BM.BlogPost()
-            post.title = title
-            post.state = state
-            post.text = text
-            post.labels = labels.split(',')
-            post.neighborhood_id = c.project.neighborhood_id
-            post.make_slug()
-            M.Thread.new(discussion_id=post.app_config.discussion_id,
-                         ref_id=post.index_id(),
-                         subject='%s discussion' % post.title)
-
-            post.viewable_by = ['all']
-            post.commit()
-            return post.__json__()
+            post = BM.BlogPost.new(
+                title=title,
+                state=state,
+                text=text,
+                labels=labels.split(','),
+                **kw)
+            return exc.HTTPCreated(headers=dict(Location=h.absurl('/rest' + post.url())))
+
         else:
+            result = RootController().index(limit=limit, page=page)
+            posts = result['posts']
             post_titles = []
-            query_filter = dict(app_config_id=c.app.config._id, deleted=False)
-            if not has_access(c.app, 'write')():
-                query_filter['state'] = 'published'
-            posts = BM.BlogPost.query.find(query_filter)
             for post in posts:
                 if has_access(post, 'read')():
                     post_titles.append({'title': post.title, 'url': h.absurl('/rest' + post.url())})
-            return dict(posts=post_titles)
+            return dict(posts=post_titles, count=result['count'], limit=result['limit'],
page=result['page'])
 
     @expose()
     def _lookup(self, year=None, month=None, title=None, *rest):

http://git-wip-us.apache.org/repos/asf/incubator-allura/blob/e23020d0/ForgeBlog/forgeblog/model/blog.py
----------------------------------------------------------------------
diff --git a/ForgeBlog/forgeblog/model/blog.py b/ForgeBlog/forgeblog/model/blog.py
index 07983a0..2cd16e0 100644
--- a/ForgeBlog/forgeblog/model/blog.py
+++ b/ForgeBlog/forgeblog/model/blog.py
@@ -256,8 +256,23 @@ class BlogPost(M.VersionedArtifact, ActivityObject):
             M.Notification.post(
                 artifact=self, topic='metadata', text=description, subject=subject)
 
+    @classmethod
+    def new(cls, **kw):
+        post = cls()
+        for k, v in kw.iteritems():
+            setattr(post, k, v)
+        post.neighborhood_id = c.project.neighborhood_id
+        post.make_slug()
+        post.commit()
+        M.Thread.new(
+            discussion_id=post.app_config.discussion_id,
+            ref_id=post.index_id(),
+            subject='%s discussion' % post.title)
+        return post
+
     def __json__(self):
         return dict(super(BlogPost, self).__json__(),
+                    author=self.author().username,
                     title=self.title,
                     url=h.absurl('/rest' + self.url()),
                     text=self.text,

http://git-wip-us.apache.org/repos/asf/incubator-allura/blob/e23020d0/ForgeBlog/forgeblog/tests/functional/test_rest.py
----------------------------------------------------------------------
diff --git a/ForgeBlog/forgeblog/tests/functional/test_rest.py b/ForgeBlog/forgeblog/tests/functional/test_rest.py
index 5addcde..0ab05c2 100644
--- a/ForgeBlog/forgeblog/tests/functional/test_rest.py
+++ b/ForgeBlog/forgeblog/tests/functional/test_rest.py
@@ -43,15 +43,16 @@ class TestBlogApi(TestRestApiBase):
             'labels': 'label1, label2'
         }
         r = self.api_post('/rest/p/test/blog/', **data)
-        assert_equal(r.status_int, 200)
+        assert_equal(r.status_int, 201)
         url = '/rest' + BM.BlogPost.query.find().first().url()
         r = self.api_get('/rest/p/test/blog/')
         assert_equal(r.json['posts'][0]['title'], 'test')
         assert_equal(r.json['posts'][0]['url'], h.absurl(url))
 
         r = self.api_get(url)
-        assert_equal(r.json['title'], 'test')
+        assert_equal(r.json['title'], data['title'])
         assert_equal(r.json['text'], data['text'])
+        assert_equal(r.json['author'], 'test-admin')
         assert_equal(r.json['state'], data['state'])
         assert_equal(r.json['labels'], data['labels'].split(','))
 
@@ -63,7 +64,7 @@ class TestBlogApi(TestRestApiBase):
             'labels': 'label1, label2'
         }
         r = self.api_post('/rest/p/test/blog/', **data)
-        assert_equal(r.status_int, 200)
+        assert_equal(r.status_int, 201)
         url = '/rest' + BM.BlogPost.query.find().first().url()
         data = {
             'text': 'test text2',
@@ -84,7 +85,7 @@ class TestBlogApi(TestRestApiBase):
             'labels': 'label1, label2'
         }
         r = self.api_post('/rest/p/test/blog/', **data)
-        assert_equal(r.status_int, 200)
+        assert_equal(r.status_int, 201)
         url = '/rest' + BM.BlogPost.query.find().first().url()
         self.api_post(url, delete='')
         r = self.api_get(url)
@@ -119,7 +120,7 @@ class TestBlogApi(TestRestApiBase):
         self.app.post('/rest/p/test/blog/',
                       params=dict(title='test', text='test text', state='published'),
                       extra_environ={'username': '*anonymous'},
-                      status=200)
+                      status=201)
 
     def test_update_post_permissons(self):
         self.api_post('/rest/p/test/blog/', title='test', text='test text', state='published')
@@ -145,7 +146,7 @@ class TestBlogApi(TestRestApiBase):
     def test_permission_draft_post(self):
         self.api_post('/rest/p/test/blog/', title='test', text='test text', state='draft')
         r = self.app.get('/rest/p/test/blog/', extra_environ={'username': '*anonymous'})
-        assert_equal(r.json, {'posts': []})
+        assert_equal(r.json['posts'], [])
         url = '/rest' + BM.BlogPost.query.find().first().url()
         self.app.post(url.encode('utf-8'),
                       params=dict(title='test2', text='test text2', state='published'),
@@ -162,8 +163,29 @@ class TestBlogApi(TestRestApiBase):
     def test_draft_post(self):
         self.api_post('/rest/p/test/blog/', title='test', text='test text', state='draft')
         r = self.app.get('/rest/p/test/blog/', extra_environ={'username': '*anonymous'})
-        assert_equal(r.json, {'posts': []})
+        assert_equal(r.json['posts'], [])
         url = '/rest' + BM.BlogPost.query.find().first().url()
         self.api_post(url, state='published')
         r = self.app.get('/rest/p/test/blog/', extra_environ={'username': '*anonymous'})
         assert_equal(r.json['posts'][0]['title'], 'test')
+
+    def test_pagination(self):
+        self.api_post('/rest/p/test/blog/', title='test1', text='test text1', state='published')
+        self.api_post('/rest/p/test/blog/', title='test2', text='test text2', state='published')
+        self.api_post('/rest/p/test/blog/', title='test3', text='test text3', state='published')
+        r = self.api_get('/rest/p/test/blog/', limit='1', page='0')
+        assert_equal(r.json['posts'][0]['title'], 'test3')
+        assert_equal(r.json['count'], 3)
+        assert_equal(r.json['limit'], 1)
+        assert_equal(r.json['page'], 0)
+        r = self.api_get('/rest/p/test/blog/', limit='2', page='0')
+        assert_equal(r.json['posts'][0]['title'], 'test3')
+        assert_equal(r.json['posts'][1]['title'], 'test2')
+        assert_equal(r.json['count'], 3)
+        assert_equal(r.json['limit'], 2)
+        assert_equal(r.json['page'], 0)
+        r = self.api_get('/rest/p/test/blog/', limit='1', page='2')
+        assert_equal(r.json['posts'][0]['title'], 'test1')
+        assert_equal(r.json['count'], 3)
+        assert_equal(r.json['limit'], 1)
+        assert_equal(r.json['page'], 2)

http://git-wip-us.apache.org/repos/asf/incubator-allura/blob/e23020d0/ForgeBlog/forgeblog/tests/unit/test_blog_post.py
----------------------------------------------------------------------
diff --git a/ForgeBlog/forgeblog/tests/unit/test_blog_post.py b/ForgeBlog/forgeblog/tests/unit/test_blog_post.py
index c915278..919ac14 100644
--- a/ForgeBlog/forgeblog/tests/unit/test_blog_post.py
+++ b/ForgeBlog/forgeblog/tests/unit/test_blog_post.py
@@ -26,6 +26,15 @@ from allura.model import Feed
 def wrapped(s):
     return '<div class="markdown_content"><p>%s</p></div>' % s
 
+
+class TestBlogPost(BlogTestWithModel):
+    def test_new(self):
+        post = M.BlogPost.new(title='test', text='test message', state='published')
+        assert_equal(post.title, 'test')
+        assert_equal(post.text, 'test message')
+        assert_equal(post.state, 'published')
+
+
 class TestFeed(BlogTestWithModel):
     def testd(self):
         post = M.BlogPost()


Mime
View raw message