Fix job field validation

Previously, fields in the Job model were not validated when the job was
created. Now 'full_clean()' is called before saving the job to ensure
valid data.
This commit is contained in:
Alexander Haase
2024-10-23 21:39:00 +02:00
committed by Jeremy Stretch
parent ac12eae0b7
commit 69e1394fef
2 changed files with 12 additions and 10 deletions
+4 -2
View File
@@ -130,7 +130,7 @@ class Job(models.Model):
super().clean()
# Validate the assigned object type
if self.object_type not in ObjectType.objects.with_feature('jobs'):
if self.object_type and self.object_type not in ObjectType.objects.with_feature('jobs'):
raise ValidationError(
_("Jobs cannot be assigned to this object type ({type}).").format(type=self.object_type)
)
@@ -223,7 +223,7 @@ class Job(models.Model):
rq_queue_name = get_queue_for_model(object_type.model if object_type else None)
queue = django_rq.get_queue(rq_queue_name)
status = JobStatusChoices.STATUS_SCHEDULED if schedule_at else JobStatusChoices.STATUS_PENDING
job = Job.objects.create(
job = Job(
object_type=object_type,
object_id=object_id,
name=name,
@@ -233,6 +233,8 @@ class Job(models.Model):
user=user,
job_id=uuid.uuid4()
)
job.full_clean()
job.save()
# Run the job immediately, rather than enqueuing it as a background task. Note that this is a synchronous
# (blocking) operation, and execution will pause until the job completes.