Skip to content

Commit

Permalink
Merge ad0b110 into 60b5612
Browse files Browse the repository at this point in the history
  • Loading branch information
nmashton committed Oct 10, 2016
2 parents 60b5612 + ad0b110 commit 5658112
Show file tree
Hide file tree
Showing 10 changed files with 109 additions and 4 deletions.
21 changes: 20 additions & 1 deletion cms/admin/placeholderadmin.py
Expand Up @@ -152,7 +152,7 @@ def _get_attached_admin(self, placeholder):

def get_urls(self):
"""
Register the plugin specific urls (add/edit/copy/remove/move)
Register the plugin specific urls (add/edit/copy/remove/move/hide)
"""
info = "%s_%s" % (self.model._meta.app_label, self.model._meta.model_name)
pat = lambda regex, fn: url(regex, self.admin_site.admin_view(fn), name='%s_%s' % (info, fn.__name__))
Expand All @@ -163,6 +163,7 @@ def get_urls(self):
pat(r'delete-plugin/(%s)/$' % SLUG_REGEXP, self.delete_plugin),
pat(r'clear-placeholder/(%s)/$' % SLUG_REGEXP, self.clear_placeholder),
pat(r'move-plugin/$', self.move_plugin),
pat(r'hide-plugin/$', self.hide_plugin),
]
return url_patterns + super(PlaceholderAdminMixin, self).get_urls()

Expand Down Expand Up @@ -743,3 +744,21 @@ def clear_placeholder(self, request, placeholder_id):
}
return TemplateResponse(request, "admin/cms/page/plugin/delete_confirmation.html", context,
current_app=self.admin_site.name)

@method_decorator(require_POST)
@xframe_options_sameorigin
def hide_plugin(self, request):
try:
plugin_id = get_int(request.POST.get('plugin_id'))
except TypeError:
raise RuntimeError("'plugin_id' is a required parameter.")

plugin = get_object_or_404(CMSPlugin, pk=plugin_id)

plugin.cmsplugin_hidden = not plugin.cmsplugin_hidden
plugin.save()

return HttpResponse(
json.dumps({'reload': True}),
content_type='application/json'
)
2 changes: 1 addition & 1 deletion cms/cms_plugins.py
Expand Up @@ -39,7 +39,7 @@ def render(self, context, instance, placeholder):
context = super(AliasPlugin, self).render(context, instance, placeholder)
cms_content_renderer = context.get('cms_content_renderer')

if not cms_content_renderer or instance.is_recursive():
if not cms_content_renderer or instance.is_recursive() or instance.cmsplugin_hidden:
return context

if instance.plugin_id:
Expand Down
19 changes: 19 additions & 0 deletions cms/migrations/0017_cmsplugin_cmsplugin_hidden.py
@@ -0,0 +1,19 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('cms', '0016_auto_20160608_1535'),
]

operations = [
migrations.AddField(
model_name='cmsplugin',
name='cmsplugin_hidden',
field=models.BooleanField(default=False, verbose_name=b'Hide plugin contents'),
),
]
19 changes: 19 additions & 0 deletions cms/migrations/0018_auto_20161010_1545.py
@@ -0,0 +1,19 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('cms', '0017_cmsplugin_cmsplugin_hidden'),
]

operations = [
migrations.AlterField(
model_name='cmsplugin',
name='cmsplugin_hidden',
field=models.BooleanField(editable=False, default=False, verbose_name='Hide plugin contents'),
),
]
1 change: 1 addition & 0 deletions cms/models/pluginmodel.py
Expand Up @@ -188,6 +188,7 @@ class CMSPlugin(six.with_metaclass(PluginModelBase, MP_Node)):
plugin_type = models.CharField(_("plugin_name"), max_length=50, db_index=True, editable=False)
creation_date = models.DateTimeField(_("creation date"), editable=False, default=timezone.now)
changed_date = models.DateTimeField(auto_now=True)
cmsplugin_hidden = models.BooleanField('Hide plugin contents', default=False, editable=False)
child_plugin_instances = None
translatable_content_excluded_fields = []

Expand Down
18 changes: 17 additions & 1 deletion cms/plugin_base.py
Expand Up @@ -8,6 +8,7 @@
from django.contrib import admin
from django.contrib import messages
from django.core.exceptions import ImproperlyConfigured, ValidationError
from django.middleware.csrf import get_token
from django.template.defaultfilters import force_escape
from django.utils import six
from django.utils.encoding import force_text, python_2_unicode_compatible, smart_str
Expand All @@ -17,6 +18,7 @@
from cms.exceptions import SubClassNeededError
from cms.models import CMSPlugin
from cms.utils import get_cms_setting
from cms.utils.urlutils import admin_reverse


class CMSPluginBaseMetaclass(forms.MediaDefiningClass):
Expand Down Expand Up @@ -456,7 +458,21 @@ def get_extra_global_plugin_menu_items(self, request, plugin):
pass

def get_extra_local_plugin_menu_items(self, request, plugin):
pass
hidden = plugin.cmsplugin_hidden

return [
PluginMenuItem(
name='Show' if hidden else 'Hide',
url=admin_reverse('cms_page_hide_plugin'),
data={
'plugin_id': plugin.pk,
'csrfmiddlewaretoken': get_token(request),
},
attributes={
'cms-icon': 'eye',
}
)
]

def __repr__(self):
return smart_str(self.name)
Expand Down
8 changes: 7 additions & 1 deletion cms/plugin_rendering.py
Expand Up @@ -339,7 +339,13 @@ def render_plugin(self, instance, context, placeholder=None, editable=False):
template = plugin._get_render_template(context, instance, placeholder)
template = self.get_cached_template(template)

content = template.render(context)
# The basic content of the visible plugin is given by rendering its
# template. If the plugin is to be hidden, this step must be halted
# and an empty string supplied as the content.
if instance.cmsplugin_hidden:
content = ''
else:
content = template.render(context)

for processor in iterload_objects(get_cms_setting('PLUGIN_PROCESSORS')):
content = processor(instance, placeholder, content, context)
Expand Down
3 changes: 3 additions & 0 deletions cms/static/cms/sass/components/_subnav.scss
Expand Up @@ -131,6 +131,9 @@
&[data-cms-icon=bin] {
@include icon(bin);
}
&[data-cms-icon=eye] {
@include icon(eye);
}
&[href*=alias_plugin] {
@include icon(alias);
}
Expand Down
1 change: 1 addition & 0 deletions cms/templates/cms/toolbar/dragitem.html
Expand Up @@ -6,6 +6,7 @@
{% if clipboard %} cms-draggable-from-clipboard{% endif %}">

<div class="cms-dragitem cms-dragitem-handler
{% if plugin.cmsplugin_hidden %} cmsplugin-hidden{% endif %}
{% if plugin.child_plugin_instances %} cms-dragitem-collapsable{% endif %}">
{% language request.toolbar.toolbar_language %}
{% if not disabled_child %}
Expand Down
21 changes: 21 additions & 0 deletions cms/tests/test_admin.py
Expand Up @@ -623,6 +623,27 @@ def test_move_plugin(self):
self.assertEqual(response.status_code, 200)
self.assertEqual(json.loads(response.content.decode('utf8')), expected)

def test_hide_plugin(self):
page = self.get_page()
source, target = list(page.placeholders.all())[:2]
plugin = add_plugin(source, 'TextPlugin', 'en', body='test', cmsplugin_hidden=True)

expected = {
'reload': True
}

admin_user = self.get_admin()
with self.login_user_context(admin_user):
request = self.get_request(post_data={'plugin_id': plugin.pk})
response = self.admin_class.hide_plugin(request)

self.assertEqual(response.status_code, 200)
self.assertEqual(json.loads(response.content.decode('utf8')), expected)

updated = CMSPlugin.objects.get(pk=plugin.pk)
self.assertFalse(updated.cmsplugin_hidden)


def test_move_language(self):
page = self.get_page()
source, target = list(page.placeholders.all())[:2]
Expand Down

0 comments on commit 5658112

Please sign in to comment.