def test_render_child_plugin_endpoint_calls_context_processors(self): simple_page = create_page('test page', 'page.html', u'en') simple_placeholder = simple_page.placeholders.get(slot='content') text_plugin = add_plugin( simple_placeholder, "TextPlugin", "en", body="I'm the first", ) text_plugin_class = text_plugin.get_plugin_class_instance() child_plugin = self._add_child_plugin( text_plugin, plugin_type='SekizaiPlugin', ) text_plugin = self.add_plugin_to_text(text_plugin, child_plugin) with self.login_user_context(self.get_superuser()): request = self.get_request() action_token = text_plugin_class.get_action_token(request, text_plugin) endpoint = self.get_custom_admin_url(TextPlugin, 'render_plugin') endpoint += '?token={}&plugin={}'.format(action_token, child_plugin.pk) response = self.client.get(endpoint) self.assertEqual(response.status_code, 200) context = RequestContext(request) context['request'] = request rendered_content = _render_cms_plugin(child_plugin, context) rendered_child_plugin = plugin_to_tag( child_plugin, content=rendered_content, admin=True, ) self.assertEqual(force_text(response.content), rendered_child_plugin)
def test_render_child_plugin_endpoint(self): simple_page = create_page('test page', 'page.html', u'en') simple_placeholder = simple_page.placeholders.get(slot='content') text_plugin = add_plugin( simple_placeholder, "TextPlugin", "en", body="I'm the first", ) text_plugin_class = text_plugin.get_plugin_class_instance() child_plugin = self._add_child_plugin(text_plugin) text_plugin = self.add_plugin_to_text(text_plugin, child_plugin) with self.login_user_context(self.get_superuser()): request = self.get_request() action_token = text_plugin_class.get_action_token(request, text_plugin) endpoint = self.get_custom_admin_url(TextPlugin, 'render_plugin') endpoint += '?token={}&plugin={}'.format(action_token, child_plugin.pk) response = self.client.get(endpoint) self.assertEqual(response.status_code, 200) context = RequestContext(request) context['request'] = request rendered_content = _render_cms_plugin(child_plugin, context) rendered_child_plugin = plugin_to_tag( child_plugin, content=rendered_content, admin=True, ) self.assertEqual(force_text(response.content), rendered_child_plugin) child_plugin = self._add_child_plugin(text_plugin, plugin_type='PreviewDisabledPlugin') text_plugin = self.add_plugin_to_text(text_plugin, child_plugin) with self.login_user_context(self.get_superuser()): request = self.get_request() action_token = text_plugin_class.get_action_token(request, text_plugin) endpoint = self.get_custom_admin_url(TextPlugin, 'render_plugin') endpoint += '?token={}&plugin={}'.format(action_token, child_plugin.pk) response = self.client.get(endpoint) self.assertEqual(response.status_code, 200) # it is important that we do not add any extra whitespace inside of # <cms-plugin></cms-plugin> rendered_child_plugin = ('<cms-plugin render-plugin=false ' 'alt="Preview Disabled Plugin - 3 ' '"title="Preview Disabled Plugin - 3" ' 'id="3"><span>Preview is disabled for this plugin</span>' '</cms-plugin>') self.assertEqual(force_text(response.content), rendered_child_plugin)
def test_child_plugin(self): page = create_page(title='pagina',template='page.html',language='en') placeholder = page.placeholders.get(slot='content') plugin = add_plugin(placeholder, 'TextPlugin','en',body="Lorem ipsum") test_image = self.create_django_image_obj() pic_plugin = add_plugin(placeholder,'PicturePlugin','en',target=plugin,image=test_image,alt="Foo") plugin.body = '%s %s'%( plugin.body,plugin_to_tag(pic_plugin)) plugin.save() page.publish('en') response = self.client.get(page.get_absolute_url('en')) self.assertContains(response, 'Lorem ipsum') self.assertContains(response, '<img src="/media/%s' % pic_plugin.image)
def notify_on_autoadd_children(self, request, conf, children): """ Method called when we auto add children to this plugin via default_plugins/<plugin>/children in CMS_PLACEHOLDER_CONF. we must replace some strings with child tag for the CKEDITOR. Strings are "%(_tag_child_<order>)s" with the inserted order of chidren """ replacements = dict() order = 1 for child in children: replacements['_tag_child_'+str(order)] = plugin_to_tag(child) order+=1 self.body = self.body % replacements self.save()
def add_plugin_to_text(self, text_plugin, plugin): text_plugin.body = "%s %s" % (text_plugin.body, plugin_to_tag(plugin)) text_plugin.save() return text_plugin
def _do_replace(obj, match): return plugin_to_tag(obj, content=new_plugin_content)
def add_plugin_to_text(self, text_plugin, plugin): text_plugin.body = '%s %s' % (text_plugin.body, plugin_to_tag(plugin)) text_plugin.save() return text_plugin
def test_render_child_plugin_endpoint(self): simple_page = create_page('test page', 'page.html', u'en') simple_placeholder = simple_page.placeholders.get(slot='content') text_plugin = add_plugin( simple_placeholder, "TextPlugin", "en", body="I'm the first", ) text_plugin_class = text_plugin.get_plugin_class_instance() child_plugin = self._add_child_plugin(text_plugin) text_plugin = self.add_plugin_to_text(text_plugin, child_plugin) with self.login_user_context(self.get_superuser()): request = self.get_request() action_token = text_plugin_class.get_action_token( request, text_plugin) endpoint = self.get_custom_admin_url(TextPlugin, 'render_plugin') endpoint += '?token={}&plugin={}'.format(action_token, child_plugin.pk) response = self.client.get(endpoint) self.assertEqual(response.status_code, 200) context = RequestContext(request) context['request'] = request rendered_content = _render_cms_plugin(child_plugin, context) rendered_child_plugin = plugin_to_tag( child_plugin, content=rendered_content, admin=True, ) self.assertEqual(force_text(response.content), rendered_child_plugin) child_plugin = self._add_child_plugin( text_plugin, plugin_type='PreviewDisabledPlugin') text_plugin = self.add_plugin_to_text(text_plugin, child_plugin) with self.login_user_context(self.get_superuser()): request = self.get_request() action_token = text_plugin_class.get_action_token( request, text_plugin) endpoint = self.get_custom_admin_url(TextPlugin, 'render_plugin') endpoint += '?token={}&plugin={}'.format(action_token, child_plugin.pk) response = self.client.get(endpoint) self.assertEqual(response.status_code, 200) # it is important that we do not add any extra whitespace inside of # <cms-plugin></cms-plugin> rendered_child_plugin = ( '<cms-plugin render-plugin=false ' 'alt="Preview Disabled Plugin - 3 ' '"title="Preview Disabled Plugin - 3" ' 'id="3"><span>Preview is disabled for this plugin</span>' '</cms-plugin>') self.assertEqual(force_text(response.content), rendered_child_plugin)
def fix_images(self, plugin, dom, entry): found = False CMSPlugin.objects.filter(parent=plugin, plugin_type='PicturePlugin').delete() for counter, img in enumerate(dom.xpath('//img')): src = img.attrib['src'] alt = img.attrib.get('alt') or '' imgid = img.attrib.get('id') logging.info("Extracting image %s: %s", imgid, src) print('image src', src) # if img.getparent().tag == 'a': # link = img.getparent() # link.getparent().replace(link, img) if imgid is not None and imgid.startswith('plugin_obj_'): plugin_id = imgid.rsplit('_', 1)[0] plugin_id continue found = True image_plugin = None filer_image = None if src.startswith('http'): file_obj = self.get_image(src) if file_obj is not None: filer_image = self.get_filer_image( file_obj=file_obj, name=alt ) else: src_part = src.split('/')[-1].lower() filer_images = Image.objects.filter(file__endswith=src_part) if not filer_images: if src.startswith('/'): src_part = src[1:] print('trying import from local folder') image_file = os.path.join(self.directory, '..', src_part) if os.path.exists(image_file): with open(image_file, 'rb') as file_obj: filer_image = self.get_filer_image( file_obj=file_obj, name=alt ) print('import success') else: print('image not found at', image_file) else: print('Could not find image', src_part) else: filer_image = filer_images[0] if not entry.main_image: entry.main_image = filer_image img.getparent().remove(img) else: image_plugin = create_image_plugin( None, None, filer_image=filer_image, parent_plugin=plugin, caption=alt, description=src, counter=counter ) if image_plugin: # render the new html for the plugin new_img_html = plugin_to_tag(image_plugin) # Get single image element new_img = etree.HTML(new_img_html).xpath('.//cms-plugin')[0] img.getparent().replace(img, new_img) return found