~ubuntu-branches/ubuntu/utopic/horizon/utopic-updates

« back to all changes in this revision

Viewing changes to horizon/templatetags/form_helpers.py

  • Committer: Package Import Robot
  • Author(s): James Page, Chris Johnston, James Page
  • Date: 2014-10-03 17:54:18 UTC
  • mfrom: (0.4.1) (1.1.44) (70.1.2 utopic)
  • Revision ID: package-import@ubuntu.com-20141003175418-1jomx0azdvnl5fxz
Tags: 1:2014.2~rc1-0ubuntu1
[ Chris Johnston ]
* d/theme/css/ubuntu.css: Fix Ubuntu theme for Instances "more" dropdown
  (LP: #1308651).

[ James Page ]
* New upstream release candidate:
  - d/p/*: Refresh.
* d/watch: Use tarballs.openstack.org for upstream releases. 

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
 
2
#    not use this file except in compliance with the License. You may obtain
 
3
#    a copy of the License at
 
4
#
 
5
#         http://www.apache.org/licenses/LICENSE-2.0
 
6
#
 
7
#    Unless required by applicable law or agreed to in writing, software
 
8
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 
9
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 
10
#    License for the specific language governing permissions and limitations
 
11
#    under the License.
 
12
 
 
13
import django.forms
 
14
from django import template as django_template
 
15
 
 
16
register = django_template.Library()
 
17
 
 
18
 
 
19
@register.filter
 
20
def add_bootstrap_class(field):
 
21
    """Add a "form-control" CSS class to the field's widget.
 
22
 
 
23
    This is so that Bootstrap styles it properly.
 
24
    """
 
25
    if not isinstance(field.field.widget, (
 
26
        django.forms.widgets.CheckboxInput,
 
27
        django.forms.widgets.CheckboxSelectMultiple,
 
28
        django.forms.widgets.RadioSelect,
 
29
        django.forms.widgets.FileInput,
 
30
        str,
 
31
    )):
 
32
        field_classes = set(field.field.widget.attrs.get('class', '').split())
 
33
        field_classes.add('form-control')
 
34
        field.field.widget.attrs['class'] = ' '.join(field_classes)
 
35
    return field
 
36
 
 
37
 
 
38
@register.filter
 
39
def is_checkbox(field):
 
40
    return isinstance(field.field.widget, django.forms.CheckboxInput)
 
41
 
 
42
 
 
43
@register.filter
 
44
def is_multiple_checkbox(field):
 
45
    return isinstance(field.field.widget, django.forms.CheckboxSelectMultiple)
 
46
 
 
47
 
 
48
@register.filter
 
49
def is_radio(field):
 
50
    return isinstance(field.field.widget, django.forms.RadioSelect)
 
51
 
 
52
 
 
53
@register.filter
 
54
def is_file(field):
 
55
    return isinstance(field.field.widget, django.forms.FileInput)
 
56
 
 
57
 
 
58
@register.filter
 
59
def is_dynamic_select(field):
 
60
    return hasattr(field.field.widget, 'add_item_link')
 
61
 
 
62
 
 
63
@register.filter
 
64
def wrapper_classes(field):
 
65
    classes = []
 
66
    if is_multiple_checkbox(field):
 
67
        classes.append('multiple-checkbox')
 
68
    if is_dynamic_select(field):
 
69
        classes.append('dynamic-select')
 
70
    return ' '.join(classes)