def update_accordion_panel(panel): accordion_block = StreamBlock([('accordion_items_panel', AccordionItemsPanel(required=True, icon='collapse-down')) ]) rich_text_block = StreamBlock([('rich_text_panel', StandardRichTextPanel(required=False))]) accordion_item_panel = [] for item in panel.value['accordions']: items = [] for text in item.value['items']: items.append(('rich_text_panel', StructValue(StandardRichTextPanel, [ ('text', text.value['text']), ('meta_variant', text.value['meta_variant']), ]))) accordion_item_panel.append( ('accordion_items_panel', StructValue(AccordionItemsPanel, [ ('heading', item.value['heading']), ('items', StreamValue(rich_text_block, items)), ]))) panel.value['accordions'] = StreamValue(accordion_block, accordion_item_panel) return panel
def update_information_panel(panel): (rendition_mobile, rendition_desktop) = get_renditions(panel.value['image_meta']) panel.value['image'] = StructValue(BackgroundImageBlock, [ ('image', panel.value['image']), ('meta_mobile_rendition', rendition_mobile if panel.value['mobile_use_renditions'] else 'none'), ('meta_desktop_rendition', rendition_desktop if panel.value['desktop_use_renditions'] else 'none'), ]) panel.value['meta_layout'] = 'desktop-image-left-mobile-image-top' if panel.value['meta_variant'] not in [ 'light_background', 'dark_background' ]: variant = { 'mobile-image-top-text-right': 'desktop-image-left-mobile-image-top', 'mobile-image-right': 'desktop-image-right-mobile-image-right', } panel.value['meta_layout'] = variant[panel.value['meta_variant']] panel.value['meta_variant'] = 'light_background' cta_stream_block = StreamBlock([('simple_menu_item', SimpleCtaLinkBlock()) ]) ctas = [] for item in panel.value['cta']: ctas.append(('simple_menu_item', StructValue(SimpleCtaLinkBlock, [ ('link_text', item.value['link_text']), ('link_external', item.value['link_external']), ('link_page', item.value['link_page']), ('link_id', item.value['link_id']), ('meta_cta_variant', 'button'), ]))) panel.value['ctas'] = StreamValue(cta_stream_block, ctas) panel.value['panel_id'] = panel.value.pop('shelf_id') keys_to_remove = [ 'image_meta', 'mobile_use_renditions', 'desktop_use_renditions', 'cta' ] for key in keys_to_remove: del (panel.value[key]) panel.block = StandardInformationPanel() return panel
def update_action_plan_shelf(shelf): action_groups_block = StreamBlock([ ('action_group', ActionGroupPanel(required=False, icon='collapse-down')), ]) action_plan_block = StreamBlock([ ('action_panel', ActionChooserBlock(target_model='oneyou.Action', icon='list-ul')), ]) action_group_items = [] for action_group in shelf.value['action_groups']: items = [] for item in action_group.value['actions']: item.value = get_or_create_action(item.value) item.block = ActionChooserBlock(target_model='oneyou.Action') items.append(('action_panel', item.value)) action_group_items.append( ('action_group', StructValue(ActionGroupPanel, [ ('title', action_group.value['title']), ('actions', StreamValue(action_plan_block, items)), ]))) shelf.value['action_groups'] = StreamValue(action_groups_block, action_group_items) cta_stream_block = StreamBlock([('simple_menu_item', SimpleCtaLinkBlock()) ]) ctas = [] for item in shelf.value['cta']: ctas.append(('simple_menu_item', StructValue(SimpleCtaLinkBlock, [ ('link_text', item.value['link_text']), ('link_external', item.value['link_external']), ('link_page', item.value['link_page']), ('link_id', item.value['link_id']), ('meta_cta_variant', 'button'), ]))) shelf.value['ctas'] = StreamValue(cta_stream_block, ctas) del (shelf.value['cta']) shelf.block = ActionPlanShelf() return shelf
def update_page_heading_shelf(shelf): (rendition_mobile, rendition_desktop) = get_renditions(shelf.value['image_meta']) shelf.value['background_image'] = StructValue(BackgroundImageBlock, [ ('image', shelf.value['background_image']), ('meta_variant', 'gradient' if shelf.value['meta_gradient'] else 'none'), ('meta_mobile_rendition', rendition_mobile if shelf.value['mobile_use_renditions'] else 'none'), ('meta_desktop_rendition', rendition_desktop if shelf.value['desktop_use_renditions'] else 'none'), ('meta_image_display', 'cover'), ]) shelf.value['image_left'] = StructValue(PositionedImageBlock, [ ('image', shelf.value['image_left']['image']), ('meta_variant', 'none'), ('meta_mobile_rendition', rendition_mobile if shelf.value['image_left'].get('meta_use_mobile_renditions') else 'none'), ('meta_desktop_rendition', rendition_desktop if shelf.value['image_left'].get('meta_use_desktop_renditions') else 'none'), ('meta_position', shelf.value['image_left']['meta_layout']), ]) shelf.value['image_right'] = StructValue(PositionedImageBlock, [ ('image', shelf.value['image_right']['image']), ('meta_variant', 'none'), ('meta_mobile_rendition', rendition_mobile if shelf.value['image_right'].get('meta_use_mobile_renditions') else 'none'), ('meta_desktop_rendition', rendition_desktop if shelf.value['image_right'].get('meta_use_desktop_renditions') else 'none'), ('meta_position', shelf.value['image_right']['meta_layout']), ]) keys_to_remove = [ 'image_meta', 'mobile_use_renditions', 'desktop_use_renditions', 'meta_gradient' ] for key in keys_to_remove: del (shelf.value[key]) shelf.block = PageHeadingShelf() return shelf
def update_icon_card_panel(panel): (rendition_mobile, rendition_desktop) = get_renditions(panel.value['image_meta']) panel.value['image'] = StructValue(BackgroundImageBlock, [ ('image', panel.value['image']), ('meta_mobile_rendition', rendition_mobile if panel.value['mobile_use_renditions'] else 'none'), ('meta_desktop_rendition', rendition_desktop if panel.value['desktop_use_renditions'] else 'none'), ]) for item in panel.value['cta']: item.block = SimpleCtaLinkBlock() item.block.name = 'simple_cta_link' panel.value['ctas'] = panel.value.pop('cta') keys_to_remove = [ 'image_meta', 'mobile_use_renditions', 'desktop_use_renditions' ] for key in keys_to_remove: del (panel.value[key]) panel.block = IconCardPanel() return panel
def update_app_teaser_panel(panel): app_teaser = get_or_create_app_teaser(panel.value) panel.value = StructValue(BannerShelf, [ ('panel', app_teaser), ]) panel.block = AppTeaserPanel() return panel
def handle(user, pk, out): story = models.Story.objects.all().get(pk=pk) changed = False new_body = [] for item in story.body: block_type = item.block.name if block_type == 'embedded_image': block_type = 'image' new_value = StructValue(StoryStreamBlock.base_blocks['image']) new_value['image'] = item.value['image'] new_value['caption'] = item.value['caption'] new_value['alignment'] = 'medium' changed = True else: new_value = item.value new_body.append((block_type, new_value)) if changed: print(f"Saving new revision for {story.url}", file=out) story.body = new_body story.save_revision(user=user) print(f"Go to https://theblacksea.eu/admin/pages/{pk}/revisions/ " "to view the revision.") else: print(f"Nothing to change for {story.url}", file=out)
def get_or_create_promo(promo_values): try: return Banner.objects.get(shelf_id=promo_values.shelf_id) except Banner.DoesNotExist: contenttype = ContentType.objects.get(app_label='dctcmsbase', model='banner') cta_stream_block = StreamBlock([('cta', SimpleCtaLinkBlock())]) ctas = StreamValue( cta_stream_block, [('cta', StructValue(SimpleCtaLinkBlock, [ ('link_text', promo_values.cta_text), ('link_external', promo_values.cta_link), ('link_page', promo_values.cta_page), ('link_id', ''), ('meta_cta_variant', 'button'), ]))], ) promo = Banner.objects.create( shelf_id=promo_values.shelf_id, heading=promo_values.heading, tracking_group=promo_values.tracking_group, content_type=contenttype, ctas=ctas, ) return promo
def update_video_teaser_panel(panel): (rendition_mobile, rendition_desktop) = get_renditions(panel.value['image_meta']) panel.value['image'] = StructValue(BackgroundImageBlock, [ ('image', panel.value['image']), ('meta_mobile_rendition', rendition_mobile if panel.value['mobile_use_renditions'] else 'none'), ('meta_desktop_rendition', rendition_desktop if panel.value['desktop_use_renditions'] else 'none'), ]) for item in panel.value['cta']: item.block = SimpleCtaLinkBlock() item.block.name = 'simple_cta_link' panel.value['ctas'] = panel.value.pop('cta') panel.value['meta_layout'] = '{}-{}'.format( panel.value['meta_layout_desktop'], panel.value['meta_layout_mobile']) panel.value['panel_id'] = panel.value.pop('shelf_id') keys_to_remove = [ 'image_meta', 'mobile_use_renditions', 'desktop_use_renditions', 'meta_layout_mobile', 'meta_layout_desktop' ] for key in keys_to_remove: del (panel.value[key]) panel.block = StandardVideoTeaserPanel() return panel
def test_rendition_image_field_original(self): """ test_rendition_image_field_original """ image = CustomImageFactory() test_page = TestPage(title='test', slug='test') parent_page = Page.objects.get(id=2) parent_page.add_child(instance=test_page) field = TestPage._meta.get_field('body') test_page.body = StreamValue(field.stream_block, [('image', StructValue(ImageWithRenditionsBlock, [ ('image', image), ('meta_mobile_rendition', 'none'), ('meta_desktop_rendition', 'none'), ]))]) test_page.save() image_with_renditions = HeaderImageTestPage( image=image, image_mobile_rendition='none', image_desktop_rendition='none', test_page=test_page, ) image_with_renditions.save() c = Client() response = c.get('/api/v2/pages/{}/'.format(test_page.id)) self.assertEqual(response.status_code, 200) content = json.loads(response.content) self.assertEqual(len(content['header_image']), 1) mobile_header_image = Image.open('./{}'.format( content['header_image'][0]['mobile_image'])) width, height = mobile_header_image.size self.assertEqual(width, 1200) self.assertEqual(height, 1200) desktop_header_image = Image.open('./{}'.format( content['header_image'][0]['desktop_image'])) width, height = desktop_header_image.size self.assertEqual(width, 1200) self.assertEqual(height, 1200) mobile_body_image = Image.open('./{}'.format( content['body'][0]['value']['renditions']['mobile'])) width, height = mobile_body_image.size self.assertEqual(width, 1200) self.assertEqual(height, 1200) desktop_body_image = Image.open('./{}'.format( content['body'][0]['value']['renditions']['desktop'])) width, height = desktop_body_image.size self.assertEqual(width, 1200) self.assertEqual(height, 1200) image.delete()
def get_or_create_banner(banner_values): try: return Banner.objects.get(shelf_id=banner_values.shelf_id) except Banner.DoesNotExist: contenttype = ContentType.objects.get(app_label='dctcmsbase', model='banner') expanded_stream_block = ExpandedStreamBlock([('background_image', BackgroundImageBlock())]) cta_stream_block = StreamBlock([('cta', SimpleCtaLinkBlock())]) attributes = StreamValue( expanded_stream_block, [('background_image', StructValue(BackgroundImageBlock, [ ('image', banner_values.background_image), ('meta_variant', 'gradient' if banner_values.meta_gradient else 'none'), ('meta_mobile_rendition', 'none'), ('meta_desktop_rendition', 'none'), ('meta_image_display', 'cover'), ]))], ) ctas = StreamValue( cta_stream_block, [('cta', StructValue(SimpleCtaLinkBlock, [ ('link_text', banner_values.cta_text), ('link_external', banner_values.cta_link), ('link_page', banner_values.cta_page), ('link_id', ''), ('meta_cta_variant', 'button'), ]))], ) banner = Banner.objects.create( shelf_id=banner_values.shelf_id, heading=banner_values.heading, body=banner_values.body, tracking_group=banner_values.tracking_group, content_type=contenttype, attributes=attributes, ctas=ctas, ) return banner
def test_get_serializable_data_for_fields_correctly_serialises_the_page(self): page = create_test_page(OneYouPage) field = OneYouPage._meta.get_field('body') page.body = StreamValue(field.stream_block, [('section_heading_shelf', StructValue([('heading', 'This is a section heading'), ('shelf_id', 'shelf1')]))]) serialized_data = get_serializable_data_for_fields(page) self.assertEqual(type(serialized_data), dict)
def update_banner_shelf(shelf): banner = get_or_create_banner(shelf.value) shelf.value = StructValue(BannerShelf, [ ('panel', banner), ('meta_layout', 'vertical_left'), ]) shelf.block = BannerShelf() return shelf
def update_grid_shelf(shelf): shelf.value['background_image'] = StructValue(BackgroundImageBlock, [ ('image', shelf.value['background_image']['image']), ('meta_variant', 'none'), ('meta_mobile_rendition', 'none'), ('meta_desktop_rendition', 'none'), ('meta_image_display', shelf.value['meta_image_display']), ]) item_block = StreamBlock([ ('rich_text_panel', StandardRichTextPanel(icon='title')), ('information_panel', StandardInformationPanel(target_model='shelves.AppTeaser', icon='image')), ('cta_panel', CtaPanel(icon='plus')), ('icon_card_panel', IconCardPanel(icon='snippet')), ('image_teaser_panel', StandardImageTeaserPanel(icon='pick')), ('video_teaser_panel', StandardVideoTeaserPanel(icon='pick')), ('plain_text_panel', PlainTextPanel(required=False)), ('simple_image_panel', StandardSimpleImagePanel(icon='image')), ('accordion_panel', AccordionPanel(required=False, icon='form')), ('inline_script_panel', InlineScriptPanel(icon='code')), ('inline_svg_panel', InlineSvgPanel(icon='snippet')), ('app_teaser_panel', AppTeaserPanel(target_model='shelves.AppTeaser', icon='image')), ('oneyou_teaser_panel', BackwardsCompatibleContentPanel(label='OneYou1 teaser', icon='folder-inverse')), ('list_item_panel', ListItemPanel(icon='list-ul')), ('action_plan_shelf', ActionPlanShelf(label='Action Plan Builder shelf', icon='form')), ('action_plan_display_shelf', ActionPlanDisplayShelf(label='Action Plan Display shelf', icon='form')), ]) items = [] for item in shelf.value['items']: if item.block_type in panels_name_conversion.keys(): block_type = panels_name_conversion[item.block_type] else: block_type = item.block_type if item.block_type.endswith( '_panel') else '{}_panel'.format(item.block_type) item = globals()['update_{}'.format(block_type)](item) item.block.name = block_type items.append((block_type, item.value)) shelf.value['items'] = StreamValue(item_block, items) keys_to_remove = ['meta_image_display'] for key in keys_to_remove: del (shelf.value[key]) shelf.block = OneYouGridShelf() return shelf
def get_or_create_app_teaser(app_teaser_values): try: return AppTeaser.objects.get(shelf_id=app_teaser_values.shelf_id) except AppTeaser.DoesNotExist: contenttype = ContentType.objects.get(app_label='dctcmsbase', model='appteaser') expanded_stream_block = ExpandedStreamBlock([('image', ImageBlock())]) cta_stream_block = StreamBlock([('cta', SimpleCtaLinkBlock())]) attributes = StreamValue( expanded_stream_block, [('image', StructValue(ImageBlock, [ ('image', app_teaser_values.image), ]))], ) ctas = StreamValue( cta_stream_block, [('cta', StructValue(SimpleCtaLinkBlock, [ ('link_text', app_teaser_values.cta_text), ('link_external', app_teaser_values.cta_link), ('link_page', app_teaser_values.cta_page), ('link_id', ''), ('meta_cta_variant', 'button'), ]))], ) app_teaser = AppTeaser.objects.create( shelf_id=app_teaser_values.shelf_id, heading=app_teaser_values.heading, body=app_teaser_values.body, attributes=attributes, tracking_group=app_teaser_values.tracking_group, content_type=contenttype, ctas=ctas, cta_googleplay=app_teaser_values.cta_googleplay, cta_appstore=app_teaser_values.cta_appstore, ) return app_teaser
def update_oneyou_teaser_panel(panel): (rendition_mobile, rendition_desktop) = get_renditions(panel.value['image_meta']) panel.value['image'] = StructValue(BackgroundImageBlock, [ ('image', panel.value['image']), ('meta_mobile_rendition', rendition_mobile if panel.value['mobile_use_renditions'] else 'none'), ('meta_desktop_rendition', rendition_desktop if panel.value['desktop_use_renditions'] else 'none'), ]) cta_stream_block = StreamBlock([('simple_menu_item', SimpleCtaLinkBlock()) ]) ctas = [] for item in panel.value['cta']: ctas.append(('simple_menu_item', StructValue(SimpleCtaLinkBlock, [ ('link_text', item.value['link_text']), ('link_external', item.value['link_external']), ('link_page', item.value['link_page']), ('link_id', item.value['link_id']), ('meta_cta_variant', 'link'), ]))) panel.value['ctas'] = StreamValue(cta_stream_block, ctas) panel.value['panel_id'] = panel.value.pop('shelf_id') keys_to_remove = [ 'image_meta', 'mobile_use_renditions', 'desktop_use_renditions', 'cta' ] for key in keys_to_remove: del (panel.value[key]) panel.block = BackwardsCompatibleContentPanel() return panel
def update_promo_shelf(shelf): value = shelf.value if shelf.value.__class__.__name__ == 'StructValue': value = shelf.value['promo'] del (shelf.value['shelf_id']) promo = get_or_create_promo(value) shelf.value = StructValue(BannerShelf, [ ('panel', promo), ('meta_layout', 'horizontal_left'), ]) shelf.block = BannerShelf() return shelf
def update_accordion_group_shelf(shelf): item_block = StreamBlock([ ('accordion_panel', AccordionPanel(required=False, icon='form')), ]) panel = update_accordion_panel(shelf) # shelf.value['items'] = StreamValue(item_block, [('accordion_panel', panel.value)]) shelf.value['items'] = StreamValue( item_block, [('accordion_panel', StructValue(AccordionPanel, [ ('accordions', panel.value['accordions']), ('scroll_items_to_top_when_selected', True), ]))]) keys_to_remove = ['tracking_group', 'accordions'] for key in keys_to_remove: del (shelf.value[key]) shelf.block = OneYouGridShelf() return shelf
def update_simple_image_panel(panel): (rendition_mobile, rendition_desktop) = get_renditions(panel.value['image_meta']) panel.value['image'] = StructValue(BackgroundImageBlock, [ ('image', panel.value['image']), ('meta_mobile_rendition', rendition_mobile if panel.value['mobile_use_renditions'] else 'none'), ('meta_desktop_rendition', rendition_desktop if panel.value['desktop_use_renditions'] else 'none'), ]) keys_to_remove = [ 'image_meta', 'mobile_use_renditions', 'desktop_use_renditions' ] for key in keys_to_remove: del (panel.value[key]) panel.block = StandardSimpleImagePanel() return panel
def update_image_teaser_panel(panel): (rendition_mobile, rendition_desktop) = get_renditions(panel.value['image_meta']) panel.value['image'] = StructValue(BackgroundImageBlock, [ ('image', panel.value['image']), ('meta_mobile_rendition', rendition_mobile if panel.value['mobile_use_renditions'] else 'none'), ('meta_desktop_rendition', rendition_desktop if panel.value['desktop_use_renditions'] else 'none'), ]) for item in panel.value['cta']: if 'link_external' in item.value: item.block = SimpleCtaLinkBlock() item.block.name = 'simple_cta_link' else: item.block = DocumentDownloadBlock() item.block.name = 'document_download' panel.value['ctas'] = panel.value.pop('cta') panel.value['meta_layout'] = '{}-{}'.format( panel.value['meta_layout_desktop'], panel.value['meta_layout_mobile']) panel.value['panel_id'] = panel.value.pop('shelf_id') keys_to_remove = [ 'image_meta', 'mobile_use_renditions', 'desktop_use_renditions', 'meta_layout_mobile', 'meta_layout_desktop', 'audio', 'meta_cta_variant' ] for key in keys_to_remove: del (panel.value[key]) panel.block = StandardImageTeaserPanel() return panel
def test_rendition_image_field_custom(self): """ test_rendition_image_field_custom """ image = CustomImageFactory() test_page = TestPage(title='test', slug='test') parent_page = Page.objects.get(id=2) parent_page.add_child(instance=test_page) field = TestPage._meta.get_field('body') test_page.body = StreamValue( field.stream_block, [('image', StructValue(ImageWithRenditionsBlock, [ ('image', image), ('meta_mobile_rendition', '100x50'), ('meta_desktop_rendition', '400x200'), ]))]) test_page.save() image_with_renditions = HeaderImageTestPage( image=image, image_mobile_rendition='100x200', image_desktop_rendition='400x800', test_page=test_page, ) image_with_renditions.save() c = Client() response = c.get('/api/v2/pages/{}/'.format(test_page.id)) self.assertEqual(response.status_code, 200) content = json.loads(response.content) self.assertEqual(len(content['header_image']), 1) mobile_header_image = Image.open('./{}'.format( content['header_image'][0]['mobile_image'])) width, height = mobile_header_image.size self.assertEqual(width, 100) self.assertEqual(height, 200) desktop_header_image = Image.open('./{}'.format( content['header_image'][0]['desktop_image'])) width, height = desktop_header_image.size self.assertEqual(width, 400) self.assertEqual(height, 800) mobile_body_image = Image.open('./{}'.format( content['body'][0]['value']['renditions']['mobile'])) width, height = mobile_body_image.size self.assertEqual(width, 100) self.assertEqual(height, 50) desktop_body_image = Image.open('./{}'.format( content['body'][0]['value']['renditions']['desktop'])) width, height = desktop_body_image.size self.assertEqual(width, 400) self.assertEqual(height, 200) with self.settings(LOCAL_ASSET_FULL_PATH=True): regexp = re.compile( '^http\:\/\/localhost\:8000\/media\/images\/example\.[A-z0-9]+\.fill-[0-9]+x[0-9]+\.jpg$' ) self.assertIsNotNone( regexp.match(image_with_renditions.mobile_image)) self.assertIsNotNone( regexp.match(image_with_renditions.desktop_image)) c = Client() response = c.get('/api/v2/pages/{}/'.format(test_page.id)) self.assertEqual(response.status_code, 200) content = json.loads(response.content) self.assertIsNotNone( regexp.match( content['body'][0]['value']['renditions']['mobile'])) self.assertIsNotNone( regexp.match( content['body'][0]['value']['renditions']['desktop'])) image.delete()