Exemplo n.º 1
0
def render_template_string(request,
                           ctx,
                           template,
                           minify_html=None,
                           extra_ctx=None):
    with graphite_timer('jinja2.render_template_string'):
        if minify_html is None:
            minify_html = template.endswith('.html')

        if extra_ctx:
            if not is_seq_not_string(extra_ctx):
                extra_ctx = [extra_ctx]

            new_ctx = {}
            page_load_hooks = ctx.get('__js_page_load_hooks', [])

            for ctx_callable in extra_ctx:
                ctx_item = ctx_callable(request)
                if ctx_item:
                    if '__js_page_load_hooks' in ctx_item:
                        page_load_hooks.extend(
                            ctx_item['__js_page_load_hooks'])
                        del ctx_item['__js_page_load_hooks']
                        new_ctx.update(ctx_item)

            new_ctx.update(ctx)
            new_ctx['__js_page_load_hooks'] = page_load_hooks

            ctx = new_ctx

        with graphite_timer('jinja2.render_to_string'):
            output = render_to_string(template,
                                      ctx,
                                      context_instance=RequestContext(request))

        if isinstance(output, unicode):
            output = output.encode('utf-8')

        if ctx and ctx.get('__no_minify'):
            minify_html = False

        if minify_html:
            with graphite_timer('jinja2.minify_html'):
                output = slimmer.html_slimmer(output)

        return output
Exemplo n.º 2
0
def render_template_string(request, ctx, template, minify_html=None, extra_ctx=None):
    with graphite_timer('jinja2.render_template_string'):
        if minify_html is None:
            minify_html = template.endswith('.html')

        if extra_ctx:
            if not is_seq_not_string(extra_ctx):
                extra_ctx = [extra_ctx]

            new_ctx = {}
            page_load_hooks = ctx.get('__js_page_load_hooks', [])

            for ctx_callable in extra_ctx:
                ctx_item = ctx_callable(request)
                if ctx_item:
                    if '__js_page_load_hooks' in ctx_item:
                        page_load_hooks.extend(ctx_item['__js_page_load_hooks'])
                        del ctx_item['__js_page_load_hooks']
                        new_ctx.update(ctx_item)

            new_ctx.update(ctx)
            new_ctx['__js_page_load_hooks'] = page_load_hooks

            ctx = new_ctx

        with graphite_timer('jinja2.render_to_string'):
            output = render_to_string(template, ctx, context_instance=RequestContext(request))

        if isinstance(output, unicode):
            output = output.encode('utf-8')

        if ctx and ctx.get('__no_minify'):
            minify_html = False

        if minify_html:
            with graphite_timer('jinja2.minify_html'):
                output = slimmer.html_slimmer(output)

        return output
Exemplo n.º 3
0
def bulk_load_related(objs, field_name):
    with graphite_timer('c4g.bulk_load_related', extras={'field':
                                                         field_name}) as timer:
        if not objs:
            timer['empty'] = True
            return []

        if not is_seq_not_string(objs):
            # if we were passed a single object, make it a list
            objs = [objs]

        timer.incr('objs', len(objs))

        sample_obj = objs[0]
        field = sample_obj._meta.get_field(field_name)
        timer['model'] = '%s.%s' % (sample_obj._meta.app_label,
                                    sample_obj._meta.module_name)
        cache_name = field.get_cache_name()

        # Additionally: possibly update this to coalesce objects
        pending_objs = itertools.ifilter(
            lambda obj: not hasattr(obj, cache_name), objs)
        unique_fks = set(
            itertools.ifilter(None, (itertools.imap(
                operator.attrgetter(field.attname), pending_objs))))

        related_objs = field.rel.to._c4g_manager.by_pks_as_dict(unique_fks)
        for obj in objs:
            related_obj_id = getattr(obj, field.attname)
            if hasattr(obj, cache_name):
                # In this case, we want to reduce all of the related objs
                # to a single instance
                if related_obj_id not in related_objs:
                    related_objs[related_obj_id] = getattr(obj, cache_name)
                else:
                    setattr(obj, cache_name, related_objs[related_obj_id])

                continue

            related_obj = related_objs.get(related_obj_id)
            if field.null or related_obj:
                setattr(obj, cache_name, related_obj)
            if field.unique and related_obj:
                setattr(related_obj, field.related.get_cache_name(), obj)

        return related_objs.values()
Exemplo n.º 4
0
Arquivo: data.py Projeto: 0xMF/alpha
def bulk_load_related(objs, field_name):
    with graphite_timer('c4g.bulk_load_related', extras={'field': field_name}) as timer:
        if not objs:
            timer['empty'] = True
            return []

        if not is_seq_not_string(objs):
            # if we were passed a single object, make it a list
            objs = [objs]

        timer.incr('objs', len(objs))

        sample_obj = objs[0]
        field = sample_obj._meta.get_field(field_name)
        timer['model'] = '%s.%s' % (sample_obj._meta.app_label, sample_obj._meta.module_name)
        cache_name = field.get_cache_name()

        # Additionally: possibly update this to coalesce objects
        pending_objs = itertools.ifilter(lambda obj: not hasattr(obj, cache_name), objs)
        unique_fks = set(itertools.ifilter(None, (itertools.imap(operator.attrgetter(field.attname), pending_objs))))

        related_objs = field.rel.to._c4g_manager.by_pks_as_dict(unique_fks)
        for obj in objs:
            related_obj_id = getattr(obj, field.attname)
            if hasattr(obj, cache_name):
                # In this case, we want to reduce all of the related objs
                # to a single instance
                if related_obj_id not in related_objs:
                    related_objs[related_obj_id] = getattr(obj, cache_name)
                else:
                    setattr(obj, cache_name, related_objs[related_obj_id])

                continue

            related_obj = related_objs.get(related_obj_id)
            if field.null or related_obj:
                setattr(obj, cache_name, related_obj)
            if field.unique and related_obj:
                setattr(related_obj, field.related.get_cache_name(), obj)

        return related_objs.values()