Skip to content

Commit

Permalink
Add render_model_add_block tag
Browse files Browse the repository at this point in the history
  • Loading branch information
mkoistinen committed Feb 15, 2015
1 parent 4a75fae commit 7725f10
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 1 deletion.
2 changes: 1 addition & 1 deletion cms/templates/cms/toolbar/plugin.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{% spaceless %}{% load i18n l10n sekizai_tags static cms_tags %}

<div class="cms_plugin cms_plugin-{% if generic %}{{ generic.app_label }}-{{ generic.module_name }}-{% if attribute_name %}{{ attribute_name|slugify }}-{% endif %}{% endif %}{{ instance.pk|unlocalize }}{% if render_model_icon %} cms_render_model_icon{% elif render_model %} cms_render_model{% elif render_model_block %} cms_render_model cms_render_model_block{% elif render_model_add %} cms_render_model_add{% endif %}">{% if render_model_icon %}<img src="{% static 'cms/img/toolbar/render_model_placeholder.png' %}">{% elif render_model_add %}<img src="{% static 'cms/img/toolbar/render_model_placeholder.png' %}">{% else %}{{ rendered_content }}{% endif %}</div>
<div class="cms_plugin cms_plugin-{% if generic %}{{ generic.app_label }}-{{ generic.module_name }}-{% if attribute_name %}{{ attribute_name|slugify }}-{% endif %}{% endif %}{{ instance.pk|unlocalize }}{% if render_model_icon %} cms_render_model_icon{% elif render_model %} cms_render_model{% elif render_model_block %} cms_render_model cms_render_model_block{% elif render_model_add %} cms_render_model_add{% endif %}">{% if content %}{{ content }}{% elif render_model_icon %}<img src="{% static 'cms/img/toolbar/render_model_placeholder.png' %}">{% elif render_model_add %}<img src="{% static 'cms/img/toolbar/render_model_placeholder.png' %}">{% else %}{{ rendered_content }}{% endif %}</div>

{% addtoblock "js" %}
<script>
Expand Down
47 changes: 47 additions & 0 deletions cms/templatetags/cms_tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -983,6 +983,53 @@ def get_context(self, context, instance, language,
register.tag(CMSEditableObjectAdd)


class CMSEditableObjectAddBlock(CMSEditableObject):
"""
Templatetag that links arbitrary content to the addform for the specified
model (based on the provided model instance).
"""
name = 'render_model_add_block'
options = Options(
Argument('instance'),
Argument('language', default=None, required=False),
Argument('view_url', default=None, required=False),
Argument('view_method', default=None, required=False),
'as',
Argument('varname', required=False, resolve=False),
blocks=[('endrender_model_add_block', 'nodelist')],
)

def render_tag(self, context, **kwargs):
"""
Renders the block and then inject the resulting HTML in the template
context
"""
context.push()
template = self.get_template(context, **kwargs)
data = self.get_context(context, **kwargs)
data['content'] = mark_safe(kwargs['nodelist'].render(data))
data['rendered_content'] = data['content']
output = render_to_string(template, data)
context.pop()
if kwargs.get('varname'):
context[kwargs['varname']] = output
return ''
else:
return output

def get_context(self, context, instance, language,
view_url, view_method, varname, nodelist):
"""
Uses _get_empty_context and adds the `render_model_icon` variable.
"""
extra_context = self._get_empty_context(context, instance, None,
language, view_url, view_method,
editmode=False)
extra_context['render_model_add'] = True
return extra_context
register.tag(CMSEditableObjectAddBlock)


class CMSEditableObjectBlock(CMSEditableObject):
"""
Templatetag that links a content extracted from a generic django model
Expand Down

0 comments on commit 7725f10

Please sign in to comment.