Improved ChainedForm.

This commit is contained in:
2018-01-20 16:38:28 +01:00
parent 346249f623
commit 873976199f
2 changed files with 25 additions and 9 deletions

View File

@@ -76,6 +76,7 @@ class SetPasswordForm(forms.Form):
class ChainedForm(forms.Form): class ChainedForm(forms.Form):
_initial_form_name = None
_next_form_name = None _next_form_name = None
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
@@ -126,21 +127,33 @@ class ChainedForm(forms.Form):
@property @property
def form_title(self): def form_title(self):
if hasattr(self, '_form_title'): return self.__class__.get_form_title()
return self._form_title
n = self.form_name @property
if n.endswith('Form'): def initial_form_name(self):
n = n[:-len('Form')] return self.__class__.get_initial_form_name()
return n
@property @property
def next_form_name(self): def next_form_name(self):
return self._next_form_name return self.__class__.get_next_form_name()
@classmethod @classmethod
def get_form_name(cls): def get_form_name(cls):
return cls.__name__ return cls.__name__
@classmethod
def get_form_title(cls):
if hasattr(cls, '_form_title'):
return cls._form_title
n = cls.get_form_name()
if n.endswith('Form'):
n = n[:-len('Form')]
return n
@classmethod
def get_initial_form_name(cls):
return cls._initial_form_name or cls.__name__
@classmethod @classmethod
def get_next_form_name(cls): def get_next_form_name(cls):
return cls._next_form_name return cls._next_form_name
@@ -172,6 +185,7 @@ class ChainedForm(forms.Form):
class EventCreateForm(ChainedForm): class EventCreateForm(ChainedForm):
_model = models.Event _model = models.Event
_initial_form_name = 'ModeForm'
class ModeForm(EventCreateForm): class ModeForm(EventCreateForm):

View File

@@ -137,7 +137,7 @@ class EventAcceptView(EventDetailView):
class EventCreateView(generic.FormView): class EventCreateView(generic.FormView):
initial_form_class = forms.ModeForm form_class = forms.EventCreateForm
template_dir = os.path.join('dav_events', 'event_create') template_dir = os.path.join('dav_events', 'event_create')
default_template_name = 'default.html' default_template_name = 'default.html'
abort_url = reverse_lazy('dav_events:home') abort_url = reverse_lazy('dav_events:home')
@@ -160,7 +160,9 @@ class EventCreateView(generic.FormView):
if not issubclass(form_class, forms.ChainedForm): if not issubclass(form_class, forms.ChainedForm):
raise SuspiciousOperation('Invalid next form: {}'.format(form_name)) raise SuspiciousOperation('Invalid next form: {}'.format(form_name))
else: else:
form_class = self.initial_form_class base_form_class = self.form_class
initial_form_name = base_form_class.get_initial_form_name()
form_class = getattr(forms, initial_form_name)
return form_class return form_class