Skip to content

Commit

Permalink
Use horizon page mixin and help text on crispy forms.
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelkuty committed Jul 15, 2016
1 parent 027c6c6 commit ffad4a5
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 14 deletions.
31 changes: 20 additions & 11 deletions horizon_contrib/forms/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

try:
from crispy_forms.helper import FormHelper
from crispy_forms.layout import Div, Layout
from crispy_forms.layout import Div, Layout, HTML
from crispy_forms.bootstrap import Tab, TabHolder
CRISPY = True
except Exception as e:
Expand Down Expand Up @@ -40,6 +40,8 @@ class SelfHandlingMixin(object):
}
}
help_text = "Select user and role."
"""
required_css_class = 'required'

Expand All @@ -51,21 +53,28 @@ def __init__(self, request=None, *args, **kwargs):
super(SelfHandlingMixin, self).__init__(*args, **kwargs)

# crispy layout
if CRISPY:
if CRISPY and not hasattr(self, 'helper'):
self.helper = FormHelper(self)
self.helper.field_class = ""
self.helper.form_tag = False
self.helper.label_class = "control-label"

# classes added only if not hidden
for item in self.helper.layout.fields:
if item in ["object_id", "id"]:
continue
try:
self.helper[item].wrap(
Div, css_class="col-lg-6 field-wrapper")
except Exception:
pass
if hasattr(self, 'help_text'):
# for helptext make two columns
self.helper[0:len(self.helper.layout.fields)].wrap_together(
Div, css_class="col-lg-6 field-wrapper")
self.helper.layout.append(
HTML("<div class='col-lg-6 help-text'>%s</div>" % self.help_text))
else:
# classes added only if not hidden
for item in self.helper.layout.fields:
if item in ["object_id", "id"]:
continue
try:
self.helper[item].wrap(
Div, css_class="col-lg-6 field-wrapper")
except Exception:
pass

def init_layout(self):
'''Call init for generic layout'''
Expand Down
12 changes: 9 additions & 3 deletions horizon_contrib/forms/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,16 @@
from django.forms.models import model_to_dict
from horizon_contrib.forms.forms import SelfHandlingModelForm

try:
from horizon.views import PageTitleMixin
BaseClass = PageTitleMixin
except ImportError:
BaseClass = object

ADD_TO_FIELD_HEADER = "HTTP_X_HORIZON_ADD_TO_FIELD"


class ModalFormMixin(object):
class ModalFormMixin(BaseClass):

def get_template_names(self):
if self.request.is_ajax():
Expand Down Expand Up @@ -61,8 +67,8 @@ def get_header(self):
return b"%s" % self.get_label()

def get_help_text(self):
return getattr(self, 'help_text', _('Empty space is so boring please\
provide `help_text on this view`'))
return getattr(self, 'help_text', _('Empty space is so boring please '
'provide `help_text on this view`'))


class ModalFormView(ModalFormMixin, generic.FormView):
Expand Down

0 comments on commit ffad4a5

Please sign in to comment.