Commits

Blaise Laflamme  committed 8e84ba3

Updated sources based on authorization example

  • Participants
  • Parent commits 3fea334

Comments (0)

Files changed (16)

 .DS_Store
 *.kpf
 *.pdf
+*/tutorial.db
 
 syntax:regexp
 env/
 src/authorization/tutorial.db
-

File src/layout/setup.py

 import os
+import sys
 
 from setuptools import setup, find_packages
 
     'SQLAlchemy',
     'transaction',
     'repoze.tm2',
-    'zope.sqlalchemy'
+    'zope.sqlalchemy',
+    'docutils',
 ]
 
 if sys.version_info[:3] < (2,5,0):
     requires.append('pysqlite')
     
-
 setup(name='tutorial',
       version='0.0',
       description='tutorial',

File src/layout/tutorial/__init__.py

     initialize_sql(db_string)
     config = Configurator(settings=settings)
     config.begin()
-    config.add_static_view('static', 'tutorial:templates/static')
-    config.add_handler('tutorial.views:MyController', pattern='/{action}',
-                       route_name='action')
-    config.add_handler('tutorial.views:MyController', pattern='/',
-                       route_name='home', action='index')
+    config.add_static_view(
+        'static',
+        'tutorial:templates/static'
+        )
+    config.add_handler(
+        'tutorial.handlers:WikiHandler',
+        pattern='/{action}'
+        )
+    config.add_handler(
+        'tutorial.handlers:WikiHandler',
+        pattern='/',
+        route_name='home',
+        action='index'
+        )
     config.end()
     return config.make_wsgi_app()
 

File src/layout/tutorial/handlers.py

+from pylons.views import action
+
+from tutorial.models import DBSession
+from tutorial.models import Model
+
+class WikiHandler(object):
+    def __init__(self, request):
+        self.request = request
+
+    @action(renderer='mytemplate.mak')
+    def index(self):
+        dbsession = DBSession()
+        root = dbsession.query(Model).filter(Model.name==u'root').first()
+        return {'root':root, 'project':'tutorial'}

File src/layout/tutorial/tests.py

 
 from pylons.configuration import Configurator
 
-class MyControllerTests(unittest.TestCase):
+class WikiHandlerTests(unittest.TestCase):
     def setUp(self):
         self.config = Configurator()
         self.config.begin()
         self.config.end()
 
     def _makeOne(self, request):
-        from tutorial.views import MyController
-        return MyController(request)
+        from tutorial.handlers import WikiHandler
+        return WikiHandler(request)
 
     def test_index(self):
         request = DummyRequest()
-        controller = self._makeOne(request)
-        info = controller.index()
+        handler = self._makeOne(request)
+        info = handler.index()
         self.assertEqual(info['project'], 'tutorial')
 
 class DummyRequest(object):

File src/layout/tutorial/views.py

-from pylons.views import action
-from tutorial.models import DBSession
-from tutorial.models import Model
-
-class MyController(object):
-    def __init__(self, request):
-        self.request = request
-
-    @action(renderer='mytemplate.mak')
-    def index(self):
-        dbsession = DBSession()
-        root = dbsession.query(Model).filter(Model.name==u'root').first()
-        return {'root':root, 'project':'tutorial'}

File src/models/setup.py

 import os
+import sys
 
 from setuptools import setup, find_packages
 
     'SQLAlchemy',
     'transaction',
     'repoze.tm2',
-    'zope.sqlalchemy'
+    'zope.sqlalchemy',
+    'docutils',
 ]
 
 if sys.version_info[:3] < (2,5,0):
     requires.append('pysqlite')
     
-
 setup(name='tutorial',
       version='0.0',
       description='tutorial',

File src/models/tutorial/__init__.py

     initialize_sql(db_string)
     config = Configurator(settings=settings)
     config.begin()
-    config.add_static_view('static', 'tutorial:templates/static')
-    config.add_handler('tutorial.views:MyController', pattern='/{action}')
-    config.add_handler('tutorial.views:MyController', pattern='/',
-                       route_name='home', action='index')
+    config.add_static_view(
+        'static',
+        'tutorial:templates/static'
+        )
+    config.add_handler(
+        'tutorial.handlers:WikiHandler',
+        pattern='/{action}'
+        )
+    config.add_handler(
+        'tutorial.handlers:WikiHandler',
+        pattern='/',
+        route_name='home',
+        action='index'
+        )
     config.end()
     return config.make_wsgi_app()
 

File src/models/tutorial/handlers.py

+from pylons.views import action
+
+from tutorial.models import DBSession
+from tutorial.models import Model
+
+class WikiHandler(object):
+    def __init__(self, request):
+        self.request = request
+
+    @action(renderer='mytemplate.mak')
+    def index(self):
+        dbsession = DBSession()
+        root = dbsession.query(Model).filter(Model.name==u'root').first()
+        return {'root':root, 'project':'tutorial'}

File src/models/tutorial/tests.py

 
 from pylons.configuration import Configurator
 
-class MyControllerTests(unittest.TestCase):
+class WikiHandlerTests(unittest.TestCase):
     def setUp(self):
         self.config = Configurator()
         self.config.begin()
         self.config.end()
 
     def _makeOne(self, request):
-        from tutorial.views import MyController
-        return MyController(request)
+        from tutorial.handlers import WikiHandler
+        return WikiHandler(request)
 
     def test_index(self):
         request = DummyRequest()
-        controller = self._makeOne(request)
-        info = controller.index()
+        handler = self._makeOne(request)
+        info = handler.index()
         self.assertEqual(info['project'], 'tutorial')
 
 class DummyRequest(object):

File src/models/tutorial/views.py

-from pylons.views import action
-from tutorial.models import DBSession
-from tutorial.models import Model
-
-class MyController(object):
-    def __init__(self, request):
-        self.request = request
-
-    @action(renderer='mytemplate.mak')
-    def index(self):
-        dbsession = DBSession()
-        root = dbsession.query(Model).filter(Model.name==u'root').first()
-        return {'root':root, 'project':'tutorial'}

File src/views/setup.py

 import os
+import sys
 
 from setuptools import setup, find_packages
 
     'SQLAlchemy',
     'transaction',
     'repoze.tm2',
-    'zope.sqlalchemy'
+    'zope.sqlalchemy',
+    'docutils',
 ]
 
 if sys.version_info[:3] < (2,5,0):

File src/views/tutorial/__init__.py

     initialize_sql(db_string)
     config = Configurator(settings=settings)
     config.begin()
-    config.add_static_view('static', 'tutorial:templates/static')
-    config.add_handler('tutorial.views:MyController', pattern='/',
-                       route_name='view_wiki', action='view_wiki')
-    config.add_handler('tutorial.views:MyController', pattern='/{pagename}',
-                       route_name='view_page', action='view_page')
-    config.add_handler('tutorial.views:MyController', pattern='/add_page/{pagename}',
-                       route_name='add_page', action='add_page')
-    config.add_handler('tutorial.views:MyController', pattern='/{pagename}/edit_page',
-                       route_name='edit_page', action='edit_page')
+    config.add_static_view(
+        'static',
+        'tutorial:templates/static'
+        )
+    config.add_handler(
+        'tutorial.handlers:WikiHandler',
+        pattern='/',
+        route_name='root',
+        )
+    config.add_handler(
+        'tutorial.handlers:WikiHandler',
+        pattern='/wiki/{pagename}/{action}',
+        route_name='wiki_action'
+        )
     config.end()
     return config.make_wsgi_app()
 

File src/views/tutorial/handlers.py

+import re
+
+from docutils.core import publish_parts
+
+from webob.exc import HTTPFound
+
+from pylons.views import action
+from pylons.url import route_url
+
+from tutorial.models import DBSession
+from tutorial.models import Page
+
+# regular expression used to find WikiWords
+wikiwords = re.compile(r"\b([A-Z]\w+[A-Z]+\w+)")
+
+class WikiHandler(object):
+    def __init__(self, request):
+        self.request = request
+
+    def __call__(self):
+        return HTTPFound(
+            location=route_url('wiki_action',
+                               self.request,
+                               pagename='FrontPage',
+                               action='view')
+            )
+    
+    @action(renderer='view.mak', permission='view')
+    def view(self):
+        matchdict = self.request.matchdict
+        session = DBSession()
+        page = session.query(Page).filter_by(name=matchdict['pagename']).one()
+        def check(match):
+            word = match.group(1)
+            exists = session.query(Page).filter_by(name=word).all()
+            if exists:
+                view_url = route_url('wiki_action',
+                                     self.request,
+                                     pagename=word,
+                                     action='view')
+                return '<a href="%s">%s</a>' % (view_url, word)
+            else:
+                add_url = route_url('wiki_action',
+                                    self.request,
+                                    pagename=word,
+                                    action='add')
+                return '<a href="%s">%s</a>' % (add_url, word)
+    
+        content = publish_parts(page.data, writer_name='html')['html_body']
+        content = wikiwords.sub(check, content)
+        edit_url = route_url('wiki_action',
+                             self.request,
+                             pagename=matchdict['pagename'],
+                             action='edit')
+        return dict(
+            page=page,
+            content=content,
+            edit_url=edit_url
+        )
+    
+    @action(renderer='edit.mak', permission="edit")
+    def add(self):
+        name = self.request.matchdict['pagename']
+        if 'form.submitted' in self.request.params:
+            session = DBSession()
+            body = self.request.params['body']
+            page = Page(name, body)
+            session.add(page)
+            return HTTPFound(
+                location = route_url(
+                    'wiki_action',
+                    self.request,
+                    pagename=name,
+                    action='view')
+                )
+        save_url = route_url(
+            'wiki_action',
+            self.request,
+            pagename=name,
+            action='add')
+        page = Page('', '')
+        return dict(
+            page=page,
+            save_url=save_url
+        )
+    
+    @action(renderer='edit.mak', permission="edit")    
+    def edit(self):
+        name = self.request.matchdict['pagename']
+        session = DBSession()
+        page = session.query(Page).filter_by(name=name).one()
+        if 'form.submitted' in self.request.params:
+            page.data = self.request.params['body']
+            session.add(page)
+            return HTTPFound(
+                location=route_url(
+                    'wiki_action',
+                    self.request,
+                    pagename=name,
+                    action='view')
+                )
+        return dict(
+            page=page,
+            save_url = route_url('wiki_action',
+                                 self.request,
+                                 pagename=name,
+                                 action='edit')
+        )
+

File src/views/tutorial/tests.py

 
 from pylons.configuration import Configurator
 
-class MyControllerTests(unittest.TestCase):
+class WikiHandlerTests(unittest.TestCase):
     def setUp(self):
         self.config = Configurator()
         self.config.begin()
         self.config.end()
 
     def _makeOne(self, request):
-        from tutorial.views import MyController
-        return MyController(request)
+        from tutorial.handlers import WikiHandler
+        return WikiHandler(request)
 
     def test_index(self):
         request = DummyRequest()
-        controller = self._makeOne(request)
-        info = controller.index()
+        handler = self._makeOne(request)
+        info = handler.index()
         self.assertEqual(info['project'], 'tutorial')
 
 class DummyRequest(object):

File src/views/tutorial/views.py

-import re
-
-from docutils.core import publish_parts
-
-from webob.exc import HTTPFound
-
-from repoze.bfg.url import route_url
-
-from pylons.views import action
-#from pylons import url
-from tutorial.models import DBSession
-from tutorial.models import Page
-
-# regular expression used to find WikiWords
-wikiwords = re.compile(r"\b([A-Z]\w+[A-Z]+\w+)")
-
-class MyController(object):
-    def __init__(self, request):
-        self.request = request
-
-    @action(renderer='mytemplate.mak')
-    def index(self):
-        return {'root':root, 'project':'tutorial'}
-    
-    @action(renderer='view.mak')
-    def view_wiki(self):
-        return HTTPFound(location=route_url('view_page', self.request, pagename='FrontPage'))
-    
-    @action(renderer='view.mak')
-    def view_page(self):
-        matchdict = self.request.matchdict
-        session = DBSession()
-        page = session.query(Page).filter_by(name=matchdict['pagename']).one()
-        def check(match):
-            word = match.group(1)
-            exists = session.query(Page).filter_by(name=word).all()
-            if exists:
-                view_url = route_url('view_page', self.request, pagename=word)
-                return '<a href="%s">%s</a>' % (view_url, word)
-            else:
-                add_url = route_url('add_page', self.request, pagename=word)
-                return '<a href="%s">%s</a>' % (add_url, word)
-    
-        content = publish_parts(page.data, writer_name='html')['html_body']
-        content = wikiwords.sub(check, content)
-        edit_url = route_url('edit_page', self.request, pagename=matchdict['pagename'])
-        return dict(page=page, content=content, edit_url=edit_url)
-    
-    @action(renderer='edit.mak')
-    def add_page(self):
-        name = self.request.matchdict['pagename']
-        if 'form.submitted' in self.request.params:
-            session = DBSession()
-            body = self.request.params['body']
-            page = Page(name, body)
-            session.add(page)
-            return HTTPFound(location = route_url('view_page', self.request, pagename=name))
-        save_url = route_url('add_page', request, pagename=name)
-        page = Page('', '')
-        return dict(page=page, save_url=save_url)
-    
-    @action(renderer='edit.mak')    
-    def edit_page(self):
-        name = self.request.matchdict['pagename']
-        session = DBSession()
-        page = session.query(Page).filter_by(name=name).one()
-        if 'form.submitted' in self.request.params:
-            page.data = self.request.params['body']
-            session.add(page)
-            return HTTPFound(location=route_url('view_page', self.request, pagename=name))
-        return dict(
-            page=page,
-            save_url = route_url('edit_page', self.request, pagename=name),
-        )
-