Updates to create job to handle scheduling

- If the job JSON contains a scheduling date then the new 'job_status" column is set to "scheduled"
- the date is persisted on the JOB row in the database
- Also the job WILL NOT be placed onto the queue of jobs. This is deferred to a later celery beat task.
This commit is contained in:
Martyn Inglis
2016-08-24 16:00:21 +01:00
parent cf3c14dbbd
commit 10f499805c
4 changed files with 152 additions and 34 deletions

View File

@@ -1,10 +1,9 @@
import re
from datetime import (
datetime,
date
)
date,
timedelta)
from flask_marshmallow.fields import fields
from marshmallow import (
post_load,
ValidationError,
@@ -40,11 +39,21 @@ def _validate_positive_number(value, msg="Not a positive integer"):
raise ValidationError(msg)
def _validate_not_more_than_24_hours_in_future(dte, msg="Date cannot be more than 24hrs in the future"):
if dte > datetime.utcnow() + timedelta(hours=24):
raise ValidationError(msg)
def _validate_not_in_future(dte, msg="Date cannot be in the future"):
if dte > date.today():
raise ValidationError(msg)
def _validate_not_in_past(dte, msg="Date cannot be in the past"):
if dte < datetime.today():
raise ValidationError(msg)
# TODO I think marshmallow provides a better integration and error handling.
# Would be better to replace functionality in dao with the marshmallow supported
# functionality.
@@ -210,6 +219,13 @@ class JobSchema(BaseSchema):
job_status = field_for(models.JobStatus, 'name', required=False)
scheduled_for = fields.DateTime()
@validates('scheduled_for')
def validate_scheduled_for(self, value):
_validate_not_in_past(value)
_validate_not_more_than_24_hours_in_future(value)
class Meta:
model = models.Job
exclude = ('notifications',)