def render_message(html=False, insert_head=False): if html and insert_head: subject = render_subject() body = render_message(html=True, insert_head=False) return render_to_string( 'emails/habillage.html', {'subject': subject, 'body': body}, ) elif html and not embed: # relative urls have to be absoluted html = re.sub( r'href=(.)/', r'href=\1{{ request.base_url }}/', richtext(template.html), ) return Template(unescape_template_symbols(richtext(html))).render( Context(context) ) elif html and embed: # embedded preview return Template( unescape_template_symbols(richtext(template.html)) ).render(Context(context)) else: return Template(autoescape(template.texte)).render( Context(context) )
def get_takwimu_services(service_settings): items = [] for service in service_settings.services: if service.value['category'].lower() == 'persona': item = { 'type': 'service', 'value': { 'title': service.value['title'], 'description': str(wagtailcore_tags.richtext( service.value['description'])), } } items.append(item) return { 'type': 'services', 'value': { 'label': service_settings.label, 'title': service_settings.title, 'description': wagtailcore_tags.richtext(service_settings.description), 'services': items, } }
def richtext_amp_formatting(value): if isinstance(value, RichText): value = richtext(value.source) else: value = richtext(value) return amp_formatting(value)
def richtext_amp(value): if isinstance(value, RichText): value = richtext(value.source) else: value = richtext(value) value = utils.convert_to_amp(value) return mark_safe(value)
def test_richtext_missing_id(): """ Expect the richtext template tag to handle bad image ids gracefully """ assert not Image.objects.filter(pk=99999).exists() database_html = '<embed embedtype="news-image" id="99999"/>' assert richtext(database_html) == richtext_div('<img>')
def convert_to_amp(value): """ Converts HTML to AMP. """ if isinstance(value, RichText): value = richtext(value.source) return mark_safe(utils.convert_to_amp(value))
def test_richtext_creates_rendition(): """ The richtext tag should create a custom-width rendition using the 'width' attribute """ image = create_valid_image('Dots') # A rendition with a max width of 123px should be created database_html = '<embed embedtype="news-image" id="{id}" width="123" />'.format( id=image.pk) richtext(database_html) assert Rendition.objects.filter(image=image, filter_spec='width-123').exists() # A rendition with a max width of default 800px should be created database_html = '<embed embedtype="news-image" id="{id}"/>'.format( id=image.pk) richtext(database_html) assert Rendition.objects.filter(image=image, filter_spec='width-800').exists()
def richtext_inline(value): "Returns HTML-formatted rich text stripped of block level elements" text = richtext(value) return mark_safe( bleach.clean(text, strip=True, tags=[ 'a', 'abbr', 'acronym', 'b', 'code', 'em', 'i', 'strong', 'span' ]))
def get_takwimu_faqs(faq_settings): items = [] for faq in faq_settings.faqs: item = { 'type': 'faq', 'value': { 'question': faq.value['question'], 'answer': str(wagtailcore_tags.richtext(faq.value['answer'])), } } items.append(item) return { 'type': 'faqs', 'value': { 'label': faq_settings.label, 'title': faq_settings.title, 'description': wagtailcore_tags.richtext(faq_settings.description), 'faqs': items } }
def test_richtext_valid_id(): """ Expect the richtext template tag to render news-image embeds """ image = create_valid_image('Dots') database_html = '<embed embedtype="news-image" id="{id}" width="999"/>'.format( id=image.pk) actual_frontend_html = richtext(database_html) rendition = Rendition.objects.filter(image=image).last() expected_frontend_html = ( '<img alt="" class="richtext-image" height="{h}" src="{src}" width="{w}">' ).format( src=rendition.url, w=rendition.width, h=rendition.height, ) assert actual_frontend_html == richtext_div(expected_frontend_html)
def to_representation(self, instance): blog_data = super().to_representation(instance) body_data = json.loads(blog_data['body']) for i, block in enumerate(body_data): if block['type'] == 'paragraph': block['value'] = wagtailcore_tags.richtext(block['value']) if block['type'] == 'image': value = instance.body[i].value block['value'] = { 'id': value.id, 'title': value.title, 'large': value.get_rendition('width-1000').attrs_dict, 'thumbnail': value.get_rendition('fill-120x120').attrs_dict, } blog_data['body'] = json.dumps(body_data) blog_data['tags'] = instance.tags.slugs() return blog_data
def tip_entity_decorator(props): """ Draft.js ContentState to database HTML. Converts the tip entities into a span tag. """ tip = props['tip'] # HACK: Convert to JSON string if necessary. # Without this it's inconsistent for some reason. if type(tip) is dict: tip = json.dumps(tip) tip_html = ContentstateConverter(features=[ 'h2', 'h3', 'bold', 'italic', 'ol', 'ul', 'link', 'image', 'embed' ]).to_database_format(tip) tip_html = richtext(tip_html) # apply |richtext filter return DOM.create_element('span', { 'data-tip': tip_html, }, props['children'])
def send_emails(self): if isinstance(self.instance, Campaign): original_lang = translation.get_language() model = self.instance template = model.template email_list = [] target_groups = model.recipients.all() for grp in target_groups: email_list += [(x.email, x.language_preference) for x in grp.users.all()] unique_list = list(set(email_list)) self.mails_tobe_sent = len(unique_list) self.mails_sent = 0 self.refused_list = [] for recipient, lang_pref in unique_list: if lang_pref: translation.activate(lang_pref) else: translation.activate(settings.LANGUAGE_CODE) num = send_mail(template.subject, self.textAppendUnsubscribeLink( recipient, template.plain_text), model.from_address, [recipient], html_message=self.htmlAppendUnsubscribeLink( recipient, richtext(template.html_body))) if num == 0: self.refused_list.append(recipient) self.mails_sent += num # reactivate original lang if original_lang: translation.activate(original_lang) else: translation.activate(settings.LANGUAGE_CODE) if self.mails_sent > 0: model.sent = True model.sent_at = datetime.now(timezone.utc) model.save()
def test_richtext_extra_data(): """ Expect the richtext template tag to render news-image embeds with extra data: - image title - alt text - image hyperlink """ image = create_valid_image('Dots') database_html = '<embed embedtype="news-image" id="{}" title="{}" href="{}" alt="{}" width="{}"/>'.format( image.pk, 'Title!', 'https://example.com', 'Alt Text!', 456) actual_frontend_html = richtext(database_html) rendition = Rendition.objects.filter(image=image).last() expected_frontend_html = ( '<a href="https://example.com">' '<img alt="Alt Text!" class="richtext-image" height="{h}" src="{src}" width="{w}">' '</a>' '<figcaption>Title!</figcaption>').format( src=rendition.url, w=rendition.width, h=rendition.height, ) assert actual_frontend_html == richtext_div(expected_frontend_html)
def test_call_with_invalid_value(self): with self.assertRaisesRegex( TypeError, "'richtext' template filter received an invalid value"): richtext(42)
def test_call_with_none(self): result = richtext(None) self.assertEqual(result, "")
def test_call_with_text(self): result = richtext("Hello world!") self.assertEqual(result, "Hello world!") self.assertIsInstance(result, SafeString)
def test_call_with_bytes(self): with self.assertRaisesRegex(TypeError, "'richtext' template filter received an invalid value"): richtext(b"Hello world!")
def test_call_with_invalid_value(self): with self.assertRaisesRegex(TypeError, "'richtext' template filter received an invalid value"): richtext(42)
def text(self): return wagtailcore_tags.richtext(self.text_field)
def text(self): return richtext(self.raw_content)
def rendered_body(self): return wagtailcore_tags.richtext(self.body)
def test_call_with_none(self): result = richtext(None) self.assertEqual(result, '<div class="rich-text"></div>')
def test_call_with_text(self): result = richtext("Hello world!") self.assertEqual(result, '<div class="rich-text">Hello world!</div>') self.assertIsInstance(result, SafeString)
def test_contrib_legacy_richtext_renders_wrapper(self): with self.modify_settings( INSTALLED_APPS={"prepend": "wagtail.contrib.legacy.richtext"}): self.assertEqual(richtext("Foo"), """<div class="rich-text">Foo</div>""")
def test_no_contrib_legacy_richtext_no_wrapper(self): self.assertEqual(richtext("Foo"), "Foo")
def description(self): return richtext(self.raw_description)
def test_call_with_bytes(self): with self.assertRaisesRegex( TypeError, "'richtext' template filter received an invalid value"): richtext(b"Hello world!")
def get_api_representation(self, value, context=None): return richtext(value.source)
def text(self): return richtext(self.content)
def rendered_description(self): return wagtailcore_tags.richtext(self.description)
def test_call_with_none(self): result = richtext(None) self.assertEqual(result, '<div class="rich-text"></div>')
def test_call_with_text(self): result = richtext("Hello world!") self.assertEqual(result, '<div class="rich-text">Hello world!</div>') self.assertIsInstance(result, SafeText)
def subtitle(self): return richtext(self.raw_subtitle)