Commits

Moritz Schlarb committed b886bef

Validation for CalendarDatePicker

It's a bit tedious, since the javascript uses a different
format string syntax which we have to parse to python to
use it for validating.

Also, a default value can be specified now

Comments (0)

Files changed (4)

tw2/bootstrap/samples.py

 class DemoSubmitButton(twb.SubmitButton):
     pass
 
+
 class DemoLinkField(twb.LinkField):
     link = "http://twitter.github.com/bootstrap/base-css.html#$"
     text = "Twitter Bootstrap CSS $"
     value = "forms"
+
+
+class DemoCalendarDatePicker(twb.CalendarDatePicker):
+    style = 'component'
+
+

tw2/bootstrap/templates/datepicker.mak

+<%namespace name="tw" module="tw2.core.mako_util"/>\
+% if w.style == 'component':
+  <div class="input-append date" id="${w.compound_id}" data-date="${w.value or ''}" data-date-format="${w.format}">
+    <input ${tw.attrs(attrs=dict((k, w.attrs[k]) for k in w.attrs if k != 'id' and k != 'value' ))} value="${w.value or ''}" />
+    <span class="add-on"><i class="icon-th"></i></span>
+  </div>
+% else:
+  <input ${tw.attrs(attrs=dict((k, w.attrs[k]) for k in w.attrs if k != 'value'))} value="${w.value or ''}" data-date="${w.value or ''}" data-date-format="${w.format}" />
+% endif

tw2/bootstrap/utils.py

+'''
+Created on 29.05.2012
+
+@author: moschlar
+'''
+
+
+def replace_all(text, items):
+    '''Replaces all old, new tuples from items in text
+
+    items may be a dict of {old: new} pairs
+    or a list of (old, new) tuples
+    '''
+    try:
+        items = items.iteritems()
+    except:
+        pass
+    for old, new in items:
+        text = text.replace(old, new)
+    return text

tw2/bootstrap/widgets.py

 import tw2.forms as twf
 import tw2.jquery as twj
 
+from datetime import datetime
+from tw2.bootstrap.utils import replace_all
+
 __all__ = [
     'bootstrap_css',
     'bootstrap_responsive_css',
 
 class CalendarDatePicker(TextField):
     resources = TextField.resources + [datepicker_js, datepicker_css]
+    template = "mako:tw2.bootstrap.templates.datepicker"
 
+    style = twc.Param(
+        'Specify the template to use. [field, component]',
+        default='field')
     format = twc.Param(
         "the date format, combination of d, dd, m, mm, yy, yyyy.",
         default="mm/dd/yyyy")
+    date_format = twc.Variable()
     weekStart = twc.Param(
         "day of the week start.  0 for Sunday - 6 for Saturday",
         default=0)
+    default = twc.Param(
+        'Default value (datetime) for the widget.  If set to a function, ' +
+        'it will be called each time before displaying.',
+        default=datetime.now)
+
+    def __init__(self, *args, **kw):
+        super(CalendarDatePicker, self).__init__(*args, **kw)
+        # Convert the bootstrap-datepicker format string to
+        # a python format string...
+        self.date_format = replace_all(self.format, [
+            ('dd', 'DAY'), ('d', 'DAY'),
+            ('mm', 'MONTH'), ('m', 'MONTH'),
+            ('yyyy', '4YEAR'), ('yy', '2YEAR')
+            ])
+        self.date_format = replace_all(self.date_format, [
+            ('DAY', '%d'),
+            ('MONTH', '%m'),
+            ('2YEAR', '%y'), ('4YEAR', '%Y')
+            ])
+        if not self.validator:
+            self.validator = twc.DateValidator(
+            format=self.date_format,
+            )
 
     def prepare(self):
         super(CalendarDatePicker, self).prepare()
             format=self.format,
             weekStart=self.weekStart,
         )))
+        if not self.value:
+            if callable(self.default):
+                self.value = self.default()
+            else:
+                self.value = self.default
+        try:
+            self.value = self.value.strftime(self.date_format)
+        except:
+            pass
 
 
 class CalendarDateTimePicker(Bootstrap, twf.CalendarDateTimePicker):
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.