def view_helper(request): # Create the form if request.method == "POST": form = TestForm(request.POST) else: form = TestForm() # create a formHelper helper = FormHelper() # Add in a class and id helper.form_id = 'this-form-rocks' helper.form_class = 'search' # add in a submit and reset button submit = Submit('search', 'search this site') helper.add_input(submit) reset = Reset('reset', 'reset button') helper.add_input(reset) hidden = Hidden('not-seen', 'hidden value stored here') helper.add_input(hidden) # create the response dictionary response_dictionary = { 'form': form, 'helper': helper, 'title': 'view helper test' } return render_to_response('test_app/generic_form_test.html', response_dictionary, context_instance=RequestContext(request))
def edit_letter(request, id): data = {} letter = Letter.objects.get(id=id) if not request.user in letter.owners.all(): messages.error(request, _(u"Zugriff verweigert!")) return redirect_to_prev_page(request) if not request.method == 'POST': letter_form = LetterForm(instance=letter) else: letter_form = LetterForm(request.POST, instance=letter) if letter_form.is_valid(): letter = letter_form.save() messages.success(request, _(u"Brief erfolgreich bearbeitet.")) return redirect(letter) else: messages.error(request, _(u"Es gibt noch Fehler im Formular.")) helper = FormHelper() helper.form_id = "foobar" helper.form_class = "search" submit = Submit("send", _(u"Speichern")) helper.add_input(submit) reset = Submit("reset", _(u"Zurücksetzen")) helper.add_input(reset) data['form'] = letter_form data['helper'] = helper data.update(csrf(request)) return render_to_response("lettr/edit_letter.html", data, context_instance=RequestContext(request))
def render (self, request, dct): ## Preconditions & preparation: results = msgs = None cls = self.__class__ ## Main: # instantiate form in one of several ways ... form_cls = cls.ToolForm # if the form has been submitted... if request.method == 'POST': form = form_cls (request.POST, request.FILES) # if the form is valid if form.is_valid(): # get the clean data and do the work msgs, results = self.process_form (form.cleaned_data) else: msgs, results = ( messages.Error ('there was a problem processing the form'), ) else: # if you're coming to the form anew, make an unbound form form = form_cls() helper = FormHelper() # Add in a class and id helper.form_id = 'this-form-rocks' helper.form_class = 'tool_form' # if necessary, do fieldsets if cls.fieldsets: sets = [] for field_pair in cls.fieldsets: if (isinstance (field_pair, basestring)): # if just a naked field name field_pair = ['', field_pair] sets.append (Fieldset (*field_pair)) helper.add_layout (Layout(*sets)) # add in submit actions and a reset button for button in cls.actions: submit = Submit (button[0], button[1]) helper.add_input (submit) reset = Reset ('reset','Reset form') helper.add_input (reset) ## Postconditions & return: context = self.context() context.update ({ 'identifier' : self.identifier, 'title' : self.title, 'description': self.description, 'form': form, 'results': results, 'msgs': msgs, 'helper': helper, }) return render_to_response ('relais.amergin/tool.html', context, context_instance=RequestContext(request))
def test_uni_form_formset_with_helper_without_layout(self): template = get_template_from_string(u""" {% load uni_form_tags %} {% uni_form testFormSet formset_helper %} """) form_helper = FormHelper() form_helper.form_id = 'thisFormsetRocks' form_helper.form_class = 'formsets-that-rock' form_helper.form_method = 'POST' form_helper.form_action = 'simpleAction' TestFormSet = formset_factory(TestForm, extra = 3) testFormSet = TestFormSet() c = Context({'testFormSet': testFormSet, 'formset_helper': form_helper, 'csrf_token': _get_new_csrf_key()}) html = template.render(c) self.assertEqual(html.count('<form'), 1) self.assertEqual(html.count("<input type='hidden' name='csrfmiddlewaretoken'"), 1) # Check formset management form self.assertTrue('form-TOTAL_FORMS' in html) self.assertTrue('form-INITIAL_FORMS' in html) self.assertTrue('form-MAX_NUM_FORMS' in html) self.assertTrue('class="uniForm formsets-that-rock"' in html) self.assertTrue('method="post"' in html) self.assertTrue('id="thisFormsetRocks">' in html) self.assertTrue('action="%s"' % reverse('simpleAction') in html)
def view_helper(request): # Create the form if request.method == "POST": form = TestForm(request.POST) else: form = TestForm() # create a formHelper helper = FormHelper() # Add in a class and id helper.form_id = 'this-form-rocks' helper.form_class = 'search' # add in a submit and reset button submit = Submit('search','search this site') helper.add_input(submit) reset = Reset('reset','reset button') helper.add_input(reset) hidden = Hidden('not-seen','hidden value stored here') helper.add_input(hidden) # create the response dictionary response_dictionary = {'form':form, 'helper': helper, 'title':'view helper test'} return render_to_response('test_app/generic_form_test.html', response_dictionary, context_instance=RequestContext(request))
def test_uni_form_with_helper_attributes(self): form_helper = FormHelper() form_helper.form_id = 'this-form-rocks' form_helper.form_class = 'forms-that-rock' form_helper.form_method = 'GET' form_helper.form_action = 'simpleAction' template = get_template_from_string(u""" {% load uni_form_tags %} {% uni_form form form_helper %} """) # now we render it c = Context({'form': TestForm(), 'form_helper': form_helper}) html = template.render(c) # Lets make sure everything loads right self.assertTrue('<form' in html) self.assertTrue('class="uniForm forms-that-rock"' in html) self.assertTrue('method="get"' in html) self.assertTrue('id="this-form-rocks">' in html) self.assertTrue('action="%s"' % reverse('simpleAction') in html) # now lets remove the form tag and render it again. All the True items above # should now be false because the form tag is removed. form_helper.form_tag = False html = template.render(c) self.assertFalse('<form' in html) self.assertFalse('class="uniForm forms-that-rock"' in html) self.assertFalse('method="get"' in html) self.assertFalse('id="this-form-rocks">' in html)
def test_uni_form_formset(self): template = get_template_from_string(u""" {% load uni_form_tags %} {% uni_form testFormSet formset_helper %} """) form_helper = FormHelper() form_helper.form_id = 'this-formset-rocks' form_helper.form_class = 'formsets-that-rock' form_helper.form_method = 'GET' form_helper.form_action = 'simpleAction' from django.forms.models import formset_factory TestFormSet = formset_factory(TestForm, extra = 3) testFormSet = TestFormSet() c = Context({'testFormSet': testFormSet, 'formset_helper': form_helper}) html = template.render(c) self.assertTrue('<form' in html) self.assertEqual(html.count('<form'), 1) self.assertTrue('class="uniForm formsets-that-rock"' in html) self.assertTrue('method="get"' in html) self.assertTrue('id="this-formset-rocks">' in html) self.assertTrue('action="%s"' % reverse('simpleAction') in html)
def test_uni_form_formset(self): template = get_template_from_string( u""" {% load uni_form_tags %} {% uni_form testFormSet formset_helper %} """ ) form_helper = FormHelper() form_helper.form_id = "thisFormsetRocks" form_helper.form_class = "formsets-that-rock" form_helper.form_method = "POST" form_helper.form_action = "simpleAction" TestFormSet = formset_factory(TestForm, extra=3) testFormSet = TestFormSet() c = Context({"testFormSet": testFormSet, "formset_helper": form_helper, "csrf_token": _get_new_csrf_key()}) html = template.render(c) self.assertEqual(html.count("<form"), 1) self.assertEqual(html.count("<input type='hidden' name='csrfmiddlewaretoken'"), 1) self.assertTrue('class="uniForm formsets-that-rock"' in html) self.assertTrue('method="post"' in html) self.assertTrue('id="thisFormsetRocks">' in html) self.assertTrue('action="%s"' % reverse("simpleAction") in html)
def test_uni_form_formset_with_helper_without_layout(self): template = get_template_from_string(u""" {% load uni_form_tags %} {% uni_form testFormSet formset_helper %} """) form_helper = FormHelper() form_helper.form_id = 'thisFormsetRocks' form_helper.form_class = 'formsets-that-rock' form_helper.form_method = 'POST' form_helper.form_action = 'simpleAction' TestFormSet = formset_factory(TestForm, extra=3) testFormSet = TestFormSet() c = Context({ 'testFormSet': testFormSet, 'formset_helper': form_helper, 'csrf_token': _get_new_csrf_key() }) html = template.render(c) self.assertEqual(html.count('<form'), 1) self.assertEqual( html.count("<input type='hidden' name='csrfmiddlewaretoken'"), 1) # Check formset management form self.assertTrue('form-TOTAL_FORMS' in html) self.assertTrue('form-INITIAL_FORMS' in html) self.assertTrue('form-MAX_NUM_FORMS' in html) self.assertTrue('class="uniForm formsets-that-rock"' in html) self.assertTrue('method="post"' in html) self.assertTrue('id="thisFormsetRocks">' in html) self.assertTrue('action="%s"' % reverse('simpleAction') in html)
def test_uni_form_helper_form_attributes(self): template = get_template_from_string(""" {% load uni_form_tags %} {% uni_form form form_helper %} """) # First we build a standard form helper form_helper = FormHelper() form_helper.form_id = 'this-form-rocks' form_helper.form_class = 'forms-that-rock' form_helper.form_method = 'GET' # now we render it c = Context({'form': TestForm(), 'form_helper': form_helper}) html = template.render(c) # Lets make sure everything loads right self.assertTrue("""<form""" in html) self.assertTrue("""class="uniForm forms-that-rock" """ in html) self.assertTrue("""method="get" """ in html) self.assertTrue("""id="this-form-rocks">""" in html) # now lets remove the form tag and render it again. All the True items above # should now be false because the form tag is removed. form_helper.form_tag = False c = Context({'form': TestForm(), 'form_helper': form_helper}) html = template.render(c) self.assertFalse("""<form""" in html) self.assertFalse("""id="this-form-rocks">""" in html) self.assertFalse("""class="uniForm forms-that-rock" """ in html) self.assertFalse("""method="get" """ in html) self.assertFalse("""id="this-form-rocks">""" in html)
def test_uni_form_helper_form_attributes(self): template = get_template_from_string(""" {% load uni_form_tags %} {% uni_form form form_helper %} """) # First we build a standard form helper form_helper = FormHelper() form_helper.form_id = 'this-form-rocks' form_helper.form_class = 'forms-that-rock' form_helper.form_method = 'GET' # now we render it c = Context({'form':TestForm(),'form_helper':form_helper}) html = template.render(c) # Lets make sure everything loads right self.assertTrue("""<form""" in html) self.assertTrue("""class="uniForm forms-that-rock" """ in html) self.assertTrue("""method="get" """ in html) self.assertTrue("""id="this-form-rocks">""" in html) # now lets remove the form tag and render it again. All the True items above # should now be false because the form tag is removed. form_helper.form_tag = False c = Context({'form':TestForm(),'form_helper':form_helper}) html = template.render(c) self.assertFalse("""<form""" in html) self.assertFalse("""id="this-form-rocks">""" in html) self.assertFalse("""class="uniForm forms-that-rock" """ in html) self.assertFalse("""method="get" """ in html) self.assertFalse("""id="this-form-rocks">""" in html)
def new_letter(request): data = {} if not request.method == 'POST': from datetime import date data = {'my_date':date.today(), } letter_form = LetterForm(data) letter_form.fields["sender"].queryset = Sender.objects.filter(owners__username=request.user.username) else: letter_form = LetterForm(request.POST) if letter_form.is_valid(): letter = letter_form.save() ownership = Ownership_Letter.objects.create(owner=request.user, letter=letter) return redirect(letter) else: messages.error(request, _(u"Es gibt noch Fehler im Formular.")) helper = FormHelper() helper.form_id = "foobar" helper.form_class = "search" submit = Submit("send", _(u"Absenden")) helper.add_input(submit) reset = Submit("reset", _(u"Zurücksetzen")) helper.add_input(reset) data['form'] = letter_form data['helper'] = helper data.update(csrf(request)) return render_to_response("lettr/new_letter.html", data, context_instance=RequestContext(request))
def setUp(self): form_helper = FormHelper() form_helper.form_id = 'this-formset-rocks' form_helper.form_class = 'formsets-that-rock' form_helper.form_method = 'GET' self.c = Context({'formset': TestFormset(), 'formset_helper': form_helper})
def index(cls, request): ## Preconditions & preparation: results = msgs = None ## Main: # instantiate form in one of several ways ... form_cls = cls.ToolForm # if the form has been submitted... if request.method == "POST": form = form_cls(request.POST, request.FILES) # if the form is valid if form.is_valid(): # get the clean data and do the work msgs, results = cls.process_form(form.cleaned_data) else: msgs = [messages.Error("there was problem processing the form")] else: # if you're coming to the form anew, make an unbound form form = form_cls() helper = FormHelper() # Add in a class and id helper.form_id = "this-form-rocks" helper.form_class = "tool_form" # if necessary, do fieldsets if cls.fieldsets: sets = [] for field_pair in cls.fieldsets: if isinstance(field_pair, basestring): # if just a naked field name field_pair = ["", field_pair] sets.append(Fieldset(*field_pair)) helper.add_layout(Layout(*sets)) # add in submit actions and a reset button for button in cls.actions: submit = Submit(button[0], button[1].title()) helper.add_input(submit) reset = Reset("reset", "Reset form") helper.add_input(reset) ## Postconditions & return: return render_to_response( "relais.amergin/base_tool.html", { "identifier": cls.identifier, "title": cls.title, "description": cls.description, "form": form, "results": results, "messages": msgs, "helper": helper, }, )
def setUp(self): form_helper = FormHelper() form_helper.form_id = 'this-formset-rocks' form_helper.form_class = 'formsets-that-rock' form_helper.form_method = 'GET' self.c = Context({ 'formset': TestFormset(), 'formset_helper': form_helper })
def test_formset_layout(self): template = get_template_from_string(u""" {% load uni_form_tags %} {% uni_form testFormSet formset_helper %} """) form_helper = FormHelper() form_helper.form_id = 'thisFormsetRocks' form_helper.form_class = 'formsets-that-rock' form_helper.form_method = 'POST' form_helper.form_action = 'simpleAction' form_helper.add_layout( Layout( Fieldset("Item {{ forloop.counter }}", 'is_company', 'email', ), HTML("{% if forloop.first %}Note for first form only{% endif %}"), Row('password1', 'password2'), Fieldset("", 'first_name', 'last_name' ) ) ) TestFormSet = formset_factory(TestForm, extra = 3) testFormSet = TestFormSet() c = Context({ 'testFormSet': testFormSet, 'formset_helper': form_helper, 'csrf_token': _get_new_csrf_key() }) html = template.render(c) # Check form parameters self.assertEqual(html.count('<form'), 1) self.assertEqual(html.count("<input type='hidden' name='csrfmiddlewaretoken'"), 1) self.assertTrue('class="uniForm formsets-that-rock"' in html) self.assertTrue('method="post"' in html) self.assertTrue('id="thisFormsetRocks">' in html) self.assertTrue('action="%s"' % reverse('simpleAction') in html) # Check form layout self.assertTrue('Item 1' in html) self.assertTrue('Item 2' in html) self.assertTrue('Item 3' in html) self.assertEqual(html.count('Note for first form only'), 1) self.assertEqual(html.count('formRow'), 3)
def test_formset_layout(self): template = get_template_from_string(u""" {% load uni_form_tags %} {% uni_form testFormSet formset_helper %} """) form_helper = FormHelper() form_helper.form_id = 'thisFormsetRocks' form_helper.form_class = 'formsets-that-rock' form_helper.form_method = 'POST' form_helper.form_action = 'simpleAction' form_helper.add_layout( Layout( Fieldset( "Item {{ forloop.counter }}", 'is_company', 'email', ), HTML( "{% if forloop.first %}Note for first form only{% endif %}" ), Row('password1', 'password2'), Fieldset("", 'first_name', 'last_name'))) TestFormSet = formset_factory(TestForm, extra=3) testFormSet = TestFormSet() c = Context({ 'testFormSet': testFormSet, 'formset_helper': form_helper, 'csrf_token': _get_new_csrf_key() }) html = template.render(c) # Check form parameters self.assertEqual(html.count('<form'), 1) self.assertEqual( html.count("<input type='hidden' name='csrfmiddlewaretoken'"), 1) self.assertTrue('class="uniForm formsets-that-rock"' in html) self.assertTrue('method="post"' in html) self.assertTrue('id="thisFormsetRocks">' in html) self.assertTrue('action="%s"' % reverse('simpleAction') in html) # Check form layout self.assertTrue('Item 1' in html) self.assertTrue('Item 2' in html) self.assertTrue('Item 3' in html) self.assertEqual(html.count('Note for first form only'), 1) self.assertEqual(html.count('formRow'), 3)
def test_uni_form_helper_generic_attributes(self): form_helper = FormHelper() form_helper.form_id = 'this-form-rocks' form_helper.form_class = 'forms-that-rock' form_helper.form_method = 'GET' c = Context({'form':TestForm(),'form_helper':form_helper}) template = get_template_from_string(""" {% load uni_form_tags %} {% uni_form form form_helper %} """) html = template.render(c) good_response = """<form action="" class="uniForm forms-that-rock" method="POST" id="this-form-rocks" >"""
def helper(self): helper = FormHelper() layout = Layout( Fieldset('Choose a School or create a new one', HTML('<div id="choose">'), Row('school', HTML('<input type="button" id="new" onclick="click_new()" value="Add a new school"/>')), HTML('</div>'), HTML('<div id="create">'), 'school_name', 'school_url', 'school_email', 'school_phone', 'school_address', 'school_city', 'school_country', HTML('<input type="button" id="old" onclick="click_old()" value="Choose an already existent school"/>'), HTML('</div>'), ), Fieldset('', HTML('<p> </p>'), ), Fieldset('Fill in the course information', 'name', 'lecturer', 'language', 'level', 'type', 'url', 'description', ) ) helper.add_layout(layout) submit = Submit('submit','Save') helper.add_input(submit) helper.form_method = 'POST' helper.form_id = 'my_form' #helper.form_action = "/add_course/?status='old'" return helper
def test_uni_form_helper_generic_attributes(self): form_helper = FormHelper() form_helper.form_id = 'this-form-rocks' form_helper.form_class = 'forms-that-rock' form_helper.form_method = 'GET' c = Context({'form': TestForm(), 'form_helper': form_helper}) template = get_template_from_string(""" {% load uni_form_tags %} {% uni_form form form_helper %} """) html = template.render(c) good_response = """<form action="" class="uniForm forms-that-rock" method="POST" id="this-form-rocks" >""" self.assertTrue( '<form action="" class="uniForm forms-that-rock" method="GET" id="this-form-rocks" >' in html)
def test_formset_layout(self): template = get_template_from_string( u""" {% load uni_form_tags %} {% uni_form testFormSet formset_helper %} """ ) form_helper = FormHelper() form_helper.form_id = "thisFormsetRocks" form_helper.form_class = "formsets-that-rock" form_helper.form_method = "POST" form_helper.form_action = "simpleAction" form_helper.add_layout( Layout( Fieldset("Item {{ forloop.counter }}", "is_company", "email"), HTML("{% if forloop.first %}Note for first form only{% endif %}"), Row("password1", "password2"), Fieldset("", "first_name", "last_name"), ) ) TestFormSet = formset_factory(TestForm, extra=3) testFormSet = TestFormSet() c = Context({"testFormSet": testFormSet, "formset_helper": form_helper, "csrf_token": _get_new_csrf_key()}) html = template.render(c) # Check form parameters self.assertEqual(html.count("<form"), 1) self.assertEqual(html.count("<input type='hidden' name='csrfmiddlewaretoken'"), 1) self.assertTrue('class="uniForm formsets-that-rock"' in html) self.assertTrue('method="post"' in html) self.assertTrue('id="thisFormsetRocks">' in html) self.assertTrue('action="%s"' % reverse("simpleAction") in html) # Check form layout self.assertTrue("Item 1" in html) self.assertTrue("Item 2" in html) self.assertTrue("Item 3" in html) self.assertEqual(html.count("Note for first form only"), 1) self.assertEqual(html.count("formRow"), 3)
def test_uni_form_with_helper_without_layout(self): form_helper = FormHelper() form_helper.form_id = 'this-form-rocks' form_helper.form_class = 'forms-that-rock' form_helper.form_method = 'GET' form_helper.form_action = 'simpleAction' form_helper.form_error_title = 'ERRORS' template = get_template_from_string(u""" {% load uni_form_tags %} {% uni_form testForm form_helper %} """) # now we render it, with errors form = TestForm({'password1': 'wargame','password2': 'god'}) form.is_valid() c = Context({'testForm': form, 'form_helper': form_helper}) html = template.render(c) # Lets make sure everything loads right self.assertTrue(html.count('<form'), 1) self.assertTrue('class="uniForm forms-that-rock"' in html) self.assertTrue('method="get"' in html) self.assertTrue('id="this-form-rocks">' in html) self.assertTrue('action="%s"' % reverse('simpleAction') in html) self.assertEqual(html.count('<fieldset'), 1) self.assertTrue("<h3>ERRORS</h3>" in html) self.assertTrue("<li>Passwords dont match</li>" in html) # now lets remove the form tag and render it again. All the True items above # should now be false because the form tag is removed. form_helper.form_tag = False html = template.render(c) self.assertFalse('<form' in html) self.assertFalse('class="uniForm forms-that-rock"' in html) self.assertFalse('method="get"' in html) self.assertFalse('id="this-form-rocks">' in html)
def test_uni_form_with_helper_attributes_without_layout(self): form_helper = FormHelper() form_helper.form_id = "this-form-rocks" form_helper.form_class = "forms-that-rock" form_helper.form_method = "GET" form_helper.form_action = "simpleAction" form_helper.form_error_title = "ERRORS" template = get_template_from_string( u""" {% load uni_form_tags %} {% uni_form testForm form_helper %} """ ) # now we render it, with errors form = TestForm({"password1": "wargame", "password2": "god"}) form.is_valid() c = Context({"testForm": form, "form_helper": form_helper}) html = template.render(c) # Lets make sure everything loads right self.assertTrue("<form" in html) self.assertTrue('class="uniForm forms-that-rock"' in html) self.assertTrue('method="get"' in html) self.assertTrue('id="this-form-rocks">' in html) self.assertTrue('action="%s"' % reverse("simpleAction") in html) self.assertTrue("<h3>ERRORS</h3>" in html) self.assertTrue("<li>Passwords dont match</li>" in html) # now lets remove the form tag and render it again. All the True items above # should now be false because the form tag is removed. form_helper.form_tag = False html = template.render(c) self.assertFalse("<form" in html) self.assertFalse('class="uniForm forms-that-rock"' in html) self.assertFalse('method="get"' in html) self.assertFalse('id="this-form-rocks">' in html)
def test_uni_form_with_helper_without_layout(self): form_helper = FormHelper() form_helper.form_id = 'this-form-rocks' form_helper.form_class = 'forms-that-rock' form_helper.form_method = 'GET' form_helper.form_action = 'simpleAction' form_helper.form_error_title = 'ERRORS' template = get_template_from_string(u""" {% load uni_form_tags %} {% uni_form testForm form_helper %} """) # now we render it, with errors form = TestForm({'password1': 'wargame', 'password2': 'god'}) form.is_valid() c = Context({'testForm': form, 'form_helper': form_helper}) html = template.render(c) # Lets make sure everything loads right self.assertTrue(html.count('<form'), 1) self.assertTrue('class="uniForm forms-that-rock"' in html) self.assertTrue('method="get"' in html) self.assertTrue('id="this-form-rocks">' in html) self.assertTrue('action="%s"' % reverse('simpleAction') in html) self.assertEqual(html.count('<fieldset'), 1) self.assertTrue("<h3>ERRORS</h3>" in html) self.assertTrue("<li>Passwords dont match</li>" in html) # now lets remove the form tag and render it again. All the True items above # should now be false because the form tag is removed. form_helper.form_tag = False html = template.render(c) self.assertFalse('<form' in html) self.assertFalse('class="uniForm forms-that-rock"' in html) self.assertFalse('method="get"' in html) self.assertFalse('id="this-form-rocks">' in html)