mirror of
https://github.com/Hopiu/wagtail.git
synced 2026-04-28 02:24:48 +00:00
Merge remote-tracking branch 'upstream/form-builder' into form-builder
This commit is contained in:
commit
77da201964
3 changed files with 16 additions and 6 deletions
|
|
@ -2,6 +2,12 @@ import django.forms
|
|||
from django.utils.datastructures import SortedDict
|
||||
|
||||
|
||||
class BaseForm(django.forms.Form):
|
||||
def __init__(self, *args, **kwargs):
|
||||
kwargs.setdefault('label_suffix', '')
|
||||
return super(BaseForm, self).__init__(*args, **kwargs)
|
||||
|
||||
|
||||
class FormBuilder():
|
||||
formfields = SortedDict()
|
||||
|
||||
|
|
@ -67,7 +73,7 @@ class FormBuilder():
|
|||
return django.forms.BooleanField(**options)
|
||||
|
||||
def get_form_class(self):
|
||||
return type('WagtailForm', (django.forms.Form,), self.formfields)
|
||||
return type('WagtailForm', (BaseForm,), self.formfields)
|
||||
|
||||
|
||||
class SelectDateForm(django.forms.Form):
|
||||
|
|
|
|||
|
|
@ -140,12 +140,16 @@ class AbstractForm(Page):
|
|||
class Meta:
|
||||
abstract = True
|
||||
|
||||
def get_form_parameters(self):
|
||||
return {}
|
||||
|
||||
def serve(self, request):
|
||||
fb = self.form_builder(self.form_fields.all())
|
||||
form_class = fb.get_form_class()
|
||||
form_params = self.get_form_parameters()
|
||||
|
||||
if request.method == 'POST':
|
||||
self.form = form_class(request.POST)
|
||||
self.form = form_class(request.POST, **form_params)
|
||||
|
||||
if self.form.is_valid():
|
||||
# remove csrf_token from form.data
|
||||
|
|
@ -170,7 +174,7 @@ class AbstractForm(Page):
|
|||
'self': self,
|
||||
})
|
||||
else:
|
||||
self.form = form_class()
|
||||
self.form = form_class(**form_params)
|
||||
|
||||
return render(request, self.template, {
|
||||
'self': self,
|
||||
|
|
|
|||
|
|
@ -8,7 +8,7 @@ class TestFormSubmission(TestCase):
|
|||
|
||||
def test_get_form(self):
|
||||
response = self.client.get('/contact-us/')
|
||||
self.assertContains(response, "Your email")
|
||||
self.assertContains(response, """<label for="id_your-email">Your email</label>""")
|
||||
self.assertNotContains(response, "Thank you for your feedback")
|
||||
|
||||
def test_post_invalid_form(self):
|
||||
|
|
@ -38,14 +38,14 @@ class TestFormsBackend(TestCase):
|
|||
|
||||
self.client.login(username='eventeditor', password='password')
|
||||
response = self.client.get('/admin/forms/')
|
||||
self.assertFalse(response.context['form_pages'].filter(id=form_page.id).exists())
|
||||
self.assertFalse(form_page in response.context['form_pages'])
|
||||
|
||||
def test_can_see_forms_with_permission(self):
|
||||
form_page = Page.objects.get(url_path='/home/contact-us/')
|
||||
|
||||
self.client.login(username='siteeditor', password='password')
|
||||
response = self.client.get('/admin/forms/')
|
||||
self.assertTrue(response.context['form_pages'].filter(id=form_page.id).exists())
|
||||
self.assertTrue(form_page in response.context['form_pages'])
|
||||
|
||||
def test_can_get_submissions(self):
|
||||
form_page = Page.objects.get(url_path='/home/contact-us/')
|
||||
|
|
|
|||
Loading…
Reference in a new issue