Skip to content

Commit

Permalink
fixed django 1.11 support
Browse files Browse the repository at this point in the history
  • Loading branch information
wolph committed Apr 24, 2017
1 parent e610232 commit 9e87846
Showing 1 changed file with 49 additions and 57 deletions.
106 changes: 49 additions & 57 deletions django_utils/view_decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import json
from django.template import loader as django_loader
from django import http
from django.template import RequestContext
from django.contrib.auth import decorators
from django.core import serializers
from django.db import models
Expand Down Expand Up @@ -47,7 +46,7 @@ def permanent_redirect(url, *args, **kwargs):

def _prepare_request(request, app, view):
'''Add context and extra methods to the request'''
request.context = RequestContext(request)
request.context = dict()
request.context['view'] = view
request.context['app'] = app
request.context['request'] = request
Expand All @@ -62,68 +61,61 @@ def _process_response(request, response, response_class):
'''Generic response processing function, always returns HttpResponse'''

'''If we add something to the context stack, pop it after adding'''
pop = False

try:
if isinstance(response, (dict, list, models.query.QuerySet)):
if request.ajax:
if isinstance(response, models.query.QuerySet):
output = serializers.serialize('json', response)
else:
output = json.dumps(response, default=json_default_handler)

callback = request.GET.get('callback', False)
if callback:
output = '%s(%s)' % (callback, output)
if request.GET.get('debug'):
title = 'Rendering %(view)r in module %(app)r' % (
request.context)

output = '''
<html>
<head>
<title>%s</title>
</head>
<body>
<textarea>%s</textarea>
</body>
</html>
''' % (title, output)
response = response_class(output, content_type='text/html')
else:
response = response_class(
output,
content_type='text/plain')

return response
if isinstance(response, (dict, list, models.query.QuerySet)):
if request.ajax:
if isinstance(response, models.query.QuerySet):
output = serializers.serialize('json', response)
else:
'''Add the dictionary to the context and let
render_to_response handle it'''
request.context.update(response)
response = None
pop = True
output = json.dumps(response, default=json_default_handler)

callback = request.GET.get('callback', False)
if callback:
output = '%s(%s)' % (callback, output)
if request.GET.get('debug'):
title = 'Rendering %(view)r in module %(app)r' % (
request.context)

output = '''
<html>
<head>
<title>%s</title>
</head>
<body>
<textarea>%s</textarea>
</body>
</html>
''' % (title, output)
response = response_class(output, content_type='text/html')
else:
response = response_class(
output,
content_type='text/plain')

if isinstance(response, http.HttpResponse):
return response
else:
'''Add the dictionary to the context and let
render_to_response handle it'''
request.context.update(response)
response = None

elif isinstance(response, six.string_types):
if request.ajax:
return response_class(response, content_type='text/plain')
else:
return response_class(response)
if isinstance(response, http.HttpResponse):
return response

elif response is None:
render_to_string = django_loader.render_to_string
elif isinstance(response, six.string_types):
if request.ajax:
return response_class(response, content_type='text/plain')
else:
return response_class(response)

return response_class(render_to_string(
request.template, context=request.context))
elif response is None:
render_to_string = django_loader.render_to_string

else:
raise UnknownViewResponseError(
'"%s" is an unsupported response type' % type(response))
finally:
if pop:
request.context.pop()
return response_class(render_to_string(
request.template, context=request.context, request=request))

else:
raise UnknownViewResponseError(
'"%s" is an unsupported response type' % type(response))


def env(function=None, login_required=False, response_class=http.HttpResponse):
Expand Down

0 comments on commit 9e87846

Please sign in to comment.