Example #1
0
    class Controller(controllers.RootController):

        [expose()]
        [validate(validators=AddingNestedSchema(), state_factory=State)]

        def validation(self, a, b, c):
            return 'counter: %d' % cherrypy.request.validation_state.counter
    class MyRoot(turbogears.controllers.RootController):
        def test(self):
            return dict(form=form)

        test = turbogears.expose(template=".form")(test)

        def test_value(self):
            value = dict(repeat=[{
                'name': 'foo',
                'comment': 'hello'
            }, None, None, {
                'name': 'bar',
                'comment': 'byebye'
            }])
            return dict(form=form, value=value)

        test_value = turbogears.expose(template=".form")(test_value)

        def test_validation(self):
            return dict(form=form)

        test_validation = turbogears.expose(template=".form")(test_validation)
        test_validation = turbogears.validate(form=form)(test_validation)
        test_validation = turbogears.error_handler(test_validation)(
            test_validation)
Example #3
0
class NestedController(controllers.Controller):

    [expose()]
    [validate(form=nestedform)]

    def checkform(self, foo):
        self.foo = foo
class MyRoot(controllers.RootController):
    def testform(self, p_data, tg_errors=None):
        if tg_errors:
            self.has_errors = True
        self.name = p_data['name']
        self.age = p_data['age']
    testform = turbogears.validate(form=myform)(testform)
    testform = turbogears.expose(template="turbogears.tests.othertemplate")(
                                 testform)

    def set_errors(self):
        self.has_errors = True

    def testform_new_style(self, p_data):
        self.name = p_data['name']
        self.age = p_data['age']
    testform_new_style = turbogears.validate(form=myform)(testform_new_style)
    testform_new_style = turbogears.error_handler(set_errors)(testform_new_style)
    testform_new_style = turbogears.expose()(testform_new_style)
    class MyRoot(turbogears.controllers.RootController):
        def test(self):
            return dict(form1=form1, form2=form2)

        test = turbogears.expose(template=".two_forms")(test)
        test = turbogears.validate(form=form1)(test)
        test = turbogears.error_handler(test)(test)

        def test2(self):
            return dict(form=form2)

        test2 = turbogears.expose(template=".form")(test2)
        test2 = turbogears.validate(form=form2)(test2)
        test2 = turbogears.error_handler(test2)(test2)

        def test3(self):
            return dict(form=form1)

        test3 = turbogears.expose(template=".form")(test3)
        test3 = turbogears.validate(form=form2)(test3)
        test3 = turbogears.error_handler(test3)(test3)
Example #6
0
class MyRoot(controllers.RootController):

    [expose(template="turbogears.tests.form")]

    def index(self):
        return dict(form=myform)

    [expose(template="turbogears.tests.form")]

    def fields(self):
        myfields.display = lambda **kw: kw.values()
        return dict(form=myfields)

    [expose(template="turbogears.tests.form")]

    def usemochi(self):
        return dict(mochi=mochikit, form=myform)

    [expose(template="turbogears.tests.othertemplate")]
    [validate(form=myform)]

    def testform(self, name, date, age, tg_errors=None):
        if tg_errors:
            self.has_errors = True
        self.name = name
        self.age = age
        self.date = date

    [expose()]
    [validate(form=myform)]

    def testform_new_style(self, name, date, age):
        if cherrypy.request.validation_errors:
            self.has_errors = True
        self.name = name
        self.age = age
        self.date = date
Example #7
0
class MyRoot(controllers.RootController):

    [expose()]
    def index(self):
        pass

    def validation_error_handler(self, tg_source, tg_errors, *args, **kw):
        self.functionname = tg_source.__name__
        self.values = kw
        self.errors = tg_errors
        return "Error Message"

    [expose(template="turbogears.tests.simple", allow_json=True)]
    def test(self):
        return dict(title="Foobar", mybool=False, someval="niggles")

    [expose(html="turbogears.tests.simple")]
    def test_deprecated(self):
        return dict(title="Oldbar", mybool=False, someval="giggles")

    [expose()]
    def invalid(self):
        return None

    [expose()]
    def pos(self, posvalue):
        self.posvalue = posvalue
        return ""

    [expose()]
    def servefile(self, tg_exceptions=None):
        self.servedit = True
        self.serve_exceptions = tg_exceptions
        return cherrypy.lib.cptools.serveFile(
            pkg_resources.resource_filename(
                "turbogears.tests", "test_controllers.py"))

    [expose(content_type='text/plain')]
    def basestring(self):
        return 'hello world'

    [expose(content_type='text/plain')]
    def list(self):
        return ['hello', 'world']

    [expose(content_type='text/plain')]
    def generator(self):
        yield 'hello'
        yield 'world'

    [expose()]
    def unicode(self):
        cherrypy.response.headers["Content-Type"] = "text/html"
        return u'\u00bfHabla espa\u00f1ol?'

    [expose()]
    def returnedtemplate(self):
        return dict(title="Foobar", mybool=False, someval="foo",
            tg_template="turbogears.tests.simple")

    [expose()]
    def returnedtemplate_short(self):
        return dict(title="Foobar", mybool=False, someval="foo",
            tg_template="turbogears.tests.simple")

    [expose(template="turbogears.tests.simple")]
    def exposetemplate_short(self):
        return dict(title="Foobar", mybool=False, someval="foo")

    [expose()]
    [validate(validators={'value': validators.StringBoolean()})]
    def istrue(self, value):
        self.value = value
        return str(value)
    istrue = error_handler(validation_error_handler)(istrue)

    [expose()]
    [validate(validators={'value': validators.StringBoolean()})]
    def nestedcall(self, value):
        return self.istrue(str(value))

    [expose()]
    [validate(validators={'value': validators.StringBoolean()})]
    def errorchain(self, value):
        return "No Error"
    errorchain = error_handler(istrue)(errorchain)

    [expose(format="json", template="turbogears.tests.simple")]
    def returnjson(self):
        return dict(title="Foobar", mybool=False, someval="foo",
            tg_template="turbogears.tests.simple")

    [expose(template="turbogears.tests.simple", allow_json=False)]
    def allowjson(self):
        return dict(title="Foobar", mybool=False, someval="foo",
             tg_template="turbogears.tests.simple")

    [expose(format="json")]
    def impliedjson(self):
        return dict(title="Blah")

    [expose('json')]
    def explicitjson(self):
        return dict(title="Blub")

    [expose(template="turbogears.tests.simple", allow_json=True)]
    def jsonerror_handler(self):
        return dict(someval="errors")

    [expose(allow_json=True)]
    def jsonerror(self):
        raise ValueError
    jsonerror = exception_handler(jsonerror_handler)(jsonerror)

    [expose(content_type="xml/atom")]
    def contenttype(self):
        return "Foobar"

    [expose()]
    [validate(validators={
        "firstname": validators.String(min=2, not_empty=True),
        "lastname": validators.String()})]
    def save(self, submit, firstname, lastname="Miller"):
        self.submit = submit
        self.firstname = firstname
        self.lastname = lastname
        self.fullname = "%s %s" % (self.firstname, self.lastname)
        return self.fullname
    save = error_handler(validation_error_handler)(save)

    class Registration(formencode.Schema):
        allow_extra_fields = True
        firstname = validators.String(min=2, not_empty=True)
        lastname = validators.String()

    [expose()]
    [validate(validators=Registration())]
    def save2(self, submit, firstname, lastname="Miller"):
        return self.save(submit, firstname, lastname)
    save2 = error_handler(validation_error_handler)(save2)

    [expose(template="turbogears.tests.simple")]
    def useother(self):
        return dict(tg_template="turbogears.tests.othertemplate")

    [expose(template="cheetah:turbogears.tests.simplecheetah")]
    def usecheetah(self):
        return dict(someval="chimps")

    rwt_called = 0
    def rwt(self, func, *args, **kw):
        self.rwt_called += 1
        func(*args, **kw)

    [expose(template="turbogears.tests.simple", allow_json=True)]
    def flash_plain(self):
        flash("plain")
        return dict(title="Foobar", mybool=False, someval="niggles")

    [expose(template="turbogears.tests.simple", allow_json=True)]
    def flash_unicode(self):
        flash(u"\xfcnicode")
        return dict(title="Foobar", mybool=False, someval="niggles")

    [expose(template="turbogears.tests.simple", allow_json=True)]
    def flash_data_structure(self):
        flash(dict(uni=u"\xfcnicode", testing=[1, 2, 3]))
        return dict(title="Foobar", mybool=False, someval="niggles")

    [expose(template="turbogears.tests.simple", allow_json=True)]
    def flash_redirect(self):
        flash(u"redirect \xfcnicode")
        redirect("/flash_redirected?tg_format=json")

    [expose(template="turbogears.tests.simple", allow_json=True)]
    def flash_redirect_with_trouble_chars(self):
        flash(u"$foo, k\xe4se;\tbar!")
        redirect("/flash_redirected?tg_format=json")

    [expose(template="turbogears.tests.simple", allow_json=True)]
    def flash_redirected(self):
        return dict(title="Foobar", mybool=False, someval="niggles")

    def exc_h_value(self, tg_exceptions=None):
        """Exception handler for the ValueError in raise_value_exc"""
        return dict(handling_value=True, exception=str(tg_exceptions))

    [expose()]
    def raise_value_exc(self):
        raise ValueError('Some Error in the controller')
    raise_value_exc = exception_handler(exc_h_value,
        "isinstance(tg_exceptions, ValueError)")(raise_value_exc)

    def exc_h_key(self, tg_exceptions=None):
        """Exception handler for KeyErrors in  raise_all_exc"""
        return dict(handling_key=True, exception=str(tg_exceptions))

    def exc_h_index(self, tg_exceptions=None):
        """Exception handler for the ValueError in raise_value_exc"""
        return dict(handling_index=True, exception=str(tg_exceptions))

    [expose()]
    def raise_index_exc(self):
        raise IndexError('Some IndexError')
    raise_index_exc = exception_handler(exc_h_index,
        "isinstance(tg_exceptions, IndexError)")(raise_index_exc)

    [expose()]
    def raise_all_exc(self, num=2):
        num = int(num)
        if num < 2:
            raise ValueError('Inferior to 2')
        elif num == 2:
            raise IndexError('Equals to 2')
        elif num > 2:
            raise KeyError('No such number 2 in the integer range')
    raise_all_exc = exception_handler(exc_h_index,
        "isinstance(tg_exceptions, IndexError)")(raise_all_exc)
    raise_all_exc = exception_handler(exc_h_value,
        "isinstance(tg_exceptions, ValueError)")(raise_all_exc)
    raise_all_exc = exception_handler(exc_h_key,
        "isinstance(tg_exceptions, KeyError)")(raise_all_exc)

    [expose()]
    def internal_redirect(self, **kwargs):
        raise cherrypy.InternalRedirect('/internal_redirect_target')

    [expose()]
    def internal_redirect_target(self, **kwargs):
        return "redirected OK"

    [expose()]
    def redirect_to_path_str(self, path):
        raise redirect(path + '/index')

    [expose()]
    def redirect_to_path_list(self, path):
        raise redirect([path, 'index'])

    [expose()]
    def redirect_to_path_tuple(self, path):
        raise redirect((path, 'index'))

    [expose()]
    def redirect_error(self):
        raise redirect('/foo\r\n')