now scraping rooms from capmus mgmt
This commit is contained in:
127
api/virtual_command_api.py
Normal file
127
api/virtual_command_api.py
Normal file
@@ -0,0 +1,127 @@
|
||||
# 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_restplus import fields, Resource
|
||||
|
||||
from backend import db, app
|
||||
from backend.api import api_virtual_command
|
||||
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('virtual_command_model',
|
||||
required=False,
|
||||
allow_null=True,
|
||||
skip_none=False,
|
||||
description='Parent virtual command.'),
|
||||
'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"""
|
||||
recorder = Recorder.query.get(id)
|
||||
if recorder is not None:
|
||||
return recorder
|
||||
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 RecorderList(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 Recorder.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):
|
||||
if "room_id" in api_virtual_command.payload:
|
||||
if api_virtual_command.payload["room_id"] is None:
|
||||
api_virtual_command.payload["room"] = None
|
||||
else:
|
||||
room = Room.query.get(api_virtual_command.payload["room_id"])
|
||||
if room is not None:
|
||||
api_virtual_command.payload["room"] = room
|
||||
else:
|
||||
return "specified room (id: {}) does not exist!".format(api_virtual_command.payload["room_id"]), 404
|
||||
if "recorder_model_id" in api_virtual_command.payload:
|
||||
if api_virtual_command.payload["recorder_model_id"] is None:
|
||||
api_virtual_command.payload["recorder_model"] = None
|
||||
else:
|
||||
rec_model = RecorderModel.query.get(api_virtual_command.payload["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 = Recorder(**api_virtual_command.payload)
|
||||
db.session.add(recorder)
|
||||
db.session.commit()
|
||||
return recorder
|
||||
Reference in New Issue
Block a user