151 lines
6.8 KiB
Python
151 lines
6.8 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.
|
|
"""
|
|
import inspect
|
|
import pkgutil
|
|
from pprint import pprint
|
|
|
|
from flask_jwt_extended import jwt_required
|
|
from flask_restx import fields, Resource
|
|
|
|
from backend import db, app
|
|
from backend.api import api_virtual_command, recorder_command_model
|
|
from backend.models import VirtualCommand
|
|
from backend.models.recorder_model import Recorder, RecorderModel, RecorderCommand
|
|
from backend.models.room_model import Room
|
|
import backend.recorder_adapters as r_a
|
|
|
|
virtual_command_model = api_virtual_command.model('VirtualCommand', {
|
|
'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(min_length=3, required=True, description='The recorder\'s name'),
|
|
'description': fields.String(required=False, description='The recorder\'s description'),
|
|
|
|
'parent_virtual_command': fields.Nested(api_virtual_command.model('VirtualCommandParent',
|
|
{
|
|
'id': fields.String(required=False,
|
|
description='The recorder\'s identifier'),
|
|
'name': fields.String(min_length=3,
|
|
required=True,
|
|
description='The recorder\'s name'),
|
|
},
|
|
required=False,
|
|
allow_null=True,
|
|
skip_none=False,
|
|
description='Parent virtual command.')),
|
|
|
|
'recorder_commands': fields.List(fields.Nested(recorder_command_model)),
|
|
|
|
'command_default_params': fields.List(fields.Raw()),
|
|
'room': fields.Nested(api_virtual_command.model('recorder_room',
|
|
{'id': fields.Integer(), 'name': fields.String(),
|
|
'number': fields.String(), 'alternate_name': fields.String()}),
|
|
r0equired=False,
|
|
allow_null=True,
|
|
skip_none=False,
|
|
description='Room in which the recorder is located.')
|
|
})
|
|
|
|
|
|
# ==
|
|
|
|
@api_virtual_command.route('/<int:id>')
|
|
@api_virtual_command.response(404, 'Recorder not found')
|
|
@api_virtual_command.param('id', 'The recorder identifier')
|
|
class VirtualCommandResource(Resource):
|
|
@jwt_required
|
|
@api_virtual_command.doc('get_recorder')
|
|
@api_virtual_command.marshal_with(virtual_command_model, skip_none=False)
|
|
def get(self, id):
|
|
"""Fetch a recorder given its identifier"""
|
|
command = VirtualCommand.query.get(id)
|
|
if command is not None:
|
|
return command
|
|
api_virtual_command.abort(404)
|
|
|
|
@jwt_required
|
|
@api_virtual_command.doc('delete_todo')
|
|
@api_virtual_command.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_virtual_command.abort(404)
|
|
|
|
virtual_command_model_parser = api_virtual_command.parser()
|
|
virtual_command_model_parser.add_argument('notes', type=str, required=True)
|
|
|
|
@jwt_required
|
|
@api_virtual_command.doc('update_recorder')
|
|
@api_virtual_command.expect(virtual_command_model_parser)
|
|
def put(self, id):
|
|
"""Update a recorder given its identifier"""
|
|
num_rows_matched = Recorder.query.filter_by(id=id).update(api_virtual_command.payload)
|
|
if num_rows_matched < 1:
|
|
api_virtual_command.abort(404)
|
|
db.session.commit()
|
|
return "ok"
|
|
|
|
|
|
@api_virtual_command.route('')
|
|
class VirtualCommandList(Resource):
|
|
@jwt_required
|
|
@api_virtual_command.doc('recorders')
|
|
@api_virtual_command.marshal_list_with(virtual_command_model, skip_none=False)
|
|
def get(self):
|
|
"""
|
|
List all recorders
|
|
:return: recorders
|
|
"""
|
|
return VirtualCommand.get_all()
|
|
|
|
virtual_command_model_parser = api_virtual_command.parser()
|
|
virtual_command_model_parser.add_argument('notes', type=str, required=True)
|
|
|
|
@jwt_required
|
|
@api_virtual_command.doc('create_recorder')
|
|
@api_virtual_command.expect(virtual_command_model_parser)
|
|
@api_virtual_command.marshal_with(virtual_command_model, skip_none=False, code=201)
|
|
def post(self):
|
|
pprint(api_virtual_command.payload)
|
|
room_id = api_virtual_command.payload.pop('recorder_id', None)
|
|
if room_id is None:
|
|
api_virtual_command.payload["room"] = None
|
|
else:
|
|
room = Room.query.get(room_id)
|
|
if room is not None:
|
|
api_virtual_command.payload["room"] = room
|
|
else:
|
|
return "specified v-command (id: {}) does not exist!".format(api_virtual_command.payload["room_id"]), 404
|
|
recorder_model_id = api_virtual_command.payload.pop('recorder_model_id', None)
|
|
if recorder_model_id is None:
|
|
api_virtual_command.payload["recorder_model"] = None
|
|
else:
|
|
rec_model = RecorderModel.query.get(recorder_model_id)
|
|
if rec_model is not None:
|
|
api_virtual_command.payload["recorder_model"] = rec_model
|
|
else:
|
|
return "specified recorder model (id: {}) does not exist!".format(
|
|
api_virtual_command.payload["recorder_model_id"]), 404
|
|
recorder_id = api_virtual_command.payload.pop('recorder_id', None)
|
|
if recorder_id is None:
|
|
api_virtual_command.payload["recorder"] = None
|
|
else:
|
|
recorder = Recorder.query.get(recorder_id)
|
|
if recorder is not None:
|
|
api_virtual_command.payload["recorder"] = recorder
|
|
else:
|
|
return "specified v-command (id: {}) does not exist!".format(
|
|
api_virtual_command.payload["recorder_id"]), 404
|
|
virtual_command = VirtualCommand(**api_virtual_command.payload)
|
|
db.session.add(virtual_command)
|
|
db.session.commit()
|
|
return virtual_command
|