Skip to content

Commit

Permalink
updated tests for choices and enabled full django 1.11 support
Browse files Browse the repository at this point in the history
  • Loading branch information
wolph committed Apr 24, 2017
2 parents 5520400 + 9e87846 commit c217a80
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 60 deletions.
7 changes: 4 additions & 3 deletions django_utils/choices.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,9 +148,10 @@ def __init__(self, value=None, label=None):
self.order = Choice.order

def __eq__(self, other):
if not isinstance(other, Choice):
return False
return self.value == other.value and self.label == self.label
if isinstance(other, Choice): # pragma: no branch
return self.value == other.value
else:
return self.value == other

def __repr__(self):
repr_ = (six.text_type('<%s[%d]:%s>') % (
Expand Down
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.flatten()))
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
19 changes: 19 additions & 0 deletions tests/test_choices.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,22 @@ def test_choices():
def test_choice():
repr(Human.Gender.Male)
str(Human.Gender.Male)


def test_choice_equals():
choice_a = choices.Choice(value=123, label='123')
choice_b = choices.Choice(value=123)
choice_c = choices.Choice(value=456, label='123')

assert not (choice_a == 'test')
assert choice_a == 123
assert not choice_a == 456
assert choice_a == choice_a
assert choice_a == choice_b
assert choice_a != choice_c


def test_choice_deconstruct():
choices.Choice().deconstruct()
choices.Choice(value=123).deconstruct()
choices.Choice(value=123, label='123').deconstruct()

0 comments on commit c217a80

Please sign in to comment.