nopaque/app/api/jobs.py

49 lines
1.3 KiB
Python
Raw Normal View History

from flask_restx import Namespace, Resource
from .auth import token_auth
2021-09-14 14:17:26 +00:00
from ..jobs import tasks
from ..models import Job
ns = Namespace('jobs', description='Job operations')
@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'''
@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
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