From 5051d3bb617ce6977eebc023c958bd10de91fe91 Mon Sep 17 00:00:00 2001 From: Syrus Akbary Date: Wed, 25 Oct 2017 10:54:13 -0700 Subject: [PATCH] Fixed lint --- graphene_django/views.py | 53 ++++++++++++++++++++++++++-------------- 1 file changed, 35 insertions(+), 18 deletions(-) diff --git a/graphene_django/views.py b/graphene_django/views.py index d7b879514..4bf7349fb 100644 --- a/graphene_django/views.py +++ b/graphene_django/views.py @@ -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): @@ -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, @@ -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, @@ -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 @@ -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 @@ -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') @@ -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: @@ -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 @@ -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()