nopaque/app/api/jobs.py
2021-09-14 16:17:26 +02:00

49 lines
1.3 KiB
Python

from flask_restx import Namespace, Resource
from .auth import token_auth
from ..jobs import tasks
from ..models import Job
ns = Namespace('jobs', description='Job operations')
@ns.route('/')
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'''
# TODO: Implement the correct get_jobs functionality
jobs = Job.query.all()
return [job.to_dict(include_relationships=False) for job in jobs]
@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