Commits

Simone Marzola committed 439477b

naive datetime validator

Comments (0)

Files changed (3)

smallpress/controllers/root.py

         article.title = kw['title']
         article.description = kw['description']
         article.content = kw['content']
-        article.publish_date = datetime.strptime(kw['publish_date'], '%Y-%m-%d %H:%M')
+        article.publish_date = kw['publish_date']
         article.update_date = datetime.now()
         app_model.Tagging.set_tags(article, kw['tags'])
 

smallpress/lib/forms.py

 from tg.i18n import lazy_ugettext as l_
 from tgext.ajaxforms import ajaxloaded
 from tgext.pluggable import plug_url
+from smallpress.lib.validators import NaiveDateTimeValidator
 
 class SpinnerIcon(object):
     @property
                          placeholder=l_('tags, comma separated'))
         content = TextArea(label=None, key='content', name='content', id="article_content",
                            validator=UnicodeString(not_empty=True, outputEncoding=None))
-        publish_date = TextField(label='Publish Date')
+        publish_date = TextField(label='Publish Date', validator=NaiveDateTimeValidator())
 
     @ajaxloaded
     class UploadForm(ListForm):
                              attrs=dict(placeholder=l_('tags, comma separated')))
             content = TextArea(suppress_label=True, validator=UnicodeString(not_empty=True),
                                attrs=dict(id='article_content'))
-            publish_date = TextField(label_text='Publish Date')
+            publish_date = TextField(label_text='Publish Date', validator=NaiveDateTimeValidator())
 
     @ajaxloaded
     class UploadForm(ListForm):

smallpress/lib/validators.py

+from datetime import datetime
+try:
+    from tw.forms.validators import FancyValidator, Invalid
+except ImportError:
+    from formencode.validators import FancyValidator, Invalid
+
+class NaiveDateTimeValidator(FancyValidator):
+
+    def _to_python(self, value, status):
+        date_string = value.strip()
+        try:
+            date = datetime.strptime(date_string, '%Y-%m-%d %H:%M')
+        except:
+            raise Invalid('date must be in format yyyy-mm-dd hh:mm', value, status)
+        return date