# -*- coding: utf-8 -*-
# widgets.py
#
# To use you have to put calendar/ (from http://www.dynarch.com/projects/calendar/)
# to your MEDIA folder and then include such links on your page:
# <!-- calendar -->
# <link rel="stylesheet" type="text/css" href="{{ MEDIA_URL }}calendar/calendar-win2k-cold-2.css" />
#<script type="text/javascript" src="{{ MEDIA_URL }}calendar/calendar.js"></script>
# <!-- this is translation file - choose your language here -->
#<script type="text/javascript" src="{{ MEDIA_URL }}calendar/lang/calendar-pl.js"></script>
#<script type="text/javascript" src="{{ MEDIA_URL }}calendar/calendar-setup.js"></script>
#<!-- /calendar -->
from django.utils.encoding import force_unicode
from django.conf import settings
from django import forms
import datetime, time
from django.utils.safestring import mark_safe
# DATETIMEWIDGET
calbtn = u"""<img src="%simages/calbutton.gif" alt="calendar" id="%s_btn" style="cursor: pointer; border: 1px solid #8888aa;" title="Select date and time"
onmouseover="this.style.background='#444444';" onmouseout="this.style.background=''" />
<script type="text/javascript">
Calendar.setup({
inputField : "%s",
ifFormat : "%s",
button : "%s_btn",
singleClick : true,
showsTime : true
});
</script>"""
class DateTimeWidget(forms.widgets.TextInput):
dformat = '%Y-%m-%d %H:%M'
def render(self, name, value, attrs=None):
if value is None: value = ''
final_attrs = self.build_attrs(attrs, type=self.input_type, name=name)
if value != '':
try:
final_attrs['value'] = \
force_unicode(value.strftime(self.dformat))
except:
final_attrs['value'] = \
force_unicode(value)
if not final_attrs.has_key('id'):
final_attrs['id'] = u'%s_id' % (name)
id = final_attrs['id']
jsdformat = self.dformat #.replace('%', '%%')
cal = calbtn % (settings.MEDIA_URL, id, id, jsdformat, id)
a = u'<input%s />%s' % (forms.util.flatatt(final_attrs), cal)
return mark_safe(a)
def value_from_datadict(self, data, files, name):
dtf = forms.fields.DEFAULT_DATETIME_INPUT_FORMATS
empty_values = forms.fields.EMPTY_VALUES
value = data.get(name, None)
if value in empty_values:
return None
if isinstance(value, datetime.datetime):
return value
if isinstance(value, datetime.date):
return datetime.datetime(value.year, value.month, value.day)
for format in dtf:
try:
return datetime.datetime(*time.strptime(value, format)[:6])
except ValueError:
continue
return None
Comments
There was a little bug with unnecessary replacing percent sign in line: jsdformat = self.dformat.replace('%', '%%')
This is fixed now.
#
Method value_from_datadict has been added so now widget returns datetime.datetime objects
#
Small fix applied to line:
final_attrs['value'] = ...
When user entered wrong value, eg: 'qwerty' as a date it was incorrectly handled.
#
I had to change line 17 to:
import datetime, time
to get this to work.
#
Thanks tttallis, I've updated the code
#
Update import:
instead of:
My version of «value_from_datadict»:
#
I needed to use mark_safe with output.
#
you will need a:
return mark_safe(a)
if you want to be able to print directly, without using as_p
#
A Media class can be added so that any page that uses this widget will include the css and js.
http://docs.djangoproject.com/en/dev/topics/forms/media/
#
Here is copy of this snippet, but for latest 1.5 version, called JSCal2!
#
The widget is good! but needs some changes, first, in the
<script><script type="text/javascript">//<![CDATA[//]]></script>second:
this line is deprecated:
dtf = forms.fields.DEFAULT_DATETIME_INPUT_FORMATS
change and import:
from django.utils import formats
...
dtf = formats.get_format("DATETIME_INPUT_FORMATS")[0]
enjoy!
#