예제 #1
0
def get_toolbar_plugin_struct(plugins_list, slot, page, parent=None):
    """
    Return the list of plugins to render in the toolbar.
    The dictionary contains the label, the classname and the module for the
    plugin.
    Names and modules can be defined on a per-placeholder basis using
    'plugin_modules' and 'plugin_labels' attributes in CMS_PLACEHOLDER_CONF

    :param plugins_list: list of plugins valid for the placeholder
    :param slot: placeholder slot name
    :param page: the page
    :param parent: parent plugin class, if any
    :return: list of dictionaries
    """
    template = None
    if page:
        template = page.template
    main_list = []
    for plugin in plugins_list:
        allowed_parents = plugin().get_parent_classes(slot, page)
        if parent:
            ## skip to the next if this plugin is not allowed to be a child
            ## of the parent
            if allowed_parents and parent.__name__ not in allowed_parents:
                continue
        else:
            if allowed_parents:
                continue
        modules = get_placeholder_conf("plugin_modules", slot, template, default={})
        names = get_placeholder_conf("plugin_labels", slot, template, default={})
        main_list.append({'value': plugin.value,
                          'name': force_unicode(names.get(plugin.value, plugin.name)),
                          'module': force_unicode(modules.get(plugin.value, plugin.module))})
    return sorted(main_list, key=operator.itemgetter("module"))
예제 #2
0
 def icon_alt(self, instance):
     """
     Overwrite this if necessary if text_enabled = True
     Return the 'alt' text to be used for an icon representing
     the plugin object in a text editor.
     """
     return "%s - %s" % (force_unicode(self.name), force_unicode(instance))
예제 #3
0
    def get_all_plugins(self, placeholder=None, page=None, setting_key="plugins", include_page_only=True):
        self.discover_plugins()
        self.set_plugin_meta()
        plugins = list(self.plugins.values())
        plugins.sort(key=lambda obj: force_unicode(obj.name))
        final_plugins = []
        template = page and page.get_template() or None
        allowed_plugins = get_placeholder_conf(
            setting_key,
            placeholder,
            template,
        ) or ()
        for plugin in plugins:
            include_plugin = False
            if placeholder and not plugin.require_parent:
                include_plugin = not allowed_plugins and setting_key == "plugins" or plugin.__name__ in allowed_plugins
            if plugin.page_only and not include_page_only:
                include_plugin = False
            if include_plugin:
                final_plugins.append(plugin)

        if final_plugins or placeholder:
            plugins = final_plugins

        # plugins sorted by modules
        plugins = sorted(plugins, key=lambda obj: force_unicode(obj.module))
        return plugins
예제 #4
0
    def add_plugin(self, request):
        """
        POST request should have the following data:

        - placeholder_id
        - plugin_type
        - plugin_language
        - plugin_parent (optional)
        """
        parent = None
        plugin_type = request.POST['plugin_type']
        placeholder_id = request.POST.get('placeholder_id', None)
        placeholder = get_object_or_404(Placeholder, pk=placeholder_id)
        parent_id = request.POST.get('plugin_parent', None)
        language = request.POST.get('plugin_language') or get_language_from_request(request)
        if not self.has_add_plugin_permission(request, placeholder, plugin_type):
            return HttpResponseForbidden(force_unicode(_('You do not have permission to add a plugin')))
        try:
            has_reached_plugin_limit(placeholder, plugin_type, language,
                                     template=self.get_placeholder_template(request, placeholder))
        except PluginLimitReached as er:
            return HttpResponseBadRequest(er)
            # page add-plugin
        if not parent_id:
            position = request.POST.get('plugin_order',
                                        CMSPlugin.objects.filter(language=language, placeholder=placeholder).count())
        # in-plugin add-plugin
        else:
            parent = get_object_or_404(CMSPlugin, pk=parent_id)
            placeholder = parent.placeholder
            position = request.POST.get('plugin_order',
                                        CMSPlugin.objects.filter(language=language, parent=parent).count())
            # placeholder (non-page) add-plugin

        # Sanity check to make sure we're not getting bogus values from JavaScript:
        if settings.USE_I18N:
            if not language or not language in [lang[0] for lang in settings.LANGUAGES]:
                return HttpResponseBadRequest(force_unicode(_("Language must be set to a supported language!")))
            if parent and parent.language != language:
                return HttpResponseBadRequest(force_unicode(_("Parent plugin language must be same as language!")))
        else:
            language = settings.LANGUAGE_CODE
        plugin = CMSPlugin(language=language, plugin_type=plugin_type, position=position, placeholder=placeholder)

        if parent:
            plugin.position = CMSPlugin.objects.filter(parent=parent).count()
            plugin.parent_id = parent.pk
        plugin.save()
        self.post_add_plugin(request, placeholder, plugin)
        response = {
            'url': force_unicode(
                admin_reverse("%s_%s_edit_plugin" % (self.model._meta.app_label, self.model._meta.model_name),
                        args=[plugin.pk])),
            'delete': force_unicode(
                admin_reverse("%s_%s_delete_plugin" % (self.model._meta.app_label, self.model._meta.model_name),
                        args=[plugin.pk])),
            'breadcrumb': plugin.get_breadcrumb(),
        }
        return HttpResponse(json.dumps(response), content_type='application/json')
예제 #5
0
 def edit_field(self, request, object_id, language):
     obj = self._get_object_for_single_field(object_id, language)
     opts = obj.__class__._meta
     saved_successfully = False
     cancel_clicked = request.POST.get("_cancel", False)
     raw_fields = request.GET.get("edit_fields")
     fields = [field for field in raw_fields.split(",") if field in self.frontend_editable_fields]
     if not fields:
         context = {
             'opts': opts,
             'message': force_unicode(_("Field %s not found")) % raw_fields
         }
         return render_to_response('admin/cms/page/plugin/error_form.html', context, RequestContext(request))
     if not request.user.has_perm("{0}.change_{1}".format(self.model._meta.app_label,
                                                          self.model._meta.model_name)):
         context = {
             'opts': opts,
             'message': force_unicode(_("You do not have permission to edit this item"))
         }
         return render_to_response('admin/cms/page/plugin/error_form.html', context, RequestContext(request))
         # Dinamically creates the form class with only `field_name` field
     # enabled
     form_class = self.get_form(request, obj, fields=fields)
     if not cancel_clicked and request.method == 'POST':
         form = form_class(instance=obj, data=request.POST)
         if form.is_valid():
             form.save()
             saved_successfully = True
     else:
         form = form_class(instance=obj)
     admin_form = AdminForm(form, fieldsets=[(None, {'fields': fields})], prepopulated_fields={},
                            model_admin=self)
     media = self.media + admin_form.media
     context = {
         'CMS_MEDIA_URL': get_cms_setting('MEDIA_URL'),
         'title': opts.verbose_name,
         'plugin': None,
         'plugin_id': None,
         'adminform': admin_form,
         'add': False,
         'is_popup': True,
         'media': media,
         'opts': opts,
         'change': True,
         'save_as': False,
         'has_add_permission': False,
         'window_close_timeout': 10,
     }
     if cancel_clicked:
         # cancel button was clicked
         context.update({
             'cancel': True,
         })
         return render_to_response('admin/cms/page/plugin/confirm_form.html', context, RequestContext(request))
     if not cancel_clicked and request.method == 'POST' and saved_successfully:
         return render_to_response('admin/cms/page/plugin/confirm_form.html', context, RequestContext(request))
     return render_to_response('admin/cms/page/plugin/change_form.html', context, RequestContext(request))
예제 #6
0
 def test_toolbar(self):
     """
     Test that PageMeta/TitleMeta items are present for superuser
     """
     from cms.toolbar.toolbar import CMSToolbar
     page1, page2 = self.get_pages()
     request = self.get_page_request(page1, self.user, '/', edit=True)
     toolbar = CMSToolbar(request)
     toolbar.get_left_items()
     page_menu = toolbar.menus['page']
     meta_menu = page_menu.find_items(SubMenu, name=force_unicode(PAGE_META_MENU_TITLE))[0].item
     self.assertEqual(len(meta_menu.find_items(ModalItem, name="%s ..." % force_unicode(PAGE_META_ITEM_TITLE))), 1)
     self.assertEqual(len(meta_menu.find_items(ModalItem)), len(self.languages) + 1)
예제 #7
0
 def test_perm(self):
     """
     Test that page meta menu is present if user has Page.change_perm
     """
     from cms.toolbar.toolbar import CMSToolbar
     page1, page2 = self.get_pages()
     self.user_staff.user_permissions.add(Permission.objects.get(codename='change_page'))
     self.user_staff = User.objects.get(pk=self.user_staff.pk)
     request = self.get_page_request(page1, self.user_staff, '/', edit=True)
     toolbar = CMSToolbar(request)
     toolbar.get_left_items()
     page_menu = toolbar.menus['page']
     meta_menu = page_menu.find_items(SubMenu, name=force_unicode(PAGE_META_MENU_TITLE))[0].item
     self.assertEqual(len(meta_menu.find_items(ModalItem, name="%s ..." % force_unicode(PAGE_META_ITEM_TITLE))), 1)
    def test_toolbar_with_an_existing_usersetting(self):
        """
        Test that UserSettings toolbar item URL is for change_view for superuser
        if UserSettings already exists for current site
        """
        usersettings_model_opts = self.UserSettings._meta
        usersettings_obj = self.UserSettings.objects.create(**self.usersettings_data)

        from cms.toolbar.toolbar import CMSToolbar
        page = self.get_pages()
        request = self.get_page_request(page, self.user, '/', edit=True)
        toolbar = CMSToolbar(request)
        toolbar.get_left_items()

        admin_menu = toolbar.menus[ADMIN_MENU_IDENTIFIER]
        MENU_ITEM_TITLE = usersettings_model_opts.verbose_name

        change_url = '%s?%s' % (
            reverse('admin:%s_%s_change' % (
                usersettings_model_opts.app_label,
                usersettings_model_opts.module_name), args=(usersettings_obj.pk,)),
            IS_POPUP_VAR)

        usersettings_menu = \
            admin_menu.find_items(ModalItem, name='%s ...' % force_unicode(MENU_ITEM_TITLE))

        self.assertEqual(change_url, usersettings_menu[0].item.url)
예제 #9
0
    def render_tag(self, context, page, language):
        if page.is_published(language) and page.publisher_public_id and page.publisher_public.is_published(language):
            if page.is_dirty(language):
                cls = "dirty"
                text = _("unpublished changes")
            else:
                cls = "published"
                text = _("published")
        else:

            if language in page.languages:
                public_pending = (
                    page.publisher_public_id
                    and page.publisher_public.get_publisher_state(language) == PUBLISHER_STATE_PENDING
                )
                if public_pending or page.get_publisher_state(language) == PUBLISHER_STATE_PENDING:
                    cls = "unpublishedparent"
                    text = _("unpublished parent")
                else:
                    cls = "unpublished"
                    text = _("unpublished")
            else:
                cls = "empty"
                text = _("no content")
        return mark_safe('<span class="%s" title="%s"></span>' % (cls, force_unicode(text)))
예제 #10
0
def urljoin(*segments):
    """Joins url segments together and appends trailing slash if required.
    
    >>> urljoin('a', 'b', 'c')
    u'a/b/c/'
    
    >>> urljoin('a', '//b//', 'c')
    u'a/b/c/'
    
    >>> urljoin('/a', '/b/', '/c/')
    u'/a/b/c/'
    
    >>> urljoin('/a', '')
    u'/a/'
    """
    cleaned_segments = map(lambda segment: force_unicode(segment).strip("/"), segments)
    nonempty_segments = filter(lambda segment: segment > "", cleaned_segments)
    url = ("/").join(nonempty_segments)
    
    if segments[0].startswith("/") and not url.startswith("/"):
        url = "/" + url
    
    if settings.APPEND_SLASH and not url.endswith("/"):
        url += "/"
    return url
예제 #11
0
    def publish_pages(self):
        from django.contrib.auth.models import User
        from cms.models import Page
        from cms.utils.permissions import set_current_user
        
        # thread locals middleware needs to know, who are we - login as a first
        # super user
        
        try:
            user = User.objects.filter(is_active=True, is_staff=True, is_superuser=True)[0]
        except IndexError:
            raise CommandError("No super user found, create one using `manage.py createsuperuser`.")
        
        set_current_user(user) # set him as current user

        qs = Page.objects.drafts().filter(published=True)
        pages_total, pages_published = qs.count(), 0
        
        print(u"\nPublishing public drafts....\n")
        
        for i, page in enumerate(qs):
            m = " "
            if page.publish():
                pages_published += 1
                m = "*"
            print(u"%d.\t%s  %s [%d]" % (i + 1, m, force_unicode(page), page.id))
        
        print(u"\n")
        print(u"=" * 40)
        print(u"Total:     %s" % pages_total)
        print(u"Published: %s" % pages_published)
예제 #12
0
def has_reached_plugin_limit(placeholder, plugin_type, language, template=None):
    """
    Checks if placeholder has reached it's global plugin limit,
    if not then it checks if it has reached it's plugin_type limit.
    """
    limits = get_placeholder_conf("limits", placeholder.slot, template)
    if limits:
        global_limit = limits.get("global")
        type_limit = limits.get(plugin_type)
        # total plugin count
        count = placeholder.cmsplugin_set.filter(language=language).count()
        if global_limit and count >= global_limit:
            raise PluginLimitReached(_("This placeholder already has the maximum number of plugins (%s)." % count))
        elif type_limit:
            # total plugin type count
            type_count = placeholder.cmsplugin_set.filter(
                language=language,
                plugin_type=plugin_type,
            ).count()
            if type_count >= type_limit:
                plugin_name = force_unicode(plugin_pool.get_plugin(plugin_type).name)
                raise PluginLimitReached(_(
                    "This placeholder already has the maximum number (%(limit)s) of allowed %(plugin_name)s plugins.") \
                                         % {'limit': type_limit, 'plugin_name': plugin_name})
    return False
예제 #13
0
 def edit_field(self, request, object_id, language):
     obj = self._get_object_for_single_field(object_id, language)
     opts = obj.__class__._meta
     saved_successfully = False
     cancel_clicked = request.POST.get("_cancel", False)
     raw_fields = request.GET.get("edit_fields")
     fields = [field for field in raw_fields.split(",") if field in self.frontend_editable_fields]
     if not fields:
         context = {"opts": opts, "message": force_unicode(_("Field %s not found")) % raw_fields}
         return render_to_response("admin/cms/page/plugin/error_form.html", context, RequestContext(request))
     if not request.user.has_perm("%s_change" % self.model._meta.module_name):
         context = {"opts": opts, "message": force_unicode(_("You do not have permission to edit this item"))}
         return render_to_response("admin/cms/page/plugin/error_form.html", context, RequestContext(request))
         # Dinamically creates the form class with only `field_name` field
     # enabled
     form_class = self.get_form(request, obj, fields=fields)
     if not cancel_clicked and request.method == "POST":
         form = form_class(instance=obj, data=request.POST)
         if form.is_valid():
             form.save()
             saved_successfully = True
     else:
         form = form_class(instance=obj)
     admin_form = AdminForm(form, fieldsets=[(None, {"fields": fields})], prepopulated_fields={}, model_admin=self)
     media = self.media + admin_form.media
     context = {
         "CMS_MEDIA_URL": get_cms_setting("MEDIA_URL"),
         "title": opts.verbose_name,
         "plugin": None,
         "plugin_id": None,
         "adminform": admin_form,
         "add": False,
         "is_popup": True,
         "media": media,
         "opts": opts,
         "change": True,
         "save_as": False,
         "has_add_permission": False,
         "window_close_timeout": 10,
     }
     if cancel_clicked:
         # cancel button was clicked
         context.update({"cancel": True})
         return render_to_response("admin/cms/page/plugin/confirm_form.html", context, RequestContext(request))
     if not cancel_clicked and request.method == "POST" and saved_successfully:
         return render_to_response("admin/cms/page/plugin/confirm_form.html", context, RequestContext(request))
     return render_to_response("admin/cms/page/plugin/change_form.html", context, RequestContext(request))
예제 #14
0
 def __init__(self, name, url, active=False, disabled=False, extra_classes=None, on_close=None, side=LEFT):
     super(SideframeItem, self).__init__(side)
     self.name = "%s ..." % force_unicode(name)
     self.url = url
     self.active = active
     self.disabled = disabled
     self.extra_classes = extra_classes or []
     self.on_close = on_close
예제 #15
0
 def get_instance_icon_alt(self):
     """
     Get alt text for instance's icon
     """
     instance, plugin = self.get_plugin_instance()
     if instance:
         return force_unicode(plugin.icon_alt(instance))
     else:
         return u''
예제 #16
0
    def delete_plugin(self, request, plugin_id):
        plugin = get_object_or_404(CMSPlugin.objects.select_related("placeholder"), pk=plugin_id)
        if not self.has_delete_plugin_permission(request, plugin):
            return HttpResponseForbidden(force_unicode(_("You do not have permission to delete this plugin")))
        plugin_cms_class = plugin.get_plugin_class()
        plugin_class = plugin_cms_class.model
        opts = plugin_class._meta
        using = router.db_for_write(plugin_class)
        app_label = opts.app_label
        (deleted_objects, perms_needed, protected) = get_deleted_objects(
            [plugin], opts, request.user, self.admin_site, using
        )

        if request.POST:  # The user has already confirmed the deletion.
            if perms_needed:
                raise PermissionDenied(_("You do not have permission to delete this plugin"))
            obj_display = force_unicode(plugin)
            self.log_deletion(request, plugin, obj_display)
            plugin.delete()
            self.message_user(
                request,
                _('The %(name)s plugin "%(obj)s" was deleted successfully.')
                % {"name": force_unicode(opts.verbose_name), "obj": force_unicode(obj_display)},
            )
            self.post_delete_plugin(request, plugin)
            return HttpResponseRedirect(reverse("admin:index", current_app=self.admin_site.name))
        plugin_name = force_unicode(plugin_pool.get_plugin(plugin.plugin_type).name)
        if perms_needed or protected:
            title = _("Cannot delete %(name)s") % {"name": plugin_name}
        else:
            title = _("Are you sure?")
        context = {
            "title": title,
            "object_name": plugin_name,
            "object": plugin,
            "deleted_objects": deleted_objects,
            "perms_lacking": perms_needed,
            "protected": protected,
            "opts": opts,
            "app_label": app_label,
        }
        return TemplateResponse(
            request, "admin/cms/page/plugin/delete_confirmation.html", context, current_app=self.admin_site.name
        )
예제 #17
0
def _scan_placeholders(nodelist, current_block=None, ignore_blocks=None):
    from cms.templatetags.cms_tags import Placeholder
    
    placeholders = []
    if ignore_blocks is None:
        # List of BlockNode instances to ignore.
        # This is important to avoid processing overriden block nodes.
        ignore_blocks = []

    for node in nodelist:
        # check if this is a placeholder first
        if isinstance(node, Placeholder):
            placeholders.append(node.get_name())
        elif isinstance(node, IncludeNode):
            # if there's an error in the to-be-included template, node.template becomes None
            if node.template:
                # This is required for Django 1.7 but works on older version too
                # Check if it quacks like a template object, if not
                # presume is a template path and get the object out of it
                if not callable(getattr(node.template, 'render', None)):
                    template = get_template(force_unicode(node.template).strip('"'))
                else:
                    template = node.template
                placeholders += _scan_placeholders(template.nodelist, current_block)
        # handle {% extends ... %} tags
        elif isinstance(node, ExtendsNode):
            placeholders += _extend_nodelist(node)
        # in block nodes we have to scan for super blocks
        elif isinstance(node, VariableNode) and current_block:
            if node.filter_expression.token == 'block.super':
                if not hasattr(current_block.super, 'nodelist'):
                    raise TemplateSyntaxError("Cannot render block.super for blocks without a parent.")
                placeholders += _scan_placeholders(current_block.super.nodelist, current_block.super)
        # ignore nested blocks which are already handled
        elif isinstance(node, BlockNode) and node.name in ignore_blocks:
            continue
        # if the node has the newly introduced 'child_nodelists' attribute, scan
        # those attributes for nodelists and recurse them
        elif hasattr(node, 'child_nodelists'):
            for nodelist_name in node.child_nodelists:
                if hasattr(node, nodelist_name):
                    subnodelist = getattr(node, nodelist_name)
                    if isinstance(subnodelist, NodeList):
                        if isinstance(node, BlockNode):
                            current_block = node
                        placeholders += _scan_placeholders(subnodelist, current_block, ignore_blocks)
        # else just scan the node for nodelist instance attributes
        else:
            for attr in dir(node):
                obj = getattr(node, attr)
                if isinstance(obj, NodeList):
                    if isinstance(node, BlockNode):
                        current_block = node
                    placeholders += _scan_placeholders(obj, current_block, ignore_blocks)
    return placeholders
예제 #18
0
 def clear_placeholder(self, request, placeholder_id):
     placeholder = get_object_or_404(Placeholder, pk=placeholder_id)
     if not self.has_clear_placeholder_permission(request, placeholder):
         return HttpResponseForbidden(force_unicode(_("You do not have permission to clear this placeholder")))
     language = request.GET.get("language", None)
     plugins = placeholder.get_plugins(language)
     opts = Placeholder._meta
     using = router.db_for_write(Placeholder)
     app_label = opts.app_label
     (deleted_objects, perms_needed, protected) = get_deleted_objects(
         plugins, opts, request.user, self.admin_site, using
     )
     obj_display = force_unicode(placeholder)
     if request.POST:  # The user has already confirmed the deletion.
         if perms_needed:
             return HttpResponseForbidden(force_unicode(_("You do not have permission to clear this placeholder")))
         self.log_deletion(request, placeholder, obj_display)
         for plugin in plugins:
             plugin.delete()
         self.message_user(
             request, _('The placeholder "%(obj)s" was cleared successfully.') % {"obj": force_unicode(obj_display)}
         )
         self.post_clear_placeholder(request, placeholder)
         return HttpResponseRedirect(reverse("admin:index", current_app=self.admin_site.name))
     if perms_needed or protected:
         title = _("Cannot delete %(name)s") % {"name": obj_display}
     else:
         title = _("Are you sure?")
     context = {
         "title": title,
         "object_name": _("placeholder"),
         "object": placeholder,
         "deleted_objects": deleted_objects,
         "perms_lacking": perms_needed,
         "protected": protected,
         "opts": opts,
         "app_label": app_label,
     }
     return TemplateResponse(
         request, "admin/cms/page/plugin/delete_confirmation.html", context, current_app=self.admin_site.name
     )
예제 #19
0
 def _has_changed(self, initial, data):
     # THIS IS A COPY OF django.forms.widgets.Widget._has_changed()
     # (except for the first if statement)
     
     """
     Return True if data differs from initial.
     """
     # For purposes of seeing whether something has changed, None is
     # the same as an empty string, if the data or inital value we get
     # is None, replace it w/ u''.
     if data is None or (len(data)>=2 and data[1] in [None,'']):
         data_value = u''
     else:
         data_value = data
     if initial is None:
         initial_value = u''
     else:
         initial_value = initial
     if force_unicode(initial_value) != force_unicode(data_value):
         return True
     return False
예제 #20
0
    def get_breadcrumb(self):
        from cms.models import Page

        models = self.placeholder._get_attached_models()
        if models:
            model = models[0]
        else:
            model = Page
        breadcrumb = []
        if not self.parent_id:
            try:
                url = force_unicode(
                    reverse("admin:%s_%s_edit_plugin" % (model._meta.app_label, model._meta.module_name),
                            args=[self.pk]))
            except NoReverseMatch:
                url = force_unicode(
                    reverse("admin:%s_%s_edit_plugin" % (Page._meta.app_label, Page._meta.module_name),
                            args=[self.pk]))
            breadcrumb.append({'title': force_unicode(self.get_plugin_name()), 'url': url})
            return breadcrumb
        for parent in self.get_ancestors(False, True):
            try:
                url = force_unicode(
                    reverse("admin:%s_%s_edit_plugin" % (model._meta.app_label, model._meta.module_name),
                            args=[parent.pk]))
            except NoReverseMatch:
                url = force_unicode(
                    reverse("admin:%s_%s_edit_plugin" % (Page._meta.app_label, Page._meta.module_name),
                            args=[parent.pk]))
            breadcrumb.append({'title': force_unicode(parent.get_plugin_name()), 'url': url})
        return breadcrumb
예제 #21
0
    def render(self, name=None, value=None, attrs=None):
        final_attrs = self.build_attrs(attrs)
        id_ = final_attrs.get('id', None)

        output = [r'''<script type="text/javascript">
(function($){
    $(function(){
        $("#%(element_id)s").select2({
            placeholder: "%(placeholder_text)s",
            allowClear: true,
            minimumInputLength: 3,
            ajax: {
                url: "%(ajax_url)s",
                dataType: 'json',
                data: function (term, page) {
                    return {
                        q: term, // search term
                        language_code: '%(language_code)s'
                    };
                },
                results: function (data, page) {
                    return {
                        more: false,
                        results: $.map(data, function(item, i){
                            return {
                                'id':item.redirect_url,
                                'text': item.title + ' (/' + item.path + ')'}
                            }
                        )
                    };
                }
            },
            // Allow creation of new entries
            createSearchChoice:function(term, data) { if ($(data).filter(function() { return this.text.localeCompare(term)===0; }).length===0) {return {id:term, text:term};} },
            multiple: false,
            initSelection : function (element, callback) {
                var initialValue = element.val()
                callback({id:initialValue, text: initialValue});
            }
        });
    })
})(django.jQuery);
</script>''' % {
            'element_id': id_,
            'placeholder_text': final_attrs.get('placeholder_text', ''),
            'language_code': self.language,
            'ajax_url': force_unicode(self.ajax_url)
        }]

        output.append(super(PageSmartLinkWidget, self).render(name, value, attrs))
        return mark_safe(u''.join(output))
예제 #22
0
def is_valid_url(url, instance, create_links=True, site=None):
    """ Checks for conflicting urls
    """
    page_root = unquote(reverse("pages-root"))
    if url and url != page_root:
        # Url sanity check via regexp
        if not any_path_re.match(url):
            raise ValidationError(_("Invalid URL, use /my/url format."))
            # We only check page FK to site object to allow is_valid_url check on
        # incomplete Page instances
        if not site and instance.site_id:
            site = instance.site
            # Retrieve complete queryset of pages with corresponding URL
        # This uses the same resolving function as ``get_page_from_path``
        if url.startswith(page_root):
            url = url[len(page_root) :]
        page_qs = get_page_queryset_from_path(url.strip("/"), site=site, draft=instance.publisher_is_draft)
        url_clashes = []
        # If queryset has pages checks for conflicting urls
        for page in page_qs:
            # Every page in the queryset except the current one is a conflicting page
            # We have to exclude both copies of the page
            if page and page.publisher_public_id != instance.pk and page.pk != instance.pk:
                if create_links:
                    # Format return message with page url
                    url_clashes.append(
                        '<a href="%(page_url)s%(pk)s" target="_blank">%(page_title)s</a>'
                        % {
                            "page_url": reverse("admin:cms_page_changelist"),
                            "pk": page.pk,
                            "page_title": force_unicode(page),
                        }
                    )
                else:
                    # Just return the page name
                    url_clashes.append("'%s'" % page)
        if url_clashes:
            # If clashing pages exist raise the exception
            raise ValidationError(
                mark_safe(
                    ungettext_lazy(
                        "Page %(pages)s has the same url '%(url)s' as current page \"%(instance)s\".",
                        "Pages %(pages)s have the same url '%(url)s' as current page \"%(instance)s\".",
                        len(url_clashes),
                    )
                    % {"pages": ", ".join(url_clashes), "url": url, "instance": instance}
                )
            )
    return True
예제 #23
0
 def test_toolbar_with_items(self):
     """
     Test that PageMeta/TitleMeta items are present for superuser if PageMeta/TitleMeta exists for current page
     """
     from cms.toolbar.toolbar import CMSToolbar
     page1, page2 = self.get_pages()
     page_ext = PageMeta.objects.create(extended_object=page1)
     request = self.get_page_request(page1, self.user, '/', edit=True)
     toolbar = CMSToolbar(request)
     toolbar.get_left_items()
     page_menu = toolbar.menus['page']
     meta_menu = page_menu.find_items(SubMenu, name=force_unicode(PAGE_META_MENU_TITLE))[0].item
     pagemeta_menu = meta_menu.find_items(ModalItem, name="%s ..." % force_unicode(PAGE_META_ITEM_TITLE))
     self.assertEqual(len(pagemeta_menu), 1)
     self.assertTrue(pagemeta_menu[0].item.url.startswith(reverse('admin:djangocms_page_meta_pagemeta_change', args=(page_ext.pk,))))
     for title in page1.title_set.all():
         language = get_language_object(title.language)
         titlemeta_menu = meta_menu.find_items(ModalItem, name="%s ..." % language['name'])
         self.assertEqual(len(titlemeta_menu), 1)
         try:
             title_ext = TitleMeta.objects.get(extended_object_id=title.pk)
             self.assertTrue(titlemeta_menu[0].item.url.startswith(reverse('admin:djangocms_page_meta_titlemeta_change', args=(title_ext.pk,))))
         except TitleMeta.DoesNotExist:
             self.assertTrue(titlemeta_menu[0].item.url.startswith(reverse('admin:djangocms_page_meta_titlemeta_add')))
예제 #24
0
    def publish_pages(self, include_unpublished, language, site):
        from cms.models import Page
        from cms.utils.compat.dj import get_user_model
        from cms.utils.permissions import set_current_user

        # thread locals middleware needs to know, who are we - login as a first
        # super user

        try:
            user = get_user_model().objects.filter(is_active=True, is_staff=True, is_superuser=True)[0]
        except IndexError:
            raise CommandError("No super user found, create one using `manage.py createsuperuser`.")

        set_current_user(user) # set him as current user

        qs = Page.objects.drafts()
        if not include_unpublished:
            qs = qs.filter(title_set__published=True).distinct()
        if site:
            qs = qs.filter(site_id=site)

        pages_total, pages_published = qs.count(), 0

        self.stdout.write(u"\nPublishing public drafts....\n")
        output_language = None
        for i, page in enumerate(qs):
            m = " "
            add = True
            titles = page.title_set
            if not include_unpublished:
                titles = titles.filter(published=True)
            for lang in titles.values_list("language", flat=True):
                if language is None or lang == language:
                    if not output_language:
                        output_language = lang
                    if not page.publish(lang):
                        add = False
            # we may need to activate the first (main) language for proper page title rendering
            activate(output_language)
            if add:
                pages_published += 1
                m = "*"
            self.stdout.write(u"%d.\t%s  %s [%d]\n" % (i + 1, m, force_unicode(page), page.id))

        self.stdout.write(u"\n")
        self.stdout.write(u"=" * 40)
        self.stdout.write(u"\nTotal:     %s\n" % pages_total)
        self.stdout.write(u"Published: %s\n" % pages_published)
예제 #25
0
    def clean(self):
        cleaned_data = self.cleaned_data
        slug = cleaned_data.get('slug', '')

        page = self.instance
        lang = cleaned_data.get('language', None)
        # No language, can not go further, but validation failed already
        if not lang:
            return cleaned_data

        if 'parent' not in cleaned_data:
            cleaned_data['parent'] = None
        parent = cleaned_data.get('parent', None)

        try:
            site = self.cleaned_data.get('site', Site.objects.get_current())
        except Site.DoesNotExist:
            raise ValidationError("No site found for current settings.")

        if parent and parent.site != site:
            raise ValidationError("Site doesn't match the parent's page site")

        if site and not is_valid_page_slug(page, parent, lang, slug, site):
            self._errors['slug'] = ErrorList(
                [_('Another page with this slug already exists')])
            del cleaned_data['slug']
        if self.instance and page.title_set.count():
            #Check for titles attached to the page makes sense only because
            #AdminFormsTests.test_clean_overwrite_url validates the form with when no page instance available
            #Looks like just a theoretical corner case
            title = page.get_title_obj(lang, fallback=False)
            if title and not isinstance(title, EmptyTitle) and slug:
                oldslug = title.slug
                title.slug = slug
                title.save()
                try:
                    is_valid_url(title.path, page)
                except ValidationError as exc:
                    title.slug = oldslug
                    title.save()
                    if 'slug' in cleaned_data:
                        del cleaned_data['slug']
                    if hasattr(exc, 'messages'):
                        errors = exc.messages
                    else:
                        errors = [force_unicode(exc.message)]
                    self._errors['slug'] = ErrorList(errors)
        return cleaned_data
    def test_toolbar(self):
        """
        Test that UserSettings toolbar are present for superuser
        """
        from cms.toolbar.toolbar import CMSToolbar
        page = self.get_pages()
        request = self.get_page_request(page, self.user, '/', edit=True)
        toolbar = CMSToolbar(request)
        toolbar.get_left_items()
        admin_menu = toolbar.menus[ADMIN_MENU_IDENTIFIER]

        usersettings_model_opts = self.UserSettings._meta
        MENU_ITEM_TITLE = usersettings_model_opts.verbose_name

        self.assertEqual(len(admin_menu.find_items(
            ModalItem, name="%s ..." % force_unicode(MENU_ITEM_TITLE))), 1)
예제 #27
0
    def clean(self):
        cleaned_data = self.cleaned_data
        slug = cleaned_data.get('slug', '')
        
        page = self.instance
        lang = cleaned_data.get('language', None)
        # No language, can not go further, but validation failed already
        if not lang:
            return cleaned_data

        if 'parent' not in cleaned_data:
            cleaned_data['parent'] = None
        parent = cleaned_data.get('parent', None)

        try:
            site = self.cleaned_data.get('site', Site.objects.get_current())
        except Site.DoesNotExist:
            raise ValidationError("No site found for current settings.")

        if parent and parent.site != site:
            raise ValidationError("Site doesn't match the parent's page site")

        if site and not is_valid_page_slug(page, parent, lang, slug, site):
            self._errors['slug'] = ErrorList([_('Another page with this slug already exists')])
            del cleaned_data['slug']
        if self.instance and page.title_set.count():
            #Check for titles attached to the page makes sense only because
            #AdminFormsTests.test_clean_overwrite_url validates the form with when no page instance available
            #Looks like just a theoretical corner case
            title = page.get_title_obj(lang, fallback=False)
            if title and not isinstance(title, EmptyTitle) and slug:
                oldslug = title.slug
                title.slug = slug
                title.save()
                try:
                    is_valid_url(title.path, page)
                except ValidationError as exc:
                    title.slug = oldslug
                    title.save()
                    if 'slug' in cleaned_data:
                        del cleaned_data['slug']
                    if hasattr(exc, 'messages'):
                        errors = exc.messages
                    else:
                        errors = [force_unicode(exc.message)]
                    self._errors['slug'] = ErrorList(errors)
        return cleaned_data
예제 #28
0
    def publish_pages(self):
        from cms.compat import get_user_model
        from cms.models import Page
        from cms.utils.permissions import set_current_user

        # thread locals middleware needs to know, who are we - login as a first
        # super user

        try:
            user = get_user_model().objects.filter(is_active=True,
                                                   is_staff=True,
                                                   is_superuser=True)[0]
        except IndexError:
            raise CommandError(
                "No super user found, create one using `manage.py createsuperuser`."
            )

        set_current_user(user)  # set him as current user

        qs = Page.objects.drafts().filter(title_set__published=True)
        pages_total, pages_published = qs.count(), 0

        print(u"\nPublishing public drafts....\n")
        output_language = None
        for i, page in enumerate(qs):
            m = " "
            add = True
            for lang in page.title_set.filter(published=True).values_list(
                    "language", flat=True):
                if not output_language:
                    output_language = lang
                if not page.publish(lang):
                    add = False
            # we may need to activate the first (main) language for proper page title rendering
            activate(output_language)
            if add:
                pages_published += 1
                m = "*"
            print(u"%d.\t%s  %s [%d]" %
                  (i + 1, m, force_unicode(page), page.id))

        print(u"\n")
        print(u"=" * 40)
        print(u"Total:     %s" % pages_total)
        print(u"Published: %s" % pages_published)
예제 #29
0
def urljoin(*segments):
    """Joins url segments together and appends trailing slash if required.
    
    >>> urljoin('a', 'b', 'c')
    u'a/b/c/'
    
    >>> urljoin('a', '//b//', 'c')
    u'a/b/c/'
    
    >>> urljoin('/a', '/b/', '/c/')
    u'/a/b/c/'
    
    >>> urljoin('/a', '')
    u'/a/'
    """
    url  = '/' if segments[0].startswith('/') else ''
    url += '/'.join(filter(None, (force_unicode(s).strip('/') for s in segments)))
    return url + '/' if settings.APPEND_SLASH else url
예제 #30
0
    def get_patterns(self):
        self.discover_plugins()

        # We want untranslated name of the plugin for its slug so we deactivate translation
        lang = get_language()
        deactivate_all()

        try:
            url_patterns = []
            for plugin in self.get_all_plugins():
                p = plugin()
                slug = slugify(force_unicode(normalize_name(p.__class__.__name__)))
                url_patterns += patterns("", url(r"^plugin/%s/" % (slug,), include(p.plugin_urls)))
        finally:
            # Reactivate translation
            activate(lang)

        return url_patterns
예제 #31
0
def is_valid_url(url, instance, create_links=True, site=None):
    """ Checks for conflicting urls
    """
    page_root = unquote(reverse("pages-root"))
    if url and url != page_root:
        # Url sanity check via regexp
        if not any_path_re.match(url):
            raise ValidationError(_('Invalid URL, use /my/url format.'))
            # We only check page FK to site object to allow is_valid_url check on
        # incomplete Page instances
        if not site and instance.site_id:
            site = instance.site
            # Retrieve complete queryset of pages with corresponding URL
        # This uses the same resolving function as ``get_page_from_path``
        if url.startswith(page_root):
            url = url[len(page_root):]
        page_qs = get_page_queryset_from_path(url.strip('/'), site=site)
        url_clashes = []
        # If queryset has pages checks for conflicting urls
        if page_qs is not None:
            # If single page is returned create a list for interface compat
            if isinstance(page_qs, Page):
                page_qs = [page_qs]
            for page in page_qs:
                # Every page in the queryset except the current one is a conflicting page
                # We have to exclude both copies of the page
                if page and page.publisher_public.pk != instance.pk:
                    if create_links:
                        # Format return message with page url
                        url_clashes.append('<a href="%(page_url)s%(pk)s" target="_blank">%(page_title)s</a>' % {
                            'page_url': reverse('admin:cms_page_changelist'), 'pk': page.pk,
                            'page_title': force_unicode(page),
                        })
                    else:
                        # Just return the page name
                        url_clashes.append("'%s'" % page)
            if url_clashes:
                # If clashing pages exist raise the exception
                raise ValidationError(mark_safe(
                    ungettext_lazy('Page %(pages)s has the same url \'%(url)s\' as current page "%(instance)s".',
                                   'Pages %(pages)s have the same url \'%(url)s\' as current page "%(instance)s".',
                                   len(url_clashes)) %
                    {'pages': ', '.join(url_clashes), 'url': url, 'instance': instance}))
    return True
예제 #32
0
def is_valid_url(url, instance, create_links=True, site=None):
    """ Checks for conflicting urls
    """
    page_root = unquote(reverse("pages-root"))
    if url and url != page_root:
        # Url sanity check via regexp
        if not any_path_re.match(url):
            raise ValidationError(_('Invalid URL, use /my/url format.'))
            # We only check page FK to site object to allow is_valid_url check on
        # incomplete Page instances
        if not site and instance.site_id:
            site = instance.site
            # Retrieve complete queryset of pages with corresponding URL
        # This uses the same resolving function as ``get_page_from_path``
        if url.startswith(page_root):
            url = url[len(page_root):]
        page_qs = get_page_queryset_from_path(url.strip('/'), site=site)
        url_clashes = []
        # If queryset has pages checks for conflicting urls
        if page_qs is not None:
            # If single page is returned create a list for interface compat
            if isinstance(page_qs, Page):
                page_qs = [page_qs]
            for page in page_qs:
                # Every page in the queryset except the current one is a conflicting page
                # We have to exclude both copies of the page
                if page and page.publisher_public.pk != instance.pk:
                    if create_links:
                        # Format return message with page url
                        url_clashes.append('<a href="%(page_url)s%(pk)s" target="_blank">%(page_title)s</a>' % {
                            'page_url': reverse('admin:cms_page_changelist'), 'pk': page.pk,
                            'page_title': force_unicode(page),
                        })
                    else:
                        # Just return the page name
                        url_clashes.append("'%s'" % page)
            if url_clashes:
                # If clashing pages exist raise the exception
                raise ValidationError(mark_safe(
                    ungettext_lazy('Page %(pages)s has the same url \'%(url)s\' as current page "%(instance)s".',
                                   'Pages %(pages)s have the same url \'%(url)s\' as current page "%(instance)s".',
                                   len(url_clashes)) %
                    {'pages': ', '.join(url_clashes), 'url': url, 'instance': instance}))
    return True
예제 #33
0
    def copy_plugins(self, request):
        """
        POST request should have the following data:

        - source_language
        - source_placeholder_id
        - source_plugin_id (optional)
        - target_language
        - target_placeholder_id
        - target_plugin_id (optional, new parent)
        """
        source_language = request.POST['source_language']
        source_placeholder_id = request.POST['source_placeholder_id']
        source_plugin_id = request.POST.get('source_plugin_id', None)
        target_language = request.POST['target_language']
        target_placeholder_id = request.POST['target_placeholder_id']
        target_plugin_id = request.POST.get('target_plugin_id', None)
        source_placeholder = get_object_or_404(Placeholder, pk=source_placeholder_id)
        target_placeholder = get_object_or_404(Placeholder, pk=target_placeholder_id)
        if not target_language or not target_language in get_language_list():
            return HttpResponseBadRequest(_("Language must be set to a supported language!"))
        if source_plugin_id:
            source_plugin = get_object_or_404(CMSPlugin, pk=source_plugin_id)
            reload_required = requires_reload(PLUGIN_COPY_ACTION, [source_plugin])
            plugins = list(
                source_placeholder.cmsplugin_set.filter(tree_id=source_plugin.tree_id, lft__gte=source_plugin.lft,
                                                        rght__lte=source_plugin.rght).order_by('tree_id', 'level', 'position'))
        else:
            plugins = list(
                source_placeholder.cmsplugin_set.filter(language=source_language).order_by('tree_id', 'level', 'position'))
            reload_required = requires_reload(PLUGIN_COPY_ACTION, plugins)
        if not self.has_copy_plugin_permission(request, source_placeholder, target_placeholder, plugins):
            return HttpResponseForbidden(_('You do not have permission to copy these plugins.'))
        copy_plugins.copy_plugins_to(plugins, target_placeholder, target_language, target_plugin_id)
        plugin_list = CMSPlugin.objects.filter(language=target_language, placeholder=target_placeholder).order_by(
            'tree_id', 'level', 'position')
        reduced_list = []
        for plugin in plugin_list:
            reduced_list.append(
                {'id': plugin.pk, 'type': plugin.plugin_type, 'parent': plugin.parent_id, 'position': plugin.position,
                    'desc': force_unicode(plugin.get_short_description())})
        self.post_copy_plugins(request, source_placeholder, target_placeholder, plugins)
        json_response = {'plugin_list': reduced_list, 'reload': reload_required}
        return HttpResponse(simplejson.dumps(json_response), content_type='application/json')
예제 #34
0
    def test_copy_plugin(self):
        static_placeholder_source = StaticPlaceholder.objects.create(name='foobar', code='foobar', site_id=1)
        static_placeholder_target = StaticPlaceholder.objects.create(name='foofoo', code='foofoo', site_id=1)
        sourceplugin = add_plugin(static_placeholder_source.draft, 'TextPlugin', 'en', body='test source')
        targetplugin = add_plugin(static_placeholder_target.draft, 'TextPlugin', 'en', body='test dest')
        StaticPlaceholder.objects.filter(pk=static_placeholder_source.pk).update(dirty=False)
        plugin_class = sourceplugin.get_plugin_class_instance()
        admin = self.get_admin()

        with self.login_user_context(admin):
            request = self.get_request(post_data={
                'source_language': 'en',
                'source_placeholder_id': static_placeholder_source.draft.pk,
                'source_plugin_id': sourceplugin.pk,
                'target_language': 'en',
                'target_placeholder_id': static_placeholder_target.draft.pk,
                'targetplugin_id': targetplugin.pk,
            })
            response = self.admin_class.copy_plugins(request)

            # generate the expected response
            plugin_list = CMSPlugin.objects.filter(
                language='en', placeholder_id=static_placeholder_target.draft.pk).order_by(
                'tree_id', 'level', 'position')
            reduced_list = []
            for plugin in plugin_list:
                reduced_list.append(
                    {
                        'id': plugin.pk, 'type': plugin.plugin_type, 'parent': plugin.parent_id,
                        'position': plugin.position, 'desc': force_unicode(plugin.get_short_description()),
                        'language': plugin.language, 'placeholder_id': static_placeholder_target.draft.pk
                    }
                )
            expected = json.loads(
                json.dumps({'plugin_list': reduced_list, 'reload': plugin_class.requires_reload(PLUGIN_COPY_ACTION)}))
            self.assertEqual(response.status_code, 200)
            self.assertEqual(json.loads(response.content.decode('utf8')), expected)

            # Check dirty bit
            source = StaticPlaceholder.objects.get(pk=static_placeholder_source.pk)
            target = StaticPlaceholder.objects.get(pk=static_placeholder_target.pk)
            self.assertFalse(source.dirty)
            self.assertTrue(target.dirty)
    def get_patterns(self):
        self.discover_plugins()

        # We want untranslated name of the plugin for its slug so we deactivate translation
        lang = get_language()
        deactivate_all()

        try:
            url_patterns = []
            for plugin in self.get_all_plugins():
                p = plugin()
                slug = slugify(force_unicode(normalize_name(p.__class__.__name__)))
                url_patterns += patterns('',
                    url(r'^plugin/%s/' % (slug,), include(p.plugin_urls)),
                )
        finally:
            # Reactivate translation
            activate(lang)

        return url_patterns
    def publish_pages(self):
        from django.contrib.auth.models import User
        from cms.models import Page
        from cms.utils.permissions import set_current_user

        # thread locals middleware needs to know, who are we - login as a first
        # super user

        try:
            user = User.objects.filter(is_active=True,
                                       is_staff=True,
                                       is_superuser=True)[0]
        except IndexError:
            raise CommandError(
                "No super user found, create one using `manage.py createsuperuser`."
            )

        set_current_user(user)  # set him as current user

        qs = Page.objects.drafts().filter(published=True)
        pages_total, pages_published = qs.count(), 0

        print(u"\nPublishing public drafts....\n")

        for i, page in enumerate(qs):
            m = " "
            if page.publish():
                pages_published += 1
                m = "*"
            print(u"%d.\t%s  %s [%d]" %
                  (i + 1, m, force_unicode(page), page.id))

        print(u"\n")
        print(u"=" * 40)
        print(u"Total:     %s" % pages_total)
        print(u"Published: %s" % pages_published)
예제 #37
0
    def get_breadcrumb(self):
        from cms.models import Page

        model = self.placeholder._get_attached_model()
        if not model:
            model = Page
        breadcrumb = []
        if not self.parent_id:
            try:
                url = force_unicode(
                    admin_reverse(
                        "%s_%s_edit_plugin" %
                        (model._meta.app_label, model._meta.module_name),
                        args=[self.pk]))
            except NoReverseMatch:
                url = force_unicode(
                    admin_reverse(
                        "%s_%s_edit_plugin" %
                        (Page._meta.app_label, Page._meta.module_name),
                        args=[self.pk]))
            breadcrumb.append({
                'title': force_unicode(self.get_plugin_name()),
                'url': url
            })
            return breadcrumb
        for parent in self.get_ancestors(False, True):
            try:
                url = force_unicode(
                    admin_reverse(
                        "%s_%s_edit_plugin" %
                        (model._meta.app_label, model._meta.module_name),
                        args=[parent.pk]))
            except NoReverseMatch:
                url = force_unicode(
                    admin_reverse(
                        "%s_%s_edit_plugin" %
                        (Page._meta.app_label, Page._meta.module_name),
                        args=[parent.pk]))
            breadcrumb.append({
                'title': force_unicode(parent.get_plugin_name()),
                'url': url
            })
        return breadcrumb
예제 #38
0
 def __repr__(self):
     return '<SideframeButton:%s>' % force_unicode(self.name)
예제 #39
0
 def __repr__(self):
     return '<ModalButton:%s>' % force_unicode(self.name)
예제 #40
0
 def __repr__(self):
     return '<AjaxItem:%s>' % force_unicode(self.name)
예제 #41
0
 def __repr__(self):
     return '<Menu:%s>' % force_unicode(self.name)
def _native_language_marker(language, lang_code):
    with force_language(lang_code):
        return force_unicode(ugettext(language))
예제 #43
0
 def get_short_description(self):
     instance = self.get_plugin_instance()[0]
     if instance is not None:
         return force_unicode(instance)
     return _("<Empty>")
 def test_check_unicode_rendering(self):
     ph = Placeholder.objects.create(slot='test', default_width=300)
     result = force_unicode(ph)
     self.assertEqual(result, u'test')
예제 #45
0
 def __str__(self):
     page = self.page_id and force_unicode(self.page) or "None"
     return "%s :: %s has: %s" % (page, self.audience,
                                  force_unicode(
                                      dict(ACCESS_CHOICES)[self.grant_on]))
예제 #46
0
 def __str__(self):
     title = self.get_menu_title(fallback=True)
     if title is None:
         title = u""
     return force_unicode(title)
예제 #47
0
    def add_plugin(self, request):
        """
        POST request should have the following data:

        - placeholder_id
        - plugin_type
        - plugin_language
        - plugin_parent (optional)
        """
        plugin_type = request.POST['plugin_type']

        placeholder_id = request.POST.get('placeholder_id', None)
        parent_id = request.POST.get('parent_id', None)
        if parent_id:
            warnings.warn("parent_id is deprecated and will be removed in 3.1, use plugin_parent instead",
                          DeprecationWarning)
        if not parent_id:
            parent_id = request.POST.get('plugin_parent', None)
        placeholder = get_object_or_404(Placeholder, pk=placeholder_id)
        if not self.has_add_plugin_permission(request, placeholder, plugin_type):
            return HttpResponseForbidden(force_unicode(_('You do not have permission to add a plugin')))
        parent = None
        language = request.POST.get('plugin_language') or get_language_from_request(request)
        try:
            has_reached_plugin_limit(placeholder, plugin_type, language,
                                     template=self.get_placeholder_template(request, placeholder))
        except PluginLimitReached as er:
            return HttpResponseBadRequest(er)
            # page add-plugin
        if not parent_id:

            position = request.POST.get('plugin_order',
                                        CMSPlugin.objects.filter(language=language, placeholder=placeholder).count())
        # in-plugin add-plugin
        else:
            parent = get_object_or_404(CMSPlugin, pk=parent_id)
            placeholder = parent.placeholder
            position = request.POST.get('plugin_order',
                                        CMSPlugin.objects.filter(language=language, parent=parent).count())
            # placeholder (non-page) add-plugin

        # Sanity check to make sure we're not getting bogus values from JavaScript:
        if settings.USE_I18N:
            if not language or not language in [lang[0] for lang in settings.LANGUAGES]:
                return HttpResponseBadRequest(force_unicode(_("Language must be set to a supported language!")))
            if parent and parent.language != language:
                return HttpResponseBadRequest(force_unicode(_("Parent plugin language must be same as language!")))
        else:
            language = settings.LANGUAGE_CODE
        plugin = CMSPlugin(language=language, plugin_type=plugin_type, position=position, placeholder=placeholder)

        if parent:
            plugin.position = CMSPlugin.objects.filter(parent=parent).count()
            plugin.insert_at(parent, position='last-child', save=False)
        plugin.save()
        self.post_add_plugin(request, placeholder, plugin)
        response = {
            'url': force_unicode(
                reverse("admin:%s_%s_edit_plugin" % (self.model._meta.app_label, self.model._meta.module_name),
                        args=[plugin.pk])),
            'delete': force_unicode(
                reverse("admin:%s_%s_delete_plugin" % (self.model._meta.app_label, self.model._meta.module_name),
                        args=[plugin.pk])),
            'breadcrumb': plugin.get_breadcrumb(),
        }
        return HttpResponse(json.dumps(response), content_type='application/json')
예제 #48
0
    def copy_plugins(self, request):
        """
        POST request should have the following data:

        - source_language
        - source_placeholder_id
        - source_plugin_id (optional)
        - target_language
        - target_placeholder_id
        - target_plugin_id (optional, new parent)
        """
        source_language = request.POST['source_language']
        source_placeholder_id = request.POST['source_placeholder_id']
        source_plugin_id = request.POST.get('source_plugin_id', None)
        target_language = request.POST['target_language']
        target_placeholder_id = request.POST['target_placeholder_id']
        target_plugin_id = request.POST.get('target_plugin_id', None)
        source_placeholder = get_object_or_404(Placeholder, pk=source_placeholder_id)
        target_placeholder = get_object_or_404(Placeholder, pk=target_placeholder_id)
        if not target_language or not target_language in get_language_list():
            return HttpResponseBadRequest(force_unicode(_("Language must be set to a supported language!")))
        if source_plugin_id:
            source_plugin = get_object_or_404(CMSPlugin, pk=source_plugin_id)
            reload_required = requires_reload(PLUGIN_COPY_ACTION, [source_plugin])
            if source_plugin.plugin_type == "PlaceholderPlugin":
                # if it is a PlaceholderReference plugin only copy the plugins it references
                inst, cls = source_plugin.get_plugin_instance(self)
                plugins = inst.placeholder_ref.get_plugins_list()
            else:
                plugins = list(
                    source_placeholder.cmsplugin_set.filter(tree_id=source_plugin.tree_id, lft__gte=source_plugin.lft,
                                                            rght__lte=source_plugin.rght).order_by('tree_id', 'level',
                                                                                                   'position'))
        else:
            plugins = list(
                source_placeholder.cmsplugin_set.filter(language=source_language).order_by('tree_id', 'level',
                                                                                           'position'))
            reload_required = requires_reload(PLUGIN_COPY_ACTION, plugins)
        if not self.has_copy_plugin_permission(request, source_placeholder, target_placeholder, plugins):
            return HttpResponseForbidden(force_unicode(_('You do not have permission to copy these plugins.')))
        if target_placeholder.pk == request.toolbar.clipboard.pk and not source_plugin_id and not target_plugin_id:
            # if we copy a whole placeholder to the clipboard create PlaceholderReference plugin instead and fill it
            # the content of the source_placeholder.
            ref = PlaceholderReference()
            ref.name = source_placeholder.get_label()
            ref.plugin_type = "PlaceholderPlugin"
            ref.language = target_language
            ref.placeholder = target_placeholder
            ref.save()
            ref.copy_from(source_placeholder, source_language)
        else:
            copy_plugins.copy_plugins_to(plugins, target_placeholder, target_language, target_plugin_id)
        plugin_list = CMSPlugin.objects.filter(language=target_language, placeholder=target_placeholder).order_by(
            'tree_id', 'level', 'position')
        reduced_list = []
        for plugin in plugin_list:
            reduced_list.append(
                {
                    'id': plugin.pk, 'type': plugin.plugin_type, 'parent': plugin.parent_id,
                    'position': plugin.position, 'desc': force_unicode(plugin.get_short_description()),
                    'language': plugin.language, 'placeholder_id': plugin.placeholder_id
                }
            )
        self.post_copy_plugins(request, source_placeholder, target_placeholder, plugins)
        json_response = {'plugin_list': reduced_list, 'reload': reload_required}
        return HttpResponse(json.dumps(json_response), content_type='application/json')
예제 #49
0
 def __str__(self):
     return force_unicode(self.user)
    def copy_plugins(self, request):
        """
        POST request should have the following data:

        - source_language
        - source_placeholder_id
        - source_plugin_id (optional)
        - target_language
        - target_placeholder_id
        - target_plugin_id (optional, new parent)
        """
        source_language = request.POST['source_language']
        source_placeholder_id = request.POST['source_placeholder_id']
        source_plugin_id = request.POST.get('source_plugin_id', None)
        target_language = request.POST['target_language']
        target_placeholder_id = request.POST['target_placeholder_id']
        target_plugin_id = request.POST.get('target_plugin_id', None)
        source_placeholder = get_object_or_404(Placeholder,
                                               pk=source_placeholder_id)
        target_placeholder = get_object_or_404(Placeholder,
                                               pk=target_placeholder_id)
        if not target_language or not target_language in get_language_list():
            return HttpResponseBadRequest(
                _("Language must be set to a supported language!"))
        if source_plugin_id:
            source_plugin = get_object_or_404(CMSPlugin, pk=source_plugin_id)
            reload_required = requires_reload(PLUGIN_COPY_ACTION,
                                              [source_plugin])
            plugins = list(
                source_placeholder.cmsplugin_set.filter(
                    tree_id=source_plugin.tree_id,
                    lft__gte=source_plugin.lft,
                    rght__lte=source_plugin.rght).order_by(
                        'tree_id', 'level', 'position'))
        else:
            plugins = list(
                source_placeholder.cmsplugin_set.filter(
                    language=source_language).order_by('tree_id', 'level',
                                                       'position'))
            reload_required = requires_reload(PLUGIN_COPY_ACTION, plugins)
        if not self.has_copy_plugin_permission(request, source_placeholder,
                                               target_placeholder, plugins):
            return HttpResponseForbidden(
                _('You do not have permission to copy these plugins.'))
        copy_plugins.copy_plugins_to(plugins, target_placeholder,
                                     target_language, target_plugin_id)
        plugin_list = CMSPlugin.objects.filter(
            language=target_language,
            placeholder=target_placeholder).order_by('tree_id', 'level',
                                                     'position')
        reduced_list = []
        for plugin in plugin_list:
            reduced_list.append({
                'id':
                plugin.pk,
                'type':
                plugin.plugin_type,
                'parent':
                plugin.parent_id,
                'position':
                plugin.position,
                'desc':
                force_unicode(plugin.get_short_description())
            })
        self.post_copy_plugins(request, source_placeholder, target_placeholder,
                               plugins)
        json_response = {
            'plugin_list': reduced_list,
            'reload': reload_required
        }
        return HttpResponse(simplejson.dumps(json_response),
                            content_type='application/json')
예제 #51
0
 def move_plugin(self, request):
     """
     POST request with following parameters:
     -plugin_id
     -placeholder_id
     -plugin_language (optional)
     -plugin_parent (optional)
     -plugin_order (array, optional)
     """
     plugin = CMSPlugin.objects.get(pk=int(request.POST['plugin_id']))
     placeholder = Placeholder.objects.get(pk=request.POST['placeholder_id'])
     parent_id = request.POST.get('plugin_parent', None)
     language = request.POST.get('plugin_language', plugin.language)
     source_placeholder = plugin.placeholder
     if not parent_id:
         parent_id = None
     else:
         parent_id = int(parent_id)
     order = request.POST.getlist("plugin_order[]")
     if not self.has_move_plugin_permission(request, plugin, placeholder):
         return HttpResponseForbidden(force_unicode(_("You have no permission to move this plugin")))
     if plugin.parent_id != parent_id:
         if parent_id:
             parent = CMSPlugin.objects.get(pk=parent_id)
             if parent.placeholder_id != placeholder.pk:
                 return HttpResponseBadRequest(force_unicode('parent must be in the same placeholder'))
             if parent.language != language:
                 return HttpResponseBadRequest(force_unicode('parent must be in the same language as plugin_language'))
         else:
             parent = None
         plugin.move_to(parent, position='last-child')
     try:
         template = self.get_placeholder_template(request, placeholder)
         has_reached_plugin_limit(placeholder, plugin.plugin_type, plugin.language, template=template)
     except PluginLimitReached as er:
         return HttpResponseBadRequest(er)
     plugin.save()
     for child in plugin.get_descendants(include_self=True):
         child.placeholder = placeholder
         child.language = language
         child.save()
     plugins = CMSPlugin.objects.filter(parent=parent_id, placeholder=placeholder, language=language).order_by('position')
     x = 0
     for level_plugin in plugins:
         if order:
             x = 0
             found = False
             for pk in order:
                 if level_plugin.pk == int(pk):
                     level_plugin.position = x
                     level_plugin.save()
                     found = True
                     break
                 x += 1
             if not found:
                 return HttpResponseBadRequest('order parameter did not have all plugins of the same level in it')
         else:
             level_plugin.position = x
             level_plugin.save()
             x += 1
     self.post_move_plugin(request, source_placeholder, placeholder, plugin)
     json_response = {'reload': requires_reload(PLUGIN_MOVE_ACTION, [plugin])}
     return HttpResponse(json.dumps(json_response), content_type='application/json')
예제 #52
0
 def edit_field(self, request, object_id, language):
     obj = self._get_object_for_single_field(object_id, language)
     opts = obj.__class__._meta
     saved_successfully = False
     cancel_clicked = request.POST.get("_cancel", False)
     raw_fields = request.GET.get("edit_fields")
     fields = [
         field for field in raw_fields.split(",")
         if field in self.frontend_editable_fields
     ]
     if not fields:
         context = {
             'opts': opts,
             'message': force_unicode(_("Field %s not found")) % raw_fields
         }
         return render_to_response('admin/cms/page/plugin/error_form.html',
                                   context, RequestContext(request))
     if not request.user.has_perm("{0}.change_{1}".format(
             self.model._meta.app_label, self.model._meta.module_name)):
         context = {
             'opts':
             opts,
             'message':
             force_unicode(
                 _("You do not have permission to edit this item"))
         }
         return render_to_response('admin/cms/page/plugin/error_form.html',
                                   context, RequestContext(request))
         # Dinamically creates the form class with only `field_name` field
     # enabled
     form_class = self.get_form(request, obj, fields=fields)
     if not cancel_clicked and request.method == 'POST':
         form = form_class(instance=obj, data=request.POST)
         if form.is_valid():
             form.save()
             saved_successfully = True
     else:
         form = form_class(instance=obj)
     admin_form = AdminForm(form,
                            fieldsets=[(None, {
                                'fields': fields
                            })],
                            prepopulated_fields={},
                            model_admin=self)
     media = self.media + admin_form.media
     context = {
         'CMS_MEDIA_URL': get_cms_setting('MEDIA_URL'),
         'title': opts.verbose_name,
         'plugin': None,
         'plugin_id': None,
         'adminform': admin_form,
         'add': False,
         'is_popup': True,
         'media': media,
         'opts': opts,
         'change': True,
         'save_as': False,
         'has_add_permission': False,
         'window_close_timeout': 10,
     }
     if cancel_clicked:
         # cancel button was clicked
         context.update({
             'cancel': True,
         })
         return render_to_response(
             'admin/cms/page/plugin/confirm_form.html', context,
             RequestContext(request))
     if not cancel_clicked and request.method == 'POST' and saved_successfully:
         return render_to_response(
             'admin/cms/page/plugin/confirm_form.html', context,
             RequestContext(request))
     return render_to_response('admin/cms/page/plugin/change_form.html',
                               context, RequestContext(request))
def _current_language_marker(language, lang_code):
    return force_unicode(ugettext(language))
예제 #54
0
    def _inner(*args, **kwargs):
        start = time.time()
        func(*args, **kwargs)
        end = time.time()

        TIMINGS[force_unicode(func)] = end - start
예제 #55
0
    def edit_plugin(self, request, plugin_id):
        plugin_id = int(plugin_id)
        cms_plugin = get_object_or_404(CMSPlugin.objects.select_related('placeholder'), pk=plugin_id)

        instance, plugin_admin = cms_plugin.get_plugin_instance(self.admin_site)
        if not self.has_change_plugin_permission(request, cms_plugin):
            return HttpResponseForbidden(force_unicode(_("You do not have permission to edit this plugin")))
        plugin_admin.cms_plugin_instance = cms_plugin
        try:
            plugin_admin.placeholder = cms_plugin.placeholder
        except Placeholder.DoesNotExist:
            pass
        if request.method == "POST":
            # set the continue flag, otherwise will plugin_admin make redirect to list
            # view, which actually doesn't exists
            request.POST['_continue'] = True
        if request.POST.get("_cancel", False):
            # cancel button was clicked
            context = {
                'CMS_MEDIA_URL': get_cms_setting('MEDIA_URL'),
                'plugin': cms_plugin,
                'is_popup': True,
                "type": cms_plugin.get_plugin_name(),
                'plugin_id': plugin_id,
                'icon': force_escape(escapejs(cms_plugin.get_instance_icon_src())),
                'alt': force_escape(escapejs(cms_plugin.get_instance_icon_alt())),
                'cancel': True,
            }
            instance = cms_plugin.get_plugin_instance()[0]
            if instance:
                context['name'] = force_unicode(instance)
            else:
                # cancelled before any content was added to plugin
                cms_plugin.delete()
                context.update({
                    "deleted": True,
                    'name': force_unicode(cms_plugin),
                })
            return render_to_response('admin/cms/page/plugin/confirm_form.html', context, RequestContext(request))

        if not instance:
            # instance doesn't exist, call add view
            response = plugin_admin.add_view(request)
        else:
            # already saved before, call change view
            # we actually have the instance here, but since i won't override
            # change_view method, is better if it will be loaded again, so
            # just pass id to plugin_admin
            response = plugin_admin.change_view(request, str(plugin_id))
        if request.method == "POST" and plugin_admin.object_successfully_changed:
            self.post_edit_plugin(request, plugin_admin.saved_object)
            saved_object = plugin_admin.saved_object
            context = {
                'CMS_MEDIA_URL': get_cms_setting('MEDIA_URL'),
                'plugin': saved_object,
                'is_popup': True,
                'name': force_unicode(saved_object),
                "type": saved_object.get_plugin_name(),
                'plugin_id': plugin_id,
                'icon': force_escape(saved_object.get_instance_icon_src()),
                'alt': force_escape(saved_object.get_instance_icon_alt()),
            }
            return render_to_response('admin/cms/page/plugin/confirm_form.html', context, RequestContext(request))
        return response