예제 #1
0
파일: backends.py 프로젝트: taliamaee/zulip
    def render(self, context=None, request=None):
        # type: (Optional[Union[Dict[str, Any], Context]], Optional[HttpRequest]) -> text_type
        if context is None:
            context = {}

        if isinstance(context, Context):
            # Jinja2 expects a dictionary
            # This condition makes sure that `flatten` is called only when
            # `context` is an instance of `Context`.
            #
            # Note: If we don't ignore then mypy complains about missing
            # `flatten` attribute in some members of union.
            context = context.flatten()  # type: ignore

        if request is not None:
            context['request'] = request
            context['csrf_input'] = csrf_input_lazy(request)
            context['csrf_token'] = csrf_token_lazy(request)

            for context_processor in self.context_processors:
                cp = import_string(context_processor)
                context.update(cp(request))

        if self.debug:
            template_rendered.send(sender=self, template=self, context=context)

        return self.template.render(context)
예제 #2
0
def instrumented_test_render(self, context):
    """
    An instrumented Template render method, providing a signal
    that can be intercepted by the test system Client
    """
    template_rendered.send(sender=self, template=self, context=context)
    return self.nodelist.render(context)
예제 #3
0
파일: base.py 프로젝트: tobych/django-jinja
 def instrumented_render(template_object, *args, **kwargs):
     context = dict(*args, **kwargs)
     template_rendered.send(sender=template_object,
                            template=template_object,
                            context=context)
     return Jinja2Template._original_render(template_object, *args,
                                            **kwargs)
    def render_to_response(self, data, response, jsoncallback=None, *args, **kwargs):
        # for successfull testing with django test client
        plain_data = deepcopy(data)

        if type(data) == dict:
            for key in data:
                if isinstance(data[key], QuerySet) \
                    or (isinstance(data[key], list)
                        and len(data[key]) != 0
                        and isinstance(data[key][0], models.Model)):
                    data[key] = self.serialize_qs(data[key], *args, **kwargs)
        else:
            data = self.serialize_qs(data, *args, **kwargs)
        if 'extras' in kwargs: del kwargs['extras']
        if 'relations' in kwargs: del kwargs['relations']
        if settings.DEBUG:
            kwargs['indent'] = 4
        response.content = simplejson.dumps(data, cls=DjangoJSONEncoder, *args, **kwargs)

        template_rendered.send(sender=self, template=self, context={'data':plain_data, 'sdata': data})

        if self.format == 'json':
            response['Content-Type'] = 'application/x-javascript; charset=utf-8'
            if jsoncallback:
                response.content = ''.join([jsoncallback, '(', force_unicode(response.content), ')'])

        return response
예제 #5
0
파일: backends.py 프로젝트: Jianchun1/zulip
        def render(self, context=None, request=None):
            # type: (Optional[Union[Dict[str, Any], Context]], Optional[HttpRequest]) -> text_type
            if context is None:
                context = {}

            if isinstance(context, Context):
                # Jinja2 expects a dictionary
                # This condition makes sure that `flatten` is called only when
                # `context` is an instance of `Context`.
                #
                # Note: If we don't ignore then mypy complains about missing
                # `flatten` attribute in some members of union.
                context = context.flatten()  # type: ignore

            if request is not None:
                context['request'] = request
                context['csrf_input'] = csrf_input_lazy(request)
                context['csrf_token'] = csrf_token_lazy(request)

                for context_processor in self.context_processors:
                    cp = import_string(context_processor)
                    context.update(cp(request))

            if self.debug:
                template_rendered.send(sender=self, template=self,
                                       context=context)

            return self.template.render(context)
예제 #6
0
def instrumented_test_render(self, context):
    """
    An instrumented Template render method, providing a signal
    that can be intercepted by the test system Client
    """
    template_rendered.send(sender=self, template=self, context=context)
    return self.nodelist.render(context)
예제 #7
0
파일: __init__.py 프로젝트: passy/coffin
    def render(self, context=None):
        """Differs from Django's own render() slightly in that makes the
        ``context`` parameter optional. We try to strike a middle ground
        here between implementing Django's interface while still supporting
        Jinja's own call syntax as well.
        """
        self.origin = Origin(self.filename)
        template_rendered.send(sender=self, template=self, context=context)

        if context is None:
            context = {}
        else:
            context = dict_from_django_context(context)
        assert isinstance(context, dict)  # Required for **-operator.
        template = super(Template, self).render(**context)
        return template
예제 #8
0
    def render_to_response(self,
                           data,
                           response,
                           jsoncallback=None,
                           *args,
                           **kwargs):
        # for successfull testing with django test client
        plain_data = deepcopy(data)

        if type(data) == dict:
            for key in data:
                if isinstance(data[key], QuerySet) \
                    or (isinstance(data[key], list)
                        and len(data[key]) != 0
                        and isinstance(data[key][0], models.Model)):
                    data[key] = self.serialize_qs(data[key], *args, **kwargs)
        else:
            data = self.serialize_qs(data, *args, **kwargs)
        if 'extras' in kwargs: del kwargs['extras']
        if 'relations' in kwargs: del kwargs['relations']
        if settings.DEBUG:
            kwargs['indent'] = 4
        response.content = simplejson.dumps(data,
                                            cls=DjangoJSONEncoder,
                                            *args,
                                            **kwargs)

        template_rendered.send(sender=self,
                               template=self,
                               context={
                                   'data': plain_data,
                                   'sdata': data
                               })

        if self.format == 'json':
            response[
                'Content-Type'] = 'application/x-javascript; charset=utf-8'
            if jsoncallback:
                response.content = ''.join(
                    [jsoncallback, '(',
                     force_unicode(response.content), ')'])

        return response
예제 #9
0
파일: backends.py 프로젝트: 150vb/zulip
        def render(self, context=None, request=None):
            if context is None:
                context = {}

            if isinstance(context, Context):
                context = context.flatten()  # Jinja2 expects a dictionary

            if request is not None:
                context['request'] = request
                context['csrf_input'] = csrf_input_lazy(request)
                context['csrf_token'] = csrf_token_lazy(request)

                for context_processor in self.context_processors:
                    cp = import_string(context_processor)
                    context.update(cp(request))

            if self.debug:
                template_rendered.send(sender=self, template=self,
                                       context=context)

            return self.template.render(context)
예제 #10
0
        def render(self, context=None, request=None):
            # type: (Optional[Union[Dict[str, Any], Context]], Optional[HttpRequest]) -> Text
            if context is None:
                context = {}

            if isinstance(context, Context):
                # Jinja2 expects a dictionary
                # This condition makes sure that `flatten` is called only when
                # `context` is an instance of `Context`.
                #
                # Note: If we don't ignore then mypy complains about missing
                # `flatten` attribute in some members of union.
                context = context.flatten()  # type: ignore

            result = super(Template, self).render(context=context,
                                                  request=request)

            if self.backend.debug:
                template_rendered.send(sender=self, template=self,
                                       context=context)

            return result
예제 #11
0
    def rendered_content(self):
        """Return the freshly rendered content for the template and context
        described by this JinjaTemplateResponse object."""

        # first, get us the template object
        template = self.resolve_template(self.template_name)

        # sanity check: if this is a **Django** template object, then I just want
        # to go through superclass' system
        if isinstance(template, DjangoTemplate):
            context = super(JinjaTemplateResponse, self).resolve_context(self.context_data)
        else:
            context = self.resolve_context(self.context_data)

            # sanity check: are we in test mode?
            if self._in_test_mode:
                template_rendered.send(sender=self, template=self, context=context)

        # now render the template and return the content
        content = template.render(context)

        return content
예제 #12
0
 def render(self, context):
     template_rendered.send(
         sender=self, template=self, context=context)
     return r(self, context)
예제 #13
0
 def mako_callable_(context, *args, **kwargs):
   template_rendered.send(sender=self, template=self, context=context)
   return self.original_callable_[-1](context, *args, **kwargs)
예제 #14
0
import logging
예제 #15
0
 def instrumented_render(template_object, *args, **kwargs):
     context = dict(*args, **kwargs)
     template_rendered.send(sender=template_object,
                            template=template_object,
                            context=context)
     return Jinja2Template._original_render(template_object, *args, **kwargs)
예제 #16
0
 def render(self, context=None):
     template_rendered.send(sender=self, template=self,
         context=context)
     return func(self, context)
예제 #17
0
 def mako_callable_(context, *args, **kwargs):
     template_rendered.send(sender=self, template=self, context=context)
     return self.original_callable_[-1](context, *args, **kwargs)