Skip to content

Commit

Permalink
Fixed lint
Browse files Browse the repository at this point in the history
  • Loading branch information
syrusakbary committed Oct 25, 2017
1 parent de39473 commit 5051d3b
Showing 1 changed file with 35 additions and 18 deletions.
53 changes: 35 additions & 18 deletions graphene_django/views.py
Expand Up @@ -81,8 +81,10 @@ def __init__(self, schema=None, executor=None, middleware=None, root_value=None,
self.graphiql = graphiql
self.batch = batch

assert isinstance(self.schema, GraphQLSchema), 'A Schema is required to be provided to GraphQLView.'
assert not all((graphiql, batch)), 'Use either graphiql or batch processing'
assert isinstance(
self.schema, GraphQLSchema), 'A Schema is required to be provided to GraphQLView.'
assert not all((graphiql, batch)
), 'Use either graphiql or batch processing'

# noinspection PyUnusedLocal
def get_root_value(self, request):
Expand All @@ -98,20 +100,27 @@ def get_context(self, request):
def dispatch(self, request, *args, **kwargs):
try:
if request.method.lower() not in ('get', 'post'):
raise HttpError(HttpResponseNotAllowed(['GET', 'POST'], 'GraphQL only supports GET and POST requests.'))
raise HttpError(HttpResponseNotAllowed(
['GET', 'POST'], 'GraphQL only supports GET and POST requests.'))

data = self.parse_body(request)
show_graphiql = self.graphiql and self.can_display_graphiql(request, data)
show_graphiql = self.graphiql and self.can_display_graphiql(
request, data)

if self.batch:
responses = [self.get_response(request, entry) for entry in data]
result = '[{}]'.format(','.join([response[0] for response in responses]))
status_code = max(responses, key=lambda response: response[1])[1]
responses = [self.get_response(
request, entry) for entry in data]
result = '[{}]'.format(
','.join([response[0] for response in responses]))
status_code = max(
responses, key=lambda response: response[1])[1]
else:
result, status_code = self.get_response(request, data, show_graphiql)
result, status_code = self.get_response(
request, data, show_graphiql)

if show_graphiql:
query, variables, operation_name, id = self.get_graphql_params(request, data)
query, variables, operation_name, id = self.get_graphql_params(
request, data)
return self.render_graphiql(
request,
graphiql_version=self.graphiql_version,
Expand All @@ -136,7 +145,8 @@ def dispatch(self, request, *args, **kwargs):
return response

def get_response(self, request, data, show_graphiql=False):
query, variables, operation_name, id = self.get_graphql_params(request, data)
query, variables, operation_name, id = self.get_graphql_params(
request, data)

execution_result = self.execute_graphql_request(
request,
Expand All @@ -152,7 +162,8 @@ def get_response(self, request, data, show_graphiql=False):
response = {}

if execution_result.errors:
response['errors'] = [self.format_error(e) for e in execution_result.errors]
response['errors'] = [self.format_error(
e) for e in execution_result.errors]

if execution_result.invalid:
status_code = 400
Expand Down Expand Up @@ -209,7 +220,8 @@ def parse_body(self, request):
except AssertionError as e:
raise HttpError(HttpResponseBadRequest(str(e)))
except (TypeError, ValueError):
raise HttpError(HttpResponseBadRequest('POST body sent invalid JSON.'))
raise HttpError(HttpResponseBadRequest(
'POST body sent invalid JSON.'))

elif content_type in ['application/x-www-form-urlencoded', 'multipart/form-data']:
return request.POST
Expand All @@ -223,7 +235,8 @@ def execute_graphql_request(self, request, data, query, variables, operation_nam
if not query:
if show_graphiql:
return None
raise HttpError(HttpResponseBadRequest('Must provide query string.'))
raise HttpError(HttpResponseBadRequest(
'Must provide query string.'))

source = Source(query, name='GraphQL request')

Expand All @@ -245,7 +258,8 @@ def execute_graphql_request(self, request, data, query, variables, operation_nam
return None

raise HttpError(HttpResponseNotAllowed(
['POST'], 'Can only perform a {} operation from a POST request.'.format(operation_ast.operation)
['POST'], 'Can only perform a {} operation from a POST request.'.format(
operation_ast.operation)
))

try:
Expand Down Expand Up @@ -283,10 +297,12 @@ def get_graphql_params(request, data):
if variables and isinstance(variables, six.text_type):
try:
variables = json.loads(variables)
except:
raise HttpError(HttpResponseBadRequest('Variables are invalid JSON.'))
except Exception:
raise HttpError(HttpResponseBadRequest(
'Variables are invalid JSON.'))

operation_name = request.GET.get('operationName') or data.get('operationName')
operation_name = request.GET.get(
'operationName') or data.get('operationName')
if operation_name == "null":
operation_name = None

Expand All @@ -302,5 +318,6 @@ def format_error(error):
@staticmethod
def get_content_type(request):
meta = request.META
content_type = meta.get('CONTENT_TYPE', meta.get('HTTP_CONTENT_TYPE', ''))
content_type = meta.get(
'CONTENT_TYPE', meta.get('HTTP_CONTENT_TYPE', ''))
return content_type.split(';', 1)[0].lower()

0 comments on commit 5051d3b

Please sign in to comment.