我们从Python开源项目中,提取了以下6个代码示例,用于说明如何使用django.template.defaultfilters.urlize()。
def get(self, request): alerts = [] try: url = urljoin(settings.PROMGEN['alertmanager']['url'], '/api/v1/alerts') response = util.get(url) except requests.exceptions.ConnectionError: logger.error('Error connecting to %s', url) return JsonResponse({}) data = response.json().get('data', []) if data is None: # Return an empty alert-all if there are no active alerts from AM return JsonResponse({}) for alert in data: alert.setdefault('annotations', {}) # Humanize dates for frontend for key in ['startsAt', 'endsAt']: if key in alert: alert[key] = parser.parse(alert[key]) # Convert any links to <a> for frontend for k, v in alert['annotations'].items(): alert['annotations'][k] = defaultfilters.urlize(v) alerts.append(alert) return JsonResponse(alerts, safe=False)
def render(self, context): s = '' try: sponsor = self.sponsor_var.resolve(context) content_type = '%s_%s' % (self.content_type, context['request'].LANGUAGE_CODE) texts = sponsor.sponsor_benefits.filter(benefit__content_type=content_type) if texts.count() > 0: s = linebreaks(urlize(texts[0].text, autoescape=True)) if self.content_var: context[self.content_var] = s s = '' except: pass return s
def url(text): """[filter url]http://...[end]??a????????. >>> url('[filter url]https://torina.top[end]') '<a target="_blank" rel="nofollow" href="https://torina.top" \ rel="nofollow">https://torina.top</a>' """ text = text.replace('<br />', '\n') text = text.replace('[filter url]', '').replace('[end]', '') tag = urlize(text) tag = tag.replace('<a ', '<a target="_blank" rel="nofollow" ') return tag
def urlize(value): from django.template.defaultfilters import urlize # TODO: Allow usage of urlizetrunc return urlize(value)
def post(self, request): silences = collections.defaultdict(list) try: url = urljoin(settings.PROMGEN['alertmanager']['url'], '/api/v1/silences') response = util.get(url) except requests.exceptions.ConnectionError: logger.error('Error connecting to %s', url) return JsonResponse({}) data = response.json().get('data', []) if data is None: # Return an empty silence-all if there are no active silences from AM return JsonResponse({}) currentAt = datetime.datetime.now(datetime.timezone.utc) for silence in data: if 'comment' in silence: silence['comment'] = defaultfilters.urlize(silence['comment']) # Since there is no status field, compare endsAt with the current time if 'endsAt' in silence: silence['endsAt'] = parser.parse(silence['endsAt']) if silence['endsAt'] < currentAt: continue silences['silence-all'].append(silence) for matcher in silence.get('matchers'): if matcher.get('name') in ['service', 'project']: silences['silence-{}-{}'.format(matcher.get('name'), matcher.get('value'))].append(silence) context = {'#' + slugify(key): render_to_string('promgen/ajax_silence.html', {'silences': silences[key], 'key': key}, request).strip() for key in silences} context['#silence-load'] = render_to_string('promgen/ajax_silence_button.html', {'silences': silences['silence-all'], 'key': 'silence-all'}).strip() return JsonResponse(context)
def transform_comment_html(_obj, value): """Urlize content, but make sure other tags are stripped as we need to output this raw""" return urlize(strip_tags(value))