From 758030733c6f23258a7338693460af582a562d4d Mon Sep 17 00:00:00 2001 From: jeremystretch Date: Mon, 12 Dec 2022 15:27:37 -0500 Subject: [PATCH] #8366: Misc cleanup --- netbox/extras/forms/reports.py | 10 ++++- netbox/extras/forms/scripts.py | 7 +++- netbox/templates/extras/report.html | 41 +++++++++---------- netbox/utilities/forms/widgets.py | 8 ++++ .../templates/widgets/select_duration.html | 11 +++++ 5 files changed, 54 insertions(+), 23 deletions(-) create mode 100644 netbox/utilities/templates/widgets/select_duration.html diff --git a/netbox/extras/forms/reports.py b/netbox/extras/forms/reports.py index ddea9e93e..338d05fa3 100644 --- a/netbox/extras/forms/reports.py +++ b/netbox/extras/forms/reports.py @@ -2,7 +2,7 @@ from django import forms from django.utils import timezone from django.utils.translation import gettext as _ -from utilities.forms import BootstrapMixin, DateTimePicker +from utilities.forms import BootstrapMixin, DateTimePicker, SelectDurationWidget __all__ = ( 'ReportForm', @@ -20,6 +20,7 @@ class ReportForm(BootstrapMixin, forms.Form): required=False, min_value=1, label=_("Recurs every"), + widget=SelectDurationWidget(), help_text=_("Interval at which this report is re-run (in minutes)") ) @@ -29,3 +30,10 @@ class ReportForm(BootstrapMixin, forms.Form): raise forms.ValidationError(_('Scheduled time must be in the future.')) return scheduled_time + + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + + # Annotate the current system time for reference + now = timezone.now().strftime('%Y-%m-%d %H:%M:%S') + self.fields['schedule_at'].help_text += f' (current time: {now})' diff --git a/netbox/extras/forms/scripts.py b/netbox/extras/forms/scripts.py index 281efc754..58388e247 100644 --- a/netbox/extras/forms/scripts.py +++ b/netbox/extras/forms/scripts.py @@ -2,7 +2,7 @@ from django import forms from django.utils import timezone from django.utils.translation import gettext as _ -from utilities.forms import BootstrapMixin, DateTimePicker +from utilities.forms import BootstrapMixin, DateTimePicker, SelectDurationWidget __all__ = ( 'ScriptForm', @@ -26,12 +26,17 @@ class ScriptForm(BootstrapMixin, forms.Form): required=False, min_value=1, label=_("Recurs every"), + widget=SelectDurationWidget(), help_text=_("Interval at which this script is re-run (in minutes)") ) def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) + # Annotate the current system time for reference + now = timezone.now().strftime('%Y-%m-%d %H:%M:%S') + self.fields['_schedule_at'].help_text += f' (current time: {now})' + # Move _commit and _schedule_at to the end of the form schedule_at = self.fields.pop('_schedule_at') interval = self.fields.pop('_interval') diff --git a/netbox/templates/extras/report.html b/netbox/templates/extras/report.html index 94f37571b..a3876d319 100644 --- a/netbox/templates/extras/report.html +++ b/netbox/templates/extras/report.html @@ -34,33 +34,32 @@ {% block content %}
{% if perms.extras.run_report %} -
-
+
+
- {% csrf_token %} - {% render_form form %} -
- -
+ {% csrf_token %} + {% render_form form %} +
+ +
- {% endif %}
-
- {% if report.result %} - Last run: - {{ report.result.created|annotated_date }} - - {% endif %} -
+
+ {% if report.result %} + Last run: + {{ report.result.created|annotated_date }} + + {% endif %} +
{% endblock content %} diff --git a/netbox/utilities/forms/widgets.py b/netbox/utilities/forms/widgets.py index 76a4bc607..1802306f1 100644 --- a/netbox/utilities/forms/widgets.py +++ b/netbox/utilities/forms/widgets.py @@ -17,6 +17,7 @@ __all__ = ( 'DatePicker', 'DateTimePicker', 'NumericArrayField', + 'SelectDurationWidget', 'SelectSpeedWidget', 'SelectWithPK', 'SlugWidget', @@ -108,6 +109,13 @@ class SelectSpeedWidget(forms.NumberInput): template_name = 'widgets/select_speed.html' +class SelectDurationWidget(forms.NumberInput): + """ + Dropdown to select one of several common options for a time duration (in minutes). + """ + template_name = 'widgets/select_duration.html' + + class NumericArrayField(SimpleArrayField): def clean(self, value): diff --git a/netbox/utilities/templates/widgets/select_duration.html b/netbox/utilities/templates/widgets/select_duration.html new file mode 100644 index 000000000..639075a8c --- /dev/null +++ b/netbox/utilities/templates/widgets/select_duration.html @@ -0,0 +1,11 @@ +
+ {% include 'django/forms/widgets/number.html' %} + + +