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
This commit is contained in:
parent
2f0678db45
commit
9ac1f9b4d0
@ -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
|
||||
|
@ -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 <django:django.forms.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 <django:django.forms.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
|
||||
|
@ -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.
|
||||
|
Loading…
x
Reference in New Issue
Block a user