예제 #1
0
파일: tree.py 프로젝트: Aendra/django_vgv
    def handle(self, *args, **options):
        """
        Repairs the tree
        """
        self.stdout.write('fixing page tree')
        Page.fix_tree()
        last = None
        try:
            first = Page.objects.filter(
                publisher_is_draft=True, parent__isnull=True
            ).order_by('path')[0]
        except IndexError:
            first = None
        for page in Page.objects.filter(
            publisher_is_draft=True, parent__isnull=True
        ).order_by('site__pk', 'path'):
            if last:
                last = last.reload()
                page = page.reload()
                page.move(target=last, pos='right')
            elif first and first.pk != page.pk:
                page.move(target=first, pos='left')
            last = page.reload()
        for page in Page.objects.filter(
            publisher_is_draft=False, parent__isnull=True
        ).order_by('publisher_public__path'):
            page = page.reload()
            public = page.publisher_public
            page.move(target=public, pos='right')

        self.stdout.write('fixing plugin tree')
        CMSPlugin.fix_tree()
        self.stdout.write('all done')
예제 #2
0
파일: tree.py 프로젝트: kasksad/DjangoCMS
    def handle(self, *args, **options):
        """
        Repairs the tree
        """
        self.stdout.write('fixing page tree')
        Page.fix_tree()
        last = None
        try:
            first = Page.objects.filter(
                publisher_is_draft=True,
                parent__isnull=True).order_by('path')[0]
        except IndexError:
            first = None
        for page in Page.objects.filter(publisher_is_draft=True,
                                        parent__isnull=True).order_by(
                                            'site__pk', 'path'):
            if last:
                last = last.reload()
                page = page.reload()
                page.move(target=last, pos='right')
            elif first and first.pk != page.pk:
                page.move(target=first, pos='left')
            last = page.reload()
        for page in Page.objects.filter(
                publisher_is_draft=False,
                parent__isnull=True).order_by('publisher_public__path'):
            page = page.reload()
            public = page.publisher_public
            page.move(target=public, pos='right')

        self.stdout.write('fixing plugin tree')
        CMSPlugin.fix_tree()
        self.stdout.write('all done')
예제 #3
0
def add_plugin(request):
    if 'history' in request.path or 'recover' in request.path:
        return HttpResponse(str("error"))
    if request.method == "POST":
        plugin_type = request.POST['plugin_type']
        page_id = request.POST.get('page_id', None)
        parent = None
        if page_id:
            page = get_object_or_404(Page, pk=page_id)
            placeholder = request.POST['placeholder'].lower()
            language = request.POST['language']
            position = CMSPlugin.objects.filter(page=page, language=language, placeholder=placeholder).count()
        else:
            parent_id = request.POST['parent_id']
            parent = get_object_or_404(CMSPlugin, pk=parent_id)
            page = parent.page
            placeholder = parent.placeholder
            language = parent.language
            position = None
        plugin = CMSPlugin(page=page, language=language, plugin_type=plugin_type, position=position, placeholder=placeholder) 
        if parent:
            plugin.parent = parent
        plugin.save()
        if 'reversion' in settings.INSTALLED_APPS:
            page.save()
            save_all_plugins(page)
            revision.user = request.user
            plugin_name = unicode(plugin_pool.get_plugin(plugin_type).name)
            revision.comment = _(u"%(plugin_name)s plugin added to %(placeholder)s") % {'plugin_name':plugin_name, 'placeholder':placeholder}       
        return HttpResponse(str(plugin.pk))
    raise Http404
 def get_plugin(self):
     instance = CMSPlugin(language='en',
                          plugin_type="BasePlugin",
                          placeholder=Placeholder(id=1111))
     instance.cmsplugin_ptr = instance
     instance.pk = 1110
     return instance
예제 #5
0
 def get_plugin(self):
     instance = CMSPlugin(language='en',
                          plugin_type="NotIndexedPlugin",
                          placeholder=Placeholder(id=1235))
     instance.cmsplugin_ptr = instance
     instance.pk = 1234  # otherwise plugin_meta_context_processor() crashes
     return instance
예제 #6
0
    def handle(self, *args, **options):
        """
        Repairs the tree
        """
        self.stdout.write('fixing page tree')
        TreeNode.fix_tree()

        root_nodes = TreeNode.objects.filter(parent__isnull=True)

        last = None

        try:
            first = root_nodes.order_by('path')[0]
        except IndexError:
            first = None

        for node in root_nodes.order_by('site__pk', 'path'):
            if last:
                last.refresh_from_db()
                node.refresh_from_db()
                node.move(target=last, pos='right')
            elif first and first.pk != node.pk:
                node.move(target=first, pos='left')
            last = node

        for root in root_nodes.order_by('site__pk', 'path'):
            self._update_descendants_tree(root)

        self.stdout.write('fixing plugin tree')
        CMSPlugin.fix_tree()
        self.stdout.write('all done')
예제 #7
0
 def setUp(self):
     placeholder = Placeholder(id=1235)
     instance = CMSPlugin(plugin_type="NotIndexedPlugin",
                          placeholder=placeholder)
     instance.cmsplugin_ptr = instance
     instance.pk = 1234  # otherwise plugin_meta_context_processor() crashes
     self.instance = instance
     self.index = TitleIndex()
     self.request = get_request_for_search(language='en')
예제 #8
0
 def save(self, no_signals=False, clear_cache=True, *args, **kwargs):
     """
     Both base classes override save(), and can't be called sequentially.
     Need to coordinate save so both base class save methods work out.
     """
     is_new_object = self._get_pk_val() is None
     CMSPlugin.save(self, no_signals, *args, **kwargs)
     if is_new_object:
         self._post_save(clear_cache)
예제 #9
0
 def get_plugin(self):
     instance = CMSPlugin(
         language='en',
         plugin_type="NotIndexedPlugin",
         placeholder=Placeholder(id=1235)
     )
     instance.cmsplugin_ptr = instance
     instance.pk = 1234  # otherwise plugin_meta_context_processor() crashes
     return instance
예제 #10
0
 def setUp(self):
     placeholder = Placeholder(id=1235)
     instance = CMSPlugin(
         plugin_type="NotIndexedPlugin",
         placeholder=placeholder
     )
     instance.cmsplugin_ptr = instance
     instance.pk = 1234 # otherwise plugin_meta_context_processor() crashes
     self.instance = instance
     self.index = TitleIndex()
     self.request = get_request_for_search(language='en')
예제 #11
0
 def save_model(self, request, obj, form, change):
     response = super().save_model(request, obj, form, change)
     for x in range(int(form.cleaned_data['create'])):
         col = CMSPlugin(
             parent=obj,
             placeholder=obj.placeholder,
             language=obj.language,
             position=CMSPlugin.objects.filter(parent=obj).count(),
             plugin_type=ColumnPlugin.__name__)
         col.save()
     return response
    def handle(self, *args, **options):
        """
        Repairs the tree
        """
        self.stdout.write('fixing page tree')
        Page.fix_tree()

        root_draft_pages = Page.objects.filter(
            publisher_is_draft=True,
            parent__isnull=True,
        )

        last = None

        try:
            first = root_draft_pages.order_by('path')[0]
        except IndexError:
            first = None

        for page in root_draft_pages.order_by('site__pk', 'path'):
            if last:
                last = last.reload()
                page = page.reload()
                page.move(target=last, pos='right')
            elif first and first.pk != page.pk:
                page.move(target=first, pos='left')
            last = page.reload()

        root_public_pages = Page.objects.filter(
            publisher_is_draft=False,
            parent__isnull=True,
        ).order_by('publisher_public__path')

        # Filter out any root public pages whose draft page
        # has a parent.
        # This avoids a tree corruption where the public root page
        # is added as a child of the draft page's draft parent
        # instead of the draft page's public parent
        root_public_pages = root_public_pages.filter(
            publisher_public__parent__isnull=True
        )

        for page in root_public_pages:
            page = page.reload()
            public = page.publisher_public
            page.move(target=public, pos='right')

        for root in root_draft_pages.order_by('site__pk', 'path'):
            self._update_descendants_tree(root)

        self.stdout.write('fixing plugin tree')
        CMSPlugin.fix_tree()
        self.stdout.write('all done')
예제 #13
0
 def save_model(self, request, obj, form, change):
     response = super(MultiColumnPlugin, self).save_model(
         request, obj, form, change
     )
     for x in range(int(form.cleaned_data['create'])):
         col = CMSPlugin(
             parent=obj,
             placeholder=obj.placeholder,
             language=obj.language,
             position=CMSPlugin.objects.filter(parent=obj).count(),
             plugin_type=ColumnPlugin.__name__
         )
         col.save()
     return response
예제 #14
0
def _restore_archived_plugins_tree(action, data, root_plugin_id=None):
    plugin_ids = [plugin.pk for plugin in data['plugins']]
    plugins_by_id = CMSPlugin.objects.in_bulk(plugin_ids)
    plugin_data = {
        'language': action.language,
        'placeholder': action.placeholder
    }

    if root_plugin_id:
        root = CMSPlugin.objects.get(pk=root_plugin_id)
    else:
        root = None

    for _plugin in data['plugins']:
        plugin = plugins_by_id[_plugin.pk]

        if root:
            plugin = plugin.update(refresh=True, parent=root, **plugin_data)
            plugin = plugin.move(root, pos='last-child')
        else:
            target = CMSPlugin.get_last_root_node()
            plugin = plugin.update(refresh=True, parent=None, **plugin_data)
            plugin = plugin.move(target, pos='right')

        # Update all children to match the parent's
        # language and placeholder
        plugin.get_descendants().update(**plugin_data)

    action.placeholder.mark_as_dirty(action.language, clear_cache=False)
예제 #15
0
 def test_get_plugin_class_cache(self):
     plugin = CMSPlugin(plugin_type='MultiColumnPlugin')
     renderer = self.get_renderer()
     plugin_class = renderer.get_plugin_class(plugin)
     self.assertIn('MultiColumnPlugin', renderer._cached_plugin_classes)
     self.assertEqual(plugin_class.__name__, 'MultiColumnPlugin')
     self.assertEqual(renderer._cached_plugin_classes['MultiColumnPlugin'],
                      plugin_class)
예제 #16
0
def update_max_root(plugins_qs=None):
    # This is done because plugins can be deleted, but must still be revertable with the same path as before.
    # The max_path marker root ensures that new root plugins must have a path that is lexicographically higher than
    # any path of any plugin in a revision.
    if plugins_qs is None:
        plugins_qs = CMSPlugin.get_root_nodes()
    if not plugins_qs.exists():
        return None
    try:
        max_root = CMSPlugin.get_root_nodes().get(plugin_type=MAX_ROOT_MARK)
    except CMSPlugin.DoesNotExist:
        max_root = None
    max_plugin = plugins_qs.filter(depth=1).latest('path')
    if not max_root or max_root.path < max_plugin.path:
        if max_root:
            max_root.delete()
        return CMSPlugin.objects.create(plugin_type=MAX_ROOT_MARK, language=MAX_ROOT_MARK)
    return None
예제 #17
0
    def add_plugin(self, request):
        """ Plugins can be added to an item or another plugin """
        # TODO: Enable number limitations from CMS placeholder configs

        if 'history' in request.path or 'recover' in request.path:
            return HttpResponseBadRequest("error")

        plugin_type = request.POST['plugin_type']
        placeholder_id = request.POST.get('placeholder', None)
        parent_id = request.POST.get('parent_id', None)

        if placeholder_id:
            try:
                placeholder_id = int(placeholder_id)
            except ValueError:
                placeholder_id = None
        if parent_id:
            try:
                parent_id = int(parent_id)
            except ValueError:
                parent_id = None

        if placeholder_id:
            placeholder = get_object_or_404(Placeholder, pk=placeholder_id)
            item = NewsItem.objects.get(placeholders=placeholder)
            if not item.has_change_permission(request):
                raise Http404
            plugin = CMSPlugin(language='en', plugin_type=plugin_type,
                    placeholder=placeholder, position=CMSPlugin.objects.filter(
                        placeholder=placeholder).count())
            plugin.save()
            if _REVERSION:
                make_revision_with_plugins(item, request.user,
                        '%(plugin_name)s plugin added to %(placeholder)s' % {
                            'plugin_name':
                                unicode(plugin_pool.get_plugin(plugin_type).name),
                            'placeholder': placeholder.slot})
        elif parent_id:
            parent = CMSPlugin.objects.select_related('placeholder').get(
                    pk=parent_id)
            item = NewsItem.objects.get(placeholders=parent.placeholder)
            if not item.has_change_permission(request):
                raise Http404
            plugin = CMSPlugin(language='en', plugin_type=plugin_type,
                    placeholder=parent.placeholder, parent=parent,
                    position=CMSPlugin.objects.filter(parent=parent).count())
            plugin.save()
            if _REVERSION:
                make_revision_with_plugins(item, request.user,
                        '%(plugin_name)s plugin added to plugin '
                        '%(plugin)s in %(placeholder)s' % {
                            'plugin_name':
                                unicode(plugin_pool.get_plugin(plugin_type).name),
                            'placeholder': parent.placeholder.slot,
                            'plugin': unicode(parent)})
        else:
            return HttpResponseBadRequest(
                    "Either parent of placeholder is required")

        return HttpResponse(unicode(plugin.pk), content_type='text/plain')
예제 #18
0
def add_plugin(request):
    if 'history' in request.path or 'recover' in request.path:
        return HttpResponse(str("error"))
    if request.method == "POST":
        plugin_type = request.POST['plugin_type']
        page_id = request.POST.get('page_id', None)
        parent = None
        if page_id:
            page = get_object_or_404(Page, pk=page_id)
            placeholder = request.POST['placeholder'].lower()
            language = request.POST['language']
            position = CMSPlugin.objects.filter(page=page, language=language, placeholder=placeholder).count()
        else:
            parent_id = request.POST['parent_id']
            parent = get_object_or_404(CMSPlugin, pk=parent_id)
            page = parent.page
            placeholder = parent.placeholder
            language = parent.language
            position = None

        if not page.has_change_permission(request):
            return HttpResponseForbidden(ugettext("You do not have permission to change this page"))

        # Sanity check to make sure we're not getting bogus values from JavaScript:
        if not language or not language in [ l[0] for l in settings.LANGUAGES ]:
            return HttpResponseBadRequest(ugettext("Language must be set to a supported language!"))
        
        plugin = CMSPlugin(page=page, language=language, plugin_type=plugin_type, position=position, placeholder=placeholder) 

        if parent:
            plugin.parent = parent
        plugin.save()
        if 'reversion' in settings.INSTALLED_APPS:
            page.save()
            save_all_plugins(request, page)
            revision.user = request.user
            plugin_name = unicode(plugin_pool.get_plugin(plugin_type).name)
            revision.comment = _(u"%(plugin_name)s plugin added to %(placeholder)s") % {'plugin_name':plugin_name, 'placeholder':placeholder}
        return HttpResponse(str(plugin.pk))
    raise Http404
예제 #19
0
def add_plugin(request):
    if 'history' in request.path or 'recover' in request.path:
        return HttpResponse(str("error"))
    if request.method == "POST":
        plugin_type = request.POST['plugin_type']
        page_id = request.POST.get('page_id', None)
        parent = None
        if page_id:
            page = get_object_or_404(Page, pk=page_id)
            placeholder = request.POST['placeholder'].lower()
            language = request.POST['language']
            position = CMSPlugin.objects.filter(page=page, language=language, placeholder=placeholder).count()
        else:
            parent_id = request.POST['parent_id']
            parent = get_object_or_404(CMSPlugin, pk=parent_id)
            page = parent.page
            placeholder = parent.placeholder
            language = parent.language
            position = None

        if not page.has_change_permission(request):
            return HttpResponseForbidden(_("You do not have permission to change this page"))

        # Sanity check to make sure we're not getting bogus values from JavaScript:
        if not language or not language in [ l[0] for l in settings.LANGUAGES ]:
            return HttpResponseBadRequest(_("Language must be set to a supported language!"))
        
        plugin = CMSPlugin(page=page, language=language, plugin_type=plugin_type, position=position, placeholder=placeholder) 

        if parent:
            plugin.parent = parent
        plugin.save()
        if 'reversion' in settings.INSTALLED_APPS:
            page.save()
            save_all_plugins(request, page)
            revision.user = request.user
            plugin_name = unicode(plugin_pool.get_plugin(plugin_type).name)
            revision.comment = _(u"%(plugin_name)s plugin added to %(placeholder)s") % {'plugin_name':plugin_name, 'placeholder':placeholder}
        return HttpResponse(str(plugin.pk))
    raise Http404
예제 #20
0
def create_plugins(page, count=5):
    placeholders = page.placeholders.all()

    for placeholder in placeholders:
        for language in page.get_languages():
            data = {
                'language': language,
                'plugin_type': 'CMSToolsNode',
                'placeholder': placeholder,
            }

            for i in range(0, count):
                # Depth 1
                parent = CMSPlugin.add_root(position=i, **data)
                # Depth 2
                child = parent.add_child(position=i, parent=parent, **data)
                # Depth 3
                child.add_child(position=i, parent=child, **data)
예제 #21
0
 def create_module_plugin(cls, name, category, plugins):
     placeholder = category.modules
     position = placeholder.get_plugins().filter(
         parent__isnull=True).count()
     plugin_kwargs = {
         'plugin_type': cls.__name__,
         'placeholder_id': category.modules_id,
         'language': settings.LANGUAGE_CODE,
         'position': position,
     }
     plugin = CMSPlugin.add_root(**plugin_kwargs)
     instance = cls.model(module_name=name, module_category=category)
     plugin.set_base_attr(instance)
     instance.save()
     copy_plugins_to_placeholder(
         plugins,
         placeholder=placeholder,
         language=plugin.language,
         root_plugin=plugin,
     )
예제 #22
0
    def restore(self, placeholder, language, parent=None):
        plugin_kwargs = {
            'pk': self.pk,
            'plugin_type': self.plugin_type,
            'placeholder': placeholder,
            'language': language,
            'parent': parent,
            'position': self.position,
        }

        if parent:
            plugin = parent.add_child(**plugin_kwargs)
        else:
            plugin = CMSPlugin.add_root(**plugin_kwargs)

        if self.plugin_type != 'CMSPlugin':
            _d_instance = self.deserialized_instance
            _d_instance.object._no_reorder = True
            plugin.set_base_attr(_d_instance.object)
            _d_instance.save()
        return plugin
예제 #23
0
    def restore(self, placeholder, language, parent=None, with_data=True):
        parent_id = parent.pk if parent else None
        plugin_kwargs = {
            'plugin_type': self.plugin_type,
            'placeholder': placeholder,
            'language': language,
            'parent_id': parent_id,
            'position': self.position,
        }

        if parent:
            plugin = parent.add_child(**plugin_kwargs)
        else:
            plugin = CMSPlugin.add_root(**plugin_kwargs)

        if with_data and self.plugin_type != 'CMSPlugin':
            _d_instance = self.deserialized_instance
            _d_instance.object._no_reorder = True
            _d_instance.object.cmsplugin_ptr = plugin
            plugin.set_base_attr(_d_instance.object)
            _d_instance.save()
            return _d_instance.object
        return plugin
예제 #24
0
 def get_unbound_plugins(self):
     return CMSPlugin.get_tree(self).order_by('path')
예제 #25
0
    def add_plugin(self, request):
        '''
        Could be either a page or a parent - if it's a parent we get the page via parent.
        '''
        if 'history' in request.path or 'recover' in request.path:
            return HttpResponse(str("error"))
        if request.method == "POST":
            plugin_type = request.POST['plugin_type']
            placeholder_id = request.POST.get('placeholder', None)
            parent_id = request.POST.get('parent_id', None)
            if placeholder_id:
                placeholder = get_object_or_404(Placeholder, pk=placeholder_id)
                page = get_item_from_placeholder_if_exists(placeholder)
            else:
                placeholder = None
                page = None
            parent = None
            # page add-plugin
            if page:
                language = request.POST['language'] or get_language_from_request(request)
                position = CMSPlugin.objects.filter(language=language, placeholder=placeholder).count()
                limits = settings.CMS_PLACEHOLDER_CONF.get("%s %s" % (page.get_template(), placeholder.slot), {}).get('limits', None)
                if not limits:
                    limits = settings.CMS_PLACEHOLDER_CONF.get(placeholder.slot, {}).get('limits', None)
                if limits:
                    global_limit = limits.get("global")
                    type_limit = limits.get(plugin_type)
                    if global_limit and position >= global_limit:
                        return HttpResponseBadRequest("This placeholder already has the maximum number of plugins")
                    elif type_limit:
                        type_count = CMSPlugin.objects.filter(language=language, placeholder=placeholder, plugin_type=plugin_type).count()
                        if type_count >= type_limit:
                            return HttpResponseBadRequest("This placeholder already has the maximum number allowed %s plugins.'%s'" % plugin_type)
            # in-plugin add-plugin
            elif parent_id:
                parent = get_object_or_404(CMSPlugin, pk=parent_id)
                placeholder = parent.placeholder
                page = get_item_from_placeholder_if_exists(placeholder)
                if not page: # Make sure we do have a page
                    raise Http404
                language = parent.language
                position = None
            # placeholder (non-page) add-plugin
            else:
                # do NOT allow non-page placeholders to use this method, they
                # should use their respective admin!
                raise Http404
            
            if not page.has_change_permission(request):
                # we raise a 404 instead of 403 for a slightly improved security
                # and to be consistent with placeholder admin
                raise Http404

            # Sanity check to make sure we're not getting bogus values from JavaScript:
            if not language or not language in [ l[0] for l in settings.LANGUAGES ]:
                return HttpResponseBadRequest(unicode(_("Language must be set to a supported language!")))

            plugin = CMSPlugin(language=language, plugin_type=plugin_type, position=position, placeholder=placeholder)

            if parent:
                plugin.parent = parent
            plugin.save()
            
            if 'reversion' in settings.INSTALLED_APPS and page:
                make_revision_with_plugins(page)
                reversion.revision.user = request.user
                plugin_name = unicode(plugin_pool.get_plugin(plugin_type).name)
                reversion.revision.comment = unicode(_(u"%(plugin_name)s plugin added to %(placeholder)s") % {'plugin_name':plugin_name, 'placeholder':placeholder})
                
            return HttpResponse(str(plugin.pk))
        raise Http404