def test_ok02(self): drange = DateRangeField().clean([CurrentYearRange.name, '', '']) dt = self.create_datetime self.assertIsInstance(drange, CurrentYearRange) self.assertEqual(( dt(year=2013, month=1, day=1, hour=0, minute=0, second=0), dt(year=2013, month=12, day=31, hour=23, minute=59, second=59), ), drange.get_dates(dt(year=2013, month=5, day=29, hour=11)))
def test_ok01(self): drange = DateRangeField().clean(['', '2013-05-29', '2013-06-16']) dt = self.create_datetime self.assertIsInstance(drange, DateRange) self.assertIsInstance(drange, CustomRange) self.assertEqual(( dt(year=2013, month=5, day=29, hour=0, minute=0, second=0), dt(year=2013, month=6, day=16, hour=23, minute=59, second=59), ), drange.get_dates(now()))
class ReportExportPreviewFilterForm(CremeForm): doc_type = ChoiceField(label=_('Extension'), required=False, choices=()) date_field = ChoiceField(label=_('Date field'), required=False, choices=()) date_filter = DateRangeField(label=_('Date filter'), required=False) error_messages = { 'custom_start': _('If you chose a Date field, and select «customized» ' 'you have to specify a start date and/or an end date.' ), } # TODO: rename "report" to "instance" to be consistent ? def __init__(self, report, *args, **kwargs): super().__init__(*args, **kwargs) fields = self.fields fields['date_field'].choices = self._date_field_choices(report) fields['date_field'].initial = 'current_month' fields['doc_type'].choices = self._backend_choices() def _date_field_choices(self, report): return [ ('', pgettext_lazy('reports-date_filter', 'None')), *((field.name, field.verbose_name) for field in report.ct.model_class()._meta.fields if is_date_field(field) ), ] def _backend_choices(self): return [(backend.id, backend.verbose_name) for backend in export_backend_registry.backends ] def clean(self): cleaned_data = super().clean() if cleaned_data.get('date_field'): date_filter = cleaned_data.get('date_filter') if not date_filter or not any(date_filter.get_dates(now())): raise ValidationError(self.error_messages['custom_start'], code='custom_start', ) return cleaned_data def get_q(self): cleaned_data = self.cleaned_data date_field = cleaned_data['date_field'] date_filter = cleaned_data['date_filter'] if date_field: return Q(**date_filter.get_q_dict(date_field, now())) return None def get_backend(self): return export_backend_registry.get_backend(self.cleaned_data['doc_type']) def export_url_data(self): cleaned_data = self.cleaned_data date_field = cleaned_data['date_field'] date_filter = cleaned_data['date_filter'] data = [('doc_type', cleaned_data['doc_type']), ('date_field', date_field), ] if date_filter is not None: d_range = date_filter.name start, end = date_filter.get_dates(now()) data.extend([('date_filter_0', d_range), ('date_filter_1', start.strftime('%d-%m-%Y') if start else ''), ('date_filter_2', end.strftime('%d-%m-%Y') if end else ''), ]) return '&'.join('{}={}'.format(key, value) for key, value in data)
def test_ok03(self): drange = DateRangeField(required=False).clean(['', '', '']) self.assertIsNone(drange)
def test_start_before_end(self): self.assertFieldValidationError(DateRangeField, 'customized_invalid', DateRangeField().clean, [u"", u"2011-05-16", u"2011-05-15"])
def test_clean_empty_customized(self): clean = DateRangeField().clean self.assertFieldValidationError(DateRangeField, 'required', clean, ['', '', '']) self.assertFieldValidationError(DateRangeField, 'required', clean, None)