Commits

Nozomu Kaneko committed 04d2990

refactor diff files

Comments (0)

Files changed (8)

 # HG changeset patch
-# Parent 1ace53a6a8a6fbde1ff44e14f3011c3819af0d82
+# Parent e81912e57a6b148ddb34a0544b512b14ef09aef1
 
-diff --git a/quickwiki/controllers/pages.py b/quickwiki/controllers/pages.py
-deleted file mode 100644
+diff --git a/quickwiki/views/__init__.py b/quickwiki/views/__init__.py
+new file mode 100644
+diff --git a/quickwiki/controllers/pages.py b/quickwiki/views/pages.py
+rename from quickwiki/controllers/pages.py
+rename to quickwiki/views/pages.py
 --- a/quickwiki/controllers/pages.py
-+++ /dev/null
-@@ -1,69 +0,0 @@
--import logging
--from cgi import escape
--
++++ b/quickwiki/views/pages.py
+@@ -1,69 +1,96 @@
+ import logging
+ from cgi import escape
+ 
 -from pylons import url, request, response, session, tmpl_context as c
 -from pylons.controllers.util import abort, redirect
 -from pylons.decorators.secure import authenticate_form
--
++# from pylons.decorators.secure import authenticate_form
+ 
 -from quickwiki.lib.base import BaseController, render
 -from quickwiki.lib.helpers import flash
 -from quickwiki.model import Page, wikiwords
 -from quickwiki.model.meta import Session
--
--log = logging.getLogger(__name__)
--
++import transaction
++from pyramid.response import Response
++from pyramid.view import view_config
++import pyramid.httpexceptions as exc
++
++from quickwiki.model import DBSession, Page, wikiwords
+ 
+ log = logging.getLogger(__name__)
+ 
 -class PagesController(BaseController):
 -    
 -    def __before__(self):
 -    def edit(self, title):
 -        c.title = title
 -        c.content = u''
--        page = self.page_q.filter_by(title=title).first()
--        if page:
--            c.title = page.title
--            c.content = page.content
--        return render('/pages/edit.mako')
--    
--    @authenticate_form
--    def save(self, title):
--        c.title = title
--        page = self.page_q.filter_by(title=title).first()
--        if not page:
--            page = Page(title=title)
--        # In a real application, you should validate and sanitize
--        # submitted data throughly! escape is a minimal example here
--        page.content = escape(request.POST.getone('content'))
--        Session.add(page)
--        Session.commit()
--        flash('Successfully saved %s!' % title)
--        redirect(url('show_page', title=title))
--    
--    def index(self):
--        c.titles = [page.title for page in self.page_q.all()]
--        return render('/pages/index.mako')
--    
--    @authenticate_form
--    def delete(self):
--        titles = request.POST.getall('title')
--        pages = self.page_q.filter(Page.title.in_(titles))
--        for page in pages:
--            Session.delete(page)
--        Session.commit()
--        # flash only after a successful commit
--        for title in titles:
--            flash('Deleted %s.' % title)
--        redirect(url('pages'))
--    
--
-diff --git a/quickwiki/views/__init__.py b/quickwiki/views/__init__.py
-new file mode 100644
-diff --git a/quickwiki/views/pages.py b/quickwiki/views/pages.py
-new file mode 100644
---- /dev/null
-+++ b/quickwiki/views/pages.py
-@@ -0,0 +1,96 @@
-+import logging
-+from cgi import escape
-+
-+# from pylons.decorators.secure import authenticate_form
-+
-+import transaction
-+from pyramid.response import Response
-+from pyramid.view import view_config
-+import pyramid.httpexceptions as exc
-+
-+from quickwiki.model import DBSession, Page, wikiwords
-+
-+log = logging.getLogger(__name__)
-+
 +class NewPage(Exception):
 +    pass
 +
 +            title = "FrontPage"
 +        else:
 +            title = self.request.matchdict["title"]
-+        page = self.page_q.filter_by(title=title).first()
-+        if page:
+         page = self.page_q.filter_by(title=title).first()
+         if page:
+-            c.title = page.title
+-            c.content = page.content
+-        return render('/pages/edit.mako')
+-    
+-    @authenticate_form
+-    def save(self, title):
+-        c.title = title
 +            return {
 +                "title": title,
 +                "content": page.get_wiki_content(self.request),
 +    # @authenticate_form
 +    def save(self):
 +        title = self.request.matchdict["title"]
-+        page = self.page_q.filter_by(title=title).first()
-+        if not page:
-+            page = Page(title=title)
-+        # In a real application, you should validate and sanitize
-+        # submitted data throughly! escape is a minimal example here
+         page = self.page_q.filter_by(title=title).first()
+         if not page:
+             page = Page(title=title)
+         # In a real application, you should validate and sanitize
+         # submitted data throughly! escape is a minimal example here
+-        page.content = escape(request.POST.getone('content'))
+-        Session.add(page)
+-        Session.commit()
+-        flash('Successfully saved %s!' % title)
+-        redirect(url('show_page', title=title))
+-    
 +        page.content = escape(self.request.POST.getone('content'))
 +        with transaction.manager:
 +            DBSession.add(page)
 +        raise exc.HTTPFound(self.request.route_url("show_page", title=title))
 +
 +    @view_config(route_name="pages", renderer='pages/index.mako')
-+    def index(self):
+     def index(self):
+-        c.titles = [page.title for page in self.page_q.all()]
+-        return render('/pages/index.mako')
+-    
+-    @authenticate_form
 +        return {
 +            "titles": [page.title for page in self.page_q.all()],
 +            }
 +
 +    @view_config(route_name="delete_page", request_method="POST")
 +    # @authenticate_form
-+    def delete(self):
+     def delete(self):
+-        titles = request.POST.getall('title')
 +        titles = self.request.POST.getall('title')
-+        pages = self.page_q.filter(Page.title.in_(titles))
+         pages = self.page_q.filter(Page.title.in_(titles))
+-        for page in pages:
+-            Session.delete(page)
+-        Session.commit()
 +        with transaction.manager:
 +            for page in pages:
 +                DBSession.delete(page)
-+        # flash only after a successful commit
-+        for title in titles:
+         # flash only after a successful commit
+         for title in titles:
+-            flash('Deleted %s.' % title)
+-        redirect(url('pages'))
+-    
+-
 +            self.request.session.flash('Deleted %s.' % title)
 +        raise exc.HTTPFound(self.request.route_url("pages"))
 # HG changeset patch
-# Parent a6aa160de38783c32a5d30e25051ea81f7376f7b
+# Parent 8ffc3473d97f2e1b82f23c90abe44d012460069c
+
 diff --git a/development.ini b/development.ini
 --- a/development.ini
 +++ b/development.ini
-@@ -1,47 +1,50 @@
+@@ -1,47 +1,49 @@
  #
 -# QuickWiki - Pylons development environment configuration
 +# QuickWiki - Pyramid development environment configuration
 -smtp_server = localhost
 -error_email_from = paste@localhost
 -
--[server:main]
+ [server:main]
 -use = egg:Paste#http
--host = 127.0.0.1
--port = 5000
--
++use = egg:waitress#main
+ host = 127.0.0.1
+ port = 5000
+ 
  [app:main]
  use = egg:QuickWiki
 -full_stack = true
 -# Debug mode will enable the interactive debugging tool, allowing ANYONE to
 -# execute malicious code after an exception is raised.
 -#set debug = false
-+
-+[server:main]
-+use = egg:waitress#main
-+host = 127.0.0.1
-+port = 5000
- 
+-
  
  # Logging configuration
  [loggers]
  
  [handlers]
  keys = console
-@@ -53,12 +56,6 @@
+@@ -53,12 +55,6 @@
  level = INFO
  handlers = console
  
 diff --git a/test.ini b/test.ini
 --- a/test.ini
 +++ b/test.ini
-@@ -1,22 +1,16 @@
+@@ -1,15 +1,8 @@
  #
 -# QuickWiki - Pylons testing environment configuration
 +# QuickWiki - Pyramid testing environment configuration
 -#email_to = you@yourdomain.com
 -smtp_server = localhost
 -error_email_from = paste@localhost
-+[app:main]
-+use = config:development.ini
-+
-+# Add additional test specific configuration options as necessary.
-+sqlalchemy.url = sqlite:///:memory:
-+
- 
+-
  [server:main]
  use = egg:Paste#http
  host = 127.0.0.1
- port = 5000
--
--[app:main]
--use = config:development.ini
--
--# Add additional test specific configuration options as necessary.
--sqlalchemy.url = sqlite:///:memory:
 # HG changeset patch
-# Parent ce60dbcf42441fe776026fac9ac5c35e5679a5f2
+# Parent e733e06af1f5d1032d83773acb6927e3f16da016
 
 diff --git a/quickwiki/model/__init__.py b/quickwiki/model/__init__.py
 --- a/quickwiki/model/__init__.py
 +++ b/quickwiki/model/__init__.py
-@@ -2,29 +2,29 @@
+@@ -2,23 +2,23 @@
  import logging
  import re
  import sys
  
  class Page(Base):
      __tablename__ = 'pages'
-     title = Column(Unicode(40), primary_key=True)
-     content = Column(UnicodeText(), default=u'')
--    
-+
-     @orm.validates('title')
-     def validate_title(self, key, title):
-         """Assure that page titles are wikiwords and valid length"""
-@@ -35,8 +35,8 @@
-                                                     title))
+@@ -36,7 +36,7 @@
              raise ValueError('Page title must be a wikiword (CamelCase)')
          return title
--    
+     
 -    def get_wiki_content(self):
-+
 +    def get_wiki_content(self, request):
          """Convert reStructuredText content to HTML for display, and
          create links for WikiWords
          """
-@@ -44,15 +44,14 @@
+@@ -44,7 +44,7 @@
                                  settings_overrides=SAFE_DOCUTILS)['html_body']
          titles = set(wikiwords.findall(content))
          for title in titles:
 +            title_url = request.route_url('show_page', title=title)
              content = content.replace(title, link_to(title, title_url))
          return content
--    
-+
-     def __unicode__(self):
-         return self.title
--    
-+
-     def __repr__(self):
-         return "<Page('%s', '%s')>" % (self.title, self.content)
--    
-+
-     __str__ = __unicode__
--    
+     
 diff --git a/quickwiki/model/meta.py b/quickwiki/model/meta.py
 deleted file mode 100644
 --- a/quickwiki/model/meta.py
 # HG changeset patch
-# Parent e57e5c3791b554eb09a5595f334b6e9bd7b2222e
+# Parent a0268f32c80cf39942f42b6d72252be06f44ef10
 
 diff --git a/quickwiki/__init__.py b/quickwiki/__init__.py
 --- a/quickwiki/__init__.py
 +++ b/quickwiki/__init__.py
-@@ -10,5 +10,21 @@
+@@ -10,5 +10,18 @@
      DBSession.configure(bind=engine)
      config = Configurator(settings=settings)
      config.include("pyramid_beaker")
      return config.make_wsgi_app()
 +
 +def make_route(config):
-+    config.add_route('catchall_static', '/*subpath',
-+                     path_info=r"/(?:favicon\.ico|quick.css)")
-+
 +    config.add_route('home', '/')
 +    config.add_route('pages', '/pages')
 +    config.add_route('show_page', '/pages/show/{title}')
 helpers.diff
 add_global.diff
 static_views.diff
+websetup.diff
 tests.diff
-websetup.diff
 delete.diff
 csrf.diff

static_views.diff

 # HG changeset patch
-# Parent 352bc03945ef5d48741a922b7d941bb02c1ff283
+# Parent f63ab4a5f5bf0444354e0e4f4da27663f2cb5a22
+
+diff --git a/quickwiki/__init__.py b/quickwiki/__init__.py
+--- a/quickwiki/__init__.py
++++ b/quickwiki/__init__.py
+@@ -15,6 +15,9 @@
+     return config.make_wsgi_app()
+ 
+ def make_route(config):
++    config.add_route('catchall_static', '/*subpath',
++                     path_info=r"/(?:favicon\.ico|quick.css)")
++
+     config.add_route('home', '/')
+     config.add_route('pages', '/pages')
+     config.add_route('show_page', '/pages/show/{title}')
 diff --git a/quickwiki/views/static.py b/quickwiki/views/static.py
 new file mode 100644
 --- /dev/null
 # HG changeset patch
-# Parent 632e9a9bc680f10a0a3b135c6e0a648117f72ebc
+# Parent 4becc0dabefd14ff2502e306fb727944464842a6
+
 diff --git a/quickwiki/tests/__init__.py b/quickwiki/tests/__init__.py
 --- a/quickwiki/tests/__init__.py
 +++ b/quickwiki/tests/__init__.py
-@@ -1,38 +1,22 @@
+@@ -1,38 +1,25 @@
 -"""Pylons application test package
-+import unittest
++from unittest import TestCase
 +import os
 +from webtest import TestApp
 +from pyramid import testing
 +from pyramid.paster import get_appsettings
++from quickwiki.model import DBSession, Page
++from quickwiki.scripts import initializedb
  
 -This package assumes the Pylons environment is already loaded, such as
 -when this script is imported from the `nosetests --with-pylons=test.ini`
 -command.
-+from quickwiki.model import DBSession, Page
-+from quickwiki.scripts import initializedb
++__all__ = ['TestController', 'testing', 'DBSession', 'Page']
  
 -This module initializes the application via ``websetup`` (`paster
 -setup-app`) and provides the base testing objects.
 -"""
 -from unittest import TestCase
-+__all__ = ['TestController', 'testing', 'DBSession', 'Page']
- 
+-
 -from paste.deploy import loadapp
 -from paste.script.appinstall import SetupCommand
 -from pylons import url
 -from webtest import TestApp
 -from quickwiki.model import Page
 -from quickwiki.model.meta import Session
-+# Invoke initializedb with the current config file
-+here = os.path.dirname(os.path.abspath(__file__))
-+root_dir = os.path.dirname(os.path.dirname(here))
-+config_uri = os.path.join(root_dir, 'test.ini')
-+initializedb.main([None, config_uri])
- 
+-
 -import pylons.test
 -
 -__all__ = ['environ', 'url', 'TestController']
 -SetupCommand('setup-app').run([pylons.test.pylonsapp.config['__file__']])
 -
 -environ = {}
--
--class TestController(TestCase):
--    
--    def __init__(self, *args, **kwargs):
++# Invoke initializedb with the current config file
++here = os.path.dirname(os.path.abspath(__file__))
++root_dir = os.path.dirname(os.path.dirname(here))
++config_uri = os.path.join(root_dir, 'test.ini')
++initializedb.main([None, config_uri])
+ 
+ class TestController(TestCase):
+     
+     def __init__(self, *args, **kwargs):
 -        wsgiapp = pylons.test.pylonsapp
 -        config = wsgiapp.config
 -        self.app = TestApp(wsgiapp)
 -        url._push_object(URLGenerator(config['routes.map'], environ))
--        TestCase.__init__(self, *args, **kwargs)
--    
--
-+class TestController(unittest.TestCase):
-+    def setUp(self):
 +        from quickwiki import main
 +        settings = get_appsettings(config_uri)
 +        self.app = TestApp(main({}, **settings))
+         TestCase.__init__(self, *args, **kwargs)
+     
+ 
 diff --git a/quickwiki/tests/functional/test_pages.py b/quickwiki/tests/functional/test_pages.py
 --- a/quickwiki/tests/functional/test_pages.py
 +++ b/quickwiki/tests/functional/test_pages.py
 diff --git a/quickwiki/tests/test_models.py b/quickwiki/tests/test_models.py
 --- a/quickwiki/tests/test_models.py
 +++ b/quickwiki/tests/test_models.py
-@@ -1,18 +1,18 @@
+@@ -1,18 +1,20 @@
 -from quickwiki.tests import *
 -from quickwiki.tests import Session, Page, TestController
 +from unittest import TestCase
 +import quickwiki.tests
  
 -class TestPages(TestController):
--    
 +class TestPages(TestCase):
-+
+     
      def test_index(self):
 -        q1 = Session.query(Page).all()
 +        q1 = DBSession.query(Page).all()
 +        q2 = DBSession.query(Page).all()
          print(q2)
 -        assert q2 == [q1[0], page]
--    
--
 +        # assert q2 == [q1[0], page]
+     
+ 
 # HG changeset patch
-# Parent 3c14855a13b2b922ecae1d5b8a020a9d40428562
+# Parent 5e7d82e5238478978965067195f36d7142c9a1b8
+
 diff --git a/quickwiki/scripts/__init__.py b/quickwiki/scripts/__init__.py
 new file mode 100644
 --- /dev/null
 +++ b/quickwiki/scripts/__init__.py
 @@ -0,0 +1,1 @@
 +# package
-diff --git a/quickwiki/scripts/initializedb.py b/quickwiki/scripts/initializedb.py
-new file mode 100644
---- /dev/null
+diff --git a/quickwiki/websetup.py b/quickwiki/scripts/initializedb.py
+rename from quickwiki/websetup.py
+rename to quickwiki/scripts/initializedb.py
+--- a/quickwiki/websetup.py
 +++ b/quickwiki/scripts/initializedb.py
-@@ -0,0 +1,41 @@
-+"""Setup the QuickWiki application"""
-+import logging
+@@ -1,29 +1,44 @@
+ """Setup the QuickWiki application"""
+ import logging
 +import os
 +import sys
-+
+ 
+-import pylons.test
 +import transaction
 +from sqlalchemy import engine_from_config
 +from pyramid.paster import (
 +    get_appsettings,
 +    setup_logging,
 +    )
-+
-+from quickwiki import model
+ 
+-from quickwiki.config.environment import load_environment
+ from quickwiki import model
+-from quickwiki.model.meta import Session, Base
 +from quickwiki.model import DBSession, Base
-+
-+log = logging.getLogger(__name__)
-+
+ 
+ log = logging.getLogger(__name__)
+ 
+-def setup_app(command, conf, vars):
+-    """Place any commands to setup {{package}} here"""
+-    # Don't reload the app if it was loaded under the testing environment
+-    if not pylons.test.pylonsapp:
+-        load_environment(conf.global_conf, conf.local_conf)
+-    
 +def usage(argv):
 +    cmd = os.path.basename(argv[0])
 +    print('usage: %s <config_uri>\n'
 +    settings = get_appsettings(config_uri)
 +    engine = engine_from_config(settings, 'sqlalchemy.')
 +    DBSession.configure(bind=engine)
-+    # Create the tables if they don't already exist
-+    log.info("Creating tables...")
++
+     # Create the tables if they don't already exist
+     log.info("Creating tables...")
+-    Base.metadata.create_all(bind=Session.bind)
 +    Base.metadata.create_all(engine)
-+    log.info("Successfully set up.")
-+    with transaction.manager:
-+        log.info("Adding front page data...")
-+        page = model.Page(title=u'FrontPage',
-+                          content=u'**Welcome** to the QuickWiki front page!')
-+        DBSession.add(page)
-+    log.info("Successfully set up.")
-diff --git a/quickwiki/websetup.py b/quickwiki/websetup.py
-deleted file mode 100644
---- a/quickwiki/websetup.py
-+++ /dev/null
-@@ -1,29 +0,0 @@
--"""Setup the QuickWiki application"""
--import logging
--
--import pylons.test
--
--from quickwiki.config.environment import load_environment
--from quickwiki import model
--from quickwiki.model.meta import Session, Base
--
--log = logging.getLogger(__name__)
--
--def setup_app(command, conf, vars):
--    """Place any commands to setup {{package}} here"""
--    # Don't reload the app if it was loaded under the testing environment
--    if not pylons.test.pylonsapp:
--        load_environment(conf.global_conf, conf.local_conf)
--    
--    # Create the tables if they don't already exist
--    log.info("Creating tables...")
--    Base.metadata.create_all(bind=Session.bind)
--    log.info("Successfully set up.")
--    
--    log.info("Adding front page data...")
+     log.info("Successfully set up.")
+     
+     log.info("Adding front page data...")
 -    page = model.Page(title=u'FrontPage',
 -                      content=u'**Welcome** to the QuickWiki front page!')
 -    Session.add(page)
 -    Session.commit()
--    log.info("Successfully set up.")
--
++    with transaction.manager:
++        page = model.Page(title=u'FrontPage',
++                          content=u'**Welcome** to the QuickWiki front page!')
++        DBSession.add(page)
+     log.info("Successfully set up.")
+