Files
notifications-api/app/job/rest.py
Rebecca Law b5c662eca8 Change services.id to a UUID
Ideally all the primary keys in the db would be UUID in order to guarantee unique ids across distributed dbs.
This updates the services.id to a UUID. All the tables with a foreign key to the services.id are also updated.
The endpoints no longer state a data type of the <service_id> path param.
All the tests are updated to reflect this update.

The thing to pay attention to is the 0011_uuid_service_id.py migration script.
This commit must go with a commit on the notifications_admin app to keep things working.
There will be a small outage until both deploys have happened.
2016-02-02 14:22:22 +00:00

52 lines
1.3 KiB
Python

from flask import (
Blueprint,
jsonify,
request
)
from sqlalchemy.exc import DataError
from sqlalchemy.orm.exc import NoResultFound
from app.dao.jobs_dao import (
save_job,
get_job,
get_jobs_by_service
)
from app.schemas import (
job_schema,
jobs_schema
)
job = Blueprint('job', __name__, url_prefix='/service/<service_id>/job')
@job.route('/<job_id>', methods=['GET'])
@job.route('', methods=['GET'])
def get_job_for_service(service_id, job_id=None):
if job_id:
try:
job = get_job(service_id, job_id)
data, errors = job_schema.dump(job)
return jsonify(data=data)
except DataError:
return jsonify(result="error", message="Invalid job id"), 400
except NoResultFound:
return jsonify(result="error", message="Job not found"), 404
else:
jobs = get_jobs_by_service(service_id)
data, errors = jobs_schema.dump(jobs)
return jsonify(data=data)
@job.route('', methods=['POST'])
def create_job(service_id):
job, errors = job_schema.load(request.get_json())
if errors:
return jsonify(result="error", message=errors), 400
try:
save_job(job)
except Exception as e:
return jsonify(result="error", message=str(e)), 500
return jsonify(data=job_schema.dump(job).data), 201