def request(self, **request): """ The master request method. Compose the environment dictionary and pass to the handler, return the result of the handler. Assume defaults for the query environment, which can be overridden using the arguments to the request. """ environ = self._base_environ(**request) # Curry a data dictionary into an instance of the template renderer # callback function. data = {} on_template_render = partial(store_rendered_templates, data) signal_uid = "template-render-%s" % id(request) signals.template_rendered.connect(on_template_render, dispatch_uid=signal_uid) # Capture exceptions created by the handler. exception_uid = "request-exception-%s" % id(request) got_request_exception.connect(self.store_exc_info, dispatch_uid=exception_uid) try: response = self.handler(environ) # Look for a signalled exception, clear the current context # exception data, then re-raise the signalled exception. # Also make sure that the signalled exception is cleared from # the local cache! response.exc_info = self.exc_info if self.exc_info: _, exc_value, _ = self.exc_info self.exc_info = None if self.raise_request_exception: raise exc_value # Save the client and request that stimulated the response. response.client = self response.request = request # Add any rendered template detail to the response. response.templates = data.get("templates", []) response.context = data.get("context") response.json = partial(self._parse_json, response) # Attach the ResolverMatch instance to the response response.resolver_match = SimpleLazyObject( lambda: resolve(request['PATH_INFO'])) # Flatten a single context. Not really necessary anymore thanks to # the __getattr__ flattening in ContextList, but has some edge-case # backwards-compatibility implications. if response.context and len(response.context) == 1: response.context = response.context[0] # Update persistent cookie data. if response.cookies: self.cookies.update(response.cookies) return response finally: signals.template_rendered.disconnect(dispatch_uid=signal_uid) got_request_exception.disconnect(dispatch_uid=exception_uid)
def test_text(self): self.assertEqual("joe", six.text_type(SimpleLazyObject(complex_object)))
def test_class(self): # This is important for classes that use __class__ in things like # equality tests. self.assertEqual(_ComplexObject, SimpleLazyObject(complex_object).__class__)
def test_hash(self): # hash() equality would not be true for many objects, but it should be # for _ComplexObject self.assertEqual(hash(complex_object()), hash(SimpleLazyObject(complex_object)))
def test_bytes(self): self.assertEqual(b"I am _ComplexObject('joe')", bytes(SimpleLazyObject(complex_object)))
def test_bool(self): x = SimpleLazyObject(lambda: 3) self.assertTrue(x) x = SimpleLazyObject(lambda: 0) self.assertFalse(x)
def test_equality(self): self.assertEqual(complex_object(), SimpleLazyObject(complex_object)) self.assertEqual(SimpleLazyObject(complex_object), complex_object())
def process_request(self, request): assert hasattr( request, 'session' ), "The Django authentication middleware requires session middleware to be installed. Edit your MIDDLEWARE_CLASSES setting to insert 'django.contrib.sessions.middleware.SessionMiddleware'." request.user = SimpleLazyObject(lambda: get_cached_user(request))
def resolve_reporter(self, info): return SimpleLazyObject( lambda: SimpleLazyObject(lambda: Reporter(id=1)))
from django.shortcuts import render_to_response from django.urls import reverse from django.utils.functional import SimpleLazyObject from django.views.generic import View from graphene_django.settings import graphene_settings from graphene_django.views import instantiate_middleware from graphql import GraphQLDocument, get_default_backend from graphql.error import ( GraphQLError, GraphQLSyntaxError, format_error as format_graphql_error, ) from graphql.execution import ExecutionResult from graphql_jwt.exceptions import PermissionDenied API_PATH = SimpleLazyObject(lambda: reverse("api")) unhandled_errors_logger = logging.getLogger("saleor.graphql.errors.unhandled") handled_errors_logger = logging.getLogger("saleor.graphql.errors.handled") class GraphQLView(View): # This class is our implementation of `graphene_django.views.GraphQLView`, # which was extended to support the following features: # - Playground as default the API explorer (see # https://github.com/prisma/graphql-playground) # - file upload (https://github.com/lmcgartland/graphene-file-upload) # - query batching # - CORS schema = None
def factory(self, **config): def setupfunc(): assert False, "setupfunc should not be called" return SimpleLazyObject(setupfunc)
def __call__(self, request): request.user = SimpleLazyObject(lambda: get_user(request)) return self.get_response(request)