diff --git a/hc/api/models.py b/hc/api/models.py index 98aca553..ccc9ccb5 100644 --- a/hc/api/models.py +++ b/hc/api/models.py @@ -115,23 +115,23 @@ class Check(models.Model): return str(self.code) - def relative_url(self): + def url(self): + """ Return check's ping url in user's preferred style. + + Note: this method reads self.project. If project is not loaded already, + this causes a SQL query. + + """ + if self.project_id and self.project.show_slugs: if not self.slug: return None - key = self.project.ping_key # If ping_key is not set, use dummy placeholder - if key is None: - key = "{ping_key}" - return key + "/" + self.slug - - return str(self.code) + key = self.project.ping_key or "{ping_key}" + return settings.PING_ENDPOINT + key + "/" + self.slug - def url(self): - s = self.relative_url() - if s: - return settings.PING_ENDPOINT + s + return settings.PING_ENDPOINT + str(self.code) def details_url(self): return settings.SITE_ROOT + reverse("hc-details", args=[self.code]) diff --git a/hc/front/templatetags/hc_extras.py b/hc/front/templatetags/hc_extras.py index 9de2207e..2d7b7d53 100644 --- a/hc/front/templatetags/hc_extras.py +++ b/hc/front/templatetags/hc_extras.py @@ -213,3 +213,17 @@ def guess_schedule(check): hours, seconds = divmod(v, 3600) if hours in (2, 3, 4, 6, 8, 12) and seconds == 0: return f"0 */{hours} * * *" + + +FORMATTED_PING_ENDPOINT = ( + f"""{settings.PING_ENDPOINT}""" +) + + +@register.filter +def format_ping_endpoint(ping_url): + """ Wrap the ping endpoint in span tags for styling with CSS. """ + + assert ping_url.startswith(settings.PING_ENDPOINT) + tail = ping_url[len(settings.PING_ENDPOINT) :] + return mark_safe(FORMATTED_PING_ENDPOINT + escape(tail)) diff --git a/hc/front/views.py b/hc/front/views.py index 6314b145..d64d0d07 100644 --- a/hc/front/views.py +++ b/hc/front/views.py @@ -182,6 +182,7 @@ def my_checks(request, code): request.session["last_project_id"] = project.id q = Check.objects.filter(project=project) + q = q.select_related("project") checks = list(q.prefetch_related("channel_set")) sortchecks(checks, request.profile.sort) diff --git a/templates/front/my_checks_desktop.html b/templates/front/my_checks_desktop.html index 2e7cad21..7b7b56ec 100644 --- a/templates/front/my_checks_desktop.html +++ b/templates/front/my_checks_desktop.html @@ -84,12 +84,8 @@ {% if project.show_slugs and not check.slug %} unavailable, set name first {% else %} - - {{ ping_endpoint }}{{ check.relative_url }} - - + {{ check.url|format_ping_endpoint }} + {% endif %}