示例#1
0
def test_filename_returns_Markup():
    annotation = _annotation(
        uri=jinja2.Markup("file:///home/seanh/MyFile.pdf"))
    assert isinstance(annotation.filename, jinja2.Markup)
示例#2
0
def impala_paginator(pager):
    t = loader.get_template('amo/impala/paginator.html')
    return jinja2.Markup(t.render({'pager': pager}))
示例#3
0
def test_href_returns_Markup_if_uri_returns_Markup(uri):
    uri.return_value = jinja2.Markup("http://www.example.com/example.html")
    assert isinstance(_annotation().href, jinja2.Markup)
示例#4
0
def test_link_text_returns_Markup_if_title_returns_Markup(title):
    for title_ in (jinja2.Markup("Example Document"),
                   jinja2.Markup("http://www.example.com/example.html"),
                   jinja2.Markup("https://www.example.com/example.html")):
        title.return_value = title_
        assert isinstance(_annotation().link_text, jinja2.Markup)
示例#5
0
def test_hostname_or_filename_returns_Markup_if_filename_does(filename):
    filename.return_value = jinja2.Markup("MyFile.pdf")
    annotation = _annotation()

    assert isinstance(annotation.hostname_or_filename, jinja2.Markup)
示例#6
0
def test_hostname_or_filename_returns_Markup_when_uri_does(uri, filename):
    filename.return_value = ""
    uri.return_value = jinja2.Markup("http://www.example.com/example.html")
    annotation = _annotation()

    assert isinstance(annotation.hostname_or_filename, jinja2.Markup)
示例#7
0
def mobile_paginator(pager):
    return jinja2.Markup(
        render_to_string("includes/mobile/paginator.html", {"pager": pager}))
示例#8
0
def test_title_returns_Markup_when_filename_returns_Markup(
        filename, annotation):
    filename.return_value = jinja2.Markup("MyFile.pdf")
    annotation = _annotation(annotation)

    assert isinstance(annotation.title, jinja2.Markup)
示例#9
0
def yesno(boolean_value):
    return jinja2.Markup(_lazy(u"Yes") if boolean_value else _lazy(u"No"))
示例#10
0
def quick_paginator(pager):
    return jinja2.Markup(
        render_to_string("includes/quick_paginator.html", {"pager": pager}))
示例#11
0
def label_with_help(f):
    """Print the label tag for a form field, including the help_text
    value as a title attribute."""
    label = u'<label for="%s" title="%s">%s</label>'
    return jinja2.Markup(label % (f.auto_id, f.help_text, f.label))
示例#12
0
def datetimeformat(context, value, format="shortdatetime"):
    """
    Returns a formatted date/time using Babel's locale settings. Uses the
    timezone from settings.py, if the user has not been authenticated.
    """
    if not isinstance(value, datetime.datetime):
        # Expecting date value
        raise ValueError(
            "Unexpected value {value} passed to datetimeformat".format(
                value=value))

    request = context.get("request")

    default_tzinfo = convert_tzinfo = timezone(settings.TIME_ZONE)
    if value.tzinfo is None:
        value = default_tzinfo.localize(value)
        new_value = value.astimezone(default_tzinfo)
    else:
        new_value = value

    if hasattr(request, "session"):
        if "timezone" not in request.session:
            if hasattr(request, "user") and request.user.is_authenticated():
                try:
                    convert_tzinfo = (
                        Profile.objects.get(user=request.user).timezone
                        or default_tzinfo)
                except (Profile.DoesNotExist, AttributeError):
                    pass
            request.session["timezone"] = convert_tzinfo
        else:
            convert_tzinfo = request.session["timezone"]

    convert_value = new_value.astimezone(convert_tzinfo)
    locale = _babel_locale(_contextual_locale(context))

    # If within a day, 24 * 60 * 60 = 86400s
    if format == "shortdatetime":
        # Check if the date is today
        today = datetime.datetime.now(tz=convert_tzinfo).toordinal()
        if convert_value.toordinal() == today:
            formatted = _lazy(u"Today at %s") % format_time(
                convert_value,
                format="short",
                tzinfo=convert_tzinfo,
                locale=locale)
        else:
            formatted = format_datetime(convert_value,
                                        format="short",
                                        tzinfo=convert_tzinfo,
                                        locale=locale)
    elif format == "longdatetime":
        formatted = format_datetime(convert_value,
                                    format="long",
                                    tzinfo=convert_tzinfo,
                                    locale=locale)
    elif format == "date":
        formatted = format_date(convert_value, locale=locale)
    elif format == "time":
        formatted = format_time(convert_value,
                                tzinfo=convert_tzinfo,
                                locale=locale)
    elif format == "datetime":
        formatted = format_datetime(convert_value,
                                    tzinfo=convert_tzinfo,
                                    locale=locale)
    elif format == "year":
        formatted = format_datetime(convert_value,
                                    format="yyyy",
                                    tzinfo=convert_tzinfo,
                                    locale=locale)
    else:
        # Unknown format
        raise DateTimeFormatError

    return jinja2.Markup('<time datetime="%s">%s</time>' %
                         (convert_value.isoformat(), formatted))
示例#13
0
def wiki_to_html(wiki_markup,
                 locale=settings.WIKI_DEFAULT_LANGUAGE,
                 nofollow=True):
    """Wiki Markup -> HTML jinja2.Markup object"""
    return jinja2.Markup(
        parser.wiki_to_html(wiki_markup, locale=locale, nofollow=nofollow))
示例#14
0
def clean(string):
    return jinja2.Markup(clean_nl(bleach.clean(string)).strip())
示例#15
0
 def f(self, *args, **kw):
     """Calls SafeFormatter.format and returns a Markup string."""
     # SafeFormatter escapes everything so this is safe.
     return jinja2.Markup(self.formatter.format(*args, **kw))