예제 #1
0
def cosinnus_render_single_object(context, object, *args, **kwargs):
    """
    Render a single cosinnus BaseTaggableObject using the
     configured renderer for that model type
    (in each cosinnus app's `cosinnus_app.ATTACHABLE_OBJECT_RENDERERS`).

    :param object: the source object to render
    """
    NAMED_ARGS = ['hide_group_name', 'no_space']

    model_name = object.__class__.__module__.split(
        '.')[0] + '.' + object.__class__.__name__

    # find manager object for attached object type
    Renderer = attached_object_registry.get(model_name)  # Renderer is a class

    rendered_output = ''
    if Renderer:
        for arg in NAMED_ARGS:
            if arg in kwargs:
                context[arg] = kwargs[arg]
        # pass the list to that manager and expect a rendered html string
        rendered_output = Renderer.render_single(context, object)
    elif settings.DEBUG:
        rendered_output = _(
            '<i>Renderer for %(model_name)s not found!</i>') % {
                'model_name': model_name
            }

    return rendered_output
예제 #2
0
def cosinnus_render_attached_objects(context,
                                     source,
                                     filter=None,
                                     skipImages=False):
    """
    Renders all attached files on a given source cosinnus object. This will
    collect and group all attached objects (`source.attached_objects`) by their
    model group and send them to the configured renderer for that model type
    (in each cosinnus app's `cosinnus_app.ATTACHABLE_OBJECT_RENDERERS`).

    :param source: the source object to check for attached objects
    :param filter: a comma seperated list of allowed Object types to be
        rendered. eg.: 'cosinnus_event.Event,cosinnus_file.FileEntry' will
        allow only Files and events to be rendered.
    :param skipImages: will not display image type attached files
    """
    attached_objects = source.attached_objects.all()
    allowed_types = filter.replace(' ', '').split(',') if filter else []

    typed_objects = defaultdict(list)
    for att in attached_objects:
        attobj = att.target_object
        content_model = att.model_name
        if filter and content_model not in allowed_types:
            continue
        if getattr(attobj, 'is_image', False):
            continue
        if attobj is not None:
            typed_objects[content_model].append(attobj)

    rendered_output = []
    for model_name, objects in six.iteritems(typed_objects):
        # find manager object for attached object type
        Renderer = attached_object_registry.get(
            model_name)  # Renderer is a class
        if Renderer:
            # pass the list to that manager and expect a rendered html string
            rendered_output.append(Renderer.render(context, objects))
        elif settings.DEBUG:
            rendered_output.append(
                _('<i>Renderer for %(model_name)s not found!</i>') %
                {'model_name': model_name})

    return ''.join(rendered_output)