websauna.system.core.templatecontext module

Websauna template filters .

websauna.system.core.templatecontext.admin_url(jinja_ctx, model_instance, *elements, **kw)[source]

Link to model in admin interface.

Takes an SQLAlchemy model instance as a filter argument and resolves its admin page. This requires that a model admin has been correctly registered for SQLAlchemy model.

Example:

{% if request.user and request.user.is_admin %}
    <li>
      <a class="btn btn-danger" href="{{ choice|admin_url("edit") }}">
        Edit in admin
      </a>
    </li>
{% endif %}

Another example:

<li>
  <a class="btn btn-danger" href="{{ choice|admin_url }}">
    View in admin
  </a>
</li>
websauna.system.core.templatecontext.arrow_format(jinja_ctx, context, *args, **kw)[source]

Format datetime using Arrow formatter string.

Context must be a time/datetime object.

Arrow is a Python helper library for parsing and formatting datetimes.

Example:

<li>
  Offer created at {{ offer.created_at|arrow_format('YYYYMMDDHHMMss') }}
</li>

See Arrow formatting.

websauna.system.core.templatecontext.escape_js(jinja_ctx, context, **kw)[source]

Make JSON strings to safe to be embedded inside <script> tag.

websauna.system.core.templatecontext.filter_datetime(jinja_ctx, context, **kw)[source]

Format datetime in a certain timezone.

websauna.system.core.templatecontext.friendly_time(jinja_ctx, context, **kw)[source]

Format timestamp in human readable format.

  • Context must be a datetimeobject

  • Takes optional keyword argument timezone which is a timezone name as a string. Assume the source datetime is in this timezone.

websauna.system.core.templatecontext.from_timestamp(jinja_ctx, context, **kw)[source]

Convert UNIX datetime to timestamp.

Example:

<p>
    Prestodoctor license expires: {{ prestodoctor.recommendation.expires|from_timestamp(timezone="US/Pacific")|friendly_time }}
</p>
Parameters

context – UNIX timestamps as float as seconds since 1970

Returns

Python datetime object

websauna.system.core.templatecontext.include_filter(config, name, func, renderers=('.html', '.txt'))[source]

Register a new Jinja 2 template filter function.

Example:

import jinja2

@jinja2.contextfilter
def negative(jinja_ctx:jinja2.runtime.Context, context:object, **kw):
    '''Output the negative number.

    Usage:

        {{ 3|neg }}

    '''
    neg = -context
    return neg

Then in your initialization::

include_filter(config, "neg", negative)
Parameters
  • config (Configurator) – Pyramid configurator

  • name (str) – Filter name in templates

  • func (Callable) – Python function which is the filter

  • renderers – List of renderers where the filter is made available

websauna.system.core.templatecontext.includeme(config)[source]
websauna.system.core.templatecontext.render_panel(jinja_ctx, context, name, **kwargs)[source]

Render a panel inline in a template.

Allows placing admin panels in templates directly.

Example how to include panel at the top of admin CRUD listing template:

{% block title %}

  <h1>{{title}}</h1>

  {{ context|render_panel(name="admin_panel", controls=False) }}

{% endblock %}
Parameters
  • context – Any resource object, like ModelAdmin instance

  • name – registered panel name, like admin_panel

  • kwargs – Passed to the panel function as is

Returns

HTML string of the rendered panel

websauna.system.core.templatecontext.timestruct(jinja_ctx, context, **kw)[source]

Render both humanized time and accurate time.

  • show_timezone

  • target_timezone

  • source_timezone

  • format

websauna.system.core.templatecontext.to_json(jinja_ctx, context, safe=True)[source]

Converts Python dict to JSON, safe to be placed inside <script> tag.

Example:

{#
  Export server side generated graph data points
  to Rickshaw client side graph rendering
#}
{% if graph_data %}
  <script>
    window.graphDataJSON = "{{ graph_data|to_json }}";
  </script>
{% endif %}
Parameters
  • context – Takes Python dictionary as input

  • safe – Set to False to not to run escape_js() on the resulting JSON. True by default.

Returns

JSON string to be included inside HTML code

websauna.system.core.templatecontext.uuid_to_slug(jinja_ctx, context, **kw)[source]

Convert UUID object to a base64 encoded slug.

Example:

{% for question in latest_question_list %}
    <li>
      <a href="{{ route_url('details', question.uuid|uuid_to_slug) }}">
        {{ question.question_text }}
      </a>
    </li>
{% endfor %}