Пример #1
0
 def test_dot_value(self):
     class Model(object):
         id = 'id'
         title = 'title'
     model = Model()
     r = Redirection('prog', args=[M.id, M.title])
     r.render(model, 'some/mimetype', errors=None)
     self.assertEquals(r.rendered_invocation, ('prog', ['id', 'title'], {}))
Пример #2
0
 def html(self, result):
     ty = type(http_redirect)
     if ty == list or ty == tuple:
         assert len(
             http_redirect) == 3, "http_redirect must be three items"
         return Redirection(http_redirect[0],
                            args=http_redirect[1],
                            kwargs=http_redirect[2])
     else:
         # assume a string was passed in.
         return Redirection(http_redirect)
Пример #3
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('/'),
                ),
            ),
        ],
    })
Пример #4
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, '/')
Пример #5
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')
Пример #6
0
 def test_kwargs(self):
     model = {'title': 'title', 'id': 'id'} 
     r = Redirection('prog', kwargs={'id': M['id'], 'title': M['title']})
     r.render(model, 'some/mimetype', errors=None)
     self.assertEquals(r.rendered_invocation, ('prog', [], {'id': 'id', 'title': 'title'}))
Пример #7
0
 def test_brackets(self):
     model = {'title': 'title', 'id': 'id'} 
     r = Redirection('prog', args=[M['id'], M['title']])
     r.render(model, 'some/mimetype', errors=None)
     self.assertEquals(r.rendered_invocation, ('prog', ['id', 'title'], {}))
Пример #8
0
 def cmd(self, request):
     if request.user:
         return request
     return Redirection(invocation)
Пример #9
0
 def http(self, request):
     if not request.user:
         return request
     return Redirection(invocation)
Пример #10
0
 def text_html(self, data):
     return Redirection(data)