1. Ches Martin
  2. quickwiki-patches-ches

Source

quickwiki-patches-ches / model_init.patch

use an `__init__` method on the model
diff --git a/quickwiki/controllers/pages.py b/quickwiki/controllers/pages.py
--- a/quickwiki/controllers/pages.py
+++ b/quickwiki/controllers/pages.py
@@ -31,11 +31,10 @@
     def save(self, title):
         page = self.page_q.filter_by(title=title).first()
         if not page:
-            page = Page()
-            page.title = title
+            page = Page(title)
         # In a real application, you should validate and sanitize
         # submitted data throughly! escape is a minimal example here.
-        page.content = escape(request.POST.get('content', ''))
+        page.content = escape(request.POST.getone('content'))
         DBSession.add(page)
         DBSession.commit()
         flash('Successfully saved %s!' % title)
@@ -54,6 +53,5 @@
         page = self.page_q.filter_by(title=title).one()
         DBSession.delete(page)
         DBSession.commit()
-        c.titles = self.page_q.all()
         flash('Deleted %s.' % title)
         redirect_to('pages')
diff --git a/quickwiki/model/__init__.py b/quickwiki/model/__init__.py
--- a/quickwiki/model/__init__.py
+++ b/quickwiki/model/__init__.py
@@ -8,12 +8,12 @@
 from sqlalchemy import orm
 
 from pylons import url
+from quickwiki.lib.helpers import link_to
 from quickwiki.model import meta
-import quickwiki.lib.helpers as h
 
 
-docutils_safety = {'file_insertion_enabled': False, 'raw_enabled': False}
 log = logging.getLogger(__name__)
+SAFE_DOCUTILS = {'file_insertion_enabled': False, 'raw_enabled': False}
 wikiwords = re.compile(r"\b([A-Z]\w+[A-Z]+\w+)", re.UNICODE)
 
 def init_model(engine):
@@ -30,14 +30,15 @@
 )
 
 class Page(object):
-    content = None
-
-    def __str__(self):
-        return self.title
+    def __init__(self, title, content=None):
+        self.title = title
+        self.content = content
 
     @orm.validates('title')
     def validate_title(self, key, title):
-        """Assure that page titles are wikiwords"""
+        """Assure that page titles are wikiwords and valid length."""
+        if len(title) > 40:
+            raise ValueError("Page title must be 40 characters or fewer")
         if wikiwords.match(title) is None:
             log.warning("%s: invalid title (%s)" % (self.__class__.__name__,
                                                     title))
@@ -49,11 +50,19 @@
         create links for WikiWords.
         """
         content = publish_parts(self.content, writer_name='html',
-                                settings_overrides=docutils_safety)['html_body']
+                                settings_overrides=SAFE_DOCUTILS)['html_body']
         titles = sets.Set(wikiwords.findall(content))
         for title in titles:
             title_url = url(controller='pages', action='show', title=title)
-            content = content.replace(title, h.link_to(title, title_url))
+            content = content.replace(title, link_to(title, title_url))
         return content
 
+    def __unicode__(self):
+        return self.title
+
+    __str__ = __unicode__
+    
+    def __repr__(self):
+        return "<Page('%s', '%s')>" % (self.title, self.content)
+
 orm.mapper(Page, pages_table)
diff --git a/quickwiki/websetup.py b/quickwiki/websetup.py
--- a/quickwiki/websetup.py
+++ b/quickwiki/websetup.py
@@ -20,9 +20,10 @@
     
     import quickwiki.model as model
     log.info("Adding front page data...")
-    page = model.Page()
-    page.title = u'FrontPage'
-    page.content = u'Welcome to the QuickWiki front page.'
+    page = model.Page(
+        title = u'FrontPage',
+        content = u'Welcome to the QuickWiki front page.'
+        )
     meta.Session.add(page)
     meta.Session.commit()
     log.info("Successfully set up.")