Example #1
0
def render_to_response(template_name, *args, **kwargs):
    """ A render_to_response wrapper that allows using it for both
    HttpRequest and XmppRequest.  `template_name` is explicit since it is
    modified to end with '.xmpp' or '.html' depending on the request.
    XMPP response rendering can only be used with RequestContext. """
    from django.shortcuts import render_to_response as render_to_response_orig
    xrargs = kwargs.pop('xrargs', {})
    # There should be other ways to determine an Xmpp request, no?
    # Or it would be 'other function'.
    # Also, may args[0] not possibly contain template name?
    try:
        request = kwargs['context_instance']['request']
        isxmpp = request.is_xmpp()
    except Exception:
        # Something is not right, but it's probably not XMPP anyway.
        request = None
        isxmpp = False
    if isxmpp:  # Return some XmppResponse with rendered template.
        # ! Wouldn't it be better to 's/(\.html)?$/.xmpp/' here?
        return XmppResponse(
          django.template.loader.render_to_string(
            re.sub(r'(\.html|\.xmpp)?$', '.xmpp', template_name),
            *args, **kwargs),
          user=request.user, **xrargs)
    else:  # Not Xmpp. Not our business.
        ## Can even require using '.html' in calls, but likely should not.
        return render_to_response_orig(
            re.sub(r'(\.html)?$', '.html', template_name),
          *args, **kwargs)
Example #2
0
def render_to_response(request, *args, **kwargs):
	kwargs['context_instance'] = RequestContext(request)
	return render_to_response_orig(*args, **kwargs)