def test_kwarg_renderer(self): """ Renderers passed into the constructor override renderers defined on the class. """ view = BasicView(html=lambda m: "inherited") result = view.render({}, 'text/html') self.assertEquals(result['body'], "inherited")
def setUp(self): initialize() self.manifest = Manifest({ '': Program(model=[none], view=BasicView()), 'named': Program(model=[defaults], view=BasicView()), "another": { '': Program(name='another root'), 'name': Program(name='another name', view=BasicView()) } })
def setUp(self): initialize() self.manifest = Manifest({ 'no_defaults': Program(model=[no_defaults], view=BasicView()), 'defaults': Program(model=[defaults], view=BasicView()), 'primitives': Program(model=[primitive], view=BasicView()), 'raw': Program(model=[raw], view=BasicView()), 'none': Program(model=[none], view=BasicView), 'order': Program(model=[order], view=BasicView) })
class RendererTests(unittest.TestCase): giotto_view = GiottoView() basic_view = BasicView() def test_mising_renderer(self): """ Exception is raises when you try to render mimetype that is not supported by the view class """ assert self.giotto_view.can_render('text/html') == False self.assertRaises(NoViewMethod, lambda: self.giotto_view.render({}, 'text/html')) def test_render_defined_mimetype(self): assert self.basic_view.can_render('text/html') == True result = self.basic_view.render({}, 'text/html') assert 'body' in result def test_kwarg_renderer(self): """ Renderers passed into the constructor override renderers defined on the class. """ view = BasicView(html=lambda m: "inherited") result = view.render({}, 'text/html') self.assertEquals(result['body'], "inherited") def test_redirection_lambda(self): view = BasicView(html=lambda m: Redirection(m)) result = view.render('res', 'text/html') self.assertEquals(type(result['body']), Redirection) self.assertEquals(result['body'].path, 'res') def test_redirection(self): view = BasicView(html=Redirection('/')) result = view.render({}, 'text/html') self.assertEquals(type(result['body']), Redirection) self.assertEquals(result['body'].path, '/') def test_subclass_renderer(self): """ A Renderer that is defined on a class takes precidence over the renderer defined in a base class. Regardless of the name of the render method function. """ class InheritedBasicView1(BasicView): @renders('text/html') def a(self, result, errors): # show up earlier than 'generic_html' in dir() return 'inherited' class InheritedBasicView2(BasicView): @renders('text/html') def zzzzzzz(self, result, errors): # show up later than 'generic_html' in dir() return 'inherited' for view in [InheritedBasicView2(), InheritedBasicView1()]: result = view.render({}, 'text/html') self.assertEquals(result['body'], "inherited")
def test_objects_txt(self): blogs = [ Blog(title="title", body="This blog body"), Blog(title="title2", body="blog body two") ] result = BasicView().render(blogs, 'text/plain')['body'] assert 'blog body two' in result assert 'This blog body' in result
def test_objects(self): blogs = [ Blog(title="title", body="This blog body"), Blog(title="title2", body="blog body two") ] result = BasicView().render(blogs, 'text/html')['body'] assert 'blog body two' in result assert 'This blog body' in result assert '<!DOCTYPE html>' in result
def test_ignore_cls(self): """ If first argument is nammed 'cls', ignore that argument (to allow classmethods) """ def test(cls, a, b, c="what"): pass program = Program(model=[test], view=BasicView()) ret = program.get_model_args_kwargs() self.assertEquals((['a', 'b'], {'c': "what"}), ret)
def test_empty(self): def test(): pass program = Program(model=[test], view=BasicView()) ret = program.get_model_args_kwargs() self.assertEquals(([], {}), ret)
def test_list_txt(self): result = BasicView().render(['one', 'two'], 'text/plain')['body'] assert 'two' in result assert 'one' in result
def test_redirection(self): view = BasicView(html=Redirection('/')) result = view.render({}, 'text/html') self.assertEquals(type(result['body']), Redirection) self.assertEquals(result['body'].path, '/')
def test_get_args_kwargs(self): def test(a, b, c="what"): pass program = Program(model=[test], view=BasicView()) ret = program.get_model_args_kwargs() self.assertEquals((['a', 'b'], {'c': "what"}), ret)
from giotto.programs import Program, Manifest from giotto.programs.shell import shell from giotto.programs.tables import syncdb, flush from giotto.views import BasicView management_manifest = Manifest({ 'syncdb': Program(name="Make Tables", controllers=['cmd'], model=[syncdb], view=BasicView()), 'flush': Program( name="Blast Tables", controllers=['cmd'], model=[flush], view=BasicView(), ), 'shell': Program( name="Giotto Shell", controllers=['cmd'], model=[shell], view=BasicView(), ), })
def test_redirection_lambda(self): view = BasicView(html=lambda m: Redirection(m)) result = view.render('res', 'text/html') self.assertEquals(type(result['body']), Redirection) self.assertEquals(result['body'].path, 'res')
from giotto.contrib.static.programs import StaticServe from giotto.programs import Program, Manifest from giotto.programs.management import management_manifest from giotto.views import BasicView, jinja_template from models import show_listings, CrawlResult, stats manifest = Manifest({ '': '/stats', 'stats': Program(model=[stats], view=BasicView(html=jinja_template('landing.html'))), 'api_docs': Program(view=BasicView(html=jinja_template('api.html'))), 'listings': Program(model=[show_listings], view=BasicView(html=jinja_template("show_listings.html"))), 'crawl': Program( controllers=['cmd'], model=[CrawlResult.do_crawl], ), 'static': StaticServe('/static'), 'mgt': management_manifest, })
def create_auth_manifest(**kwargs): """ Creates a basic authentication manifest for logging in, logging out and registering new accounts. """ class AuthProgram(Program): pre_input_middleware = [AuthenticationMiddleware] def register(username, password, password2): """ Decorated version of basic_register with a callback added. """ result = basic_register(username, password, password2) callback = kwargs.get('post_register_callback', None) if callback: user = User.objects.get(username=username) callback(user) return result return Manifest({ 'login': [ AuthProgram( """ Prints out the HTML form for logging in. """, name="Login (form)", input_middleware=[NotAuthenticatedOrRedirect('/')], view=BasicView( html=jinja_template('login.html'), ), ), AuthProgram( """ Matches up the username/password against the database, and adds the auth cookies. """, name="Login (post)", input_middleware=[NotAuthenticatedOrDie], controllers=['http-post', 'cmd'], model=[create_session, {'username': '******', 'session_key': 'XXXXXXXXXXXXXXX'}], view=BasicView( persist=lambda m: {'giotto_session': m['session_key']}, html=lambda m: Redirection('/'), ), ), ], 'logout': AuthProgram( """ Send the user here to log them out. Removes their cookies and deletes the auth session. """, name="Logout", view=BasicView( html=Redirection('/'), ), output_middleware=[LogoutMiddleware], ), 'register': [ AuthProgram( """ This program returns the HTML page with the form for registering a new account. HTTP-get only. """, name="Register (form)", input_middleware=[NotAuthenticatedOrRedirect('/')], view=BasicView( html=jinja_template('register.html'), ), ), AuthProgram( """ When you POST the register form, this program handles creating the new user, then redirecting you to '/' """, name="Register (post)", controllers=['http-post'], model=[register], view=BasicView( persist=lambda m: {'giotto_session': m['session_key']}, html=lambda m: Redirection('/'), ), ), ], })
def test_dict_txt(self): result = BasicView().render({'one': 'two'}, 'text/plain')['body'] assert 'one - two' in result
def test_string(self): result = BasicView().render("just a simple string", 'text/html')['body'] assert "just a simple string" in result
def test_nonetype(self): result = BasicView().render(None, 'text/html')['body'] assert "None" in result
def test_no_model(self): program = Program(view=BasicView()) ret = program.get_model_args_kwargs() self.assertEquals(([], {}), ret)
def test_list_html(self): result = BasicView().render(['one', 'two'], 'text/html')['body'] assert '<html>' in result assert 'one' in result
def test_preserve_order(self): def test(a=1, b=2, c=3, d=4): pass program = Program(model=[test], view=BasicView()) a, kw = program.get_model_args_kwargs() self.assertEquals(list(kw.keys()), ['a', 'b', 'c', 'd'])
def test_dict_html(self): result = BasicView().render({'one': 'two'}, 'text/html')['body'] assert '<html>' in result assert 'one' in result