|
| 1 | +# Copyright 2012 United States Government as represented by the |
| 2 | +# Administrator of the National Aeronautics and Space Administration. |
| 3 | +# All Rights Reserved. |
| 4 | +# |
| 5 | +# Copyright 2012 Nebula, Inc. |
| 6 | +# |
| 7 | +# Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 8 | +# not use this file except in compliance with the License. You may obtain |
| 9 | +# a copy of the License at |
| 10 | +# |
| 11 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | +# |
| 13 | +# Unless required by applicable law or agreed to in writing, software |
| 14 | +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 15 | +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 16 | +# License for the specific language governing permissions and limitations |
| 17 | +# under the License. |
| 18 | +from django import forms |
| 19 | + |
| 20 | + |
| 21 | +class DateForm(forms.Form): |
| 22 | + """A simple form for selecting a range of time.""" |
| 23 | + start = forms.DateField(input_formats=("%Y-%m-%d",)) |
| 24 | + end = forms.DateField(input_formats=("%Y-%m-%d",)) |
| 25 | + |
| 26 | + def __init__(self, *args, **kwargs): |
| 27 | + super().__init__(*args, **kwargs) |
| 28 | + |
| 29 | + self.fields['start'].widget.attrs['data-date-format'] = "yyyy-mm-dd" |
| 30 | + self.fields['end'].widget.attrs['data-date-format'] = "yyyy-mm-dd" |
| 31 | + |
| 32 | + |
| 33 | +class CheckBoxForm(forms.Form): |
| 34 | + """A form for selecting fields to group by in the rating summary.""" |
| 35 | + checkbox_fields = ["type", "id", "user_id"] |
| 36 | + for field in checkbox_fields: |
| 37 | + locals()[field] = forms.BooleanField(required=False) |
| 38 | + |
| 39 | + def get_selected_fields(self): |
| 40 | + """Return list of selected groupby fields.""" |
| 41 | + if not self.is_valid(): |
| 42 | + return [] |
| 43 | + # Get all selected checkbox fields |
| 44 | + selected = [ |
| 45 | + field for field in self.checkbox_fields |
| 46 | + if self.cleaned_data.get(field) |
| 47 | + ] |
| 48 | + return selected |
0 commit comments