Permalink
Browse files

Add missing files

  • Loading branch information...
julienr committed Mar 5, 2013
1 parent df06afa commit 80ebfffe2fa5844df60fbf6398d11832a046c957
Showing with 121 additions and 0 deletions.
  1. +89 −0 zivimap/forms.py
  2. +32 −0 zivimap/templates/search_form.html
View
@@ -0,0 +1,89 @@
+"""
+Adaptation of django.forms.extra.SelectDateWidget to show only month and year
+"""
+from __future__ import unicode_literals
+
+import datetime
+import re
+import calendar
+
+from django.forms.widgets import Widget, Select
+from django.utils import datetime_safe
+from django.utils.dates import MONTHS
+from django.utils.encoding import force_str
+from django.utils.safestring import mark_safe
+from django.utils.formats import get_format
+from django.utils import six
+from django.conf import settings
+
+__all__ = ('SelectDateWidget',)
+
+class SelectMonthYearWidget(Widget):
+ """
+ A Widget that splits date input into two <select> boxes and allow selection
+ of month/year
+ """
+ none_value = (0, '---')
+ month_field = '%s_month'
+ year_field = '%s_year'
+
+ def __init__(self, defaultdate, day, attrs=None, years=None,
+ required=True):
+ # dayval is the value that will be set for the days. Can be 'first'
+ # or 'last'
+ # years is an optional list/tuple of years to use in the "year" select box.
+ self.attrs = attrs or {}
+ self.required = required
+ self.dayval = day
+ self.defaultdate = defaultdate
+ if years:
+ self.years = years
+ else:
+ this_year = datetime.date.today().year
+ self.years = range(this_year, this_year+10)
+
+ def render(self, name, value, attrs=None):
+ try:
+ year_val, month_val = value.year, value.month
+ except AttributeError:
+ year_val, month_val = self.defaultdate.year, self.defaultdate.month
+
+ choices = [(i, i) for i in self.years]
+ year_html = self.create_select(name, self.year_field, value, year_val, choices)
+ choices = list(six.iteritems(MONTHS))
+ month_html = self.create_select(name, self.month_field, value, month_val, choices)
+
+ output = [month_html, year_html]
+ return mark_safe('\n'.join(output))
+
+ def id_for_label(self, id_):
+ return '%s_month' % id_
+
+ def value_from_datadict(self, data, files, name):
+ y = data.get(self.year_field % name)
+ m = data.get(self.month_field % name)
+ if y == m == "0": # No selection
+ return None
+ if y and m:
+ y = int(y)
+ m = int(m)
+ if self.dayval == 'first':
+ day = 1
+ else:
+ day = calendar.monthrange(y, m)[1]
+ return datetime.date(int(y), int(m), day)
+ else:
+ return None
+
+ def create_select(self, name, field, value, val, choices):
+ if 'id' in self.attrs:
+ id_ = self.attrs['id']
+ else:
+ id_ = 'id_%s' % name
+ #if not (self.required and val):
+ #choices.insert(0, self.none_value)
+ local_attrs = self.build_attrs(id=field % id_)
+ local_attrs['class'] = field % 'select'
+ s = Select(choices=choices)
+ select_html = s.render(field % name, val, local_attrs)
+ return select_html
@@ -0,0 +1,32 @@
+{% load i18n %}
+{% comment %}
+{{ form.as_ul }}
+{% endcomment %}
+
+<ul id="search_form">
+ {{ form.non_field_errors }}
+ <li>
+ <label for="id_domains_0">{% trans 'Domains' %}</label>
+ <ul>
+ {{ form.domains }}
+ </ul>
+ </li>
+ <li>
+ <label for="id_languages_0">{% trans 'Languages' %}</label>
+ <ul>
+ {{ form.languages }}
+ </ul>
+ </li>
+ <li>
+ <label for="id_start_min_month">{% trans 'Earliest start' %}</label>
+ <ul>
+ {{ form.start_min }}
+ </ul>
+ </li>
+ <li>
+ <label for="id_end_max_month">{% trans 'Latest end' %}</label>
+ <ul>
+ {{ form.end_max }}
+ </ul>
+ </li>
+</ul>

0 comments on commit 80ebfff

Please sign in to comment.