Skip to content

Commit

Permalink
[1.5.X] Added WizardView.file_storage exception message and docs
Browse files Browse the repository at this point in the history
Thanks Danilo Bargen for the patch.

Backport of af7ea80 from master
  • Loading branch information
timgraham committed Nov 1, 2012
1 parent a8c415f commit c5d0f49
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 7 deletions.
8 changes: 6 additions & 2 deletions django/contrib/formtools/wizard/storage/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,9 @@ def get_step_files(self, step):
wizard_files = self.data[self.step_files_key].get(step, {})

if wizard_files and not self.file_storage:
raise NoFileStorageConfigured
raise NoFileStorageConfigured(
"You need to define 'file_storage' in your "
"wizard view in order to handle file uploads.")

files = {}
for field, field_dict in six.iteritems(wizard_files):
Expand All @@ -81,7 +83,9 @@ def get_step_files(self, step):

def set_step_files(self, step, files):
if files and not self.file_storage:
raise NoFileStorageConfigured
raise NoFileStorageConfigured(
"You need to define 'file_storage' in your "
"wizard view in order to handle file uploads.")

if step not in self.data[self.step_files_key]:
self.data[self.step_files_key][step] = {}
Expand Down
12 changes: 7 additions & 5 deletions django/contrib/formtools/wizard/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,9 @@ def get_initkwargs(cls, form_list, initial_dict=None,
for field in six.itervalues(form.base_fields):
if (isinstance(field, forms.FileField) and
not hasattr(cls, 'file_storage')):
raise NoFileStorageConfigured
raise NoFileStorageConfigured(
"You need to define 'file_storage' in your "
"wizard view in order to handle file uploads.")

# build the kwargs for the wizardview instances
kwargs['form_list'] = init_form_list
Expand Down Expand Up @@ -436,8 +438,8 @@ def get_form_step_files(self, form):
def get_all_cleaned_data(self):
"""
Returns a merged dictionary of all step cleaned_data dictionaries.
If a step contains a `FormSet`, the key will be prefixed with formset
and contain a list of the formset cleaned_data dictionaries.
If a step contains a `FormSet`, the key will be prefixed with
'formset-' and contain a list of the formset cleaned_data dictionaries.
"""
cleaned_data = {}
for form_key in self.get_form_list():
Expand All @@ -458,8 +460,8 @@ def get_all_cleaned_data(self):
def get_cleaned_data_for_step(self, step):
"""
Returns the cleaned data for a given `step`. Before returning the
cleaned data, the stored values are being revalidated through the
form. If the data doesn't validate, None will be returned.
cleaned data, the stored values are revalidated through the form.
If the data doesn't validate, None will be returned.
"""
if step in self.form_list:
form_obj = self.get_form(step=step,
Expand Down
25 changes: 25 additions & 0 deletions docs/ref/contrib/formtools/form-wizard.txt
Original file line number Diff line number Diff line change
Expand Up @@ -493,6 +493,21 @@ Advanced ``WizardView`` methods
context = self.get_context_data(form=form, **kwargs)
return self.render_to_response(context)

.. method:: WizardView.get_cleaned_data_for_step(step)

This method returns the cleaned data for a given ``step``. Before returning
the cleaned data, the stored values are revalidated through the form. If
the data doesn't validate, ``None`` will be returned.

.. method:: WizardView.get_all_cleaned_data()

This method returns a merged dictionary of all form steps' ``cleaned_data``
dictionaries. If a step contains a ``FormSet``, the key will be prefixed
with ``formset-`` and contain a list of the formset's ``cleaned_data``
dictionaries. Note that if two or more steps have a field with the same
name, the value for that field from the latest step will overwrite the
value from any earlier steps.

Providing initial data for the forms
====================================

Expand Down Expand Up @@ -534,6 +549,16 @@ This storage will temporarily store the uploaded files for the wizard. The
:attr:`file_storage` attribute should be a
:class:`~django.core.files.storage.Storage` subclass.

Django provides a built-in storage class (see :ref:`the built-in filesystem
storage class <builtin-fs-storage>`)::

from django.conf import settings
from django.core.files.storage import FileSystemStorage

class CustomWizardView(WizardView):
...
file_storage = FileSystemStorage(location=os.path.join(settings.MEDIA_ROOT, 'photos'))

.. warning::

Please remember to take care of removing old files as the
Expand Down
2 changes: 2 additions & 0 deletions docs/topics/files.txt
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,8 @@ useful -- you can use the global default storage system::

See :doc:`/ref/files/storage` for the file storage API.

.. _builtin-fs-storage:

The built-in filesystem storage class
-------------------------------------

Expand Down

0 comments on commit c5d0f49

Please sign in to comment.