2021-09-14 10:52:23 +00:00
|
|
|
from flask_restx import Namespace, Resource
|
|
|
|
from .auth import token_auth
|
2021-09-14 14:17:26 +00:00
|
|
|
from ..jobs import tasks
|
2021-09-14 10:52:23 +00:00
|
|
|
from ..models import Job
|
|
|
|
|
|
|
|
|
|
|
|
ns = Namespace('jobs', description='Job operations')
|
|
|
|
|
|
|
|
|
2021-09-16 12:20:18 +00:00
|
|
|
@ns.route('')
|
2021-09-14 14:17:26 +00:00
|
|
|
class API_Jobs(Resource):
|
|
|
|
'''Shows a list of all jobs and lets you POST to add new job'''
|
2021-09-14 10:52:23 +00:00
|
|
|
|
|
|
|
@ns.doc(security='apiKey')
|
|
|
|
@token_auth.login_required
|
|
|
|
def get(self):
|
|
|
|
'''List all jobs'''
|
2021-09-14 14:17:26 +00:00
|
|
|
# TODO: Implement the correct get_jobs functionality
|
2021-09-14 10:52:23 +00:00
|
|
|
jobs = Job.query.all()
|
|
|
|
return [job.to_dict(include_relationships=False) for job in jobs]
|
2021-09-14 14:17:26 +00:00
|
|
|
|
|
|
|
@ns.doc(security='apiKey')
|
|
|
|
@token_auth.login_required
|
|
|
|
def post(self):
|
|
|
|
'''Create a new job'''
|
|
|
|
# TODO: Implement this
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
@ns.route('/<int:id>')
|
|
|
|
class API_Job(Resource):
|
|
|
|
'''Show a single job and lets you delete it'''
|
|
|
|
|
|
|
|
@ns.doc(security='apiKey')
|
|
|
|
@token_auth.login_required
|
|
|
|
def get(self, id):
|
|
|
|
'''Get a job by id'''
|
|
|
|
job = Job.query.get_or_404(id)
|
|
|
|
return job.to_dict(include_relationships=False)
|
|
|
|
|
|
|
|
@ns.doc(security='apiKey')
|
|
|
|
@token_auth.login_required
|
|
|
|
def delete(self, id):
|
|
|
|
'''Delete a job by id'''
|
|
|
|
job = Job.query.get_or_404(id)
|
|
|
|
# We use this imported task because it will run in the background
|
|
|
|
tasks.delete_job(job.id)
|
|
|
|
return '', 204
|