1# -*- coding: utf-8 -*- 2from cms.models import CMSPlugin, Placeholder 3from cms.models.aliaspluginmodel import AliasPluginModel 4from cms.models.placeholderpluginmodel import PlaceholderReference 5from cms.plugin_base import CMSPluginBase, PluginMenuItem 6from cms.plugin_pool import plugin_pool 7from cms.utils.urlutils import admin_reverse 8from django.conf.urls import url 9from django.http import HttpResponseForbidden, HttpResponseBadRequest, HttpResponse 10from django.middleware.csrf import get_token 11from django.utils.translation import ugettext, ugettext_lazy as _, get_language 12 13 14class PlaceholderPlugin(CMSPluginBase): 15 name = _("Placeholder") 16 parent_classes = ['0'] # so you will not be able to add it something 17 #require_parent = True 18 render_plugin = False 19 admin_preview = False 20 system = True 21 22 model = PlaceholderReference 23 24 25plugin_pool.register_plugin(PlaceholderPlugin) 26 27 28class AliasPlugin(CMSPluginBase): 29 name = _("Alias") 30 allow_children = False 31 model = AliasPluginModel 32 render_template = "cms/plugins/alias.html" 33 system = True 34 35 @classmethod 36 def get_render_queryset(cls): 37 queryset = super(AliasPlugin, cls).get_render_queryset() 38 return queryset.select_related('plugin', 'alias_placeholder') 39 40 @classmethod 41 def get_extra_plugin_menu_items(cls, request, plugin): 42 return [ 43 PluginMenuItem( 44 _("Create Alias"), 45 admin_reverse("cms_create_alias"), 46 data={'plugin_id': plugin.pk, 'csrfmiddlewaretoken': get_token(request)}, 47 ) 48 ] 49 50 @classmethod 51 def get_extra_placeholder_menu_items(cls, request, placeholder): 52 return [ 53 PluginMenuItem( 54 _("Create Alias"), 55 admin_reverse("cms_create_alias"), 56 data={'placeholder_id': placeholder.pk, 'csrfmiddlewaretoken': get_token(request)}, 57 ) 58 ] 59 60 def get_plugin_urls(self): 61 return [ 62 url(r'^create_alias/$', self.create_alias, name='cms_create_alias'), 63 ] 64 65 @classmethod 66 def get_empty_change_form_text(cls, obj=None): 67 original = super(AliasPlugin, cls).get_empty_change_form_text(obj=obj) 68 69 if not obj: 70 return original 71 72 instance = obj.get_plugin_instance()[0] 73 74 if not instance: 75 # Ghost plugin 76 return original 77 78 aliased_placeholder_id = instance.get_aliased_placeholder_id() 79 80 if not aliased_placeholder_id: 81 # Corrupt (sadly) Alias plugin 82 return original 83 84 aliased_placeholder = Placeholder.objects.get(pk=aliased_placeholder_id) 85 86 origin_page = aliased_placeholder.page 87 88 if not origin_page: 89 # Placeholder is not attached to a page 90 return original 91 92 # I have a feeling this could fail with a NoReverseMatch error 93 # if this is the case, then it's likely a corruption. 94 page_url = origin_page.get_absolute_url(language=obj.language) 95 page_title = origin_page.get_title(language=obj.language) 96 97 message = ugettext('This is an alias reference, ' 98 'you can edit the content only on the ' 99 '<a href="%(page_url)s?edit" target="_parent">%(page_title)s</a> page.') 100 return message % {'page_url': page_url, 'page_title': page_title} 101 102 def create_alias(self, request): 103 if not request.user.is_staff: 104 return HttpResponseForbidden("not enough privileges") 105 if not 'plugin_id' in request.POST and not 'placeholder_id' in request.POST: 106 return HttpResponseBadRequest("plugin_id or placeholder_id POST parameter missing.") 107 plugin = None 108 placeholder = None 109 if 'plugin_id' in request.POST: 110 pk = request.POST['plugin_id'] 111 try: 112 plugin = CMSPlugin.objects.get(pk=pk) 113 except CMSPlugin.DoesNotExist: 114 return HttpResponseBadRequest("plugin with id %s not found." % pk) 115 if 'placeholder_id' in request.POST: 116 pk = request.POST['placeholder_id'] 117 try: 118 placeholder = Placeholder.objects.get(pk=pk) 119 except Placeholder.DoesNotExist: 120 return HttpResponseBadRequest("placeholder with id %s not found." % pk) 121 if not placeholder.has_change_permission(request.user): 122 return HttpResponseBadRequest("You do not have enough permission to alias this placeholder.") 123 clipboard = request.toolbar.clipboard 124 clipboard.cmsplugin_set.all().delete() 125 language = get_language() 126 if plugin: 127 language = plugin.language 128 alias = AliasPluginModel(language=language, placeholder=clipboard, plugin_type="AliasPlugin") 129 if plugin: 130 alias.plugin = plugin 131 if placeholder: 132 alias.alias_placeholder = placeholder 133 alias.save() 134 return HttpResponse("ok") 135 136 137plugin_pool.register_plugin(AliasPlugin) 138