Exemple #1
0
def find_all_templates(template_name):
    template = loader.get_template(template_name)

    templates = {}

    def _store_template_info(sender, **kwargs):
        template = kwargs['template']
        if template.name in settings.TEMPLATES_BLACKLIST:
            # We don't show templates that cannot be edited.
            return
        if not template.name in templates:
            # For some reasons the Django/Jinja2 framework might load the same
            # templates multiple times.
            templates.update({
                template.name: {
                    "name": template.name,
                    "index": len(templates)
                }
            })

    template_loaded.connect(_store_template_info)
    template_rendered.connect(_store_template_info)
    try:
        template.render()
    finally:
        template_rendered.disconnect(_store_template_info)
        template_loaded.disconnect(_store_template_info)

    return templates.values()
    def process_response(self, request, response):

        if not hasattr(request, '_frelic'):
            return response

        template_rendered.disconnect(request._frelic.count_templates)

        if response.status_code != 200:
            return response

        # Check for responses where the toolbar can't be inserted.
        content_encoding = response.get('Content-Encoding', '')
        content_type = response.get('Content-Type', '').split(';')[0]
        if any((getattr(response, 'streaming', False),
                'gzip' in content_encoding,
                content_type not in _HTML_TYPES)):
            return response

        request._frelic.load_metrics()

        ga_code = request._frelic.ga_code()

        content = force_text(response.content, encoding=settings.DEFAULT_CHARSET)

        response.content = content.replace(u"<!-- /* FRELIC DATA */ -->", ga_code)
        
        if response.get('Content-Length', None):
            response['Content-Length'] = len(response.content)

        return response
    def __exit__(self, exc_type, exc_value, traceback):
        template_rendered.disconnect(self.on_template_render)
        if exc_type is not None:
            return

        if not self.test():
            message = self.message()
            if len(self.rendered_templates) == 0:
                message += " No template was rendered."
            else:
                message += " Following templates were rendered: %s" % (", ".join(self.rendered_template_names))
            self.test_case.fail(message)
Exemple #4
0
    def __exit__(self, exc_type, exc_value, traceback):
        template_rendered.disconnect(self.on_template_render)
        if exc_type is not None:
            return

        if not self.test():
            message = self.message()
            if len(self.rendered_templates) == 0:
                message += u' No template was rendered.'
            else:
                message += u' Following templates were rendered: %s' % (
                    ', '.join(self.rendered_template_names))
            self.test_case.fail(message)
Exemple #5
0
    def templates_used_to_render(self, subject_template, render_context=None):
        """Emulate django.test.client.Client (see request method)."""
        from django.test.signals import template_rendered

        templates_used = []

        def receive_template_signal(sender, template, context, **_kwargs):
            templates_used.append(template.name)

        template_rendered.connect(receive_template_signal,
                                  dispatch_uid="test_method")
        subject_template.render(render_context or Context({}))
        template_rendered.disconnect(dispatch_uid="test_method")
        return templates_used
    def process_response(self, request, response):
        template_rendered.disconnect(self._store_template_info)

        if not show_frontadmin(request):
            return response

        # Check for responses where the toolbar can't be inserted.
        content_encoding = response.get('Content-Encoding', '')
        content_type = response.get('Content-Type', '').split(';')[0]
        if any((getattr(response, 'streaming', False),
                'gzip' in content_encoding,
                content_type not in _HTML_TYPES)):
            return response

        # Insert the toolbar in the response.
        content = force_text(response.content, encoding=settings.DEFAULT_CHARSET)
        insert_before = '</body>'
        pattern = re.escape(insert_before)
        bits = re.split(pattern, content, flags=re.IGNORECASE)
        if len(bits) > 1:
            #fascript = '<link href="/static/front_admin/js/codemirror/lib/codemirror.css" rel="stylesheet" />\n'
            #fascript += '<link href="/static/front_admin/js/codemirror/theme/duotone-light.css" rel="stylesheet" />\n'
            #fascript += '<link href="https://fonts.googleapis.com/css?family=Ubuntu:300,400,500" rel="stylesheet">\n'
            #fascript += '<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">\n'
            #fascript += '<link href="/static/front_admin/css/default.css" rel="stylesheet">\n'
            fascript = '<script type="application/json" id="fa-data">' + json.dumps(self.templates) + '</script>\n'
            fascript += '<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>\n'
            fascript += '<script src="/static/front_admin/js/codemirror/codemirror.min.js"></script>\n'
            fascript += '<script src="/static/front_admin/js/codemirror/addon/mode/overlay.js"></script>\n'
            fascript += '<script src="/static/front_admin/js/codemirror/mode/django/django.js"></script>\n'
            fascript += '<script src="/static/front_admin/js/builtin_tags.js"></script>\n'
            fascript += '<script src="/static/front_admin/js/variable.js"></script>\n'
            fascript += '<script src="/static/front_admin/js/parser.js"></script>\n'
            fascript += '<script src="/static/front_admin/js/processor.js"></script>\n'
            fascript += '<script src="/static/front_admin/js/default.js"></script>\n'
            fascript += '<script src="/static/front_admin/js/start.js"></script>\n'

            bits[-2] += fascript
            response.content = insert_before.join(bits)
            if response.get('Content-Length', None):
                response['Content-Length'] = len(response.content)
        return response
def find_all_templates(template_name):
    template = loader.get_template(template_name)

    templates = {}
    def _store_template_info(sender, **kwargs):
        template = kwargs['template']
        if template.name in settings.TEMPLATES_BLACKLIST:
            # We don't show templates that cannot be edited.
            return
        if not template.name in templates:
            # For some reasons the Django/Jinja2 framework might load the same
            # templates multiple times.
            templates.update({template.name:
                {"name": template.name, "index": len(templates)}})

    template_loaded.connect(_store_template_info)
    template_rendered.connect(_store_template_info)
    try:
        template.render()
    finally:
        template_rendered.disconnect(_store_template_info)
        template_loaded.disconnect(_store_template_info)

    return templates.values()
Exemple #8
0
 def tearDown(self):
     template_rendered.disconnect(self.store_rendered_template)
Exemple #9
0
 def tearDown(self):
     template_rendered.disconnect(self.template_rendered_listener)
Exemple #10
0
def disable_instrumentation():
    template_rendered.disconnect(_store_template_info)
    template_loaded.disconnect(_store_template_info)
Exemple #11
0
def disable_instrumentation():
    template_rendered.disconnect(_store_template_info)
    template_loaded.disconnect(_store_template_info)
 def tearDown(self):
     settings.TEMPLATE_DIRS = self.old_TEMPLATE_DIRS
     
     app_settings.EMAIL_CONFIRMATION_DAYS = self._old_confirmation_days
     
     template_rendered.disconnect(self._template_rendered)
 def tearDown(self):
     template_rendered.disconnect(self.template_rendered_listener)
Exemple #14
0
 def disable_instrumentation(self):
     template_rendered.disconnect(self._store_template_info)
Exemple #15
0
 def tearDown(self):
     template_rendered.disconnect(self.store_rendered_template)