def view(self, view, status=200): """Set a string or view to be returned. Arguments: view {string|dict|list|masonite.view.View} -- Some data type that is an appropriate response. Keyword Arguments: status {int} -- The Response status code. (default: {200}) Raises: ResponseError -- If a data type that is not an acceptable response type is returned. Returns: string|dict|list -- Returns the data to be returned. """ self.request.status(status) if isinstance(view, dict) or isinstance(view, list): return self.json(view) elif isinstance(view, int): view = str(view) elif isinstance(view, View): view = view.rendered_template elif isinstance(view, self.request.__class__): view = self.data() elif view is None: raise ResponseError('Responses cannot be of type: None.') self.app.bind('Response', view) self.make_headers() return self.data()
def boot(self, Request, Response, Headers): if not Request.redirect_url: # Convert the data that is retrieved above to bytes # so the wsgi server can handle it. try: data = bytes(Response, 'utf-8') except TypeError: raise ResponseError( 'An acceptable response type was not returned') self.app.bind('StatusCode', Request.get_status_code()) Headers += [("Content-Length", str(len(data))) ] + Request.get_cookies() + Request.get_headers() else: self.app.bind('StatusCode', "302 OK") self.app.bind('Headers', [('Location', Request.redirect_url)] + Request.get_cookies()) Request.reset_redirections() self.app.bind('Response', 'redirecting ...') Request.url_params = {} Request.reset_headers() Request.cookies = [] if self.app.has('Session') and self.app.make('StatusCode') == '200 OK': self.app.make('Session').reset(flash_only=True)
def view(self, view, status=200): """Set a string or view to be returned. Arguments: view {string|dict|list|masonite.view.View} -- Some data type that is an appropriate response. Keyword Arguments: status {int} -- The Response status code. (default: {200}) Raises: ResponseError -- If a data type that is not an acceptable response type is returned. Returns: string|dict|list -- Returns the data to be returned. """ if not self.request.get_status(): self.request.status(status) if isinstance(view, (dict, list)): return self.json(view, status=self.request.get_status()) elif isinstance(view, (Collection, Model)): return self.json(view.serialize(), status=self.request.get_status()) elif isinstance(view, int): view = str(view) elif isinstance(view, View): view = view.rendered_template elif isinstance(view, self.request.__class__): view = self.data() if isinstance(view, (Paginator, LengthAwarePaginator)): return self.paginated_json(view, status=self.request.get_status()) elif view is None: raise ResponseError('Responses cannot be of type: None.') if not isinstance(view, str): raise ResponseError('Invalid response type of {}'.format( type(view))) self.app.bind('Response', view) self.make_headers() return self.data()