def as_response(self, template=None, data=None, _in_decorator=False,
                    _stream=False, **response_kwargs):
        """
        Return a response object for the rendered template::

            >>> from jinja2 import PackageLoader
            >>> from fresco import FrescoApp
            >>> app = FrescoApp()
            >>> render = Jinja2(app, loader=PackageLoader('myapp', 'tmpls'))
            >>> response = render.as_response('my_template.html',
            ...                               {'foo': 'bar'}) #doctest: +SKIP

        Can also be used as a decorator. The decorated function will merge the
        original function's return value (a dict) with the specified template::

            >>> render = Jinja2(app, loader=PackageLoader('myapp', 'tmpls'))
            >>>
            >>> @render.as_response('my_template.html') #doctest: +SKIP
            ... def handler(request):
            ...     return {'foo': 'bar'}
            ...
        """
        if data is None and not _in_decorator:
            return _make_decorator_or_filter(self.as_response, template,
                                   **response_kwargs)

        return Response(
            TemplateContent(self, template, data, None, stream=_stream),
            **response_kwargs)
Exemplo n.º 2
0
 def record_trial_app(self, environ, start_response):
     request = Request(environ)
     experiment = request.query.get('e')
     try:
         record_trial(environ, experiment)
     except KeyError:
         pass
     return Response([], cache_control="no-cache")(environ, start_response)
Exemplo n.º 3
0
def save_results():
    context.request.MAX_SIZE = 200 * 1024

    results = context.request.form.get('results')
    prolific_id = context.request.form.get('ID')

    with open('results/{}.json'.format(prolific_id), 'w') as f:
        f.write(results)

    return Response('')
Exemplo n.º 4
0
def filepond_upload(request, media_dir="media/"):
    """
    See https://pqina.nl/filepond/docs/patterns/api/server/

    Expects a single field with two values: metadata, then upload
    """
    form = request.form
    key = next(form.keys())
    metadata, upload = form.getlist(key)
    filename = fileuploads.upload(media_dir, upload)
    return Response([filename], content_type="text/plain")
Exemplo n.º 5
0
def submit_suggestion(request):
    conn = request.getconn()
    supporter_id = request.get_user_id()
    if supporter_id:
        supporter = supporters.get_supporter_by_id(conn, supporter_id)
    else:
        supporter = None
    suggestion = request.get("suggestion")
    suggestions.send_suggestion(conn, supporter, suggestion)

    return Response(headers=[("X-HX-Trigger", "suggestionSubmitted")])
Exemplo n.º 6
0
def petition_count(request):
    count = supporters.supporter_count(request.getconn()) + 31300
    return Response(request.format(count)).add_headers(
        cache_control="must-revalidate, max-age=3", vary="cookie")