66# flake8: noqa
77from __future__ import unicode_literals
88
9+ import inspect
10+
911from django .core .exceptions import ImproperlyConfigured
1012from django .conf import settings
1113from django .utils import six
1214import django
13- import inspect
1415
1516
1617# Handle django.utils.encoding rename in 1.5 onwards.
4950except ImportError :
5051 django_filters = None
5152
52-
5353if django .VERSION >= (1 , 6 ):
5454 def clean_manytomany_helptext (text ):
5555 return text
@@ -123,7 +123,6 @@ def _allowed_methods(self):
123123 return [m .upper () for m in self .http_method_names if hasattr (self , m )]
124124
125125
126-
127126# MinValueValidator, MaxValueValidator et al. only accept `message` in 1.8+
128127if django .VERSION >= (1 , 8 ):
129128 from django .core .validators import MinValueValidator , MaxValueValidator
@@ -187,28 +186,30 @@ def __init__(self, *args, **kwargs):
187186# RequestFactory only provides `generic` from 1.5 onwards
188187from django .test .client import RequestFactory as DjangoRequestFactory
189188from django .test .client import FakePayload
189+
190190try :
191191 # In 1.5 the test client uses force_bytes
192192 from django .utils .encoding import force_bytes as force_bytes_or_smart_bytes
193193except ImportError :
194194 # In 1.4 the test client just uses smart_str
195195 from django .utils .encoding import smart_str as force_bytes_or_smart_bytes
196196
197+
197198class RequestFactory (DjangoRequestFactory ):
198199 def generic (self , method , path ,
199200 data = '' , content_type = 'application/octet-stream' , ** extra ):
200201 parsed = urlparse .urlparse (path )
201202 data = force_bytes_or_smart_bytes (data , settings .DEFAULT_CHARSET )
202203 r = {
203- 'PATH_INFO' : self ._get_path (parsed ),
204- 'QUERY_STRING' : force_text (parsed [4 ]),
204+ 'PATH_INFO' : self ._get_path (parsed ),
205+ 'QUERY_STRING' : force_text (parsed [4 ]),
205206 'REQUEST_METHOD' : six .text_type (method ),
206207 }
207208 if data :
208209 r .update ({
209210 'CONTENT_LENGTH' : len (data ),
210- 'CONTENT_TYPE' : six .text_type (content_type ),
211- 'wsgi.input' : FakePayload (data ),
211+ 'CONTENT_TYPE' : six .text_type (content_type ),
212+ 'wsgi.input' : FakePayload (data ),
212213 })
213214 elif django .VERSION <= (1 , 4 ):
214215 # For 1.3 we need an empty WSGI payload
@@ -287,10 +288,12 @@ def check_nonce(request, oauth_request, oauth_nonce, oauth_timestamp):
287288 import provider as oauth2_provider
288289 from provider import scope as oauth2_provider_scope
289290 from provider import constants as oauth2_constants
291+
290292 if oauth2_provider .__version__ in ('0.2.3' , '0.2.4' ):
291293 # 0.2.3 and 0.2.4 are supported version that do not support
292294 # timezone aware datetimes
293295 import datetime
296+
294297 provider_now = datetime .datetime .now
295298 else :
296299 # Any other supported version does use timezone aware datetimes
@@ -301,7 +304,7 @@ def check_nonce(request, oauth_request, oauth_nonce, oauth_timestamp):
301304 oauth2_constants = None
302305 provider_now = None
303306
304- # `seperators ` argument to `json.dumps()` differs between 2.x and 3.x
307+ # `separators ` argument to `json.dumps()` differs between 2.x and 3.x
305308# See: http://bugs.python.org/issue22767
306309if six .PY3 :
307310 SHORT_SEPARATORS = (',' , ':' )
@@ -316,30 +319,9 @@ def check_nonce(request, oauth_request, oauth_nonce, oauth_timestamp):
316319
317320if six .PY3 :
318321 def is_non_str_iterable (obj ):
319- if (isinstance (obj , str ) or
320- (isinstance (obj , Promise ) and obj ._delegate_text )):
322+ if isinstance (obj , str ) or (isinstance (obj , Promise ) and obj ._delegate_text ):
321323 return False
322324 return hasattr (obj , '__iter__' )
323325else :
324326 def is_non_str_iterable (obj ):
325327 return hasattr (obj , '__iter__' )
326-
327-
328- try :
329- from django .utils .encoding import python_2_unicode_compatible
330- except ImportError :
331- def python_2_unicode_compatible (klass ):
332- """
333- A decorator that defines __unicode__ and __str__ methods under Python 2.
334- Under Python 3 it does nothing.
335-
336- To support Python 2 and 3 with a single code base, define a __str__ method
337- returning text and apply this decorator to the class.
338- """
339- if '__str__' not in klass .__dict__ :
340- raise ValueError ("@python_2_unicode_compatible cannot be applied "
341- "to %s because it doesn't define __str__()." %
342- klass .__name__ )
343- klass .__unicode__ = klass .__str__
344- klass .__str__ = lambda self : self .__unicode__ ().encode ('utf-8' )
345- return klass
0 commit comments