Improve validation for scheduled jobs

This commit is contained in:
jeremystretch 2022-12-08 17:03:03 -05:00
parent 839f99d537
commit 1ffccdaadc
4 changed files with 26 additions and 2 deletions

View File

@ -1,4 +1,5 @@
from django import forms
from django.utils import timezone
from django.utils.translation import gettext as _
from utilities.forms import BootstrapMixin, DateTimePicker
@ -17,6 +18,14 @@ class ReportForm(BootstrapMixin, forms.Form):
)
interval = forms.IntegerField(
required=False,
min_value=1,
label=_("Recurs every"),
help_text=_("Interval at which this report is re-run (in minutes)")
)
def clean_schedule_at(self):
scheduled_time = self.cleaned_data['schedule_at']
if scheduled_time and scheduled_time < timezone.now():
raise forms.ValidationError(_('Scheduled time must be in the future.'))
return scheduled_time

View File

@ -1,4 +1,5 @@
from django import forms
from django.utils import timezone
from django.utils.translation import gettext as _
from utilities.forms import BootstrapMixin, DateTimePicker
@ -23,6 +24,7 @@ class ScriptForm(BootstrapMixin, forms.Form):
)
_interval = forms.IntegerField(
required=False,
min_value=1,
label=_("Recurs every"),
help_text=_("Interval at which this script is re-run (in minutes)")
)
@ -38,6 +40,15 @@ class ScriptForm(BootstrapMixin, forms.Form):
self.fields['_interval'] = interval
self.fields['_commit'] = commit
def clean__schedule_at(self):
scheduled_time = self.cleaned_data['_schedule_at']
if scheduled_time and scheduled_time < timezone.now():
raise forms.ValidationError({
'_schedule_at': _('Scheduled time must be in the future.')
})
return scheduled_time
@property
def requires_input(self):
"""

View File

@ -1,3 +1,4 @@
import django.core.validators
from django.db import migrations, models
@ -16,7 +17,7 @@ class Migration(migrations.Migration):
migrations.AddField(
model_name='jobresult',
name='interval',
field=models.PositiveIntegerField(blank=True, null=True),
field=models.PositiveIntegerField(blank=True, null=True, validators=[django.core.validators.MinValueValidator(1)]),
),
migrations.AddField(
model_name='jobresult',

View File

@ -7,7 +7,7 @@ from django.contrib.auth.models import User
from django.contrib.contenttypes.fields import GenericForeignKey
from django.contrib.contenttypes.models import ContentType
from django.core.cache import cache
from django.core.validators import ValidationError
from django.core.validators import MinValueValidator, ValidationError
from django.db import models
from django.http import HttpResponse, QueryDict
from django.urls import reverse
@ -590,6 +590,9 @@ class JobResult(models.Model):
interval = models.PositiveIntegerField(
blank=True,
null=True,
validators=(
MinValueValidator(1),
),
help_text=_("Recurrence interval (in minutes)")
)
started = models.DateTimeField(