def handle_blog_model(sender, instance, **kwargs):
from wagtailmetadata.models import MetadataMixin
sender = mixin(sender, [MetadataMixin])
def get_meta_description(cls):
return cls.search_description or truncatewords(cls.description, 20)
sender.add_to_class('get_meta_description', get_meta_description)
def get_meta_image(cls):
if cls.header_image is not None:
return cls.build_absolute_uri(
cls.header_image.get_rendition('fill-800x450').url)
return super(sender, cls).get_meta_image()
sender.add_to_class('get_meta_image', get_meta_image)
sender.object_type = "blog"
sender._metadata = {
'gplus_type': "Blog",
}
python类truncatewords()的实例源码
def test_filters(self):
user = self.get_staff()
page = create_page('Test', 'col_two.html', 'en', published=True)
ex1 = Example1(char_1="char_1, <p>hello</p>, <p>hello</p>, <p>hello</p>, <p>hello</p>", char_2="char_2",
char_3="char_3",
char_4="char_4")
ex1.save()
template_text = '''{% extends "base.html" %}
{% load cms_tags %}
{% block content %}
<h1>{% render_model instance "char_1" "" "" 'truncatewords:2' %}</h1>
{% endblock content %}
'''
request = self.get_page_request(page, user, edit=True)
response = detail_view(request, ex1.pk, template_string=template_text)
self.assertContains(
response,
'<h1><div class="cms-plugin cms-plugin-%s-%s-%s-%s cms-render-model">%s</div></h1>' % (
'placeholderapp', 'example1', 'char_1', ex1.pk, truncatewords(ex1.char_1, 2)))
def test_filters_notoolbar(self):
user = self.get_staff()
page = create_page('Test', 'col_two.html', 'en', published=True)
ex1 = Example1(char_1="char_1, <p>hello</p>, <p>hello</p>, <p>hello</p>, <p>hello</p>", char_2="char_2",
char_3="char_3",
char_4="char_4")
ex1.save()
template_text = '''{% extends "base.html" %}
{% load cms_tags %}
{% block content %}
<h1>{% render_model instance "char_1" "" "" 'truncatewords:2' %}</h1>
{% endblock content %}
'''
request = self.get_page_request(page, user, edit=False)
response = detail_view(request, ex1.pk, template_string=template_text)
self.assertContains(response,
'<h1>%s</h1>' % truncatewords(ex1.char_1, 2))
def get_meta_description(self):
return self.search_description or truncatewords(strip_tags(self.body), 20)
def style_repo_description(var):
truncated_desc = truncatewords(var, 20)
return truncated_desc
def short_text(self):
return truncatewords(strip_tags(self.text), 10)
def item_description(self, item):
return truncatewords(item.body,80)
def item_description(self, item):
return truncatewords(item.body, 30)
def __str__(self):
return truncatewords(self.text, 3)
def item_description(self, item):
return truncatewords(item.body, 30)
def item_description(self, item):
return truncatewords(item.body, 30)
def item_description(self, item):
return truncatewords(item.body, 30)
def summary(self):
task_summary = ''
try:
task_summary = self.title or truncatewords(strip_tags(self.description or '').strip(), 10)
except:
pass
if not task_summary:
task_summary = '{} #{}'.format(self.is_task and 'Task' or 'Project', self.id)
return task_summary
def excerpt(self):
try:
return truncatewords(strip_tags(self.description).strip(), 20)
except:
return None
def notify_new_task_application_slack(instance, admin=True):
instance = clean_instance(instance, Application)
if not slack_utils.is_task_notification_enabled(instance.task, slugs.EVENT_APPLICATION):
return
application_url = '%s/work/%s/applications/' % (TUNGA_URL, instance.task_id)
slack_msg = "New application from %s" % instance.user.short_name
attachments = [
{
slack_utils.KEY_TITLE: instance.task.summary,
slack_utils.KEY_TITLE_LINK: application_url,
slack_utils.KEY_TEXT: '%s%s%s%s\n\n<%s|View on Tunga>' %
(truncatewords(convert_to_text(instance.pitch), 100),
instance.hours_needed and '\n*Workload:* {} hrs'.format(instance.hours_needed) or '',
instance.deliver_at and '\n*Delivery Date:* {}'.format(
instance.deliver_at.strftime("%d %b, %Y")
) or '',
instance.remarks and '\n*Remarks:* {}'.format(
truncatewords(convert_to_text(instance.remarks), 100)
) or '',
application_url),
slack_utils.KEY_MRKDWN_IN: [slack_utils.KEY_TEXT],
slack_utils.KEY_COLOR: SLACK_ATTACHMENT_COLOR_TUNGA
}
]
if admin:
slack_utils.send_incoming_webhook(
SLACK_STAFF_INCOMING_WEBHOOK,
{
slack_utils.KEY_TEXT: slack_msg,
slack_utils.KEY_ATTACHMENTS: attachments,
slack_utils.KEY_CHANNEL: SLACK_STAFF_LEADS_CHANNEL
}
)
else:
slack_utils.send_integration_message(instance.task, message=slack_msg, attachments=attachments)
def notify_task_application_response_slack(instance, admin=True):
instance = clean_instance(instance, Application)
application_url = '%s/work/%s/applications/' % (TUNGA_URL, instance.task_id)
task_url = '%s/work/%s/' % (TUNGA_URL, instance.task.id)
slack_msg = "Task Application {} | <{}|View on Tunga>".format(
instance.status == STATUS_ACCEPTED and 'accepted' or 'rejected',
task_url
)
attachments = [
{
slack_utils.KEY_TITLE: instance.task.summary,
slack_utils.KEY_TITLE_LINK: application_url,
slack_utils.KEY_TEXT: '%s%s%s%s\n\n<%s|View on Tunga>' %
(truncatewords(convert_to_text(instance.pitch), 100),
instance.hours_needed and '\n*Workload:* {} hrs'.format(instance.hours_needed) or '',
instance.deliver_at and '\n*Delivery Date:* {}'.format(
instance.deliver_at.strftime("%d %b, %Y")
) or '',
instance.remarks and '\n*Remarks:* {}'.format(
truncatewords(convert_to_text(instance.remarks), 100)
) or '',
application_url),
slack_utils.KEY_MRKDWN_IN: [slack_utils.KEY_TEXT],
slack_utils.KEY_COLOR: SLACK_ATTACHMENT_COLOR_TUNGA
}
]
if admin:
slack_utils.send_incoming_webhook(
SLACK_STAFF_INCOMING_WEBHOOK,
{
slack_utils.KEY_TEXT: slack_msg,
slack_utils.KEY_ATTACHMENTS: attachments,
slack_utils.KEY_CHANNEL: SLACK_STAFF_LEADS_CHANNEL
}
)
else:
slack_utils.send_integration_message(instance.task, message=slack_msg, attachments=attachments)
def item_description(self, item):
return truncatewords(item.body, 30)
def get_excerpt(self):
return truncatewords(
embed_videos(
html.unescape(striptags(self.content)),
strip=True
),
55
)