Python django.utils.safestring 模块,SafeString() 实例源码

我们从Python开源项目中,提取了以下8个代码示例,用于说明如何使用django.utils.safestring.SafeString()

项目:universal_notifications    作者:ArabellaTech    | 项目源码 | 文件源码
def post(self, request):
        data = request.data

        # Clean emoji for now
        if isinstance(data, dict) and data.get('Body'):
            if isinstance(data, QueryDict):
                data = data.dict()
            data['Body'] = clean_text(data['Body'])
        PhoneReceivedRaw.objects.create(data=data)

        if data.get('Direction') == 'inbound':
            if data.get('CallStatus') == 'ringing':  # incoming voice call
                text = getattr(settings, 'UNIVERSAL_NOTIFICATIONS_TWILIO_CALL_RESPONSE_DEFAULT',
                               '<?xml version="1.0" encoding="UTF-8"?>' +
                               '<Response>' +
                               '<Say>Hello, thanks for calling. ' +
                               'To leave a message wait for the tone.</Say>' +
                               '<Record timeout="30" />'
                               '</Response>')
                return Response(SafeString(text), content_type='text/xml')
        return Response({}, status=status.HTTP_202_ACCEPTED)
项目:Deploy_XXNET_Server    作者:jzp820927    | 项目源码 | 文件源码
def _cursor(self):
    if not self._valid_connection():
      kwargs = {'conv': base.django_conversions, 'dsn': None}
      settings_dict = self.settings_dict
      settings_dict.update(settings_dict.get('OPTIONS', {}))
      for settings_key, kwarg, required in _SETTINGS_CONNECT_ARGS:
        value = settings_dict.get(settings_key)
        if value:
          kwargs[kwarg] = value
        elif required:
          raise exceptions.ImproperlyConfigured(
              "You must specify a '%s' for database '%s'" %
              (settings_key, self.alias))
      self.connection = Connect(**kwargs)
      encoders = {safestring.SafeUnicode: self.connection.encoders[unicode],
                  safestring.SafeString: self.connection.encoders[str]}
      self.connection.encoders.update(encoders)
      signals.connection_created.send(sender=self.__class__, connection=self)
    cursor = base.CursorWrapper(self.connection.cursor())
    return cursor
项目:omb-eregs    作者:18F    | 项目源码 | 文件源码
def rawlayout_pdf(request, pdf):
    doc = to_doc(as_path(pdf))

    script_params = {
        'pdfPath': reverse('raw_pdf', kwargs={'pdf': pdf}),
        'workerSrc': static('ombpdf/js/pdf.worker.bundle.js'),
    }

    html, ctx = rawlayout.to_html(doc)

    return render(request, 'ombpdf/rawlayout.html', {
        'doc': doc,
        'html': SafeString(html),
        'script_params': SafeString(json.dumps(script_params)),
        **ctx,
    })
项目:tts-bug-bounty-dashboard    作者:18F    | 项目源码 | 文件源码
def test_get_bookmarklet_url_works(rf):
    request = rf.get('/')
    request.META['HTTP_HOST'] = 'boop.gov'
    url = views.get_bookmarklet_url(request)
    assert url.startswith('javascript:')
    assert '"' not in url
    assert 'https://boop.gov' in url
    assert isinstance(url, SafeString)
项目:django-email-pal    作者:18F    | 项目源码 | 文件源码
def render_body_as_html(self, ctx: T) -> SafeString:
        html_ctx = self._cast_to_dict(ctx).copy()
        html_ctx['is_html_email'] = True
        html_ctx['is_plaintext_email'] = False
        body = render_to_string(self.template_name, html_ctx)

        # TODO: This is a workaround for
        # https://github.com/18F/calc/issues/1409, need to figure
        # out the exact reason behind it.
        body = body.encode('ascii', 'xmlcharrefreplace').decode('ascii')

        return SafeString(body)
项目:CoBL-public    作者:lingdb    | 项目源码 | 文件源码
def get_cognate_class_links(self):
        def format_link(cc_id, alias):
            if alias.startswith("(") and alias.endswith(")"):
                template = '(<a href="/cognate/%s/">%s</a>)'
                alias = alias[1:-1]
            else:
                template = '<a href="/cognate/%s/">%s</a>'
            return template % (cc_id, alias)
        parts = [format_link(cc_id, alias) for cc_id, alias in
                 two_by_two(self.denormalized_cognate_classes.split(","))]
        return SafeString(", ".join(parts))
项目:maas    作者:maas    | 项目源码 | 文件源码
def test_sslkey_display_is_marked_as_HTML_safe(self):
        key_string = get_data('data/test_x509_0.pem')
        user = factory.make_User()
        key = SSLKey(key=key_string, user=user)
        display = key.display_html()
        self.assertIsInstance(display, SafeString)
项目:maas    作者:maas    | 项目源码 | 文件源码
def test_sshkey_display_is_marked_as_HTML_safe(self):
        key_string = get_data('data/test_rsa0.pub')
        user = factory.make_User()
        key = SSHKey(key=key_string, user=user)
        display = key.display_html()
        self.assertIsInstance(display, SafeString)