def templated_wrapper(request, *args, **kwargs): context = func(request, *args, **kwargs) or {} # if we got no dictionary as response type we try to force # to return a proper Response object instead of any further process if not isinstance(context, dict): return Response.force_type(context) data = render_template(template_name, context, modifier=modifier, request=request, stream=stream) response = Response(data) return response
def login(self, request): _message, _code = _(u'Please log in.'), 401 # Try to get an already logged in user if request.session.get('user_id', None): return redirect_to('portal/index') if request.authorization: auth = request.authorization try: user = User.query.get(auth.username) if user.check_password(request.authorization.password): self.set_user(request, user) request.flash(_(u'You are now logged in.'), True) return redirect_to('portal/index') else: _message, _code = _(u'Invalid login.'), 403 except db.NoResultFound: _message, _code = _(u'Invalid login'), 403 # ask for login response = Response( _message, _code, {'WWW-Authenticate': 'Basic realm="%s' % self.realm}) abort(response)
def send_service_response(request_or_format, result, config=None): """Sends the API response.""" from inyoka.core.http import Response if isinstance(result, Response): response = result else: ro = primitive(result, config) serializer, mimetype = get_serializer(request_or_format) response = Response(serializer(ro), mimetype=mimetype) # acao disallows requests by default (-- only base domain). acao = 'http://%s' % ctx.cfg['base_domain_name'] if not isinstance(request_or_format, basestring): origin = request_or_format.headers.get('Origin', None) # If the origin is in our domainspace we accept. if origin and origin.endswith(ctx.cfg['base_domain_name']): acao = origin # make the response conditional response.make_conditional(request_or_format) response.headers['Access-Control-Allow-Origin'] = acao response.add_etag() return response
def func(*args, **kwargs): if cache_key is not None: key = cache_key % kwargs content = cache.get(key) if content is not None: mimetype = 'application/atom+xml; charset=utf-8' return Response(content, mimetype=mimetype) feed = original(*args, **kwargs) if not isinstance(feed, AtomFeed): # ret is not a feed object so return it return feed response = feed.get_response() if cache_key is not None: cache.set(key, response.data, cache_timeout) return response
def serve_javascript(request): """Serves the JavaScript translations.""" from inyoka.core.http import Response code = _js_translations.get(ctx.dispatcher) if code is None: messages = {} translations = get_translations() catalog = translations._catalog if hasattr(translations, '_catalog') else {} data = {'domain': 'messages', 'locale': unicode(get_locale())} for msgid, msgstr in catalog.iteritems(): if isinstance(msgid, (list, tuple)): messages.setdefault(msgid[0], {}) messages[msgid[0]][msgid[1]] = msgstr elif msgid: messages[msgid] = msgstr else: for line in msgstr.splitlines(): line = line.strip() if not line: continue if ':' not in line: continue name, val = line.split(':', 1) name = name.strip().lower() if name == 'plural-forms': data['plural_expr'] = pluralexpr(val) break data['messages'] = messages code = u''.join(( ('// Generated messages javascript file from compiled MO file\n'), ('babel.Translations.load('), (json.dumps(data)), (').install();\n') )) _js_translations[ctx.dispatcher] = code response = Response(code, mimetype='application/javascript') response.add_etag() response.make_conditional(request) return response