Wrap a view-like function that returns a string and marshalls it into an
    HttpResponse with the given Content-Type
    If the view raises an HttpBadRequestException, it will be converted into
    an HttpResponseBadRequest.

def string_to_response(content_type):
    """
    Wrap a view-like function that returns a string and marshalls it into an
    HttpResponse with the given Content-Type
    If the view raises an HttpBadRequestException, it will be converted into
    an HttpResponseBadRequest.
    """
    def outer_wrapper(req_function):
        @wraps(req_function)
        def newreq(request, *args, **kwargs):
            try:
                outp = req_function(request, *args, **kwargs)
                if issubclass(outp.__class__, HttpResponse):
                    response = outp
                else:
                    response = HttpResponse()
                    response.write(outp)
                    response['Content-Length'] = str(len(response.content))

                response['Content-Type'] = content_type

            except HttpBadRequestException as bad_request:
                response = HttpResponseBadRequest(bad_request.message)

            return response
        return newreq
    return outer_wrapper