From eb7172cd4d9d7af690b2be06e3f925d3023be71c Mon Sep 17 00:00:00 2001 From: Dan McGee Date: Sat, 8 Nov 2014 13:43:59 -0600 Subject: Convert some of URL details to Jinja2 Anytime we have a loop with >100 items, the Django template engine begins to be the bottleneck. This one is relatively straightforward to convert, and sets the stage for converting the mirror status page as well. Signed-off-by: Dan McGee --- mirrors/templatetags/jinja2.py | 31 +++++++++++++++++++++++++++ packages/templatetags/jinja2.py | 1 - templates/mirrors/url_details.html | 29 +------------------------ templates/mirrors/url_details_logs.html.jinja | 28 ++++++++++++++++++++++++ 4 files changed, 60 insertions(+), 29 deletions(-) create mode 100644 mirrors/templatetags/jinja2.py create mode 100644 templates/mirrors/url_details_logs.html.jinja diff --git a/mirrors/templatetags/jinja2.py b/mirrors/templatetags/jinja2.py new file mode 100644 index 00000000..5d47fe9b --- /dev/null +++ b/mirrors/templatetags/jinja2.py @@ -0,0 +1,31 @@ +from django_jinja import library +from markupsafe import Markup + + +@library.global_function +def country_flag(country): + if not country: + return '' + html = ' ' % ( + unicode(country.code).lower(), unicode(country.name)) + return Markup(html) + + +@library.filter +def duration(value): + if not value and type(value) != timedelta: + return u'' + # does not take microseconds into account + total_secs = value.seconds + value.days * 24 * 3600 + mins = total_secs // 60 + hrs, mins = divmod(mins, 60) + return '%d:%02d' % (hrs, mins) + + +@library.filter +def floatvalue(value, arg=2): + if value is None: + return u'' + return '%.*f' % (arg, value) + +# vim: set ts=4 sw=4 et: diff --git a/packages/templatetags/jinja2.py b/packages/templatetags/jinja2.py index 22f9914b..88b59a96 100644 --- a/packages/templatetags/jinja2.py +++ b/packages/templatetags/jinja2.py @@ -68,5 +68,4 @@ def bug_report(package): } return link_encode(url, data) - # vim: set ts=4 sw=4 et: diff --git a/templates/mirrors/url_details.html b/templates/mirrors/url_details.html index 557a1b79..8b7646b8 100644 --- a/templates/mirrors/url_details.html +++ b/templates/mirrors/url_details.html @@ -1,5 +1,4 @@ {% extends "base.html" %} -{% load cycle from future %} {% load static from staticfiles %} {% load mirror_status %} {% load flags %} @@ -57,34 +56,8 @@ {% endif %} -

Check Logs

+ {% include "mirrors/url_details_logs.html.jinja" %} - - - - - - - - - - - - - - - {% for log in logs %} - - - - - - - - - {% endfor %} - -
Check TimeCheck LocationCheck IPLast SyncDelay (hh:mm)Duration (s)Success?Error Message
{{ log.check_time|date:'Y-m-d H:i' }}{% country_flag log.location.country %}{{ log.location.country.name }}{{ log.location.source_ip }}{{ log.last_sync|date:'Y-m-d H:i' }}{{ log.delay|duration }}{{ log.duration|floatvalue }}{{ log.is_success|yesno|capfirst }}{{ log.error|linebreaksbr }}
{% endblock %} diff --git a/templates/mirrors/url_details_logs.html.jinja b/templates/mirrors/url_details_logs.html.jinja new file mode 100644 index 00000000..8f7c5644 --- /dev/null +++ b/templates/mirrors/url_details_logs.html.jinja @@ -0,0 +1,28 @@ +

Check Logs

+ + + + + + + + + + + + + + + + {% for log in logs %} + + + + + + + + + {% endfor %} + +
Check TimeCheck LocationCheck IPLast SyncDelay (hh:mm)Duration (s)Success?Error Message
{{ log.check_time|date('Y-m-d H:i') }}{{ country_flag(log.location.country) }}{{ log.location.country.name }}{{ log.location.source_ip }}{{ log.last_sync|date('Y-m-d H:i') }}{{ log.delay()|duration }}{{ log.duration|floatvalue }}{{ log.is_success|yesno|capfirst }}{{ log.error|linebreaksbr }}
-- cgit v1.2.3-55-g3dc8