Commits

devilicecream  committed 2117ac1

make possible to change the article form

  • Participants
  • Parent commits 1bc14bf

Comments (0)

Files changed (3)

File smallpress/__init__.py

 log = logging.getLogger('tgapp-smallpress')
 
 def plugme(app_config, options):
+    tg.config['_smallpress'] = options
+
     def init_whoosh(app):
         try:
             from whoosh.index import create_in

File smallpress/controllers/root.py

 
 from smallpress.model import DBSession, Article, Attachment, Tagging, Blog
 from smallpress.helpers import *
-from smallpress.lib.forms import ArticleForm, UploadForm
+from smallpress.lib.forms import UploadForm, get_article_form
 from tgext.datahelpers.validators import SQLAEntityConverter
 from tgext.datahelpers.fields import AttachedFile
 from tgext.pluggable import plug_url
 except ImportError:
     whoosh_enabled = False
 
-article_form = ArticleForm()
 upload_form = UploadForm()
 articles_table = DataGrid(fields=[(l_('Actions'), lambda row:link_buttons(row, 'edit', 'delete', 'hide', 'publish')),
                                   (l_('Blog'), lambda row:row.blog and row.blog.name),
         }
 
         return dict(article=article, value=value, blog=blog and blog.name or '',
-                    form=article_form, action=url(self.mount_point+'/save'),
+                    form=get_article_form(), action=url(self.mount_point+'/save'),
                     upload_form=upload_form, upload_action=url(self.mount_point+'/attach'))
 
     @require(predicates.in_group('smallpress'))
         }
 
         return dict(article=article, value=value, blog=article.blog and article.blog.name or '',
-                    form=article_form, action=url(self.mount_point+'/save'),
+                    form=get_article_form(), action=url(self.mount_point+'/save'),
                     upload_form=upload_form, upload_action=url(self.mount_point+'/attach'))
 
     @require(predicates.in_group('smallpress'))
-    @validate(article_form, error_handler=edit)
+    @validate(get_article_form(), error_handler=edit)
     @expose()
     def save(self, *args, **kw):
         article = DBSession.query(Article).get(kw['uid'])

File smallpress/lib/forms.py

-from tg import request
+import sys
+import tg
 from tw.forms import ListForm, TextField, TextArea, HiddenField, FileField
 from tw.core import WidgetsList
 from tw.forms.validators import UnicodeString, FieldStorageUploadConverter
         text = TextField(suppress_label=True, validator=UnicodeString(not_empty=True),
                          attrs=dict(placeholder=l_('Search...')))
 
-    submit_text = 'Search'
+    submit_text = 'Search'
+
+def get_article_form():
+    config = tg.config['_smallpress']
+
+    article_form = config.get('form_instance')
+    if not article_form:
+        form_path = config.get('form', 'smallpress.lib.forms.ArticleForm')
+        root_module, path = form_path.split('.', 1)
+        form_class = reduce(getattr, path.split('.'), sys.modules[root_module])
+        article_form = config['form_instance'] = form_class()
+
+    return article_form