2016-01-15 15:48:05 +00:00
|
|
|
from flask import (
|
|
|
|
|
Blueprint,
|
|
|
|
|
jsonify,
|
2016-06-28 15:17:36 +01:00
|
|
|
request,
|
|
|
|
|
current_app
|
2016-06-15 16:19:28 +01:00
|
|
|
)
|
2016-01-15 15:48:05 +00:00
|
|
|
|
|
|
|
|
from app.dao.jobs_dao import (
|
2016-02-24 17:12:30 +00:00
|
|
|
dao_create_job,
|
|
|
|
|
dao_get_job_by_service_id_and_job_id,
|
2016-04-27 09:46:45 +01:00
|
|
|
dao_get_jobs_by_service_id
|
2016-01-15 15:48:05 +00:00
|
|
|
)
|
|
|
|
|
|
2016-02-24 17:12:30 +00:00
|
|
|
from app.dao.services_dao import (
|
|
|
|
|
dao_fetch_service_by_id
|
|
|
|
|
)
|
2016-02-09 14:17:42 +00:00
|
|
|
|
2016-05-11 17:04:51 +01:00
|
|
|
from app.dao.templates_dao import (dao_get_template_by_id)
|
2016-06-28 15:17:36 +01:00
|
|
|
from app.dao.notifications_dao import get_notifications_for_job
|
2016-05-11 17:04:51 +01:00
|
|
|
|
2016-06-28 15:17:36 +01:00
|
|
|
from app.schemas import job_schema, unarchived_template_schema, notifications_filter_schema, notification_status_schema
|
2016-01-15 15:48:05 +00:00
|
|
|
|
2016-02-24 17:12:30 +00:00
|
|
|
from app.celery.tasks import process_job
|
2016-01-15 15:48:05 +00:00
|
|
|
|
2016-06-28 15:17:36 +01:00
|
|
|
from app.utils import pagination_links
|
|
|
|
|
|
2016-03-11 12:39:55 +00:00
|
|
|
job = Blueprint('job', __name__, url_prefix='/service/<uuid:service_id>/job')
|
2016-01-15 15:48:05 +00:00
|
|
|
|
2016-06-14 15:07:23 +01:00
|
|
|
from app.errors import (
|
|
|
|
|
register_errors,
|
|
|
|
|
InvalidRequest
|
|
|
|
|
)
|
2016-02-24 17:12:30 +00:00
|
|
|
|
2016-02-17 17:04:50 +00:00
|
|
|
register_errors(job)
|
|
|
|
|
|
|
|
|
|
|
2016-01-15 15:48:05 +00:00
|
|
|
@job.route('/<job_id>', methods=['GET'])
|
2016-02-24 17:12:30 +00:00
|
|
|
def get_job_by_service_and_job_id(service_id, job_id):
|
|
|
|
|
job = dao_get_job_by_service_id_and_job_id(service_id, job_id)
|
2016-06-14 15:07:23 +01:00
|
|
|
data = job_schema.dump(job).data
|
2016-02-24 17:12:30 +00:00
|
|
|
return jsonify(data=data)
|
|
|
|
|
|
|
|
|
|
|
2016-06-28 15:17:36 +01:00
|
|
|
@job.route('/<job_id>/notifications', methods=['GET'])
|
|
|
|
|
def get_all_notifications_for_service_job(service_id, job_id):
|
|
|
|
|
data = notifications_filter_schema.load(request.args).data
|
|
|
|
|
page = data['page'] if 'page' in data else 1
|
|
|
|
|
page_size = data['page_size'] if 'page_size' in data else current_app.config.get('PAGE_SIZE')
|
|
|
|
|
|
|
|
|
|
pagination = get_notifications_for_job(
|
|
|
|
|
service_id,
|
|
|
|
|
job_id,
|
|
|
|
|
filter_dict=data,
|
|
|
|
|
page=page,
|
|
|
|
|
page_size=page_size)
|
|
|
|
|
kwargs = request.args.to_dict()
|
|
|
|
|
kwargs['service_id'] = service_id
|
|
|
|
|
kwargs['job_id'] = job_id
|
|
|
|
|
return jsonify(
|
|
|
|
|
notifications=notification_status_schema.dump(pagination.items, many=True).data,
|
|
|
|
|
page_size=page_size,
|
|
|
|
|
total=pagination.total,
|
|
|
|
|
links=pagination_links(
|
|
|
|
|
pagination,
|
|
|
|
|
'.get_all_notifications_for_service_job',
|
|
|
|
|
**kwargs
|
|
|
|
|
)
|
|
|
|
|
), 200
|
|
|
|
|
|
|
|
|
|
|
2016-01-15 16:34:48 +00:00
|
|
|
@job.route('', methods=['GET'])
|
2016-02-24 17:12:30 +00:00
|
|
|
def get_jobs_by_service(service_id):
|
2016-05-24 17:21:04 +01:00
|
|
|
if request.args.get('limit_days'):
|
|
|
|
|
try:
|
|
|
|
|
limit_days = int(request.args['limit_days'])
|
|
|
|
|
except ValueError as e:
|
2016-06-15 16:19:28 +01:00
|
|
|
errors = {'limit_days': ['{} is not an integer'.format(request.args['limit_days'])]}
|
2016-06-14 15:07:23 +01:00
|
|
|
raise InvalidRequest(errors, status_code=400)
|
2016-05-24 17:21:04 +01:00
|
|
|
else:
|
|
|
|
|
limit_days = None
|
|
|
|
|
|
|
|
|
|
jobs = dao_get_jobs_by_service_id(service_id, limit_days)
|
2016-06-14 15:07:23 +01:00
|
|
|
data = job_schema.dump(jobs, many=True).data
|
2016-02-24 17:12:30 +00:00
|
|
|
return jsonify(data=data)
|
2016-01-15 15:48:05 +00:00
|
|
|
|
|
|
|
|
|
2016-01-15 16:34:48 +00:00
|
|
|
@job.route('', methods=['POST'])
|
2016-01-18 09:57:04 +00:00
|
|
|
def create_job(service_id):
|
2016-03-11 12:39:55 +00:00
|
|
|
dao_fetch_service_by_id(service_id)
|
2016-02-04 20:55:09 +00:00
|
|
|
|
2016-02-24 17:12:30 +00:00
|
|
|
data = request.get_json()
|
|
|
|
|
data.update({
|
|
|
|
|
"service": service_id
|
|
|
|
|
})
|
2016-05-11 17:04:51 +01:00
|
|
|
template = dao_get_template_by_id(data['template'])
|
2016-05-23 15:44:56 +01:00
|
|
|
|
|
|
|
|
errors = unarchived_template_schema.validate({'archived': template.archived})
|
|
|
|
|
|
|
|
|
|
if errors:
|
2016-06-14 15:07:23 +01:00
|
|
|
raise InvalidRequest(errors, status_code=400)
|
2016-05-23 15:44:56 +01:00
|
|
|
|
2016-05-11 17:04:51 +01:00
|
|
|
data.update({"template_version": template.version})
|
2016-06-14 15:07:23 +01:00
|
|
|
job = job_schema.load(data).data
|
2016-02-24 17:12:30 +00:00
|
|
|
dao_create_job(job)
|
|
|
|
|
process_job.apply_async([str(job.id)], queue="process-job")
|
|
|
|
|
return jsonify(data=job_schema.dump(job).data), 201
|