In the cron expression dialog, show a human-friendly version of the expression

This commit is contained in:
Pēteris Caune 2020-06-19 11:25:46 +03:00
parent a3b58d25ff
commit eccc193b87
No known key found for this signature in database
GPG Key ID: E28D7679E9A9EDE2
6 changed files with 22 additions and 1 deletions

View File

@ -8,6 +8,7 @@ All notable changes to this project will be documented in this file.
- Add "Get a list of checks's logged pings" API call (#371) - Add "Get a list of checks's logged pings" API call (#371)
- The /api/v1/checks/ endpoint now accepts either UUID or `unique_key` (#370) - The /api/v1/checks/ endpoint now accepts either UUID or `unique_key` (#370)
- Added /api/v1/checks/uuid/flips/ endpoint (#349) - Added /api/v1/checks/uuid/flips/ endpoint (#349)
- In the cron expression dialog, show a human-friendly version of the expression
### Bug Fixes ### Bug Fixes

View File

@ -10,6 +10,7 @@ class CronPreviewTestCase(BaseTestCase):
payload = {"schedule": "* * * * *", "tz": "UTC"} payload = {"schedule": "* * * * *", "tz": "UTC"}
r = self.client.post("/checks/cron_preview/", payload) r = self.client.post("/checks/cron_preview/", payload)
self.assertContains(r, "cron-preview-title", status_code=200) self.assertContains(r, "cron-preview-title", status_code=200)
self.assertContains(r, "“Every minute”")
def test_it_rejects_invalid_cron_expression(self): def test_it_rejects_invalid_cron_expression(self):
samples = ["", "*", "100 100 100 100 100", "* * * * * *", "1,2 3,* * * *"] samples = ["", "*", "100 100 100 100 100", "* * * * * *", "1,2 3,* * * *"]

View File

@ -4,6 +4,7 @@ import os
from secrets import token_urlsafe from secrets import token_urlsafe
from urllib.parse import urlencode from urllib.parse import urlencode
from cron_descriptor import ExpressionDescriptor
from croniter import croniter from croniter import croniter
from django.conf import settings from django.conf import settings
from django.contrib import messages from django.contrib import messages
@ -408,6 +409,8 @@ def cron_preview(request):
it = croniter(schedule, now_local) it = croniter(schedule, now_local)
for i in range(0, 6): for i in range(0, 6):
ctx["dates"].append(it.get_next(datetime)) ctx["dates"].append(it.get_next(datetime))
ctx["desc"] = str(ExpressionDescriptor(schedule, use_24hour_time_format=True))
except UnknownTimeZoneError: except UnknownTimeZoneError:
ctx["bad_tz"] = True ctx["bad_tz"] = True
except: except:

View File

@ -1,3 +1,4 @@
cron-descriptor==1.2.24
croniter==0.3.31 croniter==0.3.31
Django==3.0.7 Django==3.0.7
django-compressor==2.4 django-compressor==2.4

View File

@ -87,9 +87,19 @@
width: 300px; width: 300px;
} }
#cron-description {
text-align: center;
font-weight: bold;
font-style: italic;
font-size: 18px;
color: #FFF;
color: #333;
padding: 16px 8px 0px 8px;
}
#cron-preview { #cron-preview {
background: #f8f8f8; background: #f8f8f8;
height: 256px; min-height: 298px;
} }
#cron-preview p { #cron-preview p {
@ -103,6 +113,10 @@
font-size: small; font-size: small;
} }
#cron-preview-table {
margin: 0;
}
#cron-preview-table tr td:nth-child(1) { #cron-preview-table tr td:nth-child(1) {
width: 120px; width: 120px;
} }

View File

@ -9,6 +9,7 @@
{% elif bad_tz %} {% elif bad_tz %}
<p id="invalid-arguments">Invalid timezone</p> <p id="invalid-arguments">Invalid timezone</p>
{% else %} {% else %}
{% if desc %}<div id="cron-description">“{{ desc }}”</div>{% endif %}
<table id="cron-preview-table" class="table"> <table id="cron-preview-table" class="table">
<tr><th id="cron-preview-title" colspan="3">Expected Ping Dates</th></tr> <tr><th id="cron-preview-title" colspan="3">Expected Ping Dates</th></tr>
{% for d in dates %} {% for d in dates %}