From 9ac1f9b4d003f099974580b214cd85abd75cee76 Mon Sep 17 00:00:00 2001 From: Gabriel Hurley Date: Sun, 5 Aug 2012 13:48:58 -0700 Subject: [PATCH] Separate "modal" behavior from "self-handling" in forms. Splits behaviors related to a form being modal from the requirement that the form also be self-handling by making the component classes into mixins. This allows mixing modal capabilities into other generic form classes. Change-Id: Id9fb7d278e7cb48d923e2a0dd5cc6dd524a7b073 --- horizon/forms/__init__.py | 4 ++-- horizon/forms/base.py | 15 ++++++++------- horizon/forms/views.py | 16 +++++++++------- 3 files changed, 19 insertions(+), 16 deletions(-) diff --git a/horizon/forms/__init__.py b/horizon/forms/__init__.py index 192b87307..332510c1f 100644 --- a/horizon/forms/__init__.py +++ b/horizon/forms/__init__.py @@ -20,6 +20,6 @@ from django.forms import * from django.forms import widgets # Convenience imports for public API components. -from .base import SelfHandlingForm, DateForm -from .views import ModalFormView +from .base import SelfHandlingMixin, SelfHandlingForm, DateForm +from .views import ModalFormView, ModalFormMixin from .fields import DynamicTypedChoiceField, DynamicChoiceField diff --git a/horizon/forms/base.py b/horizon/forms/base.py index b3a683b3c..2357ea504 100644 --- a/horizon/forms/base.py +++ b/horizon/forms/base.py @@ -23,19 +23,20 @@ from django.forms.forms import NON_FIELD_ERRORS from django.utils import dates, timezone -class SelfHandlingForm(forms.Form): - """ - A base :class:`Form ` class which includes - processing logic in its subclasses. - """ - +class SelfHandlingMixin(object): def __init__(self, request, *args, **kwargs): self.request = request if not hasattr(self, "handle"): raise NotImplementedError("%s does not define a handle method." % self.__class__.__name__) - super(SelfHandlingForm, self).__init__(*args, **kwargs) + super(SelfHandlingMixin, self).__init__(*args, **kwargs) + +class SelfHandlingForm(SelfHandlingMixin, forms.Form): + """ + A base :class:`Form ` class which includes + processing logic in its subclasses. + """ def api_error(self, message): """ Adds an error to the form's error dictionary after validation diff --git a/horizon/forms/views.py b/horizon/forms/views.py index f0b461a6d..40cd858c2 100644 --- a/horizon/forms/views.py +++ b/horizon/forms/views.py @@ -23,7 +23,7 @@ from horizon.openstack.common import jsonutils from horizon import exceptions -class ModalFormView(generic.FormView): +class ModalFormMixin(object): def get_template_names(self): if self.request.is_ajax(): if not hasattr(self, "ajax_template_name"): @@ -36,18 +36,20 @@ class ModalFormView(generic.FormView): template = self.template_name return template + def get_context_data(self, **kwargs): + context = super(ModalFormMixin, self).get_context_data(**kwargs) + if self.request.is_ajax(): + context['hide'] = True + return context + + +class ModalFormView(ModalFormMixin, generic.FormView): def get_object_id(self, obj): return obj.id def get_object_display(self, obj): return obj.name - def get_context_data(self, **kwargs): - context = super(ModalFormView, self).get_context_data(**kwargs) - if self.request.is_ajax(): - context['hide'] = True - return context - def get_form(self, form_class): """ Returns an instance of the form to be used in this view.