def store(self, view: View, request: Request, auth: Auth, response: Response): login = auth.attempt(request.input("username"), request.input("password")) if login: return response.redirect(name="auth.home") # Go back to login page return response.redirect(name="login").with_errors( ["The email or password is incorrect"])
def update(self, request: Request, response: Response): channel = -1 level = -1 try: print(f' channel 0: {request.all()["0"]}') channel = 0 level = int(request.all()["0"]) except KeyError: try: print(f' channel 1: {request.all()["1"]}') channel = 1 level = int(request.all()["1"]) except KeyError: print('channel not found') print(f' setting channel: {channel} to value: {level}') try: set_lights(channel, level) message = 'OK' except Exception: message = 'connection error' # return request.all() return response.redirect('/')
def store(self, auth: Auth, request: Request, response: Response): # store register user errors = request.validate({ "email": "required", "name": "required", "password": "******", }) if errors: return response.back().with_errors(errors) user = auth.register(request.only("name", "email", "password")) if not user: return response.redirect("/register") return response.redirect("/home")
class TestResponse: def setup_method(self): self.app = App() self.request = Request(generate_wsgi()).load_app(self.app) self.app.bind('Request', self.request) self.response = Response(self.app) def test_can_set_json(self): self.response.json({'test': 'value'}) assert self.request.is_status(200) assert self.request.header('Content-Length') == '17' assert self.request.header( 'Content-Type') == 'application/json; charset=utf-8' def test_redirect(self): self.response.redirect('/some/test') assert self.request.is_status(302) assert self.request.header('Location', '/some/test') def test_response_does_not_override_header_from_controller(self): self.response.view(self.app.resolve(ControllerTest().change_header)) assert self.request.header('Content-Type') == 'application/xml' def test_view(self): view = View(self.app).render('test', {'test': 'test'}) self.response.view(view) assert self.app.make('Response') == 'test' assert self.request.is_status(200) self.response.view('foobar') assert self.app.make('Response') == 'foobar' def test_view_can_return_integer_as_string(self): self.response.view(1) assert self.app.make('Response') == '1' assert self.request.is_status(200)
def redirect(self, response: Response): """ Redirect to Google """ if self.scopes: scopes = ' '.join(self.scopes) else: scopes = '' params = dict( scope=scopes, access_type='offline', redirect_uri=self._config['redirect'], response_type='code', client_id=self._config['client'] ) endpoint = f'{self.authorization_url}?{urllib.parse.urlencode(params)}' return response.redirect(endpoint)
def redirect(self, response: Response): return response.redirect('/some/test')
class TestResponse(unittest.TestCase): def setUp(self): self.app = App() self.request = Request(generate_wsgi()).load_app(self.app) self.app.bind('Request', self.request) self.app.bind('StatusCode', None) self.response = Response(self.app) self.app.bind('Response', self.response) def test_can_set_json(self): self.response.json({'test': 'value'}) self.assertTrue(self.request.is_status(200)) self.assertEqual(self.request.header('Content-Length'), '17') self.assertEqual(self.request.header('Content-Type'), 'application/json; charset=utf-8') def test_redirect(self): self.response.redirect('/some/test') self.request.header('Location', '/some/test') self.assertTrue(self.request.is_status(302)) self.assertEqual(self.request.header('Location'), '/some/test') def test_response_does_not_override_header_from_controller(self): self.response.view(self.app.resolve(ControllerTest().change_header)) self.assertEqual(self.request.header('Content-Type'), 'application/xml') def test_view(self): view = View(self.app).render('test', {'test': 'test'}) self.response.view(view) self.assertEqual(self.app.make('Response'), 'test') self.assertTrue(self.request.is_status(200)) self.response.view('foobar') self.assertEqual(self.app.make('Response'), 'foobar') def test_view_can_return_integer_as_string(self): self.response.view(1) self.assertEqual(self.app.make('Response'), '1') self.assertTrue(self.request.is_status(200)) def test_view_can_set_own_status_code_to_404(self): self.response.view(self.app.resolve(ControllerTest().change_404)) self.assertTrue(self.request.is_status(404)) def test_view_can_set_own_status_code(self): self.response.view(self.app.resolve(ControllerTest().change_status)) self.assertTrue(self.request.is_status(203)) def test_view_should_return_a_json_response_when_retrieve_a_user_from_model( self): self.assertIsInstance(MockUser(), Model) self.response.view(MockUser().all()) self.assertIn('"name": "TestUser"', self.app.make('Response')) self.assertIn('"email": "*****@*****.**"', self.app.make('Response')) self.response.view(MockUser().find(1)) self.assertIn('"name": "TestUser"', self.app.make('Response')) self.assertIn('"email": "*****@*****.**"', self.app.make('Response'))
class TestResponse: def setup_method(self): self.app = App() self.request = Request(generate_wsgi()).load_app(self.app) self.app.bind('Request', self.request) self.app.bind('StatusCode', None) self.response = Response(self.app) self.app.bind('Response', self.response) def test_can_set_json(self): self.response.json({'test': 'value'}) assert self.request.is_status(200) assert self.request.header('Content-Length') == '17' assert self.request.header( 'Content-Type') == 'application/json; charset=utf-8' def test_redirect(self): self.response.redirect('/some/test') assert self.request.is_status(302) assert self.request.header('Location', '/some/test') def test_response_does_not_override_header_from_controller(self): self.response.view(self.app.resolve(ControllerTest().change_header)) assert self.request.header('Content-Type') == 'application/xml' def test_view(self): view = View(self.app).render('test', {'test': 'test'}) self.response.view(view) assert self.app.make('Response') == 'test' assert self.request.is_status(200) self.response.view('foobar') assert self.app.make('Response') == 'foobar' def test_view_can_return_integer_as_string(self): self.response.view(1) assert self.app.make('Response') == '1' assert self.request.is_status(200) def test_view_can_set_own_status_code_to_404(self): self.response.view(self.app.resolve(ControllerTest().change_404)) assert self.request.is_status(404) def test_view_can_set_own_status_code(self): self.response.view(self.app.resolve(ControllerTest().change_status)) assert self.request.is_status(203) def test_view_should_return_a_json_response_when_retrieve_a_user_from_model( self): assert isinstance(MockUser(), Model) self.response.view(MockUser().all()) json_response = '[{"name": "TestUser", "email": "*****@*****.**"}, {"name": "TestUser", "email": "*****@*****.**"}]' assert self.app.make('Response') == json_response self.response.view(MockUser().find(1)) json_response = '{"name": "TestUser", "email": "*****@*****.**"}' assert self.app.make('Response') == json_response