""" date_select.qpy """ from calendar import setfirstweekday, monthcalendar, SUNDAY, month_name from datetime import datetime, timedelta from dulcinea.ui.form.widget import PairWidget from qp.fill.form import Form from qp.fill.html import htmltag from qp.fill.widget import StringWidget from qp.lib.util import integer, randbytes from qp.pub.common import get_fields, get_publisher, site_now, get_response from qp.pub.common import get_session class DateSelectWidget (StringWidget): def render_content:xml(self): try: value_to_show = self.value.strftime("%Y-%m-%d") year = self.value.year month = self.value.month day = self.value.day except (ValueError, AttributeError): value_to_show = self.value now = datetime.now() year = now.year month = now.month day = None if 'id' not in self.attrs: self.attrs.update(dict(id=randbytes(10))) htmltag("input", xml_end=True, type=self.HTML_TYPE, name=self.name, size=10, value=value_to_show, **self.attrs) container_id = randbytes(10) '' % container_id date_select_callback( year=year, month=month, day=day, input_id=self.attrs.get('id'), container_id=container_id) '' def _parse(self, request): StringWidget._parse(self, request) if self.value is not None: year, month, day = self.value.split('-') year = integer(year) month = integer(month) day = integer(day) if year is not None and month is not None and day is not None: self.value = datetime( year, month, day, tzinfo=get_publisher().get_time_zone()) else: self.set_error('Expected YYYY-MM-DD') def date_select_callback:xml( callback_path='/callback/date_select_callback', **fields): if not fields: fields = get_fields() year = integer(fields.get('year')) month = integer(fields.get('month')) selected_day = integer(fields.get('day')) if month is None or year is None: return '' input_id = fields.get('input_id') container_id = fields.get('container_id') setfirstweekday(SUNDAY) '
← | ' % js '' month_name[month] ' ' year ' | ' next_month = month + 1 next_year = year if next_month == 13: next_month = 1 next_year = year + 1 js = "genlib.load_from('%s', '%s?year=%s&month=%s&input_id=%s&container_id=%s')" % ( container_id, callback_path, next_year, next_month, input_id, container_id) '→ | ' % js '||||
---|---|---|---|---|---|---|
Su | ' 'Mo | ' 'Tu | ' 'We | ' 'Th | ' 'Fr | ' 'Sa | ' '
' else: js = "genlib.set_value('%s', '%s%s');genlib.set_exclusive(this, 'selected_date', '%s')" % ( input_id, date_start, day, container_id) if day == selected_day: ' | ' % js else: ' | ' % js day ' | ' '