Ejemplo n.º 1
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
Ejemplo n.º 2
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')
Ejemplo n.º 3
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)
Ejemplo n.º 4
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
Ejemplo n.º 5
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
Ejemplo n.º 6
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
Ejemplo n.º 7
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
Ejemplo n.º 8
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