horizon/openstack_dashboard/dashboards/admin/metering/forms.py
Sam Betts 0d010f601c Fix date pickers in metering modal
The metering forms were using the date picker functionality from the
d3 line chart library, this used to work when it was loaded as a whole
page as the d3 libary would run intending to setup any d3 graphs however
now it is being loaded as a modal the elements that would be affected by
the d3 code do not exist at page load time.

In order to get the date picker events to be added to the elements on
the form when the modal loads, the JS in the forms template has been
extended in this patch to support adding those events.

This patch also changes the data attributes on the form elements so that
they do not conflict with any d3 line chart elements.

Alongside the javascript this patch fixes bug to do with parsing the
dates in metering utils and adds UTs to prevent regression.

Change-Id: I4e239daa03b2f54e434254bac48ba0cceb037b5d
Closes-Bug: 1427756
2015-04-28 11:08:27 +01:00

67 lines
2.7 KiB
Python

# Copyright 2014 OpenStack Foundation
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import datetime
from django.forms import ValidationError # noqa
from django.utils.translation import ugettext_lazy as _
from horizon import forms
class UsageReportForm(forms.SelfHandlingForm):
PERIOD_CHOICES = (("1", _("Last day")),
("7", _("Last week")),
(str(datetime.date.today().day), _("Month to date")),
("15", _("Last 15 days")),
("30", _("Last 30 days")),
("365", _("Last year")),
("other", _("Other")),
)
period = forms.ChoiceField(label=_("Period"),
required=True,
choices=PERIOD_CHOICES)
date_from = forms.DateField(label=_("From"), required=False,
widget=forms.TextInput(
attrs={'data-date-picker': True}))
date_to = forms.DateField(label=_("To"), required=False,
widget=forms.TextInput(
attrs={'data-date-picker': True}))
def clean_date_from(self):
period = self.cleaned_data['period']
date_from = self.cleaned_data['date_from']
if period == 'other' and date_from is None:
raise ValidationError(_('Must specify start of period'))
return date_from
def clean_date_to(self):
data = super(UsageReportForm, self).clean()
date_from = data.get('date_from')
date_to = data.get('date_to')
period = data.get('period')
if (period == 'other' and date_to is not None
and date_from is not None and date_to < date_from):
raise ValidationError(_("Start must be earlier "
"than end of period."))
else:
return date_to
def handle(self, request, data):
if hasattr(request, 'session'):
request.session['date_from'] = data['date_from']
request.session['date_to'] = data['date_to']
request.session['period'] = data['period']
return data