Skip to content

Commit 70d7a0a

Browse files
committed
feat(extras): Enhance API script scheduling validation
Add validation for script scheduling to ensure the `schedule_at` time is in the future and default to the current time when only `interval` is provided. Incorporate `local_now` for accurate time comparisons. Fixes #20524
1 parent bee0080 commit 70d7a0a

File tree

1 file changed

+23
-3
lines changed

1 file changed

+23
-3
lines changed

netbox/extras/api/serializers_/scripts.py

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from core.api.serializers_.jobs import JobSerializer
66
from extras.models import Script
77
from netbox.api.serializers import ValidatedModelSerializer
8+
from utilities.datetime import local_now
89

910
__all__ = (
1011
'ScriptDetailSerializer',
@@ -66,11 +67,30 @@ class ScriptInputSerializer(serializers.Serializer):
6667
interval = serializers.IntegerField(required=False, allow_null=True)
6768

6869
def validate_schedule_at(self, value):
69-
if value and not self.context['script'].python_class.scheduling_enabled:
70-
raise serializers.ValidationError(_("Scheduling is not enabled for this script."))
70+
"""
71+
Validates the specified schedule time for a script execution.
72+
"""
73+
if value:
74+
if not self.context['script'].python_class.scheduling_enabled:
75+
raise serializers.ValidationError(_('Scheduling is not enabled for this script.'))
76+
if value < local_now():
77+
raise serializers.ValidationError(_('Scheduled time must be in the future.'))
7178
return value
7279

7380
def validate_interval(self, value):
81+
"""
82+
Validates the provided interval based on the script's scheduling configuration.
83+
"""
7484
if value and not self.context['script'].python_class.scheduling_enabled:
75-
raise serializers.ValidationError(_("Scheduling is not enabled for this script."))
85+
raise serializers.ValidationError(_('Scheduling is not enabled for this script.'))
7686
return value
87+
88+
def validate(self, data):
89+
"""
90+
Validates the given data and ensures the necessary fields are populated.
91+
"""
92+
# Set the schedule_at time to now if only an interval is provided.
93+
if 'interval' in data and 'schedule_at' not in data:
94+
data['schedule_at'] = local_now()
95+
96+
return super().validate(data)

0 commit comments

Comments
 (0)