Treat posts as markdown. #262
authorEevee <git@veekun.com>
Tue, 7 Sep 2010 03:34:58 +0000 (20:34 -0700)
committerEevee <git@veekun.com>
Tue, 7 Sep 2010 03:34:58 +0000 (20:34 -0700)
migration/versions/005_Added_markup_support.py [new file with mode: 0644]
splinext/forum/controllers/forum.py
splinext/forum/model/__init__.py
splinext/forum/templates/forum/lib.mako

diff --git a/migration/versions/005_Added_markup_support.py b/migration/versions/005_Added_markup_support.py
new file mode 100644 (file)
index 0000000..c27c819
--- /dev/null
@@ -0,0 +1,32 @@
+from sqlalchemy import *
+from migrate import *
+import migrate.changeset
+
+from sqlalchemy.ext.declarative import declarative_base
+TableBase = declarative_base()
+
+class Post(TableBase):
+    __tablename__ = 'posts'
+    id = Column(Integer, primary_key=True, autoincrement=True, nullable=False)
+    raw_content = Column(Unicode(5120), nullable=False, server_default=u'')
+    content = Column(Unicode(5120), nullable=False)
+
+
+def upgrade(migrate_engine):
+    TableBase.metadata.bind = migrate_engine
+    conn = migrate_engine.connect()
+
+    # Create the column with an empty-string default, copy the old column's
+    # contents to the new one then remove the default
+    Post.__table__.c.raw_content.create(connection=conn)
+    conn.execute(
+        update(Post.__table__,
+            values={ Post.__table__.c.raw_content: Post.__table__.c.content },
+        )
+    )
+    Post.__table__.c.raw_content.alter(server_default=None, connection=conn)
+
+def downgrade(migrate_engine):
+    TableBase.metadata.bind = migrate_engine
+
+    Post.__table__.c.raw_content.drop()
index 6cd4bfb..b15302c 100644 (file)
@@ -10,6 +10,7 @@ from wtforms import fields
 from spline.model import meta
 from spline.lib import helpers as h
 from spline.lib.base import BaseController, render
+import spline.lib.markdown
 from splinext.forum import model as forum_model
 
 log = logging.getLogger(__name__)
@@ -121,10 +122,12 @@ class ForumController(BaseController):
             .with_lockmode('update') \
             .get(c.thread.id)
 
+        source = c.write_post_form.content.data
         post = forum_model.Post(
             position = c.thread.post_count + 1,
             author_user_id = c.user.id,
-            content = c.write_post_form.content.data,
+            raw_content = source,
+            content = spline.lib.markdown.translate(source),
         )
 
         c.thread.posts.append(post)
index 37beb61..80ca57e 100644 (file)
@@ -73,6 +73,7 @@ class Post(TableBase):
     position = Column(Integer, nullable=False)
     author_user_id = Column(Integer, ForeignKey('users.id'), nullable=False)
     posted_time = Column(DateTime, nullable=False, index=True, default=datetime.now)
+    raw_content = Column(Unicode(5120), nullable=False)
     content = Column(Unicode(5120), nullable=False)
 
 Index('thread_position', Post.thread_id, Post.position, unique=True)
index 82884f4..fd2c073 100644 (file)
@@ -59,7 +59,7 @@
         <div class="meta">
             <time>${post.posted_time}</time>
         </div>
-        <div class="content">${post.content}</div>
+        <div class="content">${post.content | n}</div>
     </div>
     % endfor
 </div>