以编程方式将一个django CMS插件替换为另一个 [英] Programmatically replace one django CMS plugin with another

查看:51
本文介绍了以编程方式将一个django CMS插件替换为另一个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个网站,其中包含许多指向YouTube的Filer Video插件.我认为文件管理器不适用于响应式网站(它需要固定宽度),因此,我没有修复它,而是编写了自己的简单小插件,该插件可以很容易地将YouTube嵌入到可变宽度布局中.

所以现在我有大量的 cmsplugin_filer_video.models.FilerVideo 实例,我想用我的 cmsyoutube.models.YtVideo 实例替换.循环播放它们很好,但是我如何以编程方式取出一个插件,并用另一个插件替换(代替)?

大声考虑,它们(最终)都基于CMSPlugin.我可以将父项移入新班级吗?


这是到目前为止.遍历旧视频,并尝试创建一个指向相同cmsplugin_ptr的新 YtVideo (相同),然后再删除旧的 FilerVideo v ...

FilerVideo.objects.all()中v的

 :插件= v.cmsplugin_ptrv.cmsplugin_ptr_id =无新= YtVideo()new.cmsplugin_ptr =插件plugin.plugin_type ='YoutubePlugin'new.youtube_id = re.search(r'v =([^&] +)',v.movi​​e_url).groups(0)[0]new.thumbnail = v.imagenew.save()plugin.save()v.delete() 

但是TreeBeard对此非常沮丧:

  NodeAlreadySaved:尝试添加数据库中已经存在的树节点 

好像那里有一些级联保存逻辑.知道如何热线连接,以便我可以替换插件实例而又不破坏底层的 CMSPlugin 树吗?

解决方案

使用 cms.api.add_plugin 可以带来更多的运气.有趣的是,我无法坚持下去.我认为它期望的是糊涂的最后一个孩子"式的英语,而不是数字输入.所以我只是在 add_plugin 之后第二次钉了它.

从cms导入api中的

 对于FilerVideo.objects.all()中的v:youtube_id = re.search(r'v =([^&] +)',v.movi​​e_url).groups(0)[0]位置= v.位置v.delete()new = api.add_plugin(v.placeholder,'YoutubePlugin',v.language,位置,v.parent,youtube_id = youtube_id,thumbnail = v.image)new.position =位置new.save() 

I have a website with many Filer Video plugins that point to YouTube. I've decided that the filer isn't very good for responsive sites (it demands fixed widths) so rather than fix it, I've written my own simple little plugin that does YouTube embeds very easily for variable-width layouts.

So now I have a ton of cmsplugin_filer_video.models.FilerVideo instances that I want to replace with my cmsyoutube.models.YtVideo instances. Looping them is fine but how do I programmatically take out one plugin and replace it (in place) with another?

Thinking out aloud, they're both [eventually] based on CMSPlugin. Can I transpose the parent items into my new class?


This is as far as I've gotten so far. Iterate over the old videos and try to create a new YtVideo (same thing) that points at the same cmsplugin_ptr before deleting the old FilerVideo v...

for v in FilerVideo.objects.all():
    plugin = v.cmsplugin_ptr
    v.cmsplugin_ptr_id = None
    new = YtVideo()
    new.cmsplugin_ptr = plugin
    plugin.plugin_type = 'YoutubePlugin'
    new.youtube_id = re.search(r'v=([^&]+)', v.movie_url).groups(0)[0]
    new.thumbnail = v.image
    new.save()
    plugin.save()
    v.delete()

But TreeBeard gets very upset with this:

NodeAlreadySaved: Attempted to add a tree node that is already in the database

Looks like there's some cascade save logic in there. Any idea how to hotwire this so I can replace the plugin instances without destroying the underlying CMSPlugin tree?

解决方案

Had more luck with cms.api.add_plugin. Funny thing is I couldn't get the position to stick. I think it expects wafty "last-child"-style English rather than a numerical input. So I just nail it in a second time after add_plugin.

from cms import api
for v in FilerVideo.objects.all():
    youtube_id = re.search(r'v=([^&]+)', v.movie_url).groups(0)[0]
    position = v.position
    v.delete()
    new = api.add_plugin(v.placeholder, 'YoutubePlugin', v.language, position, v.parent, youtube_id=youtube_id, thumbnail=v.image)
    new.position = position
    new.save()

这篇关于以编程方式将一个django CMS插件替换为另一个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆