def redirect_to(cls, *args, **params): if len(args) == 0: url = url_for(**params) elif len(args) == 1: url = args[0] else: raise TypeError("redirect_to() takes at most 1 positional argument (%s given)" % len(args)) rparams = {} rparams['code'] = params.get('code', 303) resp = WSGIResponse(code=rparams['code'], content="You are now being redirected to <a href=\"%s\">%s</a>. Do not be alarmed." % (url, url)) resp.headers['Location'] = url return resp
def oc_json_response(obj, status=''): #x-deliverance-no-theme if not 'oc-statusMessage-container' in obj: if status: obj['oc-statusMessage-container'] = {'action': 'replace', 'html': '\n <div id="oc-statusMessage-container"><div class="oc-statusMessage oc-js-closeable">%s</div></div>\n\n' % status, 'effects': 'blink'} else: obj['oc-statusMessage-container'] = {'action': 'replace', 'html': '\n <div id="oc-statusMessage-container"> </div>\n\n', 'effects': 'blink'} response = WSGIResponse() response.write("<html><body>%s</body></html>" % escape(dumps(obj))) response.headers['X-Deliverance-No-Theme'] = '1' return response
def do_login(self): """ Set the appropriate cookies to log a user in. """ username = self.form_result['username'] # try: # user = User.byUsername(username) # except SQLObjectNotFound: # user = None # if not user or not user.check_password(request.params['password']) or not user.confirmed: cookie = confirm_password(username, self.form_result['password']) if not cookie: c.status_message.append ('Please check your username and password.' ' If you still have trouble, you can <a ' 'href="forgot">retrieve your sign in ' 'information</a>.') c.came_from = request.params.get('came_from', '') return render('user/show_login.mako') # session = environ['beaker.session'] # now = time.time() # if not self.form_result['no_expire_cookie']: # session['expiration'] = now + (60 * 60 * 8) # session['username'] = username # session.save() response = WSGIResponse(code=303) came_from = self.form_result.get('came_from') if came_from: came_from += "?portal_status_message=Welcome!+You+have+signed+in." response.headers['Location'] = str(came_from) else: #FIXME: do we need to do something special to send #the user to the tour page on their first login? script_name = request.environ['SCRIPT_NAME'] request.environ['SCRIPT_NAME'] = '' response.headers['Location'] = h.url_for(str("/people/%s/account?portal_status_message=Welcome!+You+have+signed+in." % username)) request.environ['SCRIPT_NAME'] = script_name value = cookie.split("=")[1][1:-1] #strip quotes response.set_cookie("__ac", value) return response
def _dispatch_call(self): """Handles dispatching the request to the function using Routes""" log_debug = self._pylons_log_debug req = pylons.request._current_obj() action = req.environ['pylons.routes_dict'].get('action') action_method = action.replace('-', '_') if log_debug: log.debug("Looking for %r method to handle the request", action_method) try: func = getattr(self, action_method, None) except UnicodeEncodeError: func = None if isinstance(func, types.MethodType): # Store function used to handle request req.environ['pylons.action_method'] = func response = self._inspect_call(func) else: if log_debug: log.debug("Couldn't find %r method to handle response", action) if pylons.config['debug']: raise NotImplementedError('Action %r is not implemented' % action) else: response = WSGIResponse(code=404) return response
def test_call_wsgiresponse(): resp = WSGIResponse(b'some content', 'application/octet-stream') def sp(status, response_headers): assert status == '200 OK' assert sorted(response_headers) == [ ('cache-control', 'no-cache'), ('content-type', 'application/octet-stream'), ] assert resp({}, sp) == [b'some content'] f = io.BytesIO(b'some content') resp = WSGIResponse(f, 'application/octet-stream') assert list(resp({}, sp)) == [b'some content'] f = io.BytesIO() resp = WSGIResponse(f, 'application/octet-stream') assert resp({'wsgi.file_wrapper': lambda x: x}, sp) is f
def _dispatch_call(self): """Dispatch the call to the function chosen by __call__""" raw_response = self._inspect_call(self._func) if not isinstance(raw_response, xmlrpclib.Fault): raw_response = (raw_response, ) response = xmlrpclib.dumps(raw_response, methodresponse=True, allow_none=self.allow_none) return WSGIResponse(response)
def __call__(self, environ, start_response): registry = environ['paste.registry'] environ_config = environ.setdefault('pylons.environ_config', {}) if self.setup_cache: registry.register(pylons.cache, environ['beaker.cache']) environ_config['cache'] = 'beaker.cache' if self.setup_session: registry.register(pylons.session, environ['beaker.session']) environ_config['session'] = 'beaker.session' if self.setup_g: registry.register(pylons.g, self.g) # Update the environ environ.update(self.environ) registry.register(pylons.request, WSGIRequest(environ)) registry.register(pylons.response, WSGIResponse()) return self.app(environ, start_response)
def __call__(self, *args, **kargs): """Makes our controller a callable to handle requests This is called when dispatched to as the Controller class docs explain more fully. """ req = pylons.request._current_obj() # Keep private methods private if req.environ['pylons.routes_dict'].get('action', '').startswith('_'): return WSGIResponse(code=404) if hasattr(self, '__before__'): self._inspect_call(self.__before__, **kargs) response = self._dispatch_call() # If its not a WSGI response, and we have content, it needs to # be wrapped in the response object if hasattr(response, 'wsgi_response'): # It's either a legacy WSGIResponse object, or an exception # that got tossed. Strip headers if its anything other than a # 2XX status code, and strip cookies if its anything other than # a 2XX or 3XX status code. if response.status_code < 300: response.headers.update(pylons.response.headers) if response.status_code < 400: for c in pylons.response.cookies.values(): response.headers.add('Set-Cookie', c.output(header='')) registry = req.environ['paste.registry'] registry.replace(pylons.response, response) elif isinstance(response, types.GeneratorType): pylons.response.content = response elif isinstance(response, basestring): pylons.response.write(response) response = pylons.response._current_obj() if hasattr(self, '__after__'): self._inspect_call(self.__after__) return response
def setup_app_env(self, environ, start_response): """Setup and register all the Pylons objects with the registry""" if self.log_debug: log.debug("Setting up Pylons stacked object globals") registry = environ['paste.registry'] registry.register(WSGIRequest.defaults, self.request_options) registry.register(WSGIResponse.defaults, self.response_options) req = WSGIRequest(environ) # Setup the basic pylons global objects registry.register(pylons.request, req) registry.register(pylons.response, WSGIResponse()) registry.register(pylons.buffet, self.buffet) registry.register(pylons.g, self.globals) registry.register(pylons.config, self.config) registry.register(pylons.h, self.helpers or \ pylons.legacy.load_h(self.package_name)) # Setup the translator global object registry.register(pylons.translator, gettext.NullTranslations()) lang = self.config.get('lang') if lang: set_lang(lang) if self.config['pylons.strict_c']: registry.register(pylons.c, ContextObj()) else: registry.register(pylons.c, AttribSafeContextObj()) econf = environ['pylons.environ_config'] if econf.get('session'): registry.register(pylons.session, environ[econf['session']]) if econf.get('cache'): registry.register(pylons.cache, environ[econf['cache']])
def test_wsgiresponse_charset(): response = WSGIResponse(mimetype='text/html; charset=UTF-8') assert response.content_type == 'text/html' assert response.charset == 'UTF-8' response.write(u'test') response.write(u'test2') response.write('test3') status, headers, content = response.wsgi_response() for data in content: assert isinstance(data, str) WSGIResponse.defaults._push_object( dict(content_type='text/html', charset='iso-8859-1')) try: response = WSGIResponse() response.write(u'test') response.write(u'test2') response.write('test3') status, headers, content = response.wsgi_response() for data in content: assert isinstance(data, str) finally: WSGIResponse.defaults._pop_object() # WSGIResponse will allow unicode to pass through when no charset is # set WSGIResponse.defaults._push_object( dict(content_type='text/html', charset=None)) try: response = WSGIResponse(u'test') response.write(u'test1') status, headers, content = response.wsgi_response() for data in content: assert isinstance(data, unicode) finally: WSGIResponse.defaults._pop_object() WSGIResponse.defaults._push_object( dict(content_type='text/html', charset='')) try: response = WSGIResponse(u'test') response.write(u'test1') status, headers, content = response.wsgi_response() for data in content: assert isinstance(data, unicode) finally: WSGIResponse.defaults._pop_object()
def test_wsgiresponse_charset(): response = WSGIResponse(mimetype='text/html; charset=UTF-8') assert response.content_type == 'text/html' assert response.charset == 'UTF-8' response.write(u'test') response.write(u'test2') response.write('test3') status, headers, content = response.wsgi_response() for data in content: assert isinstance(data, str) WSGIResponse.defaults._push_object(dict(content_type='text/html', charset='iso-8859-1')) try: response = WSGIResponse() response.write(u'test') response.write(u'test2') response.write('test3') status, headers, content = response.wsgi_response() for data in content: assert isinstance(data, str) finally: WSGIResponse.defaults._pop_object() # WSGIResponse will allow unicode to pass through when no charset is # set WSGIResponse.defaults._push_object(dict(content_type='text/html', charset=None)) try: response = WSGIResponse(u'test') response.write(u'test1') status, headers, content = response.wsgi_response() for data in content: assert isinstance(data, unicode) finally: WSGIResponse.defaults._pop_object() WSGIResponse.defaults._push_object(dict(content_type='text/html', charset='')) try: response = WSGIResponse(u'test') response.write(u'test1') status, headers, content = response.wsgi_response() for data in content: assert isinstance(data, unicode) finally: WSGIResponse.defaults._pop_object()
def __call__(self, environ, start_response): log_debug = self._pylons_log_debug # Keep private methods private if environ['pylons.routes_dict'].get('action', '')[:1] in ('_', '-'): if log_debug: log.debug("Action starts with _, private action not allowed. " "Returning a 404 response") return WSGIResponse(code=404)(environ, start_response) start_response_called = [] def repl_start_response(status, headers, exc_info=None): response = pylons.response._current_obj() start_response_called.append(None) # Copy the headers from the global response # XXX: TODO: This should really be done with a more efficient # header merging function at some point. if log_debug: log.debug( "Merging pylons.response headers into " "start_response call, status: %s", status) response.headers.update(HeaderDict.fromlist(headers)) headers = response.headers.headeritems() for c in pylons.response.cookies.values(): headers.append(('Set-Cookie', c.output(header=''))) return start_response(status, headers, exc_info) self.start_response = repl_start_response if hasattr(self, '__before__'): response = self._inspect_call(self.__before__) if hasattr(response, '_exception'): return response(environ, self.start_response) response = self._dispatch_call() if not start_response_called: # If its not a WSGI response, and we have content, it needs to # be wrapped in the response object if hasattr(response, 'wsgi_response'): # It's either a legacy WSGIResponse object, or an exception # that got tossed. if log_debug: log.debug("Controller returned a Response object, merging " "it with pylons.response") response.headers.update(pylons.response.headers) for c in pylons.response.cookies.values(): response.headers.add('Set-Cookie', c.output(header='')) registry = environ['paste.registry'] registry.replace(pylons.response, response) elif isinstance(response, types.GeneratorType): if log_debug: log.debug("Controller returned a generator, setting it as " "the pylons.response content") pylons.response.content = response elif response is None: if log_debug: log.debug("Controller returned None") else: if log_debug: log.debug("Assuming controller returned a basestring or " "buffer, writing it to pylons.response") pylons.response.write(response) response = pylons.response._current_obj() if hasattr(self, '__after__'): after = self._inspect_call(self.__after__) if hasattr(after, '_exception'): return after(environ, self.start_response) if hasattr(response, 'wsgi_response'): # Copy the response object into the testing vars if we're testing if 'paste.testing_variables' in environ: environ['paste.testing_variables']['response'] = response if log_debug: log.debug("Calling Response object to return WSGI data") return response(environ, self.start_response) if log_debug: log.debug( "Response assumed to be WSGI content, returning un-touched") return response
def xmlrpc_fault(code, message): """Convienence method to return a Pylons response XMLRPC Fault""" fault = xmlrpclib.Fault(code, message) return WSGIResponse(xmlrpclib.dumps(fault, methodresponse=True))
def __init__(self, *args, **kwargs): warnings.warn(response_warning, PendingDeprecationWarning, 2) WSGIResponse.__init__(self, *args, **kwargs)
def response(self, environ): from paste.wsgiwrappers import WSGIResponse headers, content = self.prepare_content(environ) resp = WSGIResponse(code=self.code, content=content) resp.headers = resp.headers.fromlist(headers) return resp
et.SubElement (bo, 'hr') fo = et.SubElement (bo, 'form', method='post') kwargs = {} if feed_id != None: kwargs['feed_id'] = feed_id fo.set ('action', web.url_for_view ('mark_read', **kwargs)) p = et.SubElement (fo, 'p') for e, d in q: ids = et.SubElement (p, 'input') ids.set ('name', 'ids') ids.set ('type', 'hidden') ids.set ('value', str (e.id)) sb = et.SubElement (p, 'button') sb.set ('type', 'submit') sb.text = 'Mark all read' r = WSGIResponse () r.headers['content-type'] = 'application/xhtml+xml; charset=utf-8' print >> r, '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">' t = et.ElementTree (ht) t.write (r, 'utf-8') return r # vim: noet sts=0
def __call__(self): return WSGIResponse.__call__(self, self.environ, self.start_response)
def list_broken_feeds (session, request): ht = et.Element ('html', xmlns = XHTML_NAMESPACE) he = et.SubElement (ht, 'head') t = et.SubElement (he, 'title') t.text = 'rssr: broken feeds' bo = et.SubElement (ht, 'body') h1 = et.SubElement (bo, 'h1') h1.text = 'broken feeds' p = et.SubElement (bo, 'p') a = et.SubElement (p, 'a') a.text = '(view entries)' a.set ('href', web.url_for_view ('view_feed')) t = et.SubElement (bo, 'table') tr = et.SubElement (t, 'tr') th = et.SubElement (tr, 'th') th.text = 'feed' th.set ('colspan', '2') th = et.SubElement (tr, 'th') th.text = 'error' th = et.SubElement (tr, 'th') th.text = 'since' th = et.SubElement (tr, 'th') th.text = 'entries' # sqlalchemy won't do DISTINCT ON :( # SELECT distinct on (feeds.id) feeds.id, feeds.error, coalesce(entries.updated, entries.published, entries.created, entries.inserted) AS coalesce_1 # FROM feeds LEFT OUTER JOIN entries ON feeds.id = entries.feed_id # WHERE feeds.error IS NOT NULL ORDER BY feeds.id, coalesce_1 desc #date_clause = sql.func.coalesce (db.Entry.updated, db.Entry.published, db.Entry.created, db.Entry.inserted) #r = WSGIResponse () #r.headers['content-type'] = 'text/plain' #print >> r, session.query (db.Feed).outerjoin ('entries').filter (db.Feed.error != None).add_column (date_clause).order_by (date_clause.desc ()).distinct (db.Feed.id) #return r for f in session.query (db.Feed).filter (db.Feed.error != None).outerjoin ('entries'): tr = et.SubElement (t, 'tr') td = et.SubElement (tr, 'td') a = et.SubElement (td, 'a') a.text = str (f.id) a.set ('href', f.href) e = et.SubElement (tr, 'td') if f.link: e = et.SubElement (e, 'a') e.set ('href', f.link) e.text = f.title.as_text () td = et.SubElement (tr, 'td') td.text = f.error td = et.SubElement (tr, 'td') date_clause = sql.func.coalesce (db.Entry.updated, db.Entry.published, db.Entry.created, db.Entry.inserted) since = f.entries.add_column (date_clause).filter (date_clause != None).order_by (date_clause.desc ()).limit (1).first () td.text = since[1].replace (tzinfo = pytz.utc).astimezone (tz).strftime ('%Y-%m-%d\xc2\xa0%H:%M\xc2\xa0%Z\xc2\xa0(%z)').decode ('utf-8') if since else '?' # XXX: sqlalchemy can't count the number of entries for each field! # SELECT count(entries.id) FROM feeds LEFT OUTER JOIN entries ON feeds.id = entries.feed_id WHERE feeds.error IS NOT NULL GROUP BY feeds.id, feeds.error; e = et.SubElement (tr, 'td') c = f.entries.count () if c > 0: e = et.SubElement (e, 'a') url = list (urlparse.urlsplit (web.url_for_view ('view_feed', feed_id = f.id))) url[3] = urllib.urlencode ({'show_all': 'yes'}) e.set ('href', urlparse.urlunsplit (url)) e.text = str (c) r = WSGIResponse () r.headers['content-type'] = 'application/xhtml+xml; charset=utf-8' print >> r, '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">' t = et.ElementTree (ht) t.write (r, 'utf-8') return r
def __init__(self, environ, start_response, *args, **kwargs): self.environ = environ self.start_response = start_response WSGIResponse.__init__(self, *args, **kwargs)