Commits

Atsushi Odagiri committed ab1e1d5

update page

Comments (0)

Files changed (4)

crosswiki/__init__.py

     config.add_route('home', '/')
     config.add_route('page', '/{page_name}')
     config.add_route('create_page', '/{page_name}/create')
+    config.add_route('edit_page', '/{page_name}/edit')
     config.scan()
     from .models import create_frontpage
     create_frontpage(engine)

crosswiki/models.py

     def __init__(self, request):
         self.request = request
 
-    def get_page(self):
-        page_name = self.request.matchdict['page_name']
+    def get_page(self, page_name):
         try:
             page = Page.query.filter_by(page_name=page_name).one()
             return page

crosswiki/tests.py

 from pyramid import testing
 
 
+class UpdatePageTests(unittest.TestCase):
+    def setUp(self):
+        self.config = testing.setUp()
+
+    def tearDown(self):
+        testing.tearDown()
+
+    def _callFUT(self, *args, **kwargs):
+        from .views import update_page
+        return update_page(*args, **kwargs)
+
+    def test_it(self):
+        self.config.add_route('page', '/{page_name}')
+        mockPage = mock.Mock()
+        mockPage.page_name = 'FrontPage'
+        context = mock.Mock()
+        context.get_page.return_value = mockPage
+        request = testing.DummyRequest(context=context, 
+            matchdict=dict(page_name=mockPage.page_name),
+            params=dict(content='updated'))
+        result = self._callFUT(request)
+        context.get_page.assert_called_with(mockPage.page_name)
+        self.assertEqual(result.location, 'http://example.com/FrontPage')
+        self.assertEqual(mockPage.content, 'updated')
+
+    def test_not_found(self):
+        context = mock.Mock()
+        context.get_page.return_value = None
+        request = testing.DummyRequest(context=context, 
+            location='not_found_location',
+            matchdict=dict(page_name='NoPage'),
+            params=dict(content='updated'))
+
+        from pyramid.httpexceptions import HTTPNotFound
+        self.assertRaises(HTTPNotFound, self._callFUT, request)
+
+
 class ShowPageTests(unittest.TestCase):
     def setUp(self):
         self.config = testing.setUp()
         context.get_page.return_value = mockPage
         request = testing.DummyRequest(context=context, matchdict=dict(page_name='FrontPage'))
         result = self._callFUT(request)
-        context.get_page.assert_called_with()
+        context.get_page.assert_called_with('FrontPage')
 
         self.assertEqual(result['page_name'], 'this is page name')
         self.assertEqual(result['content'], 'this is content')
         MockPage.query.filter_by.return_value = mockQuery
         request = testing.DummyRequest(matchdict=dict(page_name='FrontPage'))
         target = self._makeOne(request)
-        result = target.get_page()
+        result = target.get_page('FrontPage')
         MockPage.query.filter_by.assert_called_with(page_name='FrontPage')
         self.assertEqual(result, page)
 
         MockPage.query.filter_by.return_value = mockQuery
         request = testing.DummyRequest(matchdict=dict(page_name='FrontPage'))
         target = self._makeOne(request)
-        result = target.get_page()
+        result = target.get_page('FrontPage')
         self.assertIsNone(result)
 
 class PageUrlTests(unittest.TestCase):
         result = self._callFUT(request, page)
         self.assertEqual(result, 'http://example.com/FrontPage')
 
+
 class PageTests(unittest.TestCase):
     def _getTarget(self):
         from .models import Page

crosswiki/views.py

 from .helpers import page_url, create_page_url
 
 @view_config(route_name='page', renderer='page.mak')
+@view_config(route_name='edit_page', renderer='page.mak', request_method="GET")
 def show_page(request):
     page_name = request.matchdict['page_name']
-    page = request.context.get_page()
+    page = request.context.get_page(page_name)
     if page is None:
-        raise HTTPFound(location=create_page_url(request, page_name))
+        return HTTPFound(location=create_page_url(request, page_name))
     return dict(page_name=page.page_name, content=page.html_content)
 
 @view_config(route_name='create_page', request_method='GET', renderer='create_page_form.mak')
     request.context.add(page)
 
     return HTTPFound(location=page_url(request, page))
+
+@view_config(route_name='edit_page', renderer='page.mak', request_method="POST")
+def update_page(request):
+    page_name = request.matchdict['page_name']
+    page = request.context.get_page(page_name)
+    if page is None:
+        raise HTTPNotFound(location=request.url)
+
+    page.content = request.params['content']
+
+    return HTTPFound(location=page_url(request, page))