2021-09-28 10:44:53 -04:00
|
|
|
from django import forms
|
2022-11-03 11:58:26 -07:00
|
|
|
from django.utils.translation import gettext as _
|
2021-09-28 10:44:53 -04:00
|
|
|
|
2023-04-14 10:33:53 -04:00
|
|
|
from utilities.forms import BootstrapMixin
|
|
|
|
from utilities.forms.widgets import DateTimePicker, SelectDurationWidget
|
2023-01-06 09:42:13 -05:00
|
|
|
from utilities.utils import local_now
|
2021-09-28 10:44:53 -04:00
|
|
|
|
|
|
|
__all__ = (
|
|
|
|
'ScriptForm',
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
class ScriptForm(BootstrapMixin, forms.Form):
|
|
|
|
_commit = forms.BooleanField(
|
|
|
|
required=False,
|
|
|
|
initial=True,
|
2022-11-03 11:58:26 -07:00
|
|
|
label=_("Commit changes"),
|
|
|
|
help_text=_("Commit changes to the database (uncheck for a dry-run)")
|
2021-09-28 10:44:53 -04:00
|
|
|
)
|
2022-09-18 15:06:28 +02:00
|
|
|
_schedule_at = forms.DateTimeField(
|
|
|
|
required=False,
|
|
|
|
widget=DateTimePicker(),
|
2022-11-03 11:58:26 -07:00
|
|
|
label=_("Schedule at"),
|
|
|
|
help_text=_("Schedule execution of script to a set time"),
|
2022-09-18 15:06:28 +02:00
|
|
|
)
|
2022-12-08 18:17:13 -05:00
|
|
|
_interval = forms.IntegerField(
|
|
|
|
required=False,
|
|
|
|
min_value=1,
|
|
|
|
label=_("Recurs every"),
|
2022-12-12 15:27:37 -05:00
|
|
|
widget=SelectDurationWidget(),
|
2022-12-08 18:17:13 -05:00
|
|
|
help_text=_("Interval at which this script is re-run (in minutes)")
|
|
|
|
)
|
2021-09-28 10:44:53 -04:00
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
|
2022-12-12 15:27:37 -05:00
|
|
|
# Annotate the current system time for reference
|
2023-01-06 09:42:13 -05:00
|
|
|
now = local_now().strftime('%Y-%m-%d %H:%M:%S')
|
2022-12-12 15:27:37 -05:00
|
|
|
self.fields['_schedule_at'].help_text += f' (current time: <strong>{now}</strong>)'
|
|
|
|
|
2022-09-18 15:06:28 +02:00
|
|
|
# Move _commit and _schedule_at to the end of the form
|
|
|
|
schedule_at = self.fields.pop('_schedule_at')
|
2022-12-08 18:17:13 -05:00
|
|
|
interval = self.fields.pop('_interval')
|
2021-09-28 10:44:53 -04:00
|
|
|
commit = self.fields.pop('_commit')
|
2022-09-18 15:06:28 +02:00
|
|
|
self.fields['_schedule_at'] = schedule_at
|
2022-12-08 18:17:13 -05:00
|
|
|
self.fields['_interval'] = interval
|
2021-09-28 10:44:53 -04:00
|
|
|
self.fields['_commit'] = commit
|
|
|
|
|
2023-02-02 06:33:57 -08:00
|
|
|
def clean(self):
|
2022-12-08 18:17:13 -05:00
|
|
|
scheduled_time = self.cleaned_data['_schedule_at']
|
2023-02-02 06:33:57 -08:00
|
|
|
if scheduled_time and scheduled_time < local_now():
|
2023-01-05 11:42:28 +01:00
|
|
|
raise forms.ValidationError(_('Scheduled time must be in the future.'))
|
2022-12-08 18:17:13 -05:00
|
|
|
|
2023-02-02 06:33:57 -08:00
|
|
|
# When interval is used without schedule at, raise an exception
|
|
|
|
if self.cleaned_data['_interval'] and not scheduled_time:
|
2023-03-28 19:49:18 +05:30
|
|
|
self.cleaned_data['_schedule_at'] = local_now()
|
2023-02-02 06:33:57 -08:00
|
|
|
|
|
|
|
return self.cleaned_data
|
2022-12-08 18:17:13 -05:00
|
|
|
|
2021-09-28 10:44:53 -04:00
|
|
|
@property
|
|
|
|
def requires_input(self):
|
|
|
|
"""
|
2022-12-08 18:17:13 -05:00
|
|
|
A boolean indicating whether the form requires user input (ignore the built-in fields).
|
2021-09-28 10:44:53 -04:00
|
|
|
"""
|
2022-12-08 18:17:13 -05:00
|
|
|
return bool(len(self.fields) > 3)
|