""" 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) '' '' previous_month = month - 1 previous_year = year if previous_month == 0: previous_month = 12 previous_year = year - 1 js = "genlib.load_from('%s', '%s?year=%s&month=%s&input_id=%s&container_id=%s')" % ( container_id, callback_path, previous_year, previous_month, input_id, container_id) '' % js '' 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 '' '' '' '' '' '' '' '' '' '' date_start = "%s-%s-" % (year, month) for row in monthcalendar(year, month): '' for day in row: if day == 0: '' 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: '' '' '
' month_name[month] ' ' year '
SuMoTuWeThFrSa
' % js else: '' % js day '
' class DateSelectPairWidget(PairWidget): def __init__(self, name, value=None, **kwargs): PairWidget.__init__(self, name, value, element_type=DateSelectWidget, element1_kwargs=dict(title='Start Date', show_none=False), element2_kwargs=dict(title='End Date', show_none=False), **kwargs) def _parse(self, request): PairWidget._parse(self, request) if self.value: self.value = (self.value[0].replace(hour=0, minute=0), self.value[1].replace(hour=23, minute=59)) start, end = self.value if start > end: self.set_error('Start must not follow End') def get_date_select_pair_form(start_date=None, end_date=None, csv=True): form = Form(use_tokens=False) if end_date is None: end_date = site_now() if start_date is None: start_date = end_date - timedelta(30) get_response().set_expires(minutes=10) form.add(DateSelectPairWidget, 'date_select_pair', value=(start_date, end_date)) form.add_submit('search', 'Search') if csv: form.add_submit( 'csv', 'Spreadsheet', attrs=dict(title='Download as a spread sheet file (CSV format)')) return form def get_default_start_end(): if get_session().get_last_date_pair(): return get_session().get_last_date_pair() now = site_now() start = now.replace(day=1).replace(hour=0).replace(minute=0).replace(second=0) # First second of this month end = now.replace(hour=23).replace(minute=59).replace(second=59) # last second of today for day in reversed(range(28, 32)): # last day of this month try: end = end.replace(day=day) break except ValueError: continue return start, end def set_default_start_end(start, end): get_session().set_last_date_pair((start, end)) def get_date_range_text(start, end): return "%s.%s.%s-%s.%s.%s" % (start.year, start.month, start.day, end.year, end.month, end.day)