|
5 | 5 | from core.api.serializers_.jobs import JobSerializer |
6 | 6 | from extras.models import Script |
7 | 7 | from netbox.api.serializers import ValidatedModelSerializer |
| 8 | +from utilities.datetime import local_now |
8 | 9 |
|
9 | 10 | __all__ = ( |
10 | 11 | 'ScriptDetailSerializer', |
@@ -66,11 +67,30 @@ class ScriptInputSerializer(serializers.Serializer): |
66 | 67 | interval = serializers.IntegerField(required=False, allow_null=True) |
67 | 68 |
|
68 | 69 | 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.')) |
71 | 78 | return value |
72 | 79 |
|
73 | 80 | def validate_interval(self, value): |
| 81 | + """ |
| 82 | + Validates the provided interval based on the script's scheduling configuration. |
| 83 | + """ |
74 | 84 | 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.')) |
76 | 86 | 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