Commits

Atsushi Odagiri committed 0b2fb33

integrate wiki application functions

  • Participants
  • Parent commits b3bfb22

Comments (0)

Files changed (12)

+# A generic, single database configuration.
+
+[alembic]
+script_location = alembic
+sqlalchemy.url = sqlite:///%(here)s/crosswiki.db
+sqlalchemy.echo = true
+
+
+# Logging configuration
+[loggers]
+keys = root,sqlalchemy,alembic
+
+[handlers]
+keys = console
+
+[formatters]
+keys = generic
+
+[logger_root]
+level = WARN
+handlers = console
+qualname =
+
+[logger_sqlalchemy]
+level = WARN
+handlers =
+qualname = sqlalchemy.engine
+
+[logger_alembic]
+level = INFO
+handlers =
+qualname = alembic
+
+[handler_console]
+class = StreamHandler
+args = (sys.stderr,)
+level = NOTSET
+formatter = generic
+
+[formatter_generic]
+format = %(levelname)-5.5s [%(name)s] %(message)s
+datefmt = %H:%M:%S

File alembic/README

+Generic single-database configuration.

File alembic/env.py

+from __future__ import with_statement
+from alembic import context
+from sqlalchemy import engine_from_config
+from logging.config import fileConfig
+
+# this is the Alembic Config object, which provides
+# access to the values within the .ini file in use.
+config = context.config
+
+# Interpret the config file for Pyhton logging. 
+# This line sets up loggers basically.
+fileConfig(config.config_file_name)
+
+# add your model's MetaData object here
+# for 'autogenerate' support
+# from myapp import mymodel
+# target_metadata = mymodel.Base.metadata
+target_metadata = None
+from crosswiki import models
+target_metadata = models.Base.metadata
+
+# other values from the config, defined by the needs of env.py,
+# can be acquired:
+# my_important_option = config.get_main_option("my_important_option")
+# ... etc.
+
+def run_migrations_offline():
+    """Run migrations in 'offline' mode.
+
+    This configures the context with just a URL
+    and not an Engine, though an Engine is acceptable
+    here as well.  By skipping the Engine creation
+    we don't even need a DBAPI to be available.
+    
+    Calls to context.execute() here emit the given string to the
+    script output.
+    
+    """
+    url = config.get_main_option("sqlalchemy.url")
+    context.configure(url=url)
+
+    with context.begin_transaction():
+        context.run_migrations()
+
+def run_migrations_online():
+    """Run migrations in 'online' mode.
+
+    In this scenario we need to create an Engine
+    and associate a connection with the context.
+    
+    """
+    engine = engine_from_config(
+                config.get_section(config.config_ini_section), prefix='sqlalchemy.')
+
+    connection = engine.connect()
+    context.configure(
+                connection=connection, 
+                target_metadata=target_metadata
+                )
+
+    with context.begin_transaction():
+        context.run_migrations()
+
+if context.is_offline_mode():
+    run_migrations_offline()
+else:
+    run_migrations_online()
+

File alembic/script.py.mako

+"""${message}
+
+Revision ID: ${up_revision}
+Revises: ${down_revision}
+Create Date: ${create_date}
+
+"""
+
+# downgrade revision identifier, used by Alembic.
+down_revision = ${repr(down_revision)}
+
+from alembic import op
+import sqlalchemy as sa
+${imports if imports else ""}
+
+def upgrade():
+    ${upgrades if upgrades else "pass"}
+
+def downgrade():
+    ${downgrades if downgrades else "pass"}

File alembic/versions/1d70afe28535.py

+"""empty message
+
+Revision ID: 1d70afe28535
+Revises: None
+Create Date: 2012-01-20 21:31:52.859057
+
+"""
+
+# downgrade revision identifier, used by Alembic.
+down_revision = None
+
+from alembic import op
+import sqlalchemy as sa
+
+
+def upgrade():
+    ### commands auto generated by Alembic - please adjust! ###
+    op.create_table('pages',
+    sa.Column('id', sa.Integer(), nullable=False),
+    sa.Column('page_name', sa.Unicode(length=255), nullable=True, unique=True),
+    sa.Column('content', sa.UnicodeText(), nullable=True),
+    sa.PrimaryKeyConstraint('id')
+    )
+    ### end Alembic commands ###
+
+def downgrade():
+    ### commands auto generated by Alembic - please adjust! ###
+    op.drop_table('pages')
+    ### end Alembic commands ###

File crosswiki/helpers.py

 
 def create_page_url(request, page_name):
     return request.route_url('create_page', page_name=page_name)
+
+def edit_page_url(request, page=None, page_name=None):
+    if page_name is None:
+        page_name = page.page_name
+    return request.route_url('edit_page', page_name=page_name)

File crosswiki/subscribers.py

+from pyramid.events import subscriber
+from pyramid.events import BeforeRender
+
+from . import helpers
+
+@subscriber(BeforeRender)
+def add_renderer_globals(event):
+    event['h'] = helpers
+

File crosswiki/templates/edit_page.mak

 <html>
 <body>
 <h1>${page_name}</h1>
-<form method="POST">
-<textarea name="content">${content}</textarea>
-</form>
+${form.render(dict(content=content)) | n}
 </body>
 </html>

File crosswiki/templates/page.mak

 <body>
 <h1>${page_name}</h1>
 <div>
-${content | n}
+${html_content | n}
 </div>
+<a id="page-edit-url" href="${h.edit_page_url(request, page_name=page_name)}">Edit</a>
 </body>
 </html>

File crosswiki/tests.py

         context.get_page.assert_called_with('FrontPage')
 
         self.assertEqual(result['page_name'], 'this is page name')
-        self.assertEqual(result['content'], 'this is content')
+        self.assertEqual(result['html_content'], 'this is content')
 
 class CreatePageTests(unittest.TestCase):
     def setUp(self):
         app = self._makeAUT()
         res = app.get('/FrontPage')
         self.assertIn('FrontPage', res)
+        a = res.lxml.xpath("//*[@id='page-edit-url']")
+        self.assertEqual(a[0].attrib['href'], 'http://localhost/FrontPage/edit')
 
     def test_create(self):
         app = self._makeAUT()

File crosswiki/views.py

 @view_config(route_name='page', renderer='page.mak')
 @view_config(route_name='edit_page', renderer='edit_page.mak', request_method="GET")
 def show_page(request):
+    form = Form(forms.PageSchema(), buttons=('update',))
     page_name = request.matchdict['page_name']
     page = request.context.get_page(page_name)
     if page is None:
         return HTTPFound(location=create_page_url(request, page_name))
-    return dict(page_name=page.page_name, content=page.html_content)
+    return dict(page_name=page.page_name, 
+        content=page.content,
+        html_content=page.html_content, form=form)
 
 @view_config(route_name='create_page', request_method='GET', renderer='create_page_form.mak')
 def create_page_form(request):
-    form = Form(forms.PageSchema(), buttons='create')
+    form = Form(forms.PageSchema(), buttons=('create',))
     return dict(form=form)
 
 @view_config(route_name='create_page', request_method='POST', renderer='create_page.mak')
 def create_page(request):
-    form = Form(forms.PageSchema(), buttons='create')
+    form = Form(forms.PageSchema(), buttons=('create',))
     controls = request.params.items()
     try:
         params = form.validate(controls)

File development.ini

 pyramid.default_locale_name = en
 pyramid.includes = pyramid_tm
 
+sqlalchemy.url = sqlite:///%(here)s/crosswiki.db
+sqlalchemy.echo = true
+
+mako.directories = crosswiki:templates
+
 [server:main]
 use = egg:waitress#main
 host = 0.0.0.0