Skip to content

Commit 7b75862

Browse files
Zuulopenstack-gerrit
authored andcommitted
Merge "Add support for custom forms"
2 parents 8b7bdbf + d2c8505 commit 7b75862

File tree

2 files changed

+76
-0
lines changed

2 files changed

+76
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Copyright 2012 Nebula, Inc.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License"); you may
4+
# not use this file except in compliance with the License. You may obtain
5+
# a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12+
# License for the specific language governing permissions and limitations
13+
# under the License.
14+
# Importing non-modules that are not used explicitly
15+
from django.forms.fields import BooleanField
16+
from django.forms.fields import DateField
17+
from django.forms.forms import Form
18+
19+
# Convenience imports for public API components.
20+
from cloudkittydashboard.forms.base import CheckBoxForm
21+
from cloudkittydashboard.forms.base import DateForm
22+
23+
__all__ = [
24+
"DateForm",
25+
"CheckBoxForm",
26+
'DateField', 'BooleanField',
27+
'Form',
28+
]

cloudkittydashboard/forms/base.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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

Comments
 (0)