Skip to content

Commit

Permalink
Loosen formencode dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
paj committed Jul 23, 2010
1 parent d1a8501 commit 88769d1
Showing 1 changed file with 1 addition and 45 deletions.
46 changes: 1 addition & 45 deletions tw2/forms/calendars.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,48 +43,6 @@

__all__ = ["CalendarDatePicker", "CalendarDateTimePicker", "calendar_js", "calendar_setup"]

class DateTimeConverter(FancyValidator):

"""
Converts Python date and datetime objects into string representation and back.
"""
messages = {
'badFormat': 'Invalid datetime format.',
'empty': 'Please Enter a Date.',
}
if_missing = None
def __init__(self, format = "%Y/%m/%d %H:%M", tzinfo=None, *args, **kwargs):
super(FancyValidator, self).__init__(*args, **kwargs)
self.format = format
self.tzinfo = tzinfo

def _to_python(self, value, state):
""" parse a string and return a datetime object. """
if value and isinstance(value, (date, datetime)):
return value
else:
try:
tpl = time.strptime(value, self.format)
except ValueError:
raise Invalid(self.message('badFormat', state), value, state)
# shoudn't use time.mktime() because it can give OverflowError,
# depending on the date (e.g. pre 1970) and underlying C library
return datetime(year=tpl.tm_year, month=tpl.tm_mon, day=tpl.tm_mday,
hour=tpl.tm_hour, minute=tpl.tm_min,
second=tpl.tm_sec, tzinfo=self.tzinfo)

def _from_python(self, value, state):
if not value:
return None
elif isinstance(value, datetime):
# Python stdlib can only handle dates with year greater than 1900
if value.year <= 1900:
return strftime_before1900(value, self.format)
else:
return value.strftime(self.format)
else:
return value

_illegal_s = re.compile(r"((^|[^%])(%%)*%s)")

def _findall(text, substr):
Expand Down Expand Up @@ -174,10 +132,8 @@ def get_calendar_lang_file_link(self, lang):

def __init__(self, *args, **kw):
if self.validator is None:
self.validator = DateTimeConverter(
self.validator = twc.DateTimeValidator(
format=self.date_format,
not_empty=self.not_empty,
tzinfo=self.tzinfo
)
super(CalendarDatePicker, self).__init__(*args, **kw)

Expand Down

0 comments on commit 88769d1

Please sign in to comment.