Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Django 2.1 Upgrades and black formatting #3

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 55 additions & 34 deletions piston/authentication.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import binascii

from django.conf import settings
from django.contrib.auth import authenticate
from django.contrib.auth.decorators import login_required
from django.contrib.auth.models import AnonymousUser, User
from django.core.exceptions import ImproperlyConfigured
from django.core.urlresolvers import get_callable
from django.http import HttpResponse, HttpResponseRedirect
from django.shortcuts import render_to_response
from django.template import loader, RequestContext
from django.template import RequestContext, loader
from django.urls import get_callable

from piston import forms

from . import oauth
Expand Down Expand Up @@ -39,39 +41,41 @@ class HttpBasicAuthentication(object):
some kind of challenge headers and 401 code on it.
"""

def __init__(self, auth_func=authenticate, realm='API'):
def __init__(self, auth_func=authenticate, realm="API"):
self.auth_func = auth_func
self.realm = realm

def is_authenticated(self, request):
auth_string = request.META.get('HTTP_AUTHORIZATION', None)
auth_string = request.META.get("HTTP_AUTHORIZATION", None)

if not auth_string:
return False

try:
(authmeth, auth) = auth_string.split(" ", 1)

if not authmeth.lower() == 'basic':
if not authmeth.lower() == "basic":
return False

auth = auth.strip().decode('base64')
(username, password) = auth.split(':', 1)
auth = auth.strip().decode("base64")
(username, password) = auth.split(":", 1)
except (ValueError, binascii.Error):
return False

request.user = self.auth_func(username=username, password=password) or AnonymousUser()
request.user = (
self.auth_func(username=username, password=password) or AnonymousUser()
)

return not request.user in (False, None, AnonymousUser())

def challenge(self, request=None):
resp = HttpResponse("Authorization Required")
resp['WWW-Authenticate'] = 'Basic realm="%s"' % self.realm
resp["WWW-Authenticate"] = 'Basic realm="%s"' % self.realm
resp.status_code = 401
return resp

def __repr__(self):
return '<HTTPBasic: realm=%s>' % self.realm
return "<HTTPBasic: realm=%s>" % self.realm


class HttpBasicSimple(HttpBasicAuthentication):
Expand All @@ -87,22 +91,26 @@ def hash(self, username, password):


def load_data_store():
'''Load data store for OAuth Consumers, Tokens, Nonces and Resources'''
path = getattr(settings, 'OAUTH_DATA_STORE', 'piston.store.DataStore')
"""Load data store for OAuth Consumers, Tokens, Nonces and Resources"""
path = getattr(settings, "OAUTH_DATA_STORE", "piston.store.DataStore")

# stolen from django.contrib.auth.load_backend
i = path.rfind('.')
i = path.rfind(".")
module, attr = path[:i], path[i + 1 :]

try:
mod = __import__(module, {}, {}, attr)
except ImportError as e:
raise ImproperlyConfigured('Error importing OAuth data store %s: "%s"' % (module, e))
raise ImproperlyConfigured(
'Error importing OAuth data store %s: "%s"' % (module, e)
)

try:
cls = getattr(mod, attr)
except AttributeError:
raise ImproperlyConfigured('Module %s does not define a "%s" OAuth data store' % (module, attr))
raise ImproperlyConfigured(
'Module %s does not define a "%s" OAuth data store' % (module, attr)
)

return cls

Expand All @@ -116,21 +124,21 @@ def initialize_server_request(request):
Shortcut for initialization.
"""
# c.f. http://www.mail-archive.com/[email protected]/msg01556.html
if request.method == 'POST' and request.FILES == {}:
if request.method == "POST" and request.FILES == {}:
params = dict(list(request.REQUEST.items()))
else:
params = {}

# Seems that we want to put HTTP_AUTHORIZATION into 'Authorization'
# for oauth.py to understand. Lovely.
request.META['Authorization'] = request.META.get('HTTP_AUTHORIZATION', '')
request.META["Authorization"] = request.META.get("HTTP_AUTHORIZATION", "")

oauth_request = oauth.OAuthRequest.from_request(
request.method,
request.build_absolute_uri(),
headers=request.META,
parameters=params,
query_string=request.environ.get('QUERY_STRING', ''),
query_string=request.environ.get("QUERY_STRING", ""),
)

if oauth_request:
Expand All @@ -147,10 +155,10 @@ def send_oauth_error(err=None):
"""
Shortcut for sending an error.
"""
response = HttpResponse(err.message.encode('utf-8'))
response = HttpResponse(err.message.encode("utf-8"))
response.status_code = 401

realm = 'OAuth'
realm = "OAuth"
header = oauth.build_authenticate_header(realm=realm)

for k, v in list(header.items()):
Expand All @@ -177,13 +185,15 @@ def oauth_request_token(request):
def oauth_auth_view(request, token, callback, params):
form = forms.OAuthAuthenticationForm(
initial={
'oauth_token': token.key,
'oauth_callback': token.get_callback_url() or callback,
"oauth_token": token.key,
"oauth_callback": token.get_callback_url() or callback,
}
)

context = dict(form=form, token=token)
return render_to_response('piston/authorize_token.html', context, RequestContext(request))
return render_to_response(
"piston/authorize_token.html", context, RequestContext(request)
)


@login_required
Expand All @@ -206,7 +216,7 @@ def oauth_user_auth(request):
if request.method == "GET":
params = oauth_request.get_normalized_parameters()

oauth_view = getattr(settings, 'OAUTH_AUTH_VIEW', None)
oauth_view = getattr(settings, "OAUTH_AUTH_VIEW", None)
if oauth_view is None:
return oauth_auth_view(request, token, callback, params)
else:
Expand All @@ -216,20 +226,20 @@ def oauth_user_auth(request):
form = forms.OAuthAuthenticationForm(request.POST)
if form.is_valid():
token = oauth_server.authorize_token(token, request.user)
args = '?' + token.to_string(only_key=True)
args = "?" + token.to_string(only_key=True)
else:
args = '?error=%s' % 'Access not granted by user.'
args = "?error=%s" % "Access not granted by user."

if not callback:
callback = getattr(settings, 'OAUTH_CALLBACK_VIEW')
callback = getattr(settings, "OAUTH_CALLBACK_VIEW")
return get_callable(callback)(request, token)

response = HttpResponseRedirect(callback + args)

except oauth.OAuthError as err:
response = send_oauth_error(err)
else:
response = HttpResponse('Action not allowed.')
response = HttpResponse("Action not allowed.")

return response

Expand All @@ -247,15 +257,17 @@ def oauth_access_token(request):
return send_oauth_error(err)


INVALID_PARAMS_RESPONSE = send_oauth_error(oauth.OAuthError('Invalid request parameters.'))
INVALID_PARAMS_RESPONSE = send_oauth_error(
oauth.OAuthError("Invalid request parameters.")
)


class OAuthAuthentication(object):
"""
OAuth authentication. Based on work by Leah Culver.
"""

def __init__(self, realm='API'):
def __init__(self, realm="API"):
self.realm = realm
self.builder = oauth.build_authenticate_header

Expand Down Expand Up @@ -295,11 +307,13 @@ def challenge(self, request=None):
"""
response = HttpResponse()
response.status_code = 401
realm = 'API'
realm = "API"

for k, v in list(self.builder(realm=realm).items()):
response[k] = v
tmpl = loader.render_to_string('oauth/challenge.html', {'MEDIA_URL': settings.MEDIA_URL})
tmpl = loader.render_to_string(
"oauth/challenge.html", {"MEDIA_URL": settings.MEDIA_URL}
)

response.content = tmpl
return response
Expand All @@ -313,8 +327,15 @@ def is_valid_request(request):
OAuth spec, but otherwise fall back to `GET` and `POST`.
"""
must_have = [
'oauth_' + s
for s in ['consumer_key', 'token', 'signature', 'signature_method', 'timestamp', 'nonce']
"oauth_" + s
for s in [
"consumer_key",
"token",
"signature",
"signature_method",
"timestamp",
"nonce",
]
]

is_in = lambda l: all([(p in l) for p in must_have])
Expand Down
46 changes: 28 additions & 18 deletions piston/doc.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import inspect
from django.core.urlresolvers import get_callable, get_resolver, get_script_prefix

from django.shortcuts import render_to_response
from django.template import RequestContext
from django.urls import get_callable, get_resolver, get_script_prefix

from piston.handler import handler_tracker, typemapper

from . import handler
Expand All @@ -28,7 +30,7 @@ def iter_args(self):
args, _, _, defaults = inspect.getargspec(self.method)

for idx, arg in enumerate(args):
if arg in ('self', 'request', 'form'):
if arg in ("self", "request", "form"):
continue

didx = len(args) - idx
Expand All @@ -46,9 +48,9 @@ def signature(self, parse_optional=True):
spec += argn

if argdef:
spec += '=%s' % argdef
spec += "=%s" % argdef

spec += ', '
spec += ", "

spec = spec.rstrip(", ")

Expand All @@ -67,14 +69,14 @@ def name(self):

@property
def http_name(self):
if self.name == 'read':
return 'GET'
elif self.name == 'create':
return 'POST'
elif self.name == 'delete':
return 'DELETE'
elif self.name == 'update':
return 'PUT'
if self.name == "read":
return "GET"
elif self.name == "create":
return "POST"
elif self.name == "delete":
return "DELETE"
elif self.name == "update":
return "PUT"

def __repr__(self):
return "<Method: %s>" % self.name
Expand All @@ -91,13 +93,19 @@ def get_methods(self, include_default=False):
if not met:
continue

stale = inspect.getmodule(met.im_func) is not inspect.getmodule(self.handler)
stale = inspect.getmodule(met.im_func) is not inspect.getmodule(
self.handler
)

if not self.handler.is_anonymous:
if met and (not stale or include_default):
yield HandlerMethod(met, stale)
else:
if not stale or met.__name__ == "read" and 'GET' in self.allowed_methods:
if (
not stale
or met.__name__ == "read"
and "GET" in self.allowed_methods
):
yield HandlerMethod(met, stale)

def get_all_methods(self):
Expand All @@ -108,7 +116,7 @@ def is_anonymous(self):
return self.handler.is_anonymous

def get_model(self):
return getattr(self, 'model', None)
return getattr(self, "model", None)

@property
def has_anonymous(self):
Expand Down Expand Up @@ -141,7 +149,7 @@ def get_resource_uri_template(self):
def _convert(template, params=[]):
"""URI template converter"""
paths = template % dict([p, "{%s}" % p] for p in params)
return u'%s%s' % (get_script_prefix(), paths)
return "%s%s" % (get_script_prefix(), paths)

try:
resource_uri = self.handler.resource_uri()
Expand Down Expand Up @@ -172,7 +180,7 @@ def _convert(template, params=[]):
resource_uri_template = property(get_resource_uri_template)

def __repr__(self):
return u'<Documentation for "%s">' % self.name
return '<Documentation for "%s">' % self.name


def documentation_view(request):
Expand All @@ -193,4 +201,6 @@ def _compare(doc1, doc2):

docs.sort(_compare)

return render_to_response('documentation.html', {'docs': docs}, RequestContext(request))
return render_to_response(
"documentation.html", {"docs": docs}, RequestContext(request)
)
Loading