mirror of
https://github.com/GSA/notifications-api.git
synced 2026-07-28 19:59:47 -04:00
Added endpoints for creating job, and getting job/jobs.
This commit is contained in:
@@ -30,10 +30,13 @@ def create_app(config_name):
|
||||
from app.user.rest import user as user_blueprint
|
||||
from app.template.rest import template as template_blueprint
|
||||
from app.status.healthcheck import status as status_blueprint
|
||||
from app.job.rest import job as job_blueprint
|
||||
|
||||
application.register_blueprint(service_blueprint, url_prefix='/service')
|
||||
application.register_blueprint(user_blueprint, url_prefix='/user')
|
||||
application.register_blueprint(template_blueprint, url_prefix="/template")
|
||||
application.register_blueprint(status_blueprint, url_prefix='/status')
|
||||
application.register_blueprint(job_blueprint, url_prefix='/job')
|
||||
|
||||
return application
|
||||
|
||||
|
||||
@@ -7,8 +7,8 @@ def save_job(job):
|
||||
db.session.commit()
|
||||
|
||||
|
||||
def get_job_by_id(id):
|
||||
return Job.query.get(id)
|
||||
def get_job_by_id(job_id):
|
||||
return Job.query.filter_by(id=job_id).one()
|
||||
|
||||
|
||||
def get_jobs_by_service(service_id):
|
||||
|
||||
51
app/job/rest.py
Normal file
51
app/job/rest.py
Normal file
@@ -0,0 +1,51 @@
|
||||
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_by_id,
|
||||
get_jobs
|
||||
)
|
||||
|
||||
from app.schemas import (
|
||||
job_schema,
|
||||
jobs_schema
|
||||
)
|
||||
|
||||
job = Blueprint('job', __name__)
|
||||
|
||||
|
||||
@job.route('/<job_id>', methods=['GET'])
|
||||
@job.route('/', methods=['GET'])
|
||||
def get_job(job_id=None):
|
||||
if job_id:
|
||||
try:
|
||||
job = get_job_by_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()
|
||||
data, errors = jobs_schema.dump(jobs)
|
||||
return jsonify(data=data)
|
||||
|
||||
|
||||
@job.route('/', methods=['POST'])
|
||||
def create_job():
|
||||
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
|
||||
@@ -108,6 +108,7 @@ class Job(db.Model):
|
||||
service_id = db.Column(db.BigInteger, db.ForeignKey('services.id'), index=True, unique=False)
|
||||
service = db.relationship('Service', backref=db.backref('jobs', lazy='dynamic'))
|
||||
template_id = db.Column(db.BigInteger, db.ForeignKey('templates.id'), index=True, unique=False)
|
||||
template = db.relationship('Template', backref=db.backref('jobs', lazy='dynamic'))
|
||||
created_at = db.Column(
|
||||
db.DateTime,
|
||||
index=False,
|
||||
|
||||
@@ -25,7 +25,7 @@ class ServiceSchema(ma.ModelSchema):
|
||||
class TemplateSchema(ma.ModelSchema):
|
||||
class Meta:
|
||||
model = models.Template
|
||||
exclude = ("updated_at", "created_at", "service_id")
|
||||
exclude = ("updated_at", "created_at", "service_id", "jobs")
|
||||
|
||||
|
||||
class TokenSchema(ma.ModelSchema):
|
||||
@@ -34,6 +34,11 @@ class TokenSchema(ma.ModelSchema):
|
||||
exclude = ["service"]
|
||||
|
||||
|
||||
class JobSchema(ma.ModelSchema):
|
||||
class Meta:
|
||||
model = models.Job
|
||||
|
||||
|
||||
user_schema = UserSchema()
|
||||
users_schema = UserSchema(many=True)
|
||||
service_schema = ServiceSchema()
|
||||
@@ -42,3 +47,5 @@ template_schema = TemplateSchema()
|
||||
templates_schema = TemplateSchema(many=True)
|
||||
token_schema = TokenSchema()
|
||||
tokens_schema = TokenSchema(many=True)
|
||||
job_schema = JobSchema()
|
||||
jobs_schema = JobSchema(many=True)
|
||||
|
||||
Reference in New Issue
Block a user