Files
lrc-backend/api/recorder_api.py

256 lines
9.7 KiB
Python

# Copyright (c) 2019. Tobias Kurze
"""
This module provides functions related to authentication through the API.
For example: listing of available auth providers or registration of users.
Login through API does not start a new session, but instead returns JWT.
"""
from flask_jwt_extended import jwt_required
from flask_restplus import fields, Resource
from backend import db
from backend.api import api_recorder
from backend.models.recorder_model import Recorder, RecorderModel, RecorderCommand
recorder_model = api_recorder.model('Recorder', {
'id': fields.String(required=False, description='The recorder\'s identifier'),
'created_at': fields.DateTime(required=False, description='Creation date of the recorder'),
'name': fields.String(required=True, description='The recorder\'s name'),
'description': fields.String(required=False, description='The recorder\'s description'),
'ip': fields.String(required=False, description='The recorder\'s IP address'),
'network_name': fields.String(required=False, description='The recorder\'s network name'),
'recorder_model': fields.Nested(api_recorder.model('recorder_model',
{'id': fields.Integer(), 'name': fields.String()}),
required=False,
description='Model of the recorder.'),
'room': fields.Nested(api_recorder.model('recorder_room',
{'id': fields.Integer(), 'name': fields.String(),
'number': fields.String(), 'alternate_name': fields.String()}),
required=False,
description='Room in which the recorder is located.')
})
# ==
@api_recorder.route('/<int:id>')
@api_recorder.response(404, 'Recorder not found')
@api_recorder.param('id', 'The recorder identifier')
class RecorderResource(Resource):
@jwt_required
@api_recorder.doc('get_recorder')
@api_recorder.marshal_with(recorder_model)
def get(self, id):
"""Fetch a recorder given its identifier"""
recorder = Recorder.query.get(id)
if recorder is not None:
return recorder
api_recorder.abort(404)
@jwt_required
@api_recorder.doc('delete_todo')
@api_recorder.response(204, 'Todo deleted')
def delete(self, id):
"""Delete a recorder given its identifier"""
recorder = Recorder.query.get(id)
if recorder is not None:
db.session.delete(recorder)
db.session.commit()
return '', 204
api_recorder.abort(404)
@jwt_required
@api_recorder.doc('update_recorder')
@api_recorder.expect(recorder_model)
@api_recorder.marshal_with(recorder_model)
def put(self, id):
"""Update a recorder given its identifier"""
num_rows_matched = Recorder.query.filter_by(id=id).update(api_recorder.payload)
if num_rows_matched < 1:
api_recorder.abort(404)
db.session.commit()
return "ok"
@api_recorder.route('')
class RecorderList(Resource):
@jwt_required
@api_recorder.doc('recorders')
@api_recorder.marshal_list_with(recorder_model)
def get(self):
"""
List all recorders
:return: recorders
"""
return Recorder.get_all()
@jwt_required
@api_recorder.doc('create_recorder')
@api_recorder.expect(recorder_model)
@api_recorder.marshal_with(recorder_model, code=201)
def post(self):
recorder = Recorder(**api_recorder.payload)
db.session.add(recorder)
db.session.commit()
return recorder
# ==
recorder_model_model = api_recorder.model('Recorder Model', {
'id': fields.String(required=False, description='The recorder model\'s identifier'),
'name': fields.String(required=True, description='The recorder model\'s name'),
'notes': fields.String(required=False, description='The recorder model\'s notes'),
'recorders': fields.List(fields.Nested(api_recorder.model('recorder_model',
{'id': fields.Integer(), 'name': fields.String(),
'network_name': fields.String(),
'ip': fields.String()})), required=False,
description='Model of the recorder.'),
'commands': fields.List(fields.Nested(api_recorder.model('recorder_model_commands',
{'id': fields.Integer(), 'name': fields.String()})),
required=False,
description='Room in which the recorder is located.')
})
@api_recorder.route('/model/<int:id>')
@api_recorder.response(404, 'Recorder Model not found')
@api_recorder.param('id', 'The recorder model identifier')
class RecorderModelResource(Resource):
@jwt_required
@api_recorder.doc('get_recorder_model')
@api_recorder.marshal_with(recorder_model_model)
def get(self, id):
"""Fetch a recorder model given its identifier"""
recorder_model = RecorderModel.query.get(id)
if recorder_model is not None:
return recorder_model
api_recorder.abort(404)
@jwt_required
@api_recorder.doc('delete_recorder_model')
@api_recorder.response(204, 'Recorder model deleted')
def delete(self, id):
"""Delete a recorder model given its identifier"""
recorder_model = RecorderModel.query.get(id)
if recorder_model is not None:
db.session.delete(recorder_model)
db.session.commit()
return '', 204
api_recorder.abort(404)
@jwt_required
@api_recorder.doc('update_recorder_model')
@api_recorder.expect(recorder_model_model)
@api_recorder.marshal_with(recorder_model_model)
def put(self, id):
"""Update a recorder_model given its identifier"""
num_rows_matched = RecorderModel.query.filter_by(id=id).update(api_recorder.payload)
if num_rows_matched < 1:
api_recorder.abort(404)
db.session.commit()
return "ok"
@api_recorder.route('/model')
class RecorderModelList(Resource):
@jwt_required
@api_recorder.doc('recorders')
@api_recorder.marshal_list_with(recorder_model_model)
def get(self):
"""
List all recorder models
:return: recorder models
"""
return Recorder.get_all()
@jwt_required
@api_recorder.doc('create_recorder')
@api_recorder.expect(recorder_model_model)
@api_recorder.marshal_with(recorder_model_model, code=201)
def post(self):
recorder = Recorder(**api_recorder.payload)
db.session.add(recorder)
db.session.commit()
return recorder
# ==
recorder_command_model = api_recorder.model('Recorder Command', {
'id': fields.String(required=False, description='The recorder command\'s identifier'),
'name': fields.String(required=True, description='The recorder command\'s name'),
'description': fields.String(required=False, description='The recorder command\'s description'),
'command': fields.String(required=True, description='The recorder command\'s name'),
'recorder_models': fields.List(fields.Nested(api_recorder.model('recorder_command_models',
{'id': fields.Integer(), 'name': fields.String()})),
required=False,
description='Recorder models associated with the command.'),
})
@api_recorder.route('/command/<int:id>')
@api_recorder.response(404, 'Recorder Command not found')
@api_recorder.param('id', 'The recorder command identifier')
class RecorderCommandResource(Resource):
@jwt_required
@api_recorder.doc('get_recorder_command')
@api_recorder.marshal_with(recorder_command_model)
def get(self, id):
"""Fetch a recorder command given its identifier"""
recorder_command = RecorderCommand.query.get(id)
if recorder_command is not None:
return recorder_command
api_recorder.abort(404)
@jwt_required
@api_recorder.doc('delete_recorder_command')
@api_recorder.response(204, 'Recorder_command deleted')
def delete(self, id):
"""Delete a recorder command given its identifier"""
recorder_command = RecorderCommand.query.get(id)
if recorder_command is not None:
db.session.delete(recorder_command)
db.session.commit()
return '', 204
api_recorder.abort(404)
@jwt_required
@api_recorder.doc('update_recorder_command')
@api_recorder.expect(recorder_command_model)
@api_recorder.marshal_with(recorder_command_model)
def put(self, id):
"""Update a recorder command given its identifier"""
num_rows_matched = RecorderCommand.query.filter_by(id=id).update(api_recorder.payload)
if num_rows_matched < 1:
api_recorder.abort(404)
db.session.commit()
return "ok"
@api_recorder.route('/command')
class RecorderCommandList(Resource):
@jwt_required
@api_recorder.doc('recorder_commands')
@api_recorder.marshal_list_with(recorder_command_model)
def get(self):
"""
List all recorders commands
:return: recorder commands
"""
return RecorderCommand.get_all()
@jwt_required
@api_recorder.doc('create_recorder_commands')
@api_recorder.expect(recorder_command_model)
@api_recorder.marshal_with(recorder_command_model, code=201)
def post(self):
recorder_command = RecorderCommand(**api_recorder.payload)
db.session.add(recorder_command)
db.session.commit()
return recorder_command