Beispiel #1
0
def widget_download_response(request, classname):
    """Retrieve a widget as a different format."""
    method = request.GET.get("as", "svg")
    mimetype = {
        'svg': "image/svg+xml",
        'png': "image/png",
        'csv': "text/csv",
        'html': "text/html",
    }[method]

    widget_class = get_widget_class(classname)
    widget = widget_class()

    fn = getattr(widget, 'as_%s' % method)

    values = _getquery_to_dict(widget_class.options, request.GET)
    values = process_values(values, widget_class.options)

    response = HttpResponse(mimetype=mimetype)
    response['Content-Disposition'] = 'attachment; filename=%s_%s.%s' % (
        classname,
        datetime.datetime.now(),
        method)
    try:
        fn(values, response)
        return response
    except NotImplementedError:
        return HttpResponse("Sorry that format is not available.", status=404)
    def render(self, context):
        from widgets.utils import options_to_query_string
        resolved = dict((opt.name, opt.resolve_value(context, value))
                        for opt, value in self.values)
        options = process_values(resolved, self.widget.options)

        as_name = self.as_name
        if as_name:
            as_name = self.as_name.resolve(context)
        return self.widget._render_wrapper(
            options=options,
            context=context,
            uid=as_name,
            raw_options=resolved,
            query_string=options_to_query_string(
                self.widget.__class__,
                options))