Example #1
0
 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")
Example #2
0
 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")
Example #3
0
 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())
         }
     })
Example #4
0
 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)
     })
Example #5
0
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")
Example #6
0
 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
Example #7
0
 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
Example #8
0
 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)
Example #9
0
 def test_empty(self):
     def test(): pass
     program = Program(model=[test], view=BasicView())
     ret = program.get_model_args_kwargs()
     self.assertEquals(([], {}), ret)
Example #10
0
 def test_list_txt(self):
     result = BasicView().render(['one', 'two'], 'text/plain')['body']
     assert 'two' in result
     assert 'one' in result
Example #11
0
 def test_redirection(self):
     view = BasicView(html=Redirection('/'))
     result = view.render({}, 'text/html')
     self.assertEquals(type(result['body']), Redirection)
     self.assertEquals(result['body'].path, '/')
Example #12
0
 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)
Example #13
0
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(),
    ),
})
Example #14
0
 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')
Example #15
0
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,
})
Example #16
0
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('/'),
                ),
            ),
        ],
    })
Example #17
0
 def test_dict_txt(self):
     result = BasicView().render({'one': 'two'}, 'text/plain')['body']
     assert 'one - two' in result
Example #18
0
 def test_string(self):
     result = BasicView().render("just a simple string",
                                 'text/html')['body']
     assert "just a simple string" in result
Example #19
0
 def test_nonetype(self):
     result = BasicView().render(None, 'text/html')['body']
     assert "None" in result
Example #20
0
 def test_no_model(self):
     program = Program(view=BasicView())
     ret = program.get_model_args_kwargs()
     self.assertEquals(([], {}), ret)
Example #21
0
 def test_list_html(self):
     result = BasicView().render(['one', 'two'], 'text/html')['body']
     assert '<html>' in result
     assert 'one' in result
Example #22
0
 def test_redirection(self):
     view = BasicView(html=Redirection('/'))
     result = view.render({}, 'text/html')
     self.assertEquals(type(result['body']), Redirection)
     self.assertEquals(result['body'].path, '/')
Example #23
0
 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')
Example #24
0
 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'])
Example #25
0
 def test_dict_html(self):
     result = BasicView().render({'one': 'two'}, 'text/html')['body']
     assert '<html>' in result
     assert 'one' in result