Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for passing extra parameters to the form constructor (FormPreview) #233

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/preview.rst
Original file line number Diff line number Diff line change
Expand Up @@ -139,3 +139,5 @@ Optional methods
.. automethod:: FormPreview.security_hash

.. automethod:: FormPreview.failed_hash

.. automethod:: FormPreview.form_extra_params
18 changes: 15 additions & 3 deletions formtools/preview.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ def unused_name(self, name):
def preview_get(self, request):
"Displays the form"
f = self.form(auto_id=self.get_auto_id(),
initial=self.get_initial(request))
initial=self.get_initial(request),
**self.form_extra_params(request))
return render(request, self.form_template, self.get_context(request, f))

def preview_post(self, request):
Expand All @@ -61,7 +62,10 @@ def preview_post(self, request):
"""
# Even if files are not supported in preview, we still initialize files
# to give a chance to process_preview to access files content.
f = self.form(data=request.POST, files=request.FILES, auto_id=self.get_auto_id())
f = self.form(data=request.POST,
files=request.FILES,
auto_id=self.get_auto_id(),
**self.form_extra_params(request))
context = self.get_context(request, f)
if f.is_valid():
self.process_preview(request, f, context)
Expand All @@ -79,7 +83,7 @@ def post_post(self, request):
"""
Validates the POST data. If valid, calls done(). Else, redisplays form.
"""
form = self.form(request.POST, auto_id=self.get_auto_id())
form = self.form(request.POST, auto_id=self.get_auto_id(), **self.form_extra_params(request))
if form.is_valid():
if not self._check_security_hash(
request.POST.get(self.unused_name('hash'), ''),
Expand Down Expand Up @@ -158,6 +162,14 @@ def failed_hash(self, request):
"""
return self.preview_post(request)

def form_extra_params(self, request):
"""
Extra parameters to pass to the form constructor.
Returns a dictionary.
By default, returns an empty dictionary.
"""
return {}

# METHODS SUBCLASSES MUST OVERRIDE ########################################

def done(self, request, cleaned_data):
Expand Down
4 changes: 4 additions & 0 deletions tests/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ class TestForm(forms.Form):
bool1 = forms.BooleanField(required=False)
date1 = forms.DateField(required=False)

def __init__(self, *args, **kwargs):
self.request = kwargs.pop('request', None)
super(TestForm, self).__init__(*args, **kwargs)


class HashTestForm(forms.Form):
name = forms.CharField()
Expand Down
5 changes: 4 additions & 1 deletion tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ def get_context(self, request, form):
def get_initial(self, request):
return {'field1': 'Works!'}

def form_extra_params(self, request):
return {'request': request}

def done(self, request, cleaned_data):
return http.HttpResponse(success_string)

Expand Down Expand Up @@ -64,7 +67,7 @@ def test_parse_params_takes_request_object(self):

def test_unused_name(self):
"""
Verifies name mangling to get uniue field name.
Verifies name mangling to get unique field name.
"""
self.assertEqual(self.preview.unused_name('field1'), 'field1__')

Expand Down